-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #2268 Fixes #1598 Fixes #2618 `URLRewrite` middleware changes `r.URL` to the `target` path. This breaks the middleware chain, as each middleware using `r.URL`, to check if it is enabled. So instead of changing `r.URL` directly in the URLRewrite middleware, I moved it to DummyProxyHandler. Similarly, I also changed `Method Tranform` middleware. The PR also changes `cache` middleware, moving it at the end of middleware chain and also incorporating above changes.
- Loading branch information
1 parent
256bc84
commit c3cad42
Showing
9 changed files
with
316 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package gateway | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/TykTechnologies/tyk/apidef" | ||
"github.com/TykTechnologies/tyk/test" | ||
) | ||
|
||
func TestMethodTransform(t *testing.T) { | ||
ts := StartTest() | ||
defer ts.Close() | ||
|
||
t.Run("Using URL rewrite", func(t *testing.T) { | ||
|
||
methodTransform := apidef.MethodTransformMeta{} | ||
methodTransform.Path = "/get" | ||
methodTransform.Method = "GET" | ||
methodTransform.ToMethod = "POST" | ||
|
||
urlRewrite := apidef.URLRewriteMeta{} | ||
urlRewrite.Path = "/get" | ||
urlRewrite.Method = "GET" | ||
urlRewrite.MatchPattern = "/get(.*)" | ||
urlRewrite.RewriteTo = "/post$1" | ||
|
||
BuildAndLoadAPI(func(spec *APISpec) { | ||
spec.Proxy.ListenPath = "/" | ||
UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) { | ||
v.UseExtendedPaths = true | ||
v.ExtendedPaths.MethodTransforms = append(v.ExtendedPaths.MethodTransforms, methodTransform) | ||
v.ExtendedPaths.URLRewrite = append(v.ExtendedPaths.URLRewrite, urlRewrite) | ||
}) | ||
}) | ||
|
||
ts.Run(t, []test.TestCase{ | ||
{Method: "GET", Path: "/get", BodyMatch: `"Url":"/post"`}, | ||
|
||
{Method: "GET", Path: "/get?a=b", BodyMatch: `"Method":"POST"`}, | ||
}...) | ||
}) | ||
|
||
t.Run("Using cache", func(t *testing.T) { | ||
methodTransform := apidef.MethodTransformMeta{} | ||
methodTransform.Path = "/testing" | ||
methodTransform.Method = "GET" | ||
methodTransform.ToMethod = "POST" | ||
|
||
BuildAndLoadAPI(func(spec *APISpec) { | ||
spec.CacheOptions = apidef.CacheOptions{ | ||
CacheTimeout: 120, | ||
EnableCache: true, | ||
} | ||
spec.Proxy.ListenPath = "/" | ||
UpdateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) { | ||
v.UseExtendedPaths = true | ||
v.ExtendedPaths.Cached = []string{"/testing"} | ||
v.ExtendedPaths.MethodTransforms = append(v.ExtendedPaths.MethodTransforms, methodTransform) | ||
}) | ||
}) | ||
|
||
headerCache := map[string]string{"x-tyk-cached-response": "1"} | ||
|
||
ts.Run(t, []test.TestCase{ | ||
{Method: "GET", Path: "/testing", HeadersNotMatch: headerCache, BodyMatch: `"Method":"POST"`}, | ||
{Method: "GET", Path: "/testing", HeadersMatch: headerCache, BodyMatch: `"Method":"POST"`}, | ||
{Method: "POST", Path: "/testing", HeadersNotMatch: headerCache}, | ||
{Method: "GET", Path: "/testing", HeadersMatch: headerCache}, | ||
}...) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.