-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect_test.go
196 lines (174 loc) · 5.38 KB
/
redirect_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package httpbulb
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type RedirectSuite struct {
suite.Suite
testServer *httptest.Server
clientNoRedirect *http.Client
clientOneRedirect *http.Client
}
func (s *RedirectSuite) SetupSuite() {
handleFunc := NewRouter()
s.testServer = httptest.NewServer(handleFunc)
s.clientOneRedirect = &http.Client{}
s.clientOneRedirect.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) >= 1 {
return http.ErrUseLastResponse
}
return nil
}
s.clientNoRedirect = &http.Client{}
// client will not follow redirects
s.clientNoRedirect.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
func (s *RedirectSuite) TearDownSuite() {
s.testServer.Close()
}
func (s *RedirectSuite) TestRedirectTo() {
type testArgs struct {
name string
method string
statusCode int
wantStatusCode int
noCheckLocation bool
ct string
}
tests := []testArgs{
{name: "GET", method: http.MethodGet, statusCode: 301, wantStatusCode: 301},
{name: "DELETE", method: http.MethodDelete, statusCode: 301, wantStatusCode: 301},
{name: "POST", method: http.MethodPost, statusCode: 307, wantStatusCode: 307},
{name: "PUT", method: http.MethodPut, statusCode: 307, wantStatusCode: 307},
{name: "PATCH", method: http.MethodPatch, statusCode: 307, wantStatusCode: 307},
{name: "GET ignore 200", method: http.MethodGet, statusCode: 200, wantStatusCode: 302},
{
name: "POST bad content-type",
method: http.MethodPost,
wantStatusCode: 400,
ct: "x-www-form-urlencoded",
noCheckLocation: true,
},
}
for _, tt := range tests {
headers := http.Header{}
s.T().Run(tt.name, func(t *testing.T) {
dstURL := s.testServer.URL + "/" + strings.ToLower(tt.method)
apiURL, err := url.Parse(s.testServer.URL)
require.NoError(t, err)
apiURL.Path = "/redirect-to"
var body io.Reader
switch tt.method {
case http.MethodGet, http.MethodDelete:
query := url.Values{}
query.Set("url", dstURL)
query.Set("status", strconv.Itoa(tt.wantStatusCode))
apiURL.RawQuery = query.Encode()
case http.MethodPost, http.MethodPut, http.MethodPatch:
form := url.Values{}
form.Set("url", dstURL)
form.Set("status", strconv.Itoa(tt.wantStatusCode))
body = strings.NewReader(form.Encode())
ct := tt.ct
if ct == "" {
ct = "application/x-www-form-urlencoded"
}
headers.Set("Content-Type", ct)
}
req, err := http.NewRequest(tt.method, apiURL.String(), body)
require.NoError(t, err)
req.Header = headers
resp, err := s.clientNoRedirect.Do(req)
require.NoError(t, err)
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
require.Equal(t, tt.wantStatusCode, resp.StatusCode)
if tt.noCheckLocation {
return
}
require.Equal(t, dstURL, resp.Header.Get("Location"))
})
}
}
func (s *RedirectSuite) TestRedirects() {
type testArgs struct {
name string
apiURL string
wantLocation string
wantStatusCode int
}
tests := []testArgs{
{
name: "redirect to relative",
apiURL: fmt.Sprintf("%s/redirect/3", s.testServer.URL),
wantLocation: "/relative-redirect/2",
wantStatusCode: http.StatusFound,
},
{
name: "bad redirect",
apiURL: fmt.Sprintf("%s/redirect/0", s.testServer.URL),
wantStatusCode: http.StatusBadRequest,
},
{
name: "not found",
apiURL: fmt.Sprintf("%s/redirect/a", s.testServer.URL),
wantStatusCode: http.StatusNotFound,
},
{
name: "redirect to absolute",
apiURL: fmt.Sprintf("%s/redirect/3?absolute=true", s.testServer.URL),
wantLocation: fmt.Sprintf("%s/absolute-redirect/2", s.testServer.URL),
wantStatusCode: http.StatusFound,
},
{
name: "relative redirect",
apiURL: fmt.Sprintf("%s/relative-redirect/3", s.testServer.URL),
wantLocation: "/relative-redirect/2",
wantStatusCode: http.StatusFound,
},
{
name: "successful relative redirect",
apiURL: fmt.Sprintf("%s/relative-redirect/1", s.testServer.URL),
wantLocation: "/get",
wantStatusCode: http.StatusFound,
},
{
name: "absolute redirect",
apiURL: fmt.Sprintf("%s/absolute-redirect/3", s.testServer.URL),
wantLocation: fmt.Sprintf("%s/absolute-redirect/2", s.testServer.URL),
wantStatusCode: http.StatusFound,
},
{
name: "successful absolute redirect",
apiURL: fmt.Sprintf("%s/absolute-redirect/1", s.testServer.URL),
wantLocation: fmt.Sprintf("%s/get", s.testServer.URL),
wantStatusCode: http.StatusFound,
},
}
for _, tt := range tests {
s.T().Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, tt.apiURL, nil)
require.NoError(t, err)
resp, err := s.clientOneRedirect.Do(req)
require.NoError(t, err)
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
require.Equal(t, tt.wantStatusCode, resp.StatusCode)
// as this test follows only for one redirect, the location should be: /relative-redirect/n-1
require.Equal(t, tt.wantLocation, resp.Header.Get("Location"))
})
}
}
func TestRedirectSuite(t *testing.T) {
suite.Run(t, new(RedirectSuite))
}