-
Notifications
You must be signed in to change notification settings - Fork 14
/
redirect_handler_test.go
115 lines (107 loc) · 3.24 KB
/
redirect_handler_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package nethttplibrary
import (
nethttp "net/http"
httptest "net/http/httptest"
testing "testing"
"strconv"
assert "github.com/stretchr/testify/assert"
)
func TestItCreatesANewRedirectHandler(t *testing.T) {
handler := NewRedirectHandler()
if handler == nil {
t.Error("handler is nil")
}
}
func TestItDoesntRedirectWithoutMiddleware(t *testing.T) {
requestCount := int64(0)
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
requestCount++
res.Header().Set("Location", "/"+strconv.FormatInt(requestCount, 10))
res.WriteHeader(301)
res.Write([]byte("body"))
}))
defer func() { testServer.Close() }()
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
client := getDefaultClientWithoutMiddleware()
resp, err := client.Do(req)
if err != nil {
t.Error(err)
}
assert.NotNil(t, resp)
assert.Equal(t, int64(1), requestCount)
}
func TestItHonoursShouldRedirect(t *testing.T) {
requestCount := int64(0)
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
requestCount++
res.Header().Set("Location", "/"+strconv.FormatInt(requestCount, 10))
res.WriteHeader(301)
res.Write([]byte("body"))
}))
defer func() { testServer.Close() }()
handler := NewRedirectHandlerWithOptions(RedirectHandlerOptions{
ShouldRedirect: func(req *nethttp.Request, res *nethttp.Response) bool {
return false
},
})
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
resp, err := handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
assert.NotNil(t, resp)
assert.Equal(t, int64(1), requestCount)
}
func TestItHonoursMaxRedirect(t *testing.T) {
requestCount := int64(0)
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
requestCount++
res.Header().Set("Location", "/"+strconv.FormatInt(requestCount, 10))
res.WriteHeader(301)
res.Write([]byte("body"))
}))
defer func() { testServer.Close() }()
handler := NewRedirectHandler()
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
resp, err := handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
assert.NotNil(t, resp)
assert.Equal(t, int64(defaultMaxRedirects+1), requestCount)
}
func TestItStripsAuthorizationHeaderOnDifferentHost(t *testing.T) {
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
res.Header().Set("Location", "https://www.bing.com/")
res.WriteHeader(301)
res.Write([]byte("body"))
}))
defer func() { testServer.Close() }()
handler := NewRedirectHandler()
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
req.Header.Set("Authorization", "Bearer 12345")
client := getDefaultClientWithoutMiddleware()
resp, err := client.Do(req)
if err != nil {
t.Error(err)
}
result, err := handler.getRedirectRequest(req, resp)
if err != nil {
t.Error(err)
}
assert.NotNil(t, result)
assert.Equal(t, "www.bing.com", result.Host)
assert.Equal(t, "", result.Header.Get("Authorization"))
}