Skip to content

Commit

Permalink
test: test html response for error
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed May 16, 2023
1 parent d186afb commit 1b494ca
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,44 @@ func TestPretty404(t *testing.T) {
}
}

func TestBrowserErrorHTML(t *testing.T) {
ts, _, root := newTestServerAndNode(t, nil)
t.Logf("test server url: %s", ts.URL)

t.Run("plain error if request does not have Accept: text/html", func(t *testing.T) {
t.Parallel()

req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipfs/"+root.String()+"/nonexisting-link", nil)
assert.Nil(t, err)

res, err := doWithoutRedirect(req)
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
assert.NotContains(t, res.Header.Get("Content-Type"), "text/html")

body, err := io.ReadAll(res.Body)
assert.Nil(t, err)
assert.NotContains(t, string(body), "<!DOCTYPE html>")
})

t.Run("html error if request has Accept: text/html", func(t *testing.T) {
t.Parallel()

req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipfs/"+root.String()+"/nonexisting-link", nil)
assert.Nil(t, err)
req.Header.Set("Accept", "text/html")

res, err := doWithoutRedirect(req)
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
assert.Contains(t, res.Header.Get("Content-Type"), "text/html")

body, err := io.ReadAll(res.Body)
assert.Nil(t, err)
assert.Contains(t, string(body), "<!DOCTYPE html>")
})
}

func TestCacheControlImmutable(t *testing.T) {
ts, _, root := newTestServerAndNode(t, nil)
t.Logf("test server url: %s", ts.URL)
Expand Down

0 comments on commit 1b494ca

Please sign in to comment.