From 8583cae931426365110e7982e170600d8430b056 Mon Sep 17 00:00:00 2001 From: Taichi Sasaki Date: Sat, 7 Oct 2023 14:58:52 +0900 Subject: [PATCH 1/2] Fix condition in TestRedirectSlashes --- middleware/strip_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/middleware/strip_test.go b/middleware/strip_test.go index 1de90465..a623c772 100644 --- a/middleware/strip_test.go +++ b/middleware/strip_test.go @@ -125,25 +125,25 @@ func TestRedirectSlashes(t *testing.T) { ts := httptest.NewServer(r) defer ts.Close() - if resp, body := testRequest(t, ts, "GET", "/", nil); body != "root" && resp.StatusCode != 200 { + if resp, body := testRequest(t, ts, "GET", "/", nil); body != "root" || resp.StatusCode != 200 { t.Fatalf(body) } // NOTE: the testRequest client will follow the redirection.. - if resp, body := testRequest(t, ts, "GET", "//", nil); body != "root" && resp.StatusCode != 200 { + if resp, body := testRequest(t, ts, "GET", "//", nil); body != "root" || resp.StatusCode != 200 { t.Fatalf(body) } - if resp, body := testRequest(t, ts, "GET", "/accounts/admin", nil); body != "admin" && resp.StatusCode != 200 { + if resp, body := testRequest(t, ts, "GET", "/accounts/admin", nil); body != "admin" || resp.StatusCode != 200 { t.Fatalf(body) } // NOTE: the testRequest client will follow the redirection.. - if resp, body := testRequest(t, ts, "GET", "/accounts/admin/", nil); body != "admin" && resp.StatusCode != 200 { + if resp, body := testRequest(t, ts, "GET", "/accounts/admin/", nil); body != "admin" || resp.StatusCode != 200 { t.Fatalf(body) } - if resp, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" && resp.StatusCode != 200 { + if resp, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" || resp.StatusCode != 404 { t.Fatalf(body) } @@ -180,7 +180,7 @@ func TestRedirectSlashes(t *testing.T) { if u, err := url.Parse(ts.URL); err != nil && resp.Request.URL.Host != u.Host { t.Fatalf("host should remain the same. got: %q, want: %q", resp.Request.URL.Host, ts.URL) } - if body != "nothing here" && resp.StatusCode != 404 { + if body != "nothing here" || resp.StatusCode != 404 { t.Fatalf(body) } } @@ -192,7 +192,7 @@ func TestRedirectSlashes(t *testing.T) { if u, err := url.Parse(ts.URL); err != nil && resp.Request.URL.Host != u.Host { t.Fatalf("host should remain the same. got: %q, want: %q", resp.Request.URL.Host, ts.URL) } - if body != "nothing here" && resp.StatusCode != 404 { + if body != "nothing here" || resp.StatusCode != 404 { t.Fatalf(body) } } From a2d4024fb66d2db8f5106443fed875192c271950 Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Wed, 18 Sep 2024 16:34:42 +0200 Subject: [PATCH 2/2] Improve test logs in middleware/strip_test.go --- middleware/strip_test.go | 52 ++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/middleware/strip_test.go b/middleware/strip_test.go index a623c772..51fa9393 100644 --- a/middleware/strip_test.go +++ b/middleware/strip_test.go @@ -37,19 +37,19 @@ func TestStripSlashes(t *testing.T) { defer ts.Close() if _, resp := testRequest(t, ts, "GET", "/", nil); resp != "root" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "//", nil); resp != "root" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "admin" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "admin" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/nothing-here", nil); resp != "nothing here" { - t.Fatalf(resp) + t.Fatal(resp) } } @@ -80,22 +80,22 @@ func TestStripSlashesInRoute(t *testing.T) { defer ts.Close() if _, resp := testRequest(t, ts, "GET", "/hi", nil); resp != "hi" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/hi/", nil); resp != "nothing here" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "accounts index" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "accounts index" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/accounts/admin/query", nil); resp != "admin" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/accounts/admin/query/", nil); resp != "admin" { - t.Fatalf(resp) + t.Fatal(resp) } } @@ -126,32 +126,32 @@ func TestRedirectSlashes(t *testing.T) { defer ts.Close() if resp, body := testRequest(t, ts, "GET", "/", nil); body != "root" || resp.StatusCode != 200 { - t.Fatalf(body) + t.Fatal(body, resp.StatusCode) } // NOTE: the testRequest client will follow the redirection.. if resp, body := testRequest(t, ts, "GET", "//", nil); body != "root" || resp.StatusCode != 200 { - t.Fatalf(body) + t.Fatal(body, resp.StatusCode) } if resp, body := testRequest(t, ts, "GET", "/accounts/admin", nil); body != "admin" || resp.StatusCode != 200 { - t.Fatalf(body) + t.Fatal(body, resp.StatusCode) } // NOTE: the testRequest client will follow the redirection.. if resp, body := testRequest(t, ts, "GET", "/accounts/admin/", nil); body != "admin" || resp.StatusCode != 200 { - t.Fatalf(body) + t.Fatal(body, resp.StatusCode) } if resp, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" || resp.StatusCode != 404 { - t.Fatalf(body) + t.Fatal(body, resp.StatusCode) } // Ensure redirect Location url is correct { resp, body := testRequestNoRedirect(t, ts, "GET", "/accounts/someuser/", nil) if resp.StatusCode != 301 { - t.Fatalf(body) + t.Fatal(body, resp.StatusCode) } location := resp.Header.Get("Location") if !strings.HasPrefix(location, "//") || !strings.HasSuffix(location, "/accounts/someuser") { @@ -163,7 +163,7 @@ func TestRedirectSlashes(t *testing.T) { { resp, body := testRequestNoRedirect(t, ts, "GET", "/accounts/someuser/?a=1&b=2", nil) if resp.StatusCode != 301 { - t.Fatalf(body) + t.Fatal(body, resp.StatusCode) } location := resp.Header.Get("Location") if !strings.HasPrefix(location, "//") || !strings.HasSuffix(location, "/accounts/someuser?a=1&b=2") { @@ -181,7 +181,7 @@ func TestRedirectSlashes(t *testing.T) { t.Fatalf("host should remain the same. got: %q, want: %q", resp.Request.URL.Host, ts.URL) } if body != "nothing here" || resp.StatusCode != 404 { - t.Fatalf(body) + t.Fatal(body, resp.StatusCode) } } } @@ -193,7 +193,7 @@ func TestRedirectSlashes(t *testing.T) { t.Fatalf("host should remain the same. got: %q, want: %q", resp.Request.URL.Host, ts.URL) } if body != "nothing here" || resp.StatusCode != 404 { - t.Fatalf(body) + t.Fatal(body, resp.StatusCode) } } } @@ -219,21 +219,21 @@ func TestStripSlashesWithNilContext(t *testing.T) { defer ts.Close() if _, resp := testRequest(t, ts, "GET", "/", nil); resp != "root" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "//", nil); resp != "root" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/accounts", nil); resp != "accounts" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/accounts/", nil); resp != "accounts" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "admin" { - t.Fatalf(resp) + t.Fatal(resp) } if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "admin" { - t.Fatalf(resp) + t.Fatal(resp) } }