Skip to content

Commit cec853f

Browse files
committed
feat: add a bunch of tests
1 parent 0dad447 commit cec853f

File tree

4 files changed

+168
-19
lines changed

4 files changed

+168
-19
lines changed

gateway/gateway_test.go

Lines changed: 166 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestGatewayGet(t *testing.T) {
2626
ctx, cancel := context.WithCancel(context.Background())
2727
defer cancel()
2828

29-
k, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(ipath.IpfsPath(root), t.Name(), "fnord"))
29+
k, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(ipath.IpfsPath(root), "subdir", "fnord"))
3030
require.NoError(t, err)
3131

3232
backend.namesys["/ipns/example.com"] = path.FromCid(k.Cid())
@@ -92,7 +92,7 @@ func TestPretty404(t *testing.T) {
9292
ctx, cancel := context.WithCancel(context.Background())
9393
defer cancel()
9494

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

9898
host := "example.net"
@@ -131,19 +131,171 @@ func TestPretty404(t *testing.T) {
131131
}
132132
}
133133

134-
func TestCacheControlImmutable(t *testing.T) {
134+
func TestHeaders(t *testing.T) {
135+
t.Parallel()
136+
135137
ts, _, root := newTestServerAndNode(t, nil)
136138

137-
req := mustNewRequest(t, http.MethodGet, ts.URL+"/ipfs/"+root.String()+"/", nil)
138-
res := mustDoWithoutRedirect(t, req)
139+
var (
140+
dirCID = "bafybeihta5xfgxcmyxyq6druvidc7es6ogffdd6zel22l3y4wddju5xxsu"
141+
dirPath = "/ipfs/" + root.String() + "/subdir/"
142+
dirRoots = "bafybeifhvgr4ufgwpoj2iyrymrigjhpexvsyfm2elafebkmrei4skunihe," + dirCID
143+
144+
fileCID = "bafkreiba3vpkcqpc6xtp3hsatzcod6iwneouzjoq7ymy4m2js6gc3czt6i"
145+
filePath = "/ipfs/" + root.String() + "/subdir/fnord"
146+
fileRoots = dirRoots + "," + fileCID
139147

140-
// check the immutable tag isn't set
141-
hdrs, ok := res.Header["Cache-Control"]
142-
if ok {
143-
for _, hdr := range hdrs {
144-
assert.NotContains(t, hdr, "immutable", "unexpected Cache-Control: immutable on directory listing")
148+
dagCborCID = "bafyreiaocls5bt2ha5vszv5pwz34zzcdf3axk3uqa56bgsgvlkbezw67hq"
149+
dagCborPath = "/ipfs/" + root.String() + "/subdir/dag-cbor-document"
150+
dagCborRoots = dirRoots + "," + dagCborCID
151+
)
152+
153+
t.Run("Control-Cache-Immutable is not immutable for directories", func(t *testing.T) {
154+
req := mustNewRequest(t, http.MethodGet, ts.URL+"/ipfs/"+root.String()+"/", nil)
155+
res := mustDoWithoutRedirect(t, req)
156+
157+
// check the immutable tag isn't set
158+
hdrs, ok := res.Header["Cache-Control"]
159+
if ok {
160+
for _, hdr := range hdrs {
161+
assert.NotContains(t, hdr, "immutable", "unexpected Cache-Control: immutable on directory listing")
162+
}
145163
}
146-
}
164+
})
165+
166+
t.Run("ETag contains expected values", func(t *testing.T) {
167+
test := func(responseFormat string, path string, format string, args ...any) {
168+
t.Run(responseFormat, func(t *testing.T) {
169+
url := ts.URL + path
170+
req := mustNewRequest(t, http.MethodGet, url, nil)
171+
req.Header.Add("Accept", responseFormat)
172+
res := mustDoWithoutRedirect(t, req)
173+
require.Equal(t, http.StatusOK, res.StatusCode)
174+
require.Regexp(t, `^`+fmt.Sprintf(format, args...)+`$`, res.Header.Get("Etag"))
175+
})
176+
}
177+
test("", dirPath, `"DirIndex-(.*)_CID-%s"`, dirCID)
178+
test("text/html", dirPath, `"DirIndex-(.*)_CID-%s"`, dirCID)
179+
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.
180+
test(rawResponseFormat, dirPath, `"%s.raw"`, dirCID)
181+
test(tarResponseFormat, dirPath, `W/"%s.x-tar"`, dirCID)
182+
183+
test("", filePath, `"%s"`, fileCID)
184+
test("text/html", filePath, `"%s"`, fileCID)
185+
test(carResponseFormat, filePath, `W/"%s.car.fivdlu5uk7ab6"`, root.String())
186+
test(rawResponseFormat, filePath, `"%s.raw"`, fileCID)
187+
test(tarResponseFormat, filePath, `W/"%s.x-tar"`, fileCID)
188+
189+
test("", dagCborPath, `"%s.dag-cbor"`, dagCborCID)
190+
test("text/html", dagCborPath+"/", `"DagIndex-(.*)_CID-%s"`, dagCborCID)
191+
test(carResponseFormat, dagCborPath, `W/"%s.car.5b4vl0vt6odpt"`, root.String())
192+
test(rawResponseFormat, dagCborPath, `"%s.raw"`, dagCborCID)
193+
test(dagJsonResponseFormat, dagCborPath, `"%s.dag-json"`, dagCborCID)
194+
test(dagCborResponseFormat, dagCborPath, `"%s.dag-cbor"`, dagCborCID)
195+
})
196+
197+
t.Run("If-None-Match with previous Etag returns Not Modified", func(t *testing.T) {
198+
test := func(responseFormat string, path string) {
199+
t.Run(responseFormat, func(t *testing.T) {
200+
url := ts.URL + path
201+
req := mustNewRequest(t, http.MethodGet, url, nil)
202+
req.Header.Add("Accept", responseFormat)
203+
res := mustDoWithoutRedirect(t, req)
204+
require.Equal(t, http.StatusOK, res.StatusCode)
205+
etag := res.Header.Get("Etag")
206+
require.NotEmpty(t, etag)
207+
req = mustNewRequest(t, http.MethodGet, url, nil)
208+
req.Header.Add("Accept", responseFormat)
209+
req.Header.Add("If-None-Match", etag)
210+
res = mustDoWithoutRedirect(t, req)
211+
require.Equal(t, http.StatusNotModified, res.StatusCode)
212+
})
213+
}
214+
215+
test("", dirPath)
216+
test("text/html", dirPath)
217+
test(carResponseFormat, dirPath)
218+
test(rawResponseFormat, dirPath)
219+
test(tarResponseFormat, dirPath)
220+
221+
test("", filePath)
222+
test("text/html", filePath)
223+
test(carResponseFormat, filePath)
224+
test(rawResponseFormat, filePath)
225+
test(tarResponseFormat, filePath)
226+
227+
test("", dagCborPath)
228+
test("text/html", dagCborPath+"/")
229+
test(carResponseFormat, dagCborPath)
230+
test(rawResponseFormat, dagCborPath)
231+
test(dagJsonResponseFormat, dagCborPath)
232+
test(dagCborResponseFormat, dagCborPath)
233+
})
234+
235+
t.Run("X-Ipfs-Roots contains expected values", func(t *testing.T) {
236+
test := func(responseFormat string, path string, roots string) {
237+
t.Run(responseFormat, func(t *testing.T) {
238+
url := ts.URL + path
239+
req := mustNewRequest(t, http.MethodGet, url, nil)
240+
req.Header.Add("Accept", responseFormat)
241+
res := mustDoWithoutRedirect(t, req)
242+
require.Equal(t, http.StatusOK, res.StatusCode)
243+
require.Equal(t, roots, res.Header.Get("X-Ipfs-Roots"))
244+
})
245+
}
246+
247+
test("", dirPath, dirRoots)
248+
test("text/html", dirPath, dirRoots)
249+
test(carResponseFormat, dirPath, dirRoots)
250+
test(rawResponseFormat, dirPath, dirRoots)
251+
test(tarResponseFormat, dirPath, dirRoots)
252+
253+
test("", filePath, fileRoots)
254+
test("text/html", filePath, fileRoots)
255+
test(carResponseFormat, filePath, fileRoots)
256+
test(rawResponseFormat, filePath, fileRoots)
257+
test(tarResponseFormat, filePath, fileRoots)
258+
259+
test("", dagCborPath, dagCborRoots)
260+
test("text/html", dagCborPath+"/", dagCborRoots)
261+
test(carResponseFormat, dagCborPath, dagCborRoots)
262+
test(rawResponseFormat, dagCborPath, dagCborRoots)
263+
test(dagJsonResponseFormat, dagCborPath, dagCborRoots)
264+
test(dagCborResponseFormat, dagCborPath, dagCborRoots)
265+
})
266+
267+
t.Run("If-None-Match with wrong value forces path resolution, but X-Ipfs-Roots is correct (regression)", func(t *testing.T) {
268+
test := func(responseFormat string, path string, roots string) {
269+
t.Run(responseFormat, func(t *testing.T) {
270+
url := ts.URL + path
271+
req := mustNewRequest(t, http.MethodGet, url, nil)
272+
req.Header.Add("Accept", responseFormat)
273+
req.Header.Add("If-None-Match", "just-some-gibberish")
274+
res := mustDoWithoutRedirect(t, req)
275+
require.Equal(t, http.StatusOK, res.StatusCode)
276+
require.Equal(t, roots, res.Header.Get("X-Ipfs-Roots"))
277+
})
278+
}
279+
280+
test("", dirPath, dirRoots)
281+
test("text/html", dirPath, dirRoots)
282+
test(carResponseFormat, dirPath, dirRoots)
283+
test(rawResponseFormat, dirPath, dirRoots)
284+
test(tarResponseFormat, dirPath, dirRoots)
285+
286+
test("", filePath, fileRoots)
287+
test("text/html", filePath, fileRoots)
288+
test(carResponseFormat, filePath, fileRoots)
289+
test(rawResponseFormat, filePath, fileRoots)
290+
test(tarResponseFormat, filePath, fileRoots)
291+
292+
test("", dagCborPath, dagCborRoots)
293+
test("text/html", dagCborPath+"/", dagCborRoots)
294+
test(carResponseFormat, dagCborPath, dagCborRoots)
295+
test(rawResponseFormat, dagCborPath, dagCborRoots)
296+
test(dagJsonResponseFormat, dagCborPath, dagCborRoots)
297+
test(dagCborResponseFormat, dagCborPath, dagCborRoots)
298+
})
147299
}
148300

149301
func TestGoGetSupport(t *testing.T) {
@@ -157,7 +309,6 @@ func TestGoGetSupport(t *testing.T) {
157309

158310
func TestRedirects(t *testing.T) {
159311
t.Parallel()
160-
// Move here?
161312

162313
t.Run("IPNS Base58 Multihash Redirect", func(t *testing.T) {
163314
ts, _, _ := newTestServerAndNode(t, nil)
@@ -282,7 +433,6 @@ func TestDeserializedResponses(t *testing.T) {
282433
},
283434
},
284435
})
285-
t.Logf("test server url: %s", ts.URL)
286436

287437
trustedFormats := []string{"", "dag-json", "dag-cbor", "tar", "json", "cbor"}
288438
trustlessFormats := []string{"raw", "car"}
@@ -305,7 +455,7 @@ func TestDeserializedResponses(t *testing.T) {
305455

306456
doIpfsCidPathRequests := func(t *testing.T, formats []string, host string, expectedStatus int) {
307457
for _, format := range formats {
308-
doRequest(t, "/ipfs/"+root.String()+"/EmptyDir/?format="+format, host, expectedStatus)
458+
doRequest(t, "/ipfs/"+root.String()+"/empty-dir/?format="+format, host, expectedStatus)
309459
}
310460
}
311461

@@ -363,7 +513,6 @@ func TestDeserializedResponses(t *testing.T) {
363513
},
364514
},
365515
})
366-
t.Logf("test server url: %s", ts.URL)
367516

368517
doRequest := func(t *testing.T, path, host string, expectedStatus int) {
369518
req := mustNewRequest(t, http.MethodGet, ts.URL+path, nil)
@@ -378,11 +527,11 @@ func TestDeserializedResponses(t *testing.T) {
378527
// DNSLink only. Not supported for trustless. Supported for trusted, except
379528
// format=ipns-record which is unavailable for DNSLink.
380529
doRequest(t, "/", "trustless.com", http.StatusNotAcceptable)
381-
doRequest(t, "/EmptyDir/", "trustless.com", http.StatusNotAcceptable)
530+
doRequest(t, "/empty-dir/", "trustless.com", http.StatusNotAcceptable)
382531
doRequest(t, "/?format=ipns-record", "trustless.com", http.StatusNotAcceptable)
383532

384533
doRequest(t, "/", "trusted.com", http.StatusOK)
385-
doRequest(t, "/EmptyDir/", "trusted.com", http.StatusOK)
534+
doRequest(t, "/empty-dir/", "trusted.com", http.StatusOK)
386535
doRequest(t, "/?format=ipns-record", "trusted.com", http.StatusBadRequest)
387536
})
388537
}

gateway/handler_codec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestDagJsonCborPreview(t *testing.T) {
3131
ctx, cancel := context.WithCancel(context.Background())
3232
defer cancel()
3333

34-
resolvedPath, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(ipath.IpfsPath(root), t.Name(), "example"))
34+
resolvedPath, err := backend.resolvePathNoRootsReturned(ctx, ipath.Join(ipath.IpfsPath(root), "subdir", "dag-cbor-document"))
3535
require.NoError(t, err)
3636

3737
cidStr := resolvedPath.Cid().String()

gateway/handler_unixfs_dir_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
1818
ctx, cancel := context.WithCancel(context.Background())
1919
defer cancel()
2020

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

2424
// create /ipns/example.net/foo/

gateway/testdata/fixtures.car

303 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)