Skip to content

Commit

Permalink
refactor: separate fixture files
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jun 26, 2023
1 parent 57bbd67 commit c1c0ec8
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 46 deletions.
48 changes: 17 additions & 31 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func TestGatewayGet(t *testing.T) {
ts, backend, root := newTestServerAndNode(t, nil)
ts, backend, root := newTestServerAndNode(t, nil, "fixtures.car")

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -86,17 +86,11 @@ func TestGatewayGet(t *testing.T) {
}

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

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

k, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(ipath.IpfsPath(root), "subdir-404"))
assert.NoError(t, err)

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

for _, test := range []struct {
path string
Expand All @@ -108,7 +102,7 @@ func TestPretty404(t *testing.T) {
{"/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("failed to resolve /ipns/example.net/nope: no link named \"nope\" under %s\n", k.Cid().String())},
{"/nope", "application/json", http.StatusNotFound, fmt.Sprintf("failed to resolve /ipns/example.net/nope: no link named \"nope\" under %s\n", root.String())},
{"/deeper/nope", "text/html", http.StatusNotFound, "Deep custom 404"},
{"/deeper/", "text/html", http.StatusOK, ""},
{"/deeper", "text/html", http.StatusOK, ""},
Expand All @@ -134,12 +128,12 @@ func TestPretty404(t *testing.T) {
func TestHeaders(t *testing.T) {
t.Parallel()

ts, _, root := newTestServerAndNode(t, nil)
ts, _, root := newTestServerAndNode(t, nil, "headers-test.car")

var (
dirCID = "bafybeihta5xfgxcmyxyq6druvidc7es6ogffdd6zel22l3y4wddju5xxsu"
dirPath = "/ipfs/" + root.String() + "/subdir/"
dirRoots = "bafybeifhvgr4ufgwpoj2iyrymrigjhpexvsyfm2elafebkmrei4skunihe," + dirCID
dirRoots = root.String() + "," + dirCID

fileCID = "bafkreiba3vpkcqpc6xtp3hsatzcod6iwneouzjoq7ymy4m2js6gc3czt6i"
filePath = "/ipfs/" + root.String() + "/subdir/fnord"
Expand Down Expand Up @@ -176,19 +170,19 @@ func TestHeaders(t *testing.T) {
}
test("", dirPath, `"DirIndex-(.*)_CID-%s"`, dirCID)
test("text/html", dirPath, `"DirIndex-(.*)_CID-%s"`, dirCID)
test(carResponseFormat, dirPath, `W/"%s.car.5ovg7dign8ug"`, root.String()) // ETags of CARs on a Path have the root CID in the Etag and hashed information to derive the correct Etag of the full request.
test(carResponseFormat, dirPath, `W/"%s.car.7of9u8ojv38vd"`, root.String()) // ETags of CARs on a Path have the root CID in the Etag and hashed information to derive the correct Etag of the full request.
test(rawResponseFormat, dirPath, `"%s.raw"`, dirCID)
test(tarResponseFormat, dirPath, `W/"%s.x-tar"`, dirCID)

test("", filePath, `"%s"`, fileCID)
test("text/html", filePath, `"%s"`, fileCID)
test(carResponseFormat, filePath, `W/"%s.car.fivdlu5uk7ab6"`, root.String())
test(carResponseFormat, filePath, `W/"%s.car.fgq8i0qnhsq01"`, root.String())
test(rawResponseFormat, filePath, `"%s.raw"`, fileCID)
test(tarResponseFormat, filePath, `W/"%s.x-tar"`, fileCID)

test("", dagCborPath, `"%s.dag-cbor"`, dagCborCID)
test("text/html", dagCborPath+"/", `"DagIndex-(.*)_CID-%s"`, dagCborCID)
test(carResponseFormat, dagCborPath, `W/"%s.car.5b4vl0vt6odpt"`, root.String())
test(carResponseFormat, dagCborPath, `W/"%s.car.5mg3mekeviba5"`, root.String())
test(rawResponseFormat, dagCborPath, `"%s.raw"`, dagCborCID)
test(dagJsonResponseFormat, dagCborPath, `"%s.dag-json"`, dagCborCID)
test(dagCborResponseFormat, dagCborPath, `"%s.dag-cbor"`, dagCborCID)
Expand Down Expand Up @@ -299,7 +293,7 @@ func TestHeaders(t *testing.T) {
}

func TestGoGetSupport(t *testing.T) {
ts, _, root := newTestServerAndNode(t, nil)
ts, _, root := newTestServerAndNode(t, nil, "fixtures.car")

// mimic go-get
req := mustNewRequest(t, http.MethodGet, ts.URL+"/ipfs/"+root.String()+"?go-get=1", nil)
Expand All @@ -311,7 +305,7 @@ func TestRedirects(t *testing.T) {
t.Parallel()

t.Run("IPNS Base58 Multihash Redirect", func(t *testing.T) {
ts, _, _ := newTestServerAndNode(t, nil)
ts, _, _ := newTestServerAndNode(t, nil, "fixtures.car")

t.Run("ED25519 Base58-encoded key", func(t *testing.T) {
t.Parallel()
Expand All @@ -332,7 +326,7 @@ func TestRedirects(t *testing.T) {

t.Run("URI Query Redirects", func(t *testing.T) {
t.Parallel()
ts, _, _ := newTestServerAndNode(t, mockNamesys{})
ts, _, _ := newTestServerAndNode(t, mockNamesys{}, "fixtures.car")

cid := "QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR"
for _, test := range []struct {
Expand Down Expand Up @@ -371,16 +365,8 @@ func TestRedirects(t *testing.T) {
t.Run("IPNS Hostname Redirects", func(t *testing.T) {
t.Parallel()

ts, backend, root := newTestServerAndNode(t, nil)

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

k, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(ipath.IpfsPath(root), "TestIPNSHostnameRedirect"))
require.NoError(t, err)

t.Logf("k: %s\n", k)
backend.namesys["/ipns/example.net"] = path.FromString(k.String())
ts, backend, root := newTestServerAndNode(t, nil, "ipns-hostname-redirects.car")
backend.namesys["/ipns/example.net"] = path.FromCid(root)

// make request to directory containing index.html
req := mustNewRequest(t, http.MethodGet, ts.URL+"/foo", nil)
Expand Down Expand Up @@ -418,7 +404,7 @@ func TestDeserializedResponses(t *testing.T) {
t.Run("IPFS", func(t *testing.T) {
t.Parallel()

backend, root := newMockBackend(t)
backend, root := newMockBackend(t, "fixtures.car")

ts := newTestServerWithConfig(t, backend, Config{
Headers: map[string][]string{},
Expand Down Expand Up @@ -496,7 +482,7 @@ func TestDeserializedResponses(t *testing.T) {
t.Run("IPNS", func(t *testing.T) {
t.Parallel()

backend, root := newMockBackend(t)
backend, root := newMockBackend(t, "fixtures.car")
backend.namesys["/ipns/trustless.com"] = path.FromCid(root)
backend.namesys["/ipns/trusted.com"] = path.FromCid(root)

Expand Down Expand Up @@ -697,7 +683,7 @@ func TestPanicStatusCode(t *testing.T) {

func TestBrowserErrorHTML(t *testing.T) {
t.Parallel()
ts, _, root := newTestServerAndNode(t, nil)
ts, _, root := newTestServerAndNode(t, nil, "fixtures.car")

t.Run("plain error if request does not have Accept: text/html", func(t *testing.T) {
t.Parallel()
Expand Down
2 changes: 1 addition & 1 deletion gateway/handler_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestDagJsonCborPreview(t *testing.T) {
t.Parallel()
backend, root := newMockBackend(t)
backend, root := newMockBackend(t, "fixtures.car")

ts := newTestServerWithConfig(t, backend, Config{
Headers: map[string][]string{},
Expand Down
14 changes: 5 additions & 9 deletions gateway/handler_unixfs_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@ import (

func TestIPNSHostnameBacklinks(t *testing.T) {
// Test if directory listing on DNSLink Websites have correct backlinks.
ts, backend, root := newTestServerAndNode(t, nil)
ts, backend, root := newTestServerAndNode(t, nil, "dir-special-chars.car")

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

k, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(ipath.IpfsPath(root), "subdir-special-chars"))
require.NoError(t, err)

// create /ipns/example.net/foo/
k2, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(k, "foo? #<'"))
k2, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(ipath.IpfsPath(root), "foo? #<'"))
require.NoError(t, err)

k3, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(k, "foo? #<'/bar"))
k3, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(ipath.IpfsPath(root), "foo? #<'/bar"))
require.NoError(t, err)

t.Logf("k: %s\n", k)
backend.namesys["/ipns/example.net"] = path.FromString(k.String())
backend.namesys["/ipns/example.net"] = path.FromCid(root)

// make request to directory listing
req := mustNewRequest(t, http.MethodGet, ts.URL+"/foo%3F%20%23%3C%27/", nil)
Expand Down Expand Up @@ -69,7 +65,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
require.Contains(t, s, "<a href=\"/file.txt\">", "expected file in directory listing")
// https://github.com/ipfs/dir-index-html/issues/42
require.Contains(t, s, "<a class=\"ipfs-hash\" translate=\"no\" href=\"https://cid.ipfs.tech/#", "expected links to cid.ipfs.tech in CID column when on DNSLink website")
require.Contains(t, s, k.Cid().String(), "expected hash in directory listing")
require.Contains(t, s, root.String(), "expected hash in directory listing")

// make request to directory listing
req = mustNewRequest(t, http.MethodGet, ts.URL+"/foo%3F%20%23%3C%27/bar/", nil)
Expand Down
2 changes: 1 addition & 1 deletion gateway/hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestToSubdomainURL(t *testing.T) {
t.Parallel()

backend, _ := newMockBackend(t)
backend, _ := newMockBackend(t, "fixtures.car")
testCID, err := cid.Decode("bafkqaglimvwgy3zakrsxg5cun5jxkyten5wwc2lokvjeycq")
require.NoError(t, err)

Expand Down
Binary file added gateway/testdata/dir-special-chars.car
Binary file not shown.
Binary file modified gateway/testdata/fixtures.car
Binary file not shown.
Binary file added gateway/testdata/headers-test.car
Binary file not shown.
Binary file added gateway/testdata/ipns-hostname-redirects.car
Binary file not shown.
Binary file added gateway/testdata/pretty-404.car
Binary file not shown.
9 changes: 5 additions & 4 deletions gateway/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -102,8 +103,8 @@ type mockBackend struct {

var _ IPFSBackend = (*mockBackend)(nil)

func newMockBackend(t *testing.T) (*mockBackend, cid.Cid) {
r, err := os.Open("./testdata/fixtures.car")
func newMockBackend(t *testing.T, fixturesFile string) (*mockBackend, cid.Cid) {
r, err := os.Open(filepath.Join("./testdata", fixturesFile))
assert.NoError(t, err)

blockStore, err := carblockstore.NewReadOnly(r, nil)
Expand Down Expand Up @@ -202,8 +203,8 @@ func (mb *mockBackend) resolvePathNoRootsReturned(ctx context.Context, ip ipath.
return md.LastSegment, nil
}

func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, *mockBackend, cid.Cid) {
backend, root := newMockBackend(t)
func newTestServerAndNode(t *testing.T, ns mockNamesys, fixturesFile string) (*httptest.Server, *mockBackend, cid.Cid) {
backend, root := newMockBackend(t, fixturesFile)
ts := newTestServer(t, backend)
return ts, backend, root
}
Expand Down

0 comments on commit c1c0ec8

Please sign in to comment.