From 73350509aa6e198d6677df0f09c5611446e96da4 Mon Sep 17 00:00:00 2001 From: Taichi Sasaki Date: Wed, 18 Sep 2024 23:41:00 +0900 Subject: [PATCH] Fix condition in TestRedirectSlashes (#856) * Fix condition in TestRedirectSlashes * Improve test logs in middleware/strip_test.go --------- Co-authored-by: Vojtech Vitek --- middleware/strip_test.go | 66 ++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/middleware/strip_test.go b/middleware/strip_test.go index 1de90465..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) } } @@ -125,33 +125,33 @@ 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 { - t.Fatalf(body) + if resp, body := testRequest(t, ts, "GET", "/", nil); body != "root" || resp.StatusCode != 200 { + 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) + if resp, body := testRequest(t, ts, "GET", "//", nil); body != "root" || resp.StatusCode != 200 { + t.Fatal(body, resp.StatusCode) } - 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", "/accounts/admin", nil); body != "admin" || resp.StatusCode != 200 { + 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) + if resp, body := testRequest(t, ts, "GET", "/accounts/admin/", nil); body != "admin" || resp.StatusCode != 200 { + t.Fatal(body, resp.StatusCode) } - if resp, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" && resp.StatusCode != 200 { - t.Fatalf(body) + if resp, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" || resp.StatusCode != 404 { + 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") { @@ -180,8 +180,8 @@ 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 { - t.Fatalf(body) + if body != "nothing here" || resp.StatusCode != 404 { + t.Fatal(body, resp.StatusCode) } } } @@ -192,8 +192,8 @@ 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 { - t.Fatalf(body) + if body != "nothing here" || resp.StatusCode != 404 { + 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) } }