Skip to content

Commit

Permalink
refactor TestTransformResponseBody to res_handler_transform.go.
Browse files Browse the repository at this point in the history
improve ResponseTransformMiddleware_Enabled.
  • Loading branch information
jeffy-mathew committed Jul 26, 2023
1 parent bc45d08 commit eb7ae06
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 37 deletions.
36 changes: 0 additions & 36 deletions gateway/mw_transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,39 +388,3 @@ func TestTransformRequestBody(t *testing.T) {
_, _ = ts.Run(t, test.TestCase{Path: "/get", Data: body, BodyNotMatch: bodyMatch, Code: http.StatusOK})
})
}

func TestTransformResponseBody(t *testing.T) {
ts := StartTest(nil)
defer ts.Close()
loadAPI := func(disabled bool) {
transformResponseConf := apidef.TemplateMeta{
Disabled: disabled,
Path: "/transform",
Method: http.MethodGet,
TemplateData: apidef.TemplateData{
Mode: "blob",
TemplateSource: base64.StdEncoding.EncodeToString([]byte(`{"http_method":"{{.Method}}"}`)),
},
}
ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
})
})
}

t.Run("transform body enabled", func(t *testing.T) {
loadAPI(false)
_, _ = ts.Run(t, test.TestCase{
Path: "/transform", Code: 200, BodyMatch: `{"http_method":"GET"}`,
})
})

t.Run("transform body disabled", func(t *testing.T) {
loadAPI(true)
_, _ = ts.Run(t, test.TestCase{
Path: "/transform", Code: 200, BodyNotMatch: `{"http_method":"GET"}`,
})
})
}
6 changes: 5 additions & 1 deletion gateway/res_handler_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ type ResponseTransformMiddleware struct {
func (r *ResponseTransformMiddleware) Enabled() bool {
for _, version := range r.Spec.VersionData.Versions {
if len(version.ExtendedPaths.TransformResponse) > 0 {
return true
for _, transformResponse := range version.ExtendedPaths.TransformResponse {
if !transformResponse.Disabled {
return true
}
}
}
}
return false
Expand Down
106 changes: 106 additions & 0 deletions gateway/res_handler_transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package gateway

import (
"encoding/base64"
"net/http"
"testing"
"time"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/test"
"github.com/stretchr/testify/assert"
)

func TestTransformResponseWithURLRewrite(t *testing.T) {
Expand Down Expand Up @@ -166,3 +168,107 @@ func TestTransformResponse_WithCache(t *testing.T) {
}...)

}

func TestTransformResponseBody(t *testing.T) {
ts := StartTest(nil)
defer ts.Close()
loadAPI := func(disabled bool) {
transformResponseConf := apidef.TemplateMeta{
Disabled: disabled,
Path: "/transform",
Method: http.MethodGet,
TemplateData: apidef.TemplateData{
Mode: "blob",
TemplateSource: base64.StdEncoding.EncodeToString([]byte(`{"http_method":"{{.Method}}"}`)),
},
}
ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
})
})
}

t.Run("transform body enabled", func(t *testing.T) {
loadAPI(false)
_, _ = ts.Run(t, test.TestCase{
Path: "/transform", Code: 200, BodyMatch: `{"http_method":"GET"}`,
})
})

t.Run("transform body disabled", func(t *testing.T) {
loadAPI(true)
_, _ = ts.Run(t, test.TestCase{
Path: "/transform", Code: 200, BodyNotMatch: `{"http_method":"GET"}`,
})
})
}

func TestResponseTransformMiddleware_Enabled(t *testing.T) {
getTransformResponseConf := func(disabled bool, path string) apidef.TemplateMeta {
return apidef.TemplateMeta{
Disabled: disabled,
Path: path,
Method: http.MethodGet,
TemplateData: apidef.TemplateData{
Mode: "blob",
TemplateSource: base64.StdEncoding.EncodeToString([]byte(`{"http_method":"{{.Method}}"}`)),
},
}
}

getSpec := func(transformResponseMWs []apidef.TemplateMeta) *APISpec {
return &APISpec{
APIDefinition: &apidef.APIDefinition{
VersionData: apidef.VersionData{
DefaultVersion: "Default",
NotVersioned: true,
Versions: map[string]apidef.VersionInfo{
"Default": {
Name: "Default",
ExtendedPaths: apidef.ExtendedPathsSet{
TransformResponse: transformResponseMWs,
},
},
},
},
},
}
}

testCases := []struct {
name string
spec *APISpec
mwEnabled bool
}{
{
name: "all disabled",
spec: getSpec([]apidef.TemplateMeta{
getTransformResponseConf(true, "/transform1"),
getTransformResponseConf(true, "/transform2"),
}),
mwEnabled: false,
},
{
name: "at least one enabled",
spec: getSpec([]apidef.TemplateMeta{
getTransformResponseConf(false, "/transform1"),
getTransformResponseConf(true, "/transform2"),
}),
mwEnabled: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
responseTransformMW := ResponseTransformMiddleware{
BaseTykResponseHandler{
Spec: tc.spec,
},
}

assert.Equal(t, tc.mwEnabled, responseTransformMW.Enabled())
})
}
}

0 comments on commit eb7ae06

Please sign in to comment.