Skip to content

Commit

Permalink
[TT-7489/TT-7490]add migration for response body transform. (TykTechn…
Browse files Browse the repository at this point in the history
…ologies#5344)

## Description
Response body transform middleware can now work without
`response_body_transform` response processor configured.
Removing `response_body_transform` from response processors for
consistency.
<!-- Describe your changes in detail -->

## Related Issue
https://tyktech.atlassian.net/browse/TT-7490

## Motivation and Context

<!-- Why is this change required? What problem does it solve? -->

## How This Has Been Tested

<!-- Please describe in detail how you tested your changes -->
<!-- Include details of your testing environment, and the tests -->
<!-- you ran to see how your change affects other areas of the code,
etc. -->
<!-- This information is helpful for reviewers and QA. -->

## Screenshots (if appropriate)

## Types of changes

<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Refactoring or add test (improvements in base code or adds test
coverage to functionality)

## Checklist

<!-- Go over all the following points, and put an `x` in all the boxes
that apply -->
<!-- If there are no documentation updates required, mark the item as
checked. -->
<!-- Raise up any additional concerns not covered by the checklist. -->

- [ ] I ensured that the documentation is up to date
- [ ] I explained why this PR updates go.mod in detail with reasoning
why it's required
- [ ] I would like a code coverage CI quality gate exception and have
explained why
  • Loading branch information
jeffy-mathew authored Jul 25, 2023
1 parent 6fde16e commit 542b56d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
17 changes: 17 additions & 0 deletions apidef/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"github.com/TykTechnologies/tyk/internal/uuid"
)

const (
ResponseProcessorResponseBodyTransform = "response_body_transform"
)

var (
ErrMigrationNewVersioningEnabled = errors.New("not migratable - new versioning is already enabled")
)
Expand Down Expand Up @@ -233,6 +237,7 @@ func (a *APIDefinition) Migrate() (versions []APIDefinition, err error) {
a.migrateIDExtractor()
a.migrateCustomDomain()
a.migrateScopeToPolicy()
a.migrateResponseProcessors()

versions, err = a.MigrateVersioning()
if err != nil {
Expand Down Expand Up @@ -443,3 +448,15 @@ func (a *APIDefinition) migrateScopeToPolicy() {

a.Scopes.JWT = scopeClaim
}

func (a *APIDefinition) migrateResponseProcessors() {
var responseProcessors []ResponseProcessor
for i := range a.ResponseProcessors {
if a.ResponseProcessors[i].Name == ResponseProcessorResponseBodyTransform {
continue
}
responseProcessors = append(responseProcessors, a.ResponseProcessors[i])
}

a.ResponseProcessors = responseProcessors
}
31 changes: 31 additions & 0 deletions apidef/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,32 @@ var testV1ExtendedPaths = ExtendedPathsSet{
WhiteList: []EndPointMeta{
{Method: http.MethodGet, Path: "/get1"},
},
TransformResponse: []TemplateMeta{
{
Method: http.MethodGet, Path: "/transform1",
TemplateData: TemplateData{
EnableSession: true,
Mode: UseBlob,
TemplateSource: `{"http_method":"{{.Method}}"}`,
Input: RequestJSON,
}},
},
}

var testV2ExtendedPaths = ExtendedPathsSet{
WhiteList: []EndPointMeta{
{Method: http.MethodGet, Path: "/get2"},
},
TransformResponse: []TemplateMeta{
{
Method: http.MethodGet, Path: "/transform2",
TemplateData: TemplateData{
EnableSession: true,
Mode: UseBlob,
TemplateSource: `{"http_method":"{{.Method}}"}`,
Input: RequestJSON,
}},
},
}

func oldTestAPI() APIDefinition {
Expand Down Expand Up @@ -100,6 +120,9 @@ func oldTestAPI() APIDefinition {
CookieName: "Authorization",
},
},
ResponseProcessors: []ResponseProcessor{
{Name: ResponseProcessorResponseBodyTransform},
},
}
}

Expand Down Expand Up @@ -731,3 +754,11 @@ func TestAPIDefinition_migrateScopeToPolicy(t *testing.T) {
})

}

func TestAPIDefinition_migrateResponseProcessors(t *testing.T) {
base := oldTestAPI()
_, err := base.Migrate()
assert.NoError(t, err)

assert.Empty(t, base.ResponseProcessors)
}
11 changes: 0 additions & 11 deletions gateway/res_handler_transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ func TestTransformResponseWithURLRewrite(t *testing.T) {
RewriteTo: "get",
}

responseProcessorConf := []apidef.ResponseProcessor{{Name: "response_body_transform"}}

t.Run("Transform without rewrite", func(t *testing.T) {
ts := StartTest(nil)
defer ts.Close()

ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.ResponseProcessors = responseProcessorConf
UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
})
Expand All @@ -51,7 +48,6 @@ func TestTransformResponseWithURLRewrite(t *testing.T) {

ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.ResponseProcessors = responseProcessorConf

UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
Expand All @@ -70,7 +66,6 @@ func TestTransformResponseWithURLRewrite(t *testing.T) {

ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.ResponseProcessors = responseProcessorConf

transformResponseConf.Path = "abc"

Expand Down Expand Up @@ -99,12 +94,9 @@ func TestTransformResponse_ContextVars(t *testing.T) {
},
}

responseProcessorConf := []apidef.ResponseProcessor{{Name: "response_body_transform"}}

// When Context Vars are disabled
ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.ResponseProcessors = responseProcessorConf
UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
})
Expand All @@ -118,7 +110,6 @@ func TestTransformResponse_ContextVars(t *testing.T) {
ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.EnableContextVars = true
spec.ResponseProcessors = responseProcessorConf
UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
})
Expand All @@ -143,15 +134,13 @@ func TestTransformResponse_WithCache(t *testing.T) {
TemplateSource: base64.StdEncoding.EncodeToString([]byte(`{"foo":"{{._tyk_context.headers_Foo}}"}`)),
},
}
responseProcessorConf := []apidef.ResponseProcessor{{Name: "response_body_transform"}}

createAPI := func(withCache bool) {
ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.CacheOptions.CacheTimeout = 60
spec.EnableContextVars = true
spec.CacheOptions.EnableCache = withCache
spec.ResponseProcessors = responseProcessorConf
UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) {
v.ExtendedPaths.TransformResponse = []apidef.TemplateMeta{transformResponseConf}
v.ExtendedPaths.Cached = []string{path}
Expand Down

0 comments on commit 542b56d

Please sign in to comment.