From 52b93a7276d8422181daad2c05682dae50e1070a Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 12 May 2023 13:29:07 +0200 Subject: [PATCH] test: test html response for error --- gateway/gateway_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go index 7eada3a1a..18f5b2457 100644 --- a/gateway/gateway_test.go +++ b/gateway/gateway_test.go @@ -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), "") + }) + + 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), "") + }) +} + func TestCacheControlImmutable(t *testing.T) { ts, _, root := newTestServerAndNode(t, nil) t.Logf("test server url: %s", ts.URL)