Skip to content

Commit 7335050

Browse files
tchsskVojtechVitek
andauthored
Fix condition in TestRedirectSlashes (#856)
* Fix condition in TestRedirectSlashes * Improve test logs in middleware/strip_test.go --------- Co-authored-by: Vojtech Vitek <[email protected]>
1 parent df0303d commit 7335050

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

middleware/strip_test.go

+33-33
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ func TestStripSlashes(t *testing.T) {
3737
defer ts.Close()
3838

3939
if _, resp := testRequest(t, ts, "GET", "/", nil); resp != "root" {
40-
t.Fatalf(resp)
40+
t.Fatal(resp)
4141
}
4242
if _, resp := testRequest(t, ts, "GET", "//", nil); resp != "root" {
43-
t.Fatalf(resp)
43+
t.Fatal(resp)
4444
}
4545
if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "admin" {
46-
t.Fatalf(resp)
46+
t.Fatal(resp)
4747
}
4848
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "admin" {
49-
t.Fatalf(resp)
49+
t.Fatal(resp)
5050
}
5151
if _, resp := testRequest(t, ts, "GET", "/nothing-here", nil); resp != "nothing here" {
52-
t.Fatalf(resp)
52+
t.Fatal(resp)
5353
}
5454
}
5555

@@ -80,22 +80,22 @@ func TestStripSlashesInRoute(t *testing.T) {
8080
defer ts.Close()
8181

8282
if _, resp := testRequest(t, ts, "GET", "/hi", nil); resp != "hi" {
83-
t.Fatalf(resp)
83+
t.Fatal(resp)
8484
}
8585
if _, resp := testRequest(t, ts, "GET", "/hi/", nil); resp != "nothing here" {
86-
t.Fatalf(resp)
86+
t.Fatal(resp)
8787
}
8888
if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "accounts index" {
89-
t.Fatalf(resp)
89+
t.Fatal(resp)
9090
}
9191
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "accounts index" {
92-
t.Fatalf(resp)
92+
t.Fatal(resp)
9393
}
9494
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/query", nil); resp != "admin" {
95-
t.Fatalf(resp)
95+
t.Fatal(resp)
9696
}
9797
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/query/", nil); resp != "admin" {
98-
t.Fatalf(resp)
98+
t.Fatal(resp)
9999
}
100100
}
101101

