Skip to content

Commit

Permalink
feat: MustGetCidWithCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jun 6, 2023
1 parent cc7ee35 commit f0977e6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
42 changes: 21 additions & 21 deletions tests/t0118_gateway_car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestGatewayCarPathing(t *testing.T) {
the last path segment (terminating entity returned recursively).
`,
Request: Request().
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirTwoSingleBlockFilesFixture.MustGetCid()).
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirTwoSingleBlockFilesFixture.MustGetCidWithCodec(0x70)).
Query("format", "car"),
Response: Expect().
Status(200).
Expand All @@ -47,7 +47,7 @@ func TestGatewayCarPathing(t *testing.T) {
the last path segment, recursively.
`,
Request: Request().
Path("/ipfs/{{cid}}/685.txt", singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
Path("/ipfs/{{cid}}/685.txt", singleLayerHamtMultiBlockFilesFixture.MustGetCidWithCodec(0x70)).
Query("format", "car"),
Response: Expect().
Status(200).
Expand All @@ -72,7 +72,7 @@ func TestGatewayCarPathing(t *testing.T) {
inside the last path segment, recursively.
`,
Request: Request().
Path("/ipfs/{{cid}}/files/single", dirWithDagCborWithLinksFixture.MustGetCid("document")).
Path("/ipfs/{{cid}}/files/single", dirWithDagCborWithLinksFixture.MustGetCidWithCodec(0x71, "document")).
Query("format", "car"),
Response: Expect().
Status(200).
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestGatewayCarDagScopeBlock(t *testing.T) {
end of the path and blocks required to verify the specified path segments.
`,
Request: Request().
Path("/ipfs/{{cid}}/subdir", subdirTwoSingleBlockFilesFixture.MustGetCid()).
Path("/ipfs/{{cid}}/subdir", subdirTwoSingleBlockFilesFixture.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "block"),
Response: Expect().
Expand All @@ -128,7 +128,7 @@ func TestGatewayCarDagScopeBlock(t *testing.T) {
end of the path and blocks required to verify the specified path segments.
`,
Request: Request().
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirTwoSingleBlockFilesFixture.MustGetCid()).
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirTwoSingleBlockFilesFixture.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "block"),
Response: Expect().
Expand All @@ -154,7 +154,7 @@ func TestGatewayCarDagScopeBlock(t *testing.T) {
traversal, not the entire HAMT and not skipping all intermediate nodes.
`,
Request: Request().
Path("/ipfs/{{cid}}/1.txt", singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
Path("/ipfs/{{cid}}/1.txt", singleLayerHamtMultiBlockFilesFixture.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "block"),
Response: Expect().
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
at the end of the specified path.
`,
Request: Request().
Path("/ipfs/{{cid}}", subdirTwoSingleBlockFilesFixture.MustGetCid()).
Path("/ipfs/{{cid}}", subdirTwoSingleBlockFilesFixture.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "entity"),
Response: Expect().
Expand All @@ -214,7 +214,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
as all of the blocks in the HAMT, but not any of blocks below the HAMT.
`,
Request: Request().
Path("/ipfs/{{cid}}", singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
Path("/ipfs/{{cid}}", singleLayerHamtMultiBlockFilesFixture.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "entity"),
Response: Expect().
Expand All @@ -238,7 +238,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
blocks to verify the path, as well as to decode the full file.
`,
Request: Request().
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirWithMixedBlockFiles.MustGetCid()).
Path("/ipfs/{{cid}}/subdir/ascii.txt", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "entity"),
Response: Expect().
Expand All @@ -264,7 +264,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
all necessary blocks to verify the path, as well as to decode the full file.
`,
Request: Request().
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCid()).
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "entity"),
Response: Expect().
Expand All @@ -291,7 +291,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
all necessary blocks to verify the path, but not the content of the IPLD Links.
`,
Request: Request().
Path("/ipfs/{{cid}}/document", dirWithDagCborWithLinksFixture.MustGetCid()).
Path("/ipfs/{{cid}}/document", dirWithDagCborWithLinksFixture.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "entity"),
Response: Expect().
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestGatewayCarDagScopeAll(t *testing.T) {
that begins at the end of the path query, after blocks required to verify path segments.
`,
Request: Request().
Path("/ipfs/{{cid}}/subdir", subdirWithMixedBlockFiles.MustGetCid()).
Path("/ipfs/{{cid}}/subdir", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "all"),
Response: Expect().
Expand All @@ -351,7 +351,7 @@ func TestGatewayCarDagScopeAll(t *testing.T) {
all necessary blocks to verify the path, as well as to decode the full file.
`,
Request: Request().
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCid()).
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "all"),
Response: Expect().
Expand Down Expand Up @@ -389,7 +389,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
the full UnixFS file at the end of the specified path
`,
Request: Request().
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCid()).
Path("/ipfs/{{cid}}/subdir/multiblock.txt", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "entity").
Query("entity-bytes", "0:*"),
Expand All @@ -416,7 +416,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
(i.e. entity-bytes is effectively optional if the entity is not a file)
`,
Request: Request().
Path("/ipfs/{{cid}}", singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
Path("/ipfs/{{cid}}", singleLayerHamtMultiBlockFilesFixture.MustGetCidWithCodec(0x70)).
Query("format", "car").
Query("dag-scope", "entity").
Query("entity-bytes", "0:*"),
Expand All @@ -440,7 +440,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
`,
Request: Request().
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
Query("format", "car").
Query("dag-scope", "entity").
Query("entity-bytes", "512:*"),
Expand All @@ -464,7 +464,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
`,
Request: Request().
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
Query("format", "car").
Query("dag-scope", "entity").
Query("entity-bytes", "512:1023"),
Expand All @@ -488,7 +488,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
`,
Request: Request().
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
Query("format", "car").
Query("dag-scope", "entity").
Query("entity-bytes", "512:-256"),
Expand All @@ -512,7 +512,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
`,
Request: Request().
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
Query("format", "car").
Query("dag-scope", "entity").
Query("entity-bytes", "-5:*"),
Expand All @@ -536,7 +536,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
`,
Request: Request().
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
Query("format", "car").
Query("dag-scope", "entity").
Query("entity-bytes", "-999999:-3"),
Expand All @@ -560,7 +560,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
The response MUST contain only the first block of the file.
`,
Request: Request().
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
Query("format", "car").
Query("dag-scope", "entity").
Query("entity-bytes", "0:0"),
Expand Down
8 changes: 8 additions & 0 deletions tooling/car/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ func (d *UnixfsDag) MustGetCid(names ...string) string {
return d.mustGetNode(names...).Cid().String()
}

func (d *UnixfsDag) MustGetCidWithCodec(codec uint64, names ...string) string {
c := d.mustGetNode(names...).Cid()
if c.Prefix().GetCodec() != codec {
panic(fmt.Errorf("expected codec of cid to be %d, is %d", codec, c.Prefix().GetCodec()))
}
return c.String()
}

func (d *UnixfsDag) MustGetRawData(names ...string) []byte {
return d.mustGetNode(names...).RawData()
}
Expand Down
4 changes: 2 additions & 2 deletions tooling/helpers/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func StandardCARTestTransforms(t *testing.T, sts test.SugarTests) test.SugarTest

var out test.SugarTests
for _, st := range sts {
out = append(out, checkBothFormatAndAcceptHeaderCAR(t, applyStandardCarResponseHeaders(t, st))...)
out = append(out, checkBothFormatURLParameterAndAcceptHeaderCAR(t, applyStandardCarResponseHeaders(t, st))...)
}
return out
}
Expand Down Expand Up @@ -40,7 +40,7 @@ func applyStandardCarResponseHeaders(t *testing.T, st test.SugarTest) test.Sugar
return st
}

func checkBothFormatAndAcceptHeaderCAR(t *testing.T, testWithFormatParam test.SugarTest) test.SugarTests {
func checkBothFormatURLParameterAndAcceptHeaderCAR(t *testing.T, testWithFormatParam test.SugarTest) test.SugarTests {
t.Helper()

formatParamReq := testWithFormatParam.Request
Expand Down

0 comments on commit f0977e6

Please sign in to comment.