Skip to content

Commit

Permalink
test: add a bunch of new tests and fixes
Browse files Browse the repository at this point in the history
- do not expect specific filename, that's impl. detail
- add new fixtures, rename some, delete others
- fix byte range tests (limit is inclusive)
- dag-cbor tests with links
- content-type and content-disposition checks in all car tests
- and much more
  • Loading branch information
hacdias committed Jun 2, 2023
1 parent bd52cf8 commit bd58502
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 124 deletions.
Binary file removed fixtures/t0118-carv1-basic.car
Binary file not shown.
Binary file removed fixtures/t0118-deterministic.car
Binary file not shown.
Binary file removed fixtures/t0118-gateway-car.car
Binary file not shown.
Binary file removed fixtures/t0118-test-dag.car
Binary file not shown.
Binary file not shown.
File renamed without changes.
Binary file added fixtures/t0118/subdir-with-mixed-block-files.car
Binary file not shown.
Binary file not shown.
438 changes: 341 additions & 97 deletions tests/t0118_gateway_car_test.go

Large diffs are not rendered by default.

29 changes: 16 additions & 13 deletions tooling/car/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package car
import (
"bytes"
"context"
"errors"
"fmt"
dagpb "github.com/ipld/go-codec-dagpb"
"io"
"os"
"path"
Expand All @@ -22,6 +22,7 @@ import (
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-unixfsnode"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
_ "github.com/ipld/go-ipld-prime/codec/cbor"
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
Expand Down Expand Up @@ -78,15 +79,27 @@ func (d *UnixfsDag) loadLinks(node format.Node) (map[string]*UnixfsDag, error) {
}

func (d *UnixfsDag) getNode(names ...string) (format.Node, error) {
for _, name := range names {
for i, name := range names {
node, err := d.getNode()
if err != nil {
return nil, err
}

if d.links == nil {
d.links, err = d.loadLinks(node)
if err != nil {
if errors.Is(err, uio.ErrNotADir) {
// Maybe it's an IPLD Link!
lnk, _, err := node.ResolveLink(names[i:])
if err != nil {
return nil, fmt.Errorf("node is neither a unixfs directory, or includes an ipld link: %w", err)
}
n, err := lnk.GetNode(context.Background(), d.dsvc)
if err != nil {
return nil, fmt.Errorf("found link node could not be fetched: %w", err)
}
d.node = n
return d.node, nil
} else if err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -173,16 +186,6 @@ func (d *UnixfsDag) MustGetChildrenCids(names ...string) []string {
return cids
}

func (d *UnixfsDag) MustGetIPLDChildrenCids(names ...string) []string {
node := d.MustGetNode(names...)
lnks := node.node.Links()
var cids []string
for _, l := range lnks {
cids = append(cids, l.Cid.String())
}
return cids
}

// MustGetCidsInHAMT returns the cids in the HAMT at the given path. Does not include the CID of the HAMT root
func (d *UnixfsDag) MustGetCidsInHAMT(names ...string) []string {
node := d.MustGetNode(names...)
Expand Down
14 changes: 11 additions & 3 deletions tooling/helpers/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ func StandardCARTestTransforms(t *testing.T, sts test.SugarTests) test.SugarTest

func applyStandardCarResponseHeaders(t *testing.T, st test.SugarTest) test.SugarTest {
st.Response = st.Response.Headers(
test.Header("Content-Length").
Hint("CAR is streamed, gateway may not have the entire thing, unable to calculate total size").
IsEmpty(),
// TODO: Go always sends Content-Length and it's not possible to explicitly disable the behavior.
// For now, we ignore this check. It should be able to be resolved soon: https://github.com/ipfs/boxo/pull/177
// test.Header("Content-Length").
// Hint("CAR is streamed, gateway may not have the entire thing, unable to calculate total size").
// IsEmpty(),
test.Header("X-Content-Type-Options").
Hint("CAR is streamed, gateway may not have the entire thing, unable to calculate total size").
Equals("nosniff"),
test.Header("Accept-Ranges").
Hint("CAR is streamed, gateway may not have the entire thing, unable to support range-requests. Partial downloads and resumes should be handled using IPLD selectors: https://github.com/ipfs/go-ipfs/issues/8769").
Equals("none"),
test.Header("Content-Type").
Hint("Expected content type to be application/vnd.ipld.car").
Contains("application/vnd.ipld.car"),
test.Header("Content-Disposition").
Hint(`Expected content disposition to be attachment; filename="*.car"`).
Matches(`attachment; filename=".*\.car"`),
)
return st
}
Expand Down
22 changes: 11 additions & 11 deletions tooling/test/sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type RequestBuilder struct {

func Request() RequestBuilder {
return RequestBuilder{
Method_: "GET",
Query_: make(url.Values),
Method_: "GET",
Query_: make(url.Values),
FollowRedirects_: false,
}
}
Expand Down Expand Up @@ -116,15 +116,15 @@ func (r RequestBuilder) Clone() RequestBuilder {
}

return RequestBuilder{
Method_: r.Method_,
Path_: r.Path_,
URL_: r.URL_,
Proxy_: r.Proxy_,
UseProxyTunnel_: r.UseProxyTunnel_,
Headers_: clonedHeaders,
DoNotFollowRedirects_: r.DoNotFollowRedirects_,
Query_: clonedQuery,
Body_: bytes.Clone(r.Body_),
Method_: r.Method_,
Path_: r.Path_,
URL_: r.URL_,
Proxy_: r.Proxy_,
UseProxyTunnel_: r.UseProxyTunnel_,
Headers_: clonedHeaders,
FollowRedirects_: r.FollowRedirects_,
Query_: clonedQuery,
Body_: bytes.Clone(r.Body_),
}
}

Expand Down

0 comments on commit bd58502

Please sign in to comment.