Skip to content

Commit

Permalink
Merge pull request apache#202 from tydhot/0701unittest
Browse files Browse the repository at this point in the history
Enrich filter test case

Former-commit-id: 9c3b199
  • Loading branch information
AlexStocks authored Jul 5, 2021
2 parents 0704c2e + e2b8800 commit 2b997bf
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/context/mock/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
import (
pkgcontext "github.com/apache/dubbo-go-pixiu/pkg/context"
contexthttp "github.com/apache/dubbo-go-pixiu/pkg/context/http"
"github.com/apache/dubbo-go-pixiu/pkg/model"
)

// GetMockHTTPContext mock context for test.
Expand All @@ -52,6 +53,37 @@ func GetMockHTTPContext(r *http.Request, fc ...fc.FilterFunc) *contexthttp.HttpC
return result
}

// GetMockHTTPAuthContext mock context with auth for test.
func GetMockHTTPAuthContext(r *http.Request, black bool, fc ...fc.FilterFunc) *contexthttp.HttpContext {
var rules []model.AuthorityRule
if black {
rules = []model.AuthorityRule{{Strategy: model.Blacklist, Items: append([]string{}, "")}}
} else {
rules = []model.AuthorityRule{{Strategy: model.Whitelist, Items: append([]string{}, "127.0.0.1")}}
}
result := &contexthttp.HttpContext{
BaseContext: &pkgcontext.BaseContext{
Index: -1,
},
HttpConnectionManager: model.HttpConnectionManager{
AuthorityConfig: model.AuthorityConfiguration{
Rules: rules,
},
},
Request: r,
}

w := mockWriter{header: map[string][]string{}}
result.ResetWritermen(&w)
result.Reset()
result.BaseContext.Ctx = context.Background()
for i := range fc {
result.Filters = append(result.Filters, fc[i])
}

return result
}

type mockWriter struct {
header http.Header
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/filter/authority/authority_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package authority

import (
"bytes"
"math"
"net/http"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go-pixiu/pkg/context/mock"
"github.com/apache/dubbo-go-pixiu/pkg/filter/recovery"
)

func TestAuthority(t *testing.T) {
request, err := http.NewRequest("POST", "http://www.dubbogopixiu.com/mock/test?name=tc", bytes.NewReader([]byte("{\"id\":\"12345\"}")))
assert.NoError(t, err)
c := mock.GetMockHTTPAuthContext(request, true, New().Do(), recovery.New().Do())
c.Next()
assert.Equal(t, int8(math.MaxInt8/2+1), c.BaseContext.Index)

request.Header.Set("X-Forwarded-For", "0.0.0.0")
c1 := mock.GetMockHTTPAuthContext(request, false, New().Do(), recovery.New().Do())
c1.Next()
assert.Equal(t, int8(math.MaxInt8/2+1), c1.BaseContext.Index)

c2 := mock.GetMockHTTPAuthContext(request, true, New().Do(), recovery.New().Do())
c2.Next()
assert.Equal(t, int8(len(c2.Filters)*2), c2.BaseContext.Index)

request.Header.Set("X-Forwarded-For", "127.0.0.1")
c3 := mock.GetMockHTTPAuthContext(request, false, New().Do(), recovery.New().Do())
c3.Next()
assert.Equal(t, int8(len(c3.Filters)*2), c3.BaseContext.Index)
}
74 changes: 74 additions & 0 deletions pkg/filter/header/header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package header

import (
"bytes"
"math"
"net/http"
"testing"
)

import (
"github.com/dubbogo/dubbo-go-pixiu-filter/pkg/api/config"
"github.com/dubbogo/dubbo-go-pixiu-filter/pkg/router"
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go-pixiu/pkg/context/mock"
"github.com/apache/dubbo-go-pixiu/pkg/filter/recovery"
)

func TestHeader(t *testing.T) {
api := router.API{
URLPattern: "/mock/:id/:name",
Method: getMockMethod(config.MethodGet),
Headers: map[string]string{},
}
request, err := http.NewRequest("POST", "http://www.dubbogopixiu.com/mock/test?name=tc", bytes.NewReader([]byte("{\"id\":\"12345\"}")))
assert.NoError(t, err)
c := mock.GetMockHTTPContext(request, New().Do(), recovery.New().Do())
c.API(api)
c.Next()
assert.Equal(t, int8(len(c.Filters)*2), c.BaseContext.Index)

request.Header.Set("filter", "test")
api.Headers["filter"] = "test"
c1 := mock.GetMockHTTPContext(request, New().Do(), recovery.New().Do())
c1.API(api)
c1.Next()
assert.Equal(t, int8(len(c1.Filters)*2-1), c1.BaseContext.Index)

request.Header.Set("filter", "test1")
c2 := mock.GetMockHTTPContext(request, New().Do(), recovery.New().Do())
c2.API(api)
c2.Next()
assert.Equal(t, int8(math.MaxInt8/2+1), c2.BaseContext.Index)
}

func getMockMethod(verb config.HTTPVerb) config.Method {
inbound := config.InboundRequest{}
integration := config.IntegrationRequest{}
return config.Method{
OnAir: true,
HTTPVerb: verb,
InboundRequest: inbound,
IntegrationRequest: integration,
}
}
41 changes: 41 additions & 0 deletions pkg/filter/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package logger

import (
"bytes"
"net/http"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go-pixiu/pkg/context/mock"
"github.com/apache/dubbo-go-pixiu/pkg/filter/recovery"
)

func TestLogger(t *testing.T) {
request, err := http.NewRequest("POST", "http://www.dubbogopixiu.com/mock/test?name=tc", bytes.NewReader([]byte("{\"id\":\"12345\"}")))
assert.NoError(t, err)
c := mock.GetMockHTTPContext(request, New().Do(), recovery.New().Do())
c.Next()
t.Log("log filter test is finished")
}

0 comments on commit 2b997bf

Please sign in to comment.