Skip to content

Commit

Permalink
feat: TestPretty404
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 7, 2023
1 parent c3d64c7 commit 989a077
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,65 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
}
}

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

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

k, err := api.ResolvePath(ctx, ipath.Join(ipath.IpfsPath(root), t.Name()))
if err != nil {
t.Fatal(err)
}

host := "example.net"
api.namesys["/ipns/"+host] = path.FromString(k.String())

for _, test := range []struct {
path string
accept string
status int
text string
}{
{"/ipfs-404.html", "text/html", http.StatusOK, "Custom 404"},
{"/nope", "text/html", http.StatusNotFound, "Custom 404"},
{"/nope", "text/*", http.StatusNotFound, "Custom 404"},
{"/nope", "*/*", http.StatusNotFound, "Custom 404"},
{"/nope", "application/json", http.StatusNotFound, fmt.Sprintf("ipfs resolve -r /ipns/example.net/nope: no link named \"nope\" under %s\n", k.Cid().String())},
{"/deeper/nope", "text/html", http.StatusNotFound, "Deep custom 404"},
{"/deeper/", "text/html", http.StatusOK, ""},
{"/deeper", "text/html", http.StatusOK, ""},
{"/nope/nope", "text/html", http.StatusNotFound, "Custom 404"},
} {
var c http.Client
req, err := http.NewRequest("GET", ts.URL+test.path, nil)
if err != nil {
t.Fatal(err)
}
req.Header.Add("Accept", test.accept)
req.Host = host
resp, err := c.Do(req)

if err != nil {
t.Fatalf("error requesting %s: %s", test.path, err)
}

defer resp.Body.Close()
if resp.StatusCode != test.status {
t.Fatalf("got %d, expected %d, from %s", resp.StatusCode, test.status, test.path)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("error reading response from %s: %s", test.path, err)
}

if test.text != "" && string(body) != test.text {
t.Fatalf("unexpected response body from %s: got %q, expected %q", test.path, body, test.text)
}
}
}

func TestCacheControlImmutable(t *testing.T) {
ts, _, root := newTestServerAndNode(t, nil)
t.Logf("test server url: %s", ts.URL)
Expand Down
Binary file modified gateway/test.car
Binary file not shown.

0 comments on commit 989a077

Please sign in to comment.