From 124ae4b3a578bd8e53f16b4e6d460040504d840a Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 23 Feb 2023 13:20:19 +0100 Subject: [PATCH] refactor: use assert in remaining gateway tests --- gateway/gateway_test.go | 339 ++++++++++++--------------------------- gateway/handler_test.go | 5 +- gateway/hostname_test.go | 109 ++++++------- gateway/lazyseek_test.go | 88 +++------- 4 files changed, 179 insertions(+), 362 deletions(-) diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go index 8848b3d32..2b9ef0c01 100644 --- a/gateway/gateway_test.go +++ b/gateway/gateway_test.go @@ -40,6 +40,7 @@ import ( "github.com/ipld/go-ipld-prime/schema" "github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/routing" + "github.com/stretchr/testify/assert" ) type mockNamesys map[string]path.Path @@ -96,14 +97,10 @@ type mockAPI struct { func newMockAPI(t *testing.T) (*mockAPI, cid.Cid) { r, err := os.Open("./testdata/fixtures.car") - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) blockStore, err := carblockstore.NewReadOnly(r, nil) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) t.Cleanup(func() { blockStore.Close() @@ -111,13 +108,8 @@ func newMockAPI(t *testing.T) (*mockAPI, cid.Cid) { }) cids, err := blockStore.Roots() - if err != nil { - t.Fatal(err) - } - - if len(cids) != 1 { - t.Fatal(fmt.Errorf("car has %d roots, expected 1", len(cids))) - } + assert.Nil(t, err) + assert.Len(t, cids, 1) blockService := blockservice.New(blockStore, offline.Exchange(blockStore)) dagService := merkledag.NewDAGService(blockService) @@ -325,9 +317,7 @@ func TestGatewayGet(t *testing.T) { defer cancel() k, err := api.ResolvePath(ctx, ipath.Join(ipath.IpfsPath(root), t.Name(), "fnord")) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) api.namesys["/ipns/example.com"] = path.FromCid(k.Cid()) api.namesys["/ipns/working.example.com"] = path.FromString(k.String()) @@ -344,7 +334,7 @@ func TestGatewayGet(t *testing.T) { api.namesys["/ipns/example.man"] = path.FromString(k.String()) t.Log(ts.URL) - for i, test := range []struct { + for _, test := range []struct { host string path string status int @@ -369,37 +359,21 @@ func TestGatewayGet(t *testing.T) { // This test case ensures we don't treat the TLD as a file extension. {"example.man", "/", http.StatusOK, "fnord"}, } { - var c http.Client - r, err := http.NewRequest(http.MethodGet, ts.URL+test.path, nil) - if err != nil { - t.Fatal(err) - } - r.Host = test.host - resp, err := c.Do(r) - - urlstr := "http://" + test.host + test.path - if err != nil { - t.Errorf("error requesting %s: %s", urlstr, err) - continue - } - defer resp.Body.Close() - contentType := resp.Header.Get("Content-Type") - if contentType != "text/plain; charset=utf-8" { - t.Errorf("expected content type to be text/plain, got %s", contentType) - } - body, err := io.ReadAll(resp.Body) - if resp.StatusCode != test.status { - t.Errorf("(%d) got %d, expected %d from %s", i, resp.StatusCode, test.status, urlstr) - t.Errorf("Body: %s", body) - continue - } - if err != nil { - t.Fatalf("error reading response from %s: %s", urlstr, err) - } - if string(body) != test.text { - t.Errorf("unexpected response body from %s: expected %q; got %q", urlstr, test.text, body) - continue - } + testName := "http://" + test.host + test.path + t.Run(testName, func(t *testing.T) { + var c http.Client + r, err := http.NewRequest(http.MethodGet, ts.URL+test.path, nil) + assert.Nil(t, err) + r.Host = test.host + resp, err := c.Do(r) + assert.Nil(t, err) + defer resp.Body.Close() + assert.Equal(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type")) + body, err := io.ReadAll(resp.Body) + assert.Nil(t, err) + assert.Equal(t, test.status, resp.StatusCode, "body", body) + assert.Equal(t, test.text, string(body)) + }) } } @@ -407,7 +381,7 @@ func TestUriQueryRedirect(t *testing.T) { ts, _, _ := newTestServerAndNode(t, mockNamesys{}) cid := "QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR" - for i, test := range []struct { + for _, test := range []struct { path string status int location string @@ -429,25 +403,16 @@ func TestUriQueryRedirect(t *testing.T) { {"/ipfs/?uri=invaliduri", http.StatusBadRequest, ""}, {"/ipfs/?uri=" + cid, http.StatusBadRequest, ""}, } { - - r, err := http.NewRequest(http.MethodGet, ts.URL+test.path, nil) - if err != nil { - t.Fatal(err) - } - resp, err := doWithoutRedirect(r) - if err != nil { - t.Fatal(err) - } - defer resp.Body.Close() - - if resp.StatusCode != test.status { - t.Errorf("(%d) got %d, expected %d from %s", i, resp.StatusCode, test.status, ts.URL+test.path) - } - - locHdr := resp.Header.Get("Location") - if locHdr != test.location { - t.Errorf("(%d) location header got %s, expected %s from %s", i, locHdr, test.location, ts.URL+test.path) - } + testName := ts.URL + test.path + t.Run(testName, func(t *testing.T) { + r, err := http.NewRequest(http.MethodGet, ts.URL+test.path, nil) + assert.Nil(t, err) + resp, err := doWithoutRedirect(r) + assert.Nil(t, err) + defer resp.Body.Close() + assert.Equal(t, test.status, resp.StatusCode) + assert.Equal(t, test.location, resp.Header.Get("Location")) + }) } } @@ -459,74 +424,47 @@ func TestIPNSHostnameRedirect(t *testing.T) { defer cancel() k, err := api.ResolvePath(ctx, ipath.Join(ipath.IpfsPath(root), t.Name())) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) t.Logf("k: %s\n", k) api.namesys["/ipns/example.net"] = path.FromString(k.String()) // make request to directory containing index.html req, err := http.NewRequest(http.MethodGet, ts.URL+"/foo", nil) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) req.Host = "example.net" res, err := doWithoutRedirect(req) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) // expect 301 redirect to same path, but with trailing slash - if res.StatusCode != 301 { - t.Errorf("status is %d, expected 301", res.StatusCode) - } + assert.Equal(t, http.StatusMovedPermanently, res.StatusCode) hdr := res.Header["Location"] - if len(hdr) < 1 { - t.Errorf("location header not present") - } else if hdr[0] != "/foo/" { - t.Errorf("location header is %v, expected /foo/", hdr[0]) - } + assert.Positive(t, len(hdr), "location header not present") + assert.Equal(t, hdr[0], "/foo/") // make request with prefix to directory containing index.html req, err = http.NewRequest(http.MethodGet, ts.URL+"/foo", nil) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) req.Host = "example.net" res, err = doWithoutRedirect(req) - if err != nil { - t.Fatal(err) - } - + assert.Nil(t, err) // expect 301 redirect to same path, but with prefix and trailing slash - if res.StatusCode != 301 { - t.Errorf("status is %d, expected 301", res.StatusCode) - } + assert.Equal(t, http.StatusMovedPermanently, res.StatusCode) + hdr = res.Header["Location"] - if len(hdr) < 1 { - t.Errorf("location header not present") - } else if hdr[0] != "/foo/" { - t.Errorf("location header is %v, expected /foo/", hdr[0]) - } + assert.Positive(t, len(hdr), "location header not present") + assert.Equal(t, hdr[0], "/foo/") // make sure /version isn't exposed req, err = http.NewRequest(http.MethodGet, ts.URL+"/version", nil) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) req.Host = "example.net" res, err = doWithoutRedirect(req) - if err != nil { - t.Fatal(err) - } - - if res.StatusCode != 404 { - t.Fatalf("expected a 404 error, got: %s", res.Status) - } + assert.Nil(t, err) + assert.Equal(t, http.StatusNotFound, res.StatusCode) } // Test directory listing on DNSLink website @@ -541,130 +479,80 @@ func TestIPNSHostnameBacklinks(t *testing.T) { defer cancel() k, err := api.ResolvePath(ctx, ipath.Join(ipath.IpfsPath(root), t.Name())) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) // create /ipns/example.net/foo/ k2, err := api.ResolvePath(ctx, ipath.Join(k, "foo? #<'")) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) k3, err := api.ResolvePath(ctx, ipath.Join(k, "foo? #<'/bar")) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) t.Logf("k: %s\n", k) api.namesys["/ipns/example.net"] = path.FromString(k.String()) // make request to directory listing req, err := http.NewRequest(http.MethodGet, ts.URL+"/foo%3F%20%23%3C%27/", nil) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) req.Host = "example.net" res, err := doWithoutRedirect(req) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) // expect correct links body, err := io.ReadAll(res.Body) - if err != nil { - t.Fatalf("error reading response: %s", err) - } + assert.Nil(t, err) s := string(body) t.Logf("body: %s\n", string(body)) - if !matchPathOrBreadcrumbs(s, "/ipns/example.net/foo? #<'") { - t.Fatalf("expected a path in directory listing") - } - if !strings.Contains(s, "") { - t.Fatalf("expected backlink in directory listing") - } - if !strings.Contains(s, "") { - t.Fatalf("expected file in directory listing") - } - if !strings.Contains(s, "example.net/foo? #<'"), "expected a path in directory listing") + // https://github.com/ipfs/dir-index-html/issues/42 + assert.Contains(t, s, "", "expected backlink in directory listing") + assert.Contains(t, s, "", "expected file in directory listing") + assert.Contains(t, s, s, k2.Cid().String(), "expected hash in directory listing") // make request to directory listing at root req, err = http.NewRequest(http.MethodGet, ts.URL, nil) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) req.Host = "example.net" res, err = doWithoutRedirect(req) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) // expect correct backlinks at root body, err = io.ReadAll(res.Body) - if err != nil { - t.Fatalf("error reading response: %s", err) - } + assert.Nil(t, err) + s = string(body) t.Logf("body: %s\n", string(body)) - if !matchPathOrBreadcrumbs(s, "/") { - t.Fatalf("expected a path in directory listing") - } - if strings.Contains(s, "") { - t.Fatalf("expected no backlink in directory listing of the root CID") - } - if !strings.Contains(s, "") { - t.Fatalf("expected file in directory listing") - } - if !strings.Contains(s, "", "expected no backlink in directory listing of the root CID") + assert.Contains(t, s, "", "expected file in directory listing") + // https://github.com/ipfs/dir-index-html/issues/42 + assert.Contains(t, s, "example.net/foo? #<'/bar") { - t.Fatalf("expected a path in directory listing") - } - if !strings.Contains(s, "") { - t.Fatalf("expected backlink in directory listing") - } - if !strings.Contains(s, "") { - t.Fatalf("expected file in directory listing") - } - if !strings.Contains(s, k3.Cid().String()) { - t.Fatalf("expected hash in directory listing") - } + assert.True(t, matchPathOrBreadcrumbs(s, "/ipns/example.net/foo? #<'/bar"), "expected a path in directory listing") + assert.Contains(t, s, "", "expected backlink in directory listing") + assert.Contains(t, s, "", "expected file in directory listing") + assert.Contains(t, s, k3.Cid().String(), "expected hash in directory listing") } func TestPretty404(t *testing.T) { @@ -675,9 +563,7 @@ func TestPretty404(t *testing.T) { defer cancel() k, err := api.ResolvePath(ctx, ipath.Join(ipath.IpfsPath(root), t.Name())) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) host := "example.net" api.namesys["/ipns/"+host] = path.FromString(k.String()) @@ -698,31 +584,23 @@ func TestPretty404(t *testing.T) { {"/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) - } + testName := fmt.Sprintf("%s %s", test.path, test.accept) + t.Run(testName, func(t *testing.T) { + var c http.Client + req, err := http.NewRequest("GET", ts.URL+test.path, nil) + assert.Nil(t, err) + req.Header.Add("Accept", test.accept) + req.Host = host + resp, err := c.Do(req) + assert.Nil(t, err) + defer resp.Body.Close() + assert.Equal(t, test.status, resp.StatusCode) + body, err := io.ReadAll(resp.Body) + assert.Nil(t, err) + if test.text != "" { + assert.Equal(t, test.text, string(body)) + } + }) } } @@ -731,22 +609,16 @@ func TestCacheControlImmutable(t *testing.T) { t.Logf("test server url: %s", ts.URL) req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipfs/"+root.String()+"/", nil) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) res, err := doWithoutRedirect(req) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) // check the immutable tag isn't set hdrs, ok := res.Header["Cache-Control"] if ok { for _, hdr := range hdrs { - if strings.Contains(hdr, "immutable") { - t.Fatalf("unexpected Cache-Control: immutable on directory listing: %s", hdr) - } + assert.NotContains(t, hdr, "immutable", "unexpected Cache-Control: immutable on directory listing") } } } @@ -757,16 +629,9 @@ func TestGoGetSupport(t *testing.T) { // mimic go-get req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipfs/"+root.String()+"?go-get=1", nil) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) res, err := doWithoutRedirect(req) - if err != nil { - t.Fatal(err) - } - - if res.StatusCode != 200 { - t.Errorf("status is %d, expected 200", res.StatusCode) - } + assert.Nil(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) } diff --git a/gateway/handler_test.go b/gateway/handler_test.go index 78be41721..c3dcf9606 100644 --- a/gateway/handler_test.go +++ b/gateway/handler_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "testing" "time" @@ -36,9 +37,7 @@ func TestEtagMatch(t *testing.T) { {`*`, `"etag"`, "", true}, // wildcard etag match } { result := etagMatch(test.header, test.cidEtag, test.dirEtag) - if result != test.expected { - t.Fatalf("unexpected result of etagMatch(%q, %q, %q), got %t, expected %t", test.header, test.cidEtag, test.dirEtag, result, test.expected) - } + assert.Equalf(t, test.expected, result, "etagMatch(%q, %q, %q)", test.header, test.cidEtag, test.dirEtag) } } diff --git a/gateway/hostname_test.go b/gateway/hostname_test.go index 96905f271..65970f5aa 100644 --- a/gateway/hostname_test.go +++ b/gateway/hostname_test.go @@ -2,20 +2,20 @@ package gateway import ( "errors" + "fmt" "net/http" "net/http/httptest" "testing" cid "github.com/ipfs/go-cid" path "github.com/ipfs/go-path" + "github.com/stretchr/testify/assert" ) func TestToSubdomainURL(t *testing.T) { gwAPI, _ := newMockAPI(t) testCID, err := cid.Decode("bafkqaglimvwgy3zakrsxg5cun5jxkyten5wwc2lokvjeycq") - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) gwAPI.namesys["/ipns/dnslink.long-name.example.com"] = path.FromString(testCID.String()) gwAPI.namesys["/ipns/dnslink.too-long.f1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5o.example.com"] = path.FromString(testCID.String()) @@ -57,11 +57,12 @@ func TestToSubdomainURL(t *testing.T) { {httpRequest, "localhost", true, "/ipns/dnslink.long-name.example.com", "http://dnslink-long--name-example-com.ipns.localhost/", nil}, {httpRequest, "dweb.link", true, "/ipns/dnslink.long-name.example.com", "http://dnslink-long--name-example-com.ipns.dweb.link/", nil}, } { - - url, err := toSubdomainURL(test.gwHostname, test.path, test.request, test.inlineDNSLink, gwAPI) - if url != test.url || !equalError(err, test.err) { - t.Errorf("(%s, %v, %s) returned (%s, %v), expected (%s, %v)", test.gwHostname, test.inlineDNSLink, test.path, url, err, test.url, test.err) - } + testName := fmt.Sprintf("%s, %v, %s", test.gwHostname, test.inlineDNSLink, test.path) + t.Run(testName, func(t *testing.T) { + url, err := toSubdomainURL(test.gwHostname, test.path, test.request, test.inlineDNSLink, gwAPI) + assert.Equal(t, test.url, url) + assert.Equal(t, test.err, err) + }) } } @@ -74,10 +75,11 @@ func TestToDNSLinkDNSLabel(t *testing.T) { {"dnslink.long-name.example.com", "dnslink-long--name-example-com", nil}, {"dnslink.too-long.f1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5o.example.com", "", errors.New("DNSLink representation incompatible with DNS label length limit of 63: dnslink-too--long-f1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5o-example-com")}, } { - out, err := toDNSLinkDNSLabel(test.in) - if out != test.out || !equalError(err, test.err) { - t.Errorf("(%s) returned (%s, %v), expected (%s, %v)", test.in, out, err, test.out, test.err) - } + t.Run(test.in, func(t *testing.T) { + out, err := toDNSLinkDNSLabel(test.in) + assert.Equal(t, test.out, out) + assert.Equal(t, test.err, err) + }) } } @@ -90,10 +92,10 @@ func TestToDNSLinkFQDN(t *testing.T) { {"docs-ipfs-tech", "docs.ipfs.tech"}, {"dnslink-long--name-example-com", "dnslink.long-name.example.com"}, } { - out := toDNSLinkFQDN(test.in) - if out != test.out { - t.Errorf("(%s) returned (%s), expected (%s)", test.in, out, test.out) - } + t.Run(test.in, func(t *testing.T) { + out := toDNSLinkFQDN(test.in) + assert.Equal(t, test.out, out) + }) } } @@ -115,10 +117,11 @@ func TestIsHTTPSRequest(t *testing.T) { {httpProxiedRequest, false}, {oddballRequest, false}, } { - out := isHTTPSRequest(test.in) - if out != test.out { - t.Errorf("(%+v): returned %t, expected %t", test.in, out, test.out) - } + testName := fmt.Sprintf("%+v", test.in) + t.Run(testName, func(t *testing.T) { + out := isHTTPSRequest(test.in) + assert.Equal(t, test.out, out) + }) } } @@ -133,10 +136,11 @@ func TestHasPrefix(t *testing.T) { {[]string{"/version/"}, "/version", true}, {[]string{"/version"}, "/version", true}, } { - out := hasPrefix(test.path, test.prefixes...) - if out != test.out { - t.Errorf("(%+v, %s) returned '%t', expected '%t'", test.prefixes, test.path, out, test.out) - } + testName := fmt.Sprintf("%+v, %s", test.prefixes, test.path) + t.Run(testName, func(t *testing.T) { + out := hasPrefix(test.path, test.prefixes...) + assert.Equal(t, test.out, out) + }) } } @@ -152,10 +156,10 @@ func TestIsDomainNameAndNotPeerID(t *testing.T) { {"12D3KooWFB51PRY9BxcXSH6khFXw1BZeszeLDy7C8GciskqCTZn5", false}, // valid peerid {"k51qzi5uqu5di608geewp3nqkg0bpujoasmka7ftkyxgcm3fh1aroup0gsdrna", false}, // valid peerid } { - out := isDomainNameAndNotPeerID(test.hostname) - if out != test.out { - t.Errorf("(%s) returned '%t', expected '%t'", test.hostname, out, test.out) - } + t.Run(test.hostname, func(t *testing.T) { + out := isDomainNameAndNotPeerID(test.hostname) + assert.Equal(t, test.out, out) + }) } } @@ -172,10 +176,10 @@ func TestPortStripping(t *testing.T) { {"localhost", "localhost"}, {"[::1]:8080", "::1"}, } { - out := stripPort(test.in) - if out != test.out { - t.Errorf("(%s): returned '%s', expected '%s'", test.in, out, test.out) - } + t.Run(test.in, func(t *testing.T) { + out := stripPort(test.in) + assert.Equal(t, test.out, out) + }) } } @@ -194,13 +198,13 @@ func TestToDNSLabel(t *testing.T) { // CIDv1 with long sha512 → error {"bafkrgqe3ohjcjplc6n4f3fwunlj6upltggn7xqujbsvnvyw764srszz4u4rshq6ztos4chl4plgg4ffyyxnayrtdi5oc4xb2332g645433aeg", "", errors.New("CID incompatible with DNS label length limit of 63: kf1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5oj")}, } { - inCID, _ := cid.Decode(test.in) - out, err := toDNSLabel(test.in, inCID) - if out != test.out || !equalError(err, test.err) { - t.Errorf("(%s): returned (%s, %v) expected (%s, %v)", test.in, out, err, test.out, test.err) - } + t.Run(test.in, func(t *testing.T) { + inCID, _ := cid.Decode(test.in) + out, err := toDNSLabel(test.in, inCID) + assert.Equal(t, test.out, out) + assert.Equal(t, test.err, err) + }) } - } func TestKnownSubdomainDetails(t *testing.T) { @@ -274,26 +278,13 @@ func TestKnownSubdomainDetails(t *testing.T) { {"bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am.ipfs.sub1.sub2.wildcard1.tld", nil, "", "", "", false}, {"bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am.ipfs.sub1.sub2.wildcard2.tld", gwWildcard2, "sub1.sub2.wildcard2.tld", "ipfs", "bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am", true}, } { - gw, hostname, ns, rootID, ok := gateways.knownSubdomainDetails(test.hostHeader) - if ok != test.ok { - t.Errorf("knownSubdomainDetails(%s): ok is %t, expected %t", test.hostHeader, ok, test.ok) - } - if rootID != test.rootID { - t.Errorf("knownSubdomainDetails(%s): rootID is '%s', expected '%s'", test.hostHeader, rootID, test.rootID) - } - if ns != test.ns { - t.Errorf("knownSubdomainDetails(%s): ns is '%s', expected '%s'", test.hostHeader, ns, test.ns) - } - if hostname != test.hostname { - t.Errorf("knownSubdomainDetails(%s): hostname is '%s', expected '%s'", test.hostHeader, hostname, test.hostname) - } - if gw != test.gw { - t.Errorf("knownSubdomainDetails(%s): gw is %+v, expected %+v", test.hostHeader, gw, test.gw) - } + t.Run(test.hostHeader, func(t *testing.T) { + gw, hostname, ns, rootID, ok := gateways.knownSubdomainDetails(test.hostHeader) + assert.Equal(t, test.ok, ok) + assert.Equal(t, test.rootID, rootID) + assert.Equal(t, test.ns, ns) + assert.Equal(t, test.hostname, hostname) + assert.Equal(t, test.gw, gw) + }) } - -} - -func equalError(a, b error) bool { - return (a == nil && b == nil) || (a != nil && b != nil && a.Error() == b.Error()) } diff --git a/gateway/lazyseek_test.go b/gateway/lazyseek_test.go index 09997a797..b10b6a275 100644 --- a/gateway/lazyseek_test.go +++ b/gateway/lazyseek_test.go @@ -5,6 +5,8 @@ import ( "io" "strings" "testing" + + "github.com/stretchr/testify/assert" ) type badSeeker struct { @@ -28,57 +30,33 @@ func TestLazySeekerError(t *testing.T) { size: underlyingBuffer.Size(), } off, err := s.Seek(0, io.SeekEnd) - if err != nil { - t.Fatal(err) - } - if off != s.size { - t.Fatal("expected to seek to the end") - } + assert.Nil(t, err) + assert.Equal(t, s.size, off, "expected to seek to the end") // shouldn't have actually seeked. b, err := io.ReadAll(s) - if err != nil { - t.Fatal(err) - } - if len(b) != 0 { - t.Fatal("expected to read nothing") - } + assert.Nil(t, err) + assert.Equal(t, 0, len(b), "expected to read nothing") // shouldn't need to actually seek. off, err = s.Seek(0, io.SeekStart) - if err != nil { - t.Fatal(err) - } - if off != 0 { - t.Fatal("expected to seek to the start") - } + assert.Nil(t, err) + assert.Equal(t, int64(0), off, "expected to seek to the start") + b, err = io.ReadAll(s) - if err != nil { - t.Fatal(err) - } - if string(b) != "fubar" { - t.Fatal("expected to read string") - } + assert.Nil(t, err) + assert.Equal(t, "fubar", string(b), "expected to read string") // should fail the second time. off, err = s.Seek(0, io.SeekStart) - if err != nil { - t.Fatal(err) - } - if off != 0 { - t.Fatal("expected to seek to the start") - } + assert.Nil(t, err) + assert.Equal(t, int64(0), off, "expected to seek to the start") + // right here... b, err = io.ReadAll(s) - if err == nil { - t.Fatalf("expected an error, got output %s", string(b)) - } - if err != errBadSeek { - t.Fatalf("expected a bad seek error, got %s", err) - } - if len(b) != 0 { - t.Fatalf("expected to read nothing") - } + assert.NotNil(t, err) + assert.Equal(t, errBadSeek, err) + assert.Equal(t, 0, len(b), "expected to read nothing") } func TestLazySeeker(t *testing.T) { @@ -91,41 +69,25 @@ func TestLazySeeker(t *testing.T) { t.Helper() var buf [1]byte n, err := io.ReadFull(s, buf[:]) - if err != nil { - t.Fatal(err) - } - if n != 1 { - t.Fatalf("expected to read one byte, read %d", n) - } - if buf[0] != b { - t.Fatalf("expected %b, got %b", b, buf[0]) - } + assert.Nil(t, err) + assert.Equal(t, 1, n, "expected to read one byte, read %d", n) + assert.Equal(t, b, buf[0]) } expectSeek := func(whence int, off, expOff int64, expErr string) { t.Helper() n, err := s.Seek(off, whence) if expErr == "" { - if err != nil { - t.Fatal("unexpected seek error: ", err) - } + assert.Nil(t, err) } else { - if err == nil || err.Error() != expErr { - t.Fatalf("expected %s, got %s", err, expErr) - } - } - if n != expOff { - t.Fatalf("expected offset %d, got, %d", expOff, n) + assert.EqualError(t, err, expErr) } + assert.Equal(t, expOff, n) } expectSeek(io.SeekEnd, 0, s.size, "") b, err := io.ReadAll(s) - if err != nil { - t.Fatal(err) - } - if len(b) != 0 { - t.Fatal("expected to read nothing") - } + assert.Nil(t, err) + assert.Equal(t, 0, len(b), "expected to read nothing") expectSeek(io.SeekEnd, -1, s.size-1, "") expectByte('r') expectSeek(io.SeekStart, 0, 0, "")