-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
rewrite_test.go
58 lines (47 loc) · 1.74 KB
/
rewrite_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
48
49
50
51
52
53
54
55
56
57
58
package rest
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRewrite(t *testing.T) {
rr := httptest.NewRecorder()
testHandler := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
t.Logf("%+v", r)
assert.Equal(t, "/xyzzz/params?foo=bar", r.URL.String())
assert.Equal(t, "/xyzzz/params", r.URL.Path)
assert.Equal(t, "/api/v1/params", r.Header.Get("X-Original-URL"))
})
handler := Rewrite("/api/v1/(.*)", "/xyzzz/$1?foo=bar")(testHandler)
req, err := http.NewRequest("GET", "/api/v1/params", http.NoBody)
require.NoError(t, err)
handler.ServeHTTP(rr, req)
}
func TestRewriteCleanup(t *testing.T) {
rr := httptest.NewRecorder()
testHandler := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
t.Logf("%+v", r)
assert.Equal(t, "/xyzzz/params?foo=bar", r.URL.String())
assert.Equal(t, "/xyzzz/params", r.URL.Path)
assert.Equal(t, "/api/v1/params", r.Header.Get("X-Original-URL"))
})
handler := Rewrite("/api/v1/(.*)", "/xyzzz/abc/../$1?foo=bar")(testHandler)
req, err := http.NewRequest("GET", "/api/v1/params", http.NoBody)
require.NoError(t, err)
handler.ServeHTTP(rr, req)
}
func TestRewriteCleanupWithSlash(t *testing.T) {
rr := httptest.NewRecorder()
testHandler := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
t.Logf("%+v", r)
assert.Equal(t, "/xyzzz/params/", r.URL.String())
assert.Equal(t, "/xyzzz/params/", r.URL.Path)
assert.Equal(t, "/api/v1/params/", r.Header.Get("X-Original-URL"))
})
handler := Rewrite("/api/v1/(.*)/", "/xyzzz/abc/../$1/")(testHandler)
req, err := http.NewRequest("GET", "/api/v1/params/", http.NoBody)
require.NoError(t, err)
handler.ServeHTTP(rr, req)
}