Skip to content

Commit

Permalink
Add rewrite test for correct escaping of replacement (labstack#1798)
Browse files Browse the repository at this point in the history
  • Loading branch information
lammel committed Mar 8, 2021
1 parent b727630 commit 16e13ca
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions middleware/rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,42 @@ func TestEchoRewriteWithRegexRules(t *testing.T) {
})
}
}

// Ensure correct escaping as defined in replacement (issue #1798)
func TestEchoRewriteReplacementEscaping(t *testing.T) {
e := echo.New()

e.Pre(RewriteWithConfig(RewriteConfig{
Rules: map[string]string{
"^/a/*": "/$1?query=param",
"^/b/*": "/$1;part#one",
},
RegexRules: map[*regexp.Regexp]string{
regexp.MustCompile("^/x/(.*)"): "/$1?query=param",
regexp.MustCompile("^/y/(.*)"): "/$1;part#one",
},
}))

var rec *httptest.ResponseRecorder
var req *http.Request

testCases := []struct {
requestPath string
expectPath string
}{
{"/unmatched", "/unmatched"},
{"/a/test", "/test?query=param"},
{"/b/foo/bar", "/foo/bar;part#one"},
{"/x/test", "/test?query=param"},
{"/y/foo/bar", "/foo/bar;part#one"},
}

for _, tc := range testCases {
t.Run(tc.requestPath, func(t *testing.T) {
req = httptest.NewRequest(http.MethodGet, tc.requestPath, nil)
rec = httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, tc.expectPath, req.URL.Path)
})
}
}

0 comments on commit 16e13ca

Please sign in to comment.