Skip to content

Commit

Permalink
Fix tests, allocate BaseMiddleware ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
Tit Petric committed Dec 28, 2023
1 parent 03cfd0f commit 5867d8d
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion gateway/coprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func equalHeaders(h1, h2 []*coprocess.Header) bool {

func TestCoProcessMiddlewareName(t *testing.T) {
// Initialize the CoProcessMiddleware
m := &CoProcessMiddleware{}
m := &CoProcessMiddleware{BaseMiddleware: &BaseMiddleware{}}

// Get the name using the method
name := m.Name()
Expand Down
4 changes: 3 additions & 1 deletion gateway/mw_go_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func TestLoadPlugin(t *testing.T) {
}

func TestGoPluginMiddleware_EnabledForSpec(t *testing.T) {
gpm := GoPluginMiddleware{}
gpm := GoPluginMiddleware{
BaseMiddleware: &BaseMiddleware{},
}
apiSpec := &APISpec{APIDefinition: &apidef.APIDefinition{}}
gpm.Spec = apiSpec

Expand Down
4 changes: 2 additions & 2 deletions gateway/mw_ip_blacklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestIPBlacklistMiddleware(t *testing.T) {
req.Header.Set(header.XRealIP, tc.xRealIP)
}

mw := &IPBlackListMiddleware{}
mw := &IPBlackListMiddleware{BaseMiddleware: &BaseMiddleware{}}
mw.Spec = spec
_, code := mw.ProcessRequest(rec, req, nil)

Expand All @@ -58,7 +58,7 @@ func BenchmarkIPBlacklistMiddleware(b *testing.B) {

spec := testPrepareIPBlacklistMiddleware()

mw := &IPBlackListMiddleware{}
mw := &IPBlackListMiddleware{BaseMiddleware: &BaseMiddleware{}}
mw.Spec = spec

rec := httptest.NewRecorder()
Expand Down
4 changes: 2 additions & 2 deletions gateway/mw_ip_whitelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestIPMiddlewarePass(t *testing.T) {
req.Header.Set(header.XRealIP, tc.xRealIP)
}

mw := &IPWhiteListMiddleware{}
mw := &IPWhiteListMiddleware{BaseMiddleware: &BaseMiddleware{}}
mw.Spec = spec
_, code := mw.ProcessRequest(rec, req, nil)

Expand All @@ -57,7 +57,7 @@ func BenchmarkIPMiddlewarePass(b *testing.B) {
b.ReportAllocs()

spec := testPrepareIPMiddlewarePass()
mw := &IPWhiteListMiddleware{}
mw := &IPWhiteListMiddleware{BaseMiddleware: &BaseMiddleware{}}
mw.Spec = spec

rec := httptest.NewRecorder()
Expand Down
4 changes: 2 additions & 2 deletions gateway/mw_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2306,7 +2306,7 @@ func TestGetUserIDFromClaim(t *testing.T) {
func TestJWTMiddleware_getSecretToVerifySignature_JWKNoKID(t *testing.T) {
const jwkURL = "https://jwk.com"

m := JWTMiddleware{}
m := JWTMiddleware{BaseMiddleware: &BaseMiddleware{}}
api := &apidef.APIDefinition{JWTSource: jwkURL}
m.Spec = &APISpec{APIDefinition: api}

Expand Down Expand Up @@ -2408,7 +2408,7 @@ func Test_getOAuthClientIDFromClaim(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
j := JWTMiddleware{}
j := JWTMiddleware{BaseMiddleware: &BaseMiddleware{}}
j.Spec = &APISpec{APIDefinition: &apidef.APIDefinition{}}

oauthClientID := j.getOAuthClientIDFromClaim(tc.claims)
Expand Down
2 changes: 1 addition & 1 deletion gateway/mw_modify_headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestTransformHeaders_EnabledForSpec(t *testing.T) {
"Default": versionInfo,
}

th := TransformHeaders{}
th := TransformHeaders{BaseMiddleware: &BaseMiddleware{}}
th.Spec = &APISpec{APIDefinition: &apidef.APIDefinition{}}
th.Spec.VersionData.Versions = versions

Expand Down
4 changes: 2 additions & 2 deletions gateway/mw_redis_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestRedisCacheMiddlewareUnit(t *testing.T) {
{
Name: "isTimeStampExpired",
Fn: func(t *testing.T) {
mw := &RedisCacheMiddleware{}
mw := &RedisCacheMiddleware{BaseMiddleware: &BaseMiddleware{}}

assert.True(t, mw.isTimeStampExpired("invalid"))
assert.True(t, mw.isTimeStampExpired("1"))
Expand All @@ -38,7 +38,7 @@ func TestRedisCacheMiddlewareUnit(t *testing.T) {
{
Name: "decodePayload",
Fn: func(t *testing.T) {
mw := &RedisCacheMiddleware{}
mw := &RedisCacheMiddleware{BaseMiddleware: &BaseMiddleware{}}

if data, expire, err := mw.decodePayload("dGVzdGluZwo=|123"); true {
assert.Equal(t, "testing\n", data)
Expand Down
10 changes: 5 additions & 5 deletions gateway/mw_strip_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestStripAuth_stripFromHeaders(t *testing.T) {
for _, tc := range testCases {
t.Run(fmt.Sprintf("stripping %+v", tc), func(t *testing.T) {

sa := StripAuth{}
sa := StripAuth{BaseMiddleware: &BaseMiddleware{}}
sa.Spec = &APISpec{APIDefinition: &apidef.APIDefinition{}}

req, err := http.NewRequest("GET", "http://example.com", nil)
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestStripAuth_stripFromHeaders(t *testing.T) {
if err != nil {
t.Fatal(err)
}
sa := StripAuth{}
sa := StripAuth{BaseMiddleware: &BaseMiddleware{}}
sa.Spec = &APISpec{APIDefinition: &apidef.APIDefinition{}}

key := "Authorization"
Expand Down Expand Up @@ -114,7 +114,7 @@ func BenchmarkStripAuth_stripFromHeaders(b *testing.B) {

for i := 0; i < b.N; i++ {
for _, tc := range testCases {
sa := StripAuth{}
sa := StripAuth{BaseMiddleware: &BaseMiddleware{}}
sa.Spec = &APISpec{APIDefinition: &apidef.APIDefinition{}}

req, err := http.NewRequest("GET", "http://example.com", nil)
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestStripAuth_stripFromParams(t *testing.T) {
for _, tc := range testCases {
t.Run(fmt.Sprintf("stripping %s", tc.QueryParam), func(t *testing.T) {

sa := StripAuth{}
sa := StripAuth{BaseMiddleware: &BaseMiddleware{}}
sa.Spec = &APISpec{APIDefinition: &apidef.APIDefinition{}}

rawUrl := "http://example.com/abc"
Expand Down Expand Up @@ -207,7 +207,7 @@ func BenchmarkStripAuth_stripFromParams(b *testing.B) {

for i := 0; i < b.N; i++ {
for _, tc := range testCases {
sa := StripAuth{}
sa := StripAuth{BaseMiddleware: &BaseMiddleware{}}
sa.Spec = &APISpec{APIDefinition: &apidef.APIDefinition{}}

req, err := http.NewRequest("GET", "http://example.com/abc", nil)
Expand Down
2 changes: 1 addition & 1 deletion gateway/mw_url_rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ func TestURLRewriteMiddleware_CheckHostRewrite(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &URLRewriteMiddleware{}
m := &URLRewriteMiddleware{BaseMiddleware: &BaseMiddleware{}}
r := &http.Request{}
err := m.CheckHostRewrite(tt.args.oldPath, tt.args.newTarget, r)
assert.Equal(t, tt.errExpected, err != nil)
Expand Down

0 comments on commit 5867d8d

Please sign in to comment.