@@ -8,11 +8,13 @@ import (
88 "testing"
99)
1010
11- func TestRewritePath (t * testing.T ) {
11+ func TestRewriteURL (t * testing.T ) {
1212 var testCases = []struct {
1313 whenURL string
1414 expectPath string
1515 expectRawPath string
16+ expectQuery string
17+ expectErr string
1618 }{
1719 {
1820 whenURL : "http://localhost:8080/old" ,
@@ -28,16 +30,18 @@ func TestRewritePath(t *testing.T) {
2830 whenURL : "http://localhost:8080/users/+_+/orders/___++++?test=1" ,
2931 expectPath : "/user/+_+/order/___++++" ,
3032 expectRawPath : "" ,
33+ expectQuery : "test=1" ,
3134 },
3235 {
3336 whenURL : "http://localhost:8080/users/%20a/orders/%20aa" ,
3437 expectPath : "/user/ a/order/ aa" ,
3538 expectRawPath : "" ,
3639 },
3740 {
38- whenURL : "http://localhost:8080/%47%6f%2f" ,
41+ whenURL : "http://localhost:8080/%47%6f%2f?test=1 " ,
3942 expectPath : "/Go/" ,
4043 expectRawPath : "/%47%6f%2f" ,
44+ expectQuery : "test=1" ,
4145 },
4246 {
4347 whenURL : "/users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F" ,
@@ -49,21 +53,40 @@ func TestRewritePath(t *testing.T) {
4953 expectPath : "/user/jill/order/T/cO4lW/t/Vp/" ,
5054 expectRawPath : "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F" ,
5155 },
56+ {
57+ whenURL : "http://localhost:8080/static" ,
58+ expectPath : "/static/path" ,
59+ expectRawPath : "" ,
60+ expectQuery : "role=AUTHOR&limit=1000" ,
61+ },
62+ {
63+ whenURL : "/static" ,
64+ expectPath : "/static/path" ,
65+ expectRawPath : "" ,
66+ expectQuery : "role=AUTHOR&limit=1000" ,
67+ },
5268 }
5369
5470 rules := map [* regexp.Regexp ]string {
5571 regexp .MustCompile ("^/old$" ): "/new" ,
5672 regexp .MustCompile ("^/users/(.*?)/orders/(.*?)$" ): "/user/$1/order/$2" ,
73+ regexp .MustCompile ("^/static$" ): "/static/path?role=AUTHOR&limit=1000" ,
5774 }
5875
5976 for _ , tc := range testCases {
6077 t .Run (tc .whenURL , func (t * testing.T ) {
6178 req := httptest .NewRequest (http .MethodGet , tc .whenURL , nil )
6279
63- rewritePath (rules , req )
80+ err := rewriteURL (rules , req )
6481
82+ if tc .expectErr != "" {
83+ assert .EqualError (t , err , tc .expectErr )
84+ } else {
85+ assert .NoError (t , err )
86+ }
6587 assert .Equal (t , tc .expectPath , req .URL .Path ) // Path field is stored in decoded form: /%47%6f%2f becomes /Go/.
6688 assert .Equal (t , tc .expectRawPath , req .URL .RawPath ) // RawPath, an optional field which only gets set if the default encoding is different from Path.
89+ assert .Equal (t , tc .expectQuery , req .URL .RawQuery )
6790 })
6891 }
6992}
0 commit comments