Skip to content

Commit

Permalink
Fix condition in TestRedirectSlashes (#856)
Browse files Browse the repository at this point in the history
* Fix condition in TestRedirectSlashes

* Improve test logs in middleware/strip_test.go

---------

Co-authored-by: Vojtech Vitek <vojtech.vitek@golang.cz>
  • Loading branch information
tchssk and VojtechVitek authored Sep 18, 2024
1 parent df0303d commit 7335050
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions middleware/strip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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") {
Expand All @@ -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") {
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}

0 comments on commit 7335050

Please sign in to comment.