forked from a-h/jwtproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewritehandler_test.go
47 lines (41 loc) · 1.09 KB
/
rewritehandler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import "testing"
import "net/http"
import "net/url"
import "net/http/httptest"
func TestRewriteHandler(t *testing.T) {
tests := []struct {
IncomingURL string
Prefix string
ExpectedOutgoingURL string
}{
{
IncomingURL: "/api/user?id=123",
Prefix: "/api",
ExpectedOutgoingURL: "/user?id=123",
},
{
IncomingURL: "/api/user?id=123",
Prefix: "/api/",
ExpectedOutgoingURL: "/user?id=123",
},
{
IncomingURL: "/api/user?id=123&test=t",
Prefix: "",
ExpectedOutgoingURL: "/api/user?id=123&test=t",
},
}
for i, test := range tests {
var actualURL *url.URL
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
actualURL = r.URL
})
rh := NewRewriteHandler(test.Prefix, testHandler)
r := httptest.NewRequest("GET", test.IncomingURL, nil)
w := httptest.NewRecorder()
rh.ServeHTTP(w, r)
if actualURL.String() != test.ExpectedOutgoingURL {
t.Errorf("%d: expected '%s', got '%s'", i, test.ExpectedOutgoingURL, actualURL.String())
}
}
}