Skip to content

Commit ff75ad8

Browse files
committed
feat: MustGetCidWithCodec
1 parent 54ef60f commit ff75ad8

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

tests/t0118_gateway_car_test.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestGatewayCarPathing(t *testing.T) {
2323
the last path segment (terminating entity returned recursively).
2424
`,
2525
Request: Request().
26-
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirTwoSingleBlockFilesFixture.MustGetCid()).
26+
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirTwoSingleBlockFilesFixture.MustGetCidWithCodec(0x70)).
2727
Query("format", "car"),
2828
Response: Expect().
2929
Status(200).
@@ -47,7 +47,7 @@ func TestGatewayCarPathing(t *testing.T) {
4747
the last path segment, recursively.
4848
`,
4949
Request: Request().
50-
Path("/ipfs/{{cid}}/685.txt", singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
50+
Path("/ipfs/{{cid}}/685.txt", singleLayerHamtMultiBlockFilesFixture.MustGetCidWithCodec(0x70)).
5151
Query("format", "car"),
5252
Response: Expect().
5353
Status(200).
@@ -72,7 +72,7 @@ func TestGatewayCarPathing(t *testing.T) {
7272
inside the last path segment, recursively.
7373
`,
7474
Request: Request().
75-
Path("/ipfs/{{cid}}/files/single", dirWithDagCborWithLinksFixture.MustGetCid("document")).
75+
Path("/ipfs/{{cid}}/files/single", dirWithDagCborWithLinksFixture.MustGetCidWithCodec(0x71, "document")).
7676
Query("format", "car"),
7777
Response: Expect().
7878
Status(200).
@@ -105,7 +105,7 @@ func TestGatewayCarDagScopeBlock(t *testing.T) {
105105
end of the path and blocks required to verify the specified path segments.
106106
`,
107107
Request: Request().
108-
Path("/ipfs/{{cid}}/subdir", subdirTwoSingleBlockFilesFixture.MustGetCid()).
108+
Path("/ipfs/{{cid}}/subdir", subdirTwoSingleBlockFilesFixture.MustGetCidWithCodec(0x70)).
109109
Query("format", "car").
110110
Query("dag-scope", "block"),
111111
Response: Expect().
@@ -128,7 +128,7 @@ func TestGatewayCarDagScopeBlock(t *testing.T) {
128128
end of the path and blocks required to verify the specified path segments.
129129
`,
130130
Request: Request().
131-
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirTwoSingleBlockFilesFixture.MustGetCid()).
131+
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirTwoSingleBlockFilesFixture.MustGetCidWithCodec(0x70)).
132132
Query("format", "car").
133133
Query("dag-scope", "block"),
134134
Response: Expect().
@@ -154,7 +154,7 @@ func TestGatewayCarDagScopeBlock(t *testing.T) {
154154
traversal, not the entire HAMT and not skipping all intermediate nodes.
155155
`,
156156
Request: Request().
157-
Path("/ipfs/{{cid}}/1.txt", singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
157+
Path("/ipfs/{{cid}}/1.txt", singleLayerHamtMultiBlockFilesFixture.MustGetCidWithCodec(0x70)).
158158
Query("format", "car").
159159
Query("dag-scope", "block"),
160160
Response: Expect().
@@ -192,7 +192,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
192192
at the end of the specified path.
193193
`,
194194
Request: Request().
195-
Path("/ipfs/{{cid}}", subdirTwoSingleBlockFilesFixture.MustGetCid()).
195+
Path("/ipfs/{{cid}}", subdirTwoSingleBlockFilesFixture.MustGetCidWithCodec(0x70)).
196196
Query("format", "car").
197197
Query("dag-scope", "entity"),
198198
Response: Expect().
@@ -214,7 +214,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
214214
as all of the blocks in the HAMT, but not any of blocks below the HAMT.
215215
`,
216216
Request: Request().
217-
Path("/ipfs/{{cid}}", singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
217+
Path("/ipfs/{{cid}}", singleLayerHamtMultiBlockFilesFixture.MustGetCidWithCodec(0x70)).
218218
Query("format", "car").
219219
Query("dag-scope", "entity"),
220220
Response: Expect().
@@ -238,7 +238,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
238238
blocks to verify the path, as well as to decode the full file.
239239
`,
240240
Request: Request().
241-
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirWithMixedBlockFiles.MustGetCid()).
241+
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70)).
242242
Query("format", "car").
243243
Query("dag-scope", "entity"),
244244
Response: Expect().
@@ -264,7 +264,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
264264
all necessary blocks to verify the path, as well as to decode the full file.
265265
`,
266266
Request: Request().
267-
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCid()).
267+
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70)).
268268
Query("format", "car").
269269
Query("dag-scope", "entity"),
270270
Response: Expect().
@@ -291,7 +291,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
291291
all necessary blocks to verify the path, but not the content of the IPLD Links.
292292
`,
293293
Request: Request().
294-
Path("/ipfs/{{cid}}/document", dirWithDagCborWithLinksFixture.MustGetCid()).
294+
Path("/ipfs/{{cid}}/document", dirWithDagCborWithLinksFixture.MustGetCidWithCodec(0x71)).
295295
Query("format", "car").
296296
Query("dag-scope", "entity"),
297297
Response: Expect().
@@ -324,7 +324,7 @@ func TestGatewayCarDagScopeAll(t *testing.T) {
324324
that begins at the end of the path query, after blocks required to verify path segments.
325325
`,
326326
Request: Request().
327-
Path("/ipfs/{{cid}}/subdir", subdirWithMixedBlockFiles.MustGetCid()).
327+
Path("/ipfs/{{cid}}/subdir", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70)).
328328
Query("format", "car").
329329
Query("dag-scope", "all"),
330330
Response: Expect().
@@ -351,7 +351,7 @@ func TestGatewayCarDagScopeAll(t *testing.T) {
351351
all necessary blocks to verify the path, as well as to decode the full file.
352352
`,
353353
Request: Request().
354-
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCid()).
354+
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70)).
355355
Query("format", "car").
356356
Query("dag-scope", "all"),
357357
Response: Expect().
@@ -389,7 +389,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
389389
the full UnixFS file at the end of the specified path
390390
`,
391391
Request: Request().
392-
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCid()).
392+
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70)).
393393
Query("format", "car").
394394
Query("dag-scope", "entity").
395395
Query("entity-bytes", "0:*"),
@@ -416,7 +416,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
416416
(i.e. entity-bytes is effectively optional if the entity is not a file)
417417
`,
418418
Request: Request().
419-
Path("/ipfs/{{cid}}", singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
419+
Path("/ipfs/{{cid}}", singleLayerHamtMultiBlockFilesFixture.MustGetCidWithCodec(0x70)).
420420
Query("format", "car").
421421
Query("dag-scope", "entity").
422422
Query("entity-bytes", "0:*"),
@@ -440,7 +440,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
440440
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
441441
`,
442442
Request: Request().
443-
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
443+
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
444444
Query("format", "car").
445445
Query("dag-scope", "entity").
446446
Query("entity-bytes", "512:*"),
@@ -464,7 +464,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
464464
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
465465
`,
466466
Request: Request().
467-
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
467+
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
468468
Query("format", "car").
469469
Query("dag-scope", "entity").
470470
Query("entity-bytes", "512:1023"),
@@ -488,7 +488,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
488488
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
489489
`,
490490
Request: Request().
491-
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
491+
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
492492
Query("format", "car").
493493
Query("dag-scope", "entity").
494494
Query("entity-bytes", "512:-256"),
@@ -512,7 +512,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
512512
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
513513
`,
514514
Request: Request().
515-
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
515+
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
516516
Query("format", "car").
517517
Query("dag-scope", "entity").
518518
Query("entity-bytes", "-5:*"),
@@ -536,7 +536,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
536536
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
537537
`,
538538
Request: Request().
539-
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
539+
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
540540
Query("format", "car").
541541
Query("dag-scope", "entity").
542542
Query("entity-bytes", "-999999:-3"),
@@ -560,7 +560,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
560560
The response MUST contain only the first block of the file.
561561
`,
562562
Request: Request().
563-
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
563+
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
564564
Query("format", "car").
565565
Query("dag-scope", "entity").
566566
Query("entity-bytes", "0:0"),

tooling/car/unixfs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ func (d *UnixfsDag) MustGetCid(names ...string) string {
250250
return d.mustGetNode(names...).Cid().String()
251251
}
252252

253+
func (d *UnixfsDag) MustGetCidWithCodec(codec uint64, names ...string) string {
254+
c := d.mustGetNode(names...).Cid()
255+
if c.Prefix().GetCodec() != codec {
256+
panic(fmt.Errorf("expected codec of cid to be %d, is %d", codec, c.Prefix().GetCodec()))
257+
}
258+
return c.String()
259+
}
260+
253261
func (d *UnixfsDag) MustGetRawData(names ...string) []byte {
254262
return d.mustGetNode(names...).RawData()
255263
}

tooling/helpers/car.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func StandardCARTestTransforms(t *testing.T, sts test.SugarTests) test.SugarTest
1212

1313
var out test.SugarTests
1414
for _, st := range sts {
15-
out = append(out, checkBothFormatAndAcceptHeaderCAR(t, applyStandardCarResponseHeaders(t, st))...)
15+
out = append(out, checkBothFormatURLParameterAndAcceptHeaderCAR(t, applyStandardCarResponseHeaders(t, st))...)
1616
}
1717
return out
1818
}
@@ -40,7 +40,7 @@ func applyStandardCarResponseHeaders(t *testing.T, st test.SugarTest) test.Sugar
4040
return st
4141
}
4242

43-
func checkBothFormatAndAcceptHeaderCAR(t *testing.T, testWithFormatParam test.SugarTest) test.SugarTests {
43+
func checkBothFormatURLParameterAndAcceptHeaderCAR(t *testing.T, testWithFormatParam test.SugarTest) test.SugarTests {
4444
t.Helper()
4545

4646
formatParamReq := testWithFormatParam.Request

0 commit comments

Comments
 (0)