Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enrich filter test case #202

Merged
merged 6 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pkg/context/mock/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package mock
import (
"context"
"fmt"
"github.com/apache/dubbo-go-pixiu/pkg/model"
tydhot marked this conversation as resolved.
Show resolved Hide resolved
"net/http"
)

Expand Down Expand Up @@ -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
50 changes: 50 additions & 0 deletions pkg/filter/authority/authority_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 (
tydhot marked this conversation as resolved.
Show resolved Hide resolved
"bytes"
"github.com/apache/dubbo-go-pixiu/pkg/context/mock"
"github.com/apache/dubbo-go-pixiu/pkg/filter/recovery"
"github.com/stretchr/testify/assert"
"math"
"net/http"
"testing"
)

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)
}
68 changes: 68 additions & 0 deletions pkg/filter/header/header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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"
"github.com/apache/dubbo-go-pixiu/pkg/context/mock"
tydhot marked this conversation as resolved.
Show resolved Hide resolved
"github.com/apache/dubbo-go-pixiu/pkg/filter/recovery"
"github.com/dubbogo/dubbo-go-pixiu-filter/pkg/api/config"
"github.com/dubbogo/dubbo-go-pixiu-filter/pkg/router"
"github.com/stretchr/testify/assert"
"math"
"net/http"
"testing"
)

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,
}
}
35 changes: 35 additions & 0 deletions pkg/filter/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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"
"github.com/apache/dubbo-go-pixiu/pkg/context/mock"
tydhot marked this conversation as resolved.
Show resolved Hide resolved
"github.com/apache/dubbo-go-pixiu/pkg/filter/recovery"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

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")
}