@@ -125,33 +125,33 @@ func TestRedirectSlashes(t *testing.T) {
125125
ts := httptest.NewServer(r)
126126
defer ts.Close()
127127

128-
if resp, body := testRequest(t, ts, "GET", "/", nil); body != "root" && resp.StatusCode != 200 {
129-
t.Fatalf(body)
128+
if resp, body := testRequest(t, ts, "GET", "/", nil); body != "root" || resp.StatusCode != 200 {
129+
t.Fatal(body, resp.StatusCode)
130130
}
131131

132132
// NOTE: the testRequest client will follow the redirection..
133-
if resp, body := testRequest(t, ts, "GET", "//", nil); body != "root" && resp.StatusCode != 200 {
134-
t.Fatalf(body)
133+
if resp, body := testRequest(t, ts, "GET", "//", nil); body != "root" || resp.StatusCode != 200 {
134+
t.Fatal(body, resp.StatusCode)
135135
}
136136

137-
if resp, body := testRequest(t, ts, "GET", "/accounts/admin", nil); body != "admin" && resp.StatusCode != 200 {
138-
t.Fatalf(body)
137+
if resp, body := testRequest(t, ts, "GET", "/accounts/admin", nil); body != "admin" || resp.StatusCode != 200 {
138+
t.Fatal(body, resp.StatusCode)
139139
}
140140

141141
// NOTE: the testRequest client will follow the redirection..
142-
if resp, body := testRequest(t, ts, "GET", "/accounts/admin/", nil); body != "admin" && resp.StatusCode != 200 {
143-
t.Fatalf(body)
142+
if resp, body := testRequest(t, ts, "GET", "/accounts/admin/", nil); body != "admin" || resp.StatusCode != 200 {
143+
t.Fatal(body, resp.StatusCode)
144144
}
145145

146-
if resp, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" && resp.StatusCode != 200 {
147-
t.Fatalf(body)
146+
if resp, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" || resp.StatusCode != 404 {
147+
t.Fatal(body, resp.StatusCode)
148148
}
149149

150150
// Ensure redirect Location url is correct
151151
{
152152
resp, body := testRequestNoRedirect(t, ts, "GET", "/accounts/someuser/", nil)
153153
if resp.StatusCode != 301 {
154-
t.Fatalf(body)
154+
t.Fatal(body, resp.StatusCode)
155155
}
156156
location := resp.Header.Get("Location")
157157
if !strings.HasPrefix(location, "//") || !strings.HasSuffix(location, "/accounts/someuser") {
@@ -163,7 +163,7 @@ func TestRedirectSlashes(t *testing.T) {
163163
{
164164
resp, body := testRequestNoRedirect(t, ts, "GET", "/accounts/someuser/?a=1&b=2", nil)
165165
if resp.StatusCode != 301 {
166-
t.Fatalf(body)
166+
t.Fatal(body, resp.StatusCode)
167167
}
168168
location := resp.Header.Get("Location")
169169
if !strings.HasPrefix(location, "//") || !strings.HasSuffix(location, "/accounts/someuser?a=1&b=2") {
@@ -180,8 +180,8 @@ func TestRedirectSlashes(t *testing.T) {
180180
if u, err := url.Parse(ts.URL); err != nil && resp.Request.URL.Host != u.Host {
181181
t.Fatalf("host should remain the same. got: %q, want: %q", resp.Request.URL.Host, ts.URL)
182182
}
183-
if body != "nothing here" && resp.StatusCode != 404 {
184-
t.Fatalf(body)
183+
if body != "nothing here" || resp.StatusCode != 404 {
184+
t.Fatal(body, resp.StatusCode)
185185
}
186186
}
187187
}
@@ -192,8 +192,8 @@ func TestRedirectSlashes(t *testing.T) {
192192
if u, err := url.Parse(ts.URL); err != nil && resp.Request.URL.Host != u.Host {
193193
t.Fatalf("host should remain the same. got: %q, want: %q", resp.Request.URL.Host, ts.URL)
194194
}
195-
if body != "nothing here" && resp.StatusCode != 404 {
196-
t.Fatalf(body)
195+
if body != "nothing here" || resp.StatusCode != 404 {
196+
t.Fatal(body, resp.StatusCode)
197197
}
198198
}
199199
}
@@ -219,21 +219,21 @@ func TestStripSlashesWithNilContext(t *testing.T) {
219219
defer ts.Close()
220220

221221
if _, resp := testRequest(t, ts, "GET", "/", nil); resp != "root" {
222-
t.Fatalf(resp)
222+
t.Fatal(resp)
223223
}
224224
if _, resp := testRequest(t, ts, "GET", "//", nil); resp != "root" {
225-
t.Fatalf(resp)
225+
t.Fatal(resp)
226226
}
227227
if _, resp := testRequest(t, ts, "GET", "/accounts", nil); resp != "accounts" {
228-
t.Fatalf(resp)
228+
t.Fatal(resp)
229229
}
230230
if _, resp := testRequest(t, ts, "GET", "/accounts/", nil); resp != "accounts" {
231-
t.Fatalf(resp)
231+
t.Fatal(resp)
232232
}
233233
if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "admin" {
234-
t.Fatalf(resp)
234+
t.Fatal(resp)
235235
}
236236
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "admin" {
237-
t.Fatalf(resp)
237+
t.Fatal(resp)
238238
}
239239
}

0 commit comments

Comments
 (0)