-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fix performance regression #1777 and avoid double escaping in rewrite/proxy middleware. * Add rewrite test for correct escaping of replacement (#1798) Co-authored-by: Roland Lammel <rl@neotel.at>
- Loading branch information
Showing
6 changed files
with
376 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestRewritePath(t *testing.T) { | ||
var testCases = []struct { | ||
whenURL string | ||
expectPath string | ||
expectRawPath string | ||
}{ | ||
{ | ||
whenURL: "http://localhost:8080/old", | ||
expectPath: "/new", | ||
expectRawPath: "", | ||
}, | ||
{ // encoded `ol%64` (decoded `old`) should not be rewritten to `/new` | ||
whenURL: "/ol%64", // `%64` is decoded `d` | ||
expectPath: "/old", | ||
expectRawPath: "/ol%64", | ||
}, | ||
{ | ||
whenURL: "http://localhost:8080/users/+_+/orders/___++++?test=1", | ||
expectPath: "/user/+_+/order/___++++", | ||
expectRawPath: "", | ||
}, | ||
{ | ||
whenURL: "http://localhost:8080/users/%20a/orders/%20aa", | ||
expectPath: "/user/ a/order/ aa", | ||
expectRawPath: "", | ||
}, | ||
{ | ||
whenURL: "http://localhost:8080/%47%6f%2f", | ||
expectPath: "/Go/", | ||
expectRawPath: "/%47%6f%2f", | ||
}, | ||
{ | ||
whenURL: "/users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F", | ||
expectPath: "/user/jill/order/T/cO4lW/t/Vp/", | ||
expectRawPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F", | ||
}, | ||
{ // do nothing, replace nothing | ||
whenURL: "http://localhost:8080/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F", | ||
expectPath: "/user/jill/order/T/cO4lW/t/Vp/", | ||
expectRawPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F", | ||
}, | ||
} | ||
|
||
rules := map[*regexp.Regexp]string{ | ||
regexp.MustCompile("^/old$"): "/new", | ||
regexp.MustCompile("^/users/(.*?)/orders/(.*?)$"): "/user/$1/order/$2", | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.whenURL, func(t *testing.T) { | ||
req := httptest.NewRequest(http.MethodGet, tc.whenURL, nil) | ||
|
||
rewritePath(rules, req) | ||
|
||
assert.Equal(t, tc.expectPath, req.URL.Path) // Path field is stored in decoded form: /%47%6f%2f becomes /Go/. | ||
assert.Equal(t, tc.expectRawPath, req.URL.RawPath) // RawPath, an optional field which only gets set if the default encoding is different from Path. | ||
}) | ||
} | ||
} |
Oops, something went wrong.