Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix condition in TestRedirectSlashes #856

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}