Skip to content

Commit c912f0c

Browse files
committed
test: add tests for most functions
1 parent b922dd3 commit c912f0c

File tree

6 files changed

+216
-42
lines changed

6 files changed

+216
-42
lines changed

gateway/blocks_backend.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,12 @@ func (bb *BlocksBackend) GetCAR(ctx context.Context, p path.ImmutablePath, param
259259
// Setup the UnixFS resolver.
260260
f := newNodeGetterFetcherSingleUseFactory(ctx, blockGetter)
261261
pathResolver := resolver.NewBasicResolver(f)
262-
ip := ipfspath.FromString(p.String())
263-
_, _, err = pathResolver.ResolveToLastNode(ctx, ip)
262+
_, _, err = pathResolver.ResolveToLastNode(ctx, p)
264263

265264
if isErrNotFound(err) {
266265
return ContentPathMetadata{
267266
PathSegmentRoots: nil,
268-
LastSegment: ifacepath.NewResolvedPath(ip, rootCid, rootCid, ""),
267+
LastSegment: path.NewIPFSPath(rootCid),
269268
ContentType: "",
270269
}, io.NopCloser(&buf), nil
271270
}

gateway/gateway_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ func TestGatewayGet(t *testing.T) {
5858
text string
5959
}{
6060
{"127.0.0.1:8080", "/", http.StatusNotFound, "404 page not found\n"},
61-
{"127.0.0.1:8080", "/ipfs", http.StatusBadRequest, "invalid path \"/ipfs/\": not enough path components\n"},
62-
{"127.0.0.1:8080", "/ipns", http.StatusBadRequest, "invalid path \"/ipns/\": not enough path components\n"},
61+
{"127.0.0.1:8080", "/ipfs", http.StatusBadRequest, "invalid path \"/ipfs/\": path does not have enough components\n"},
62+
{"127.0.0.1:8080", "/ipns", http.StatusBadRequest, "invalid path \"/ipns/\": path does not have enough components\n"},
6363
{"127.0.0.1:8080", "/" + k.Cid().String(), http.StatusNotFound, "404 page not found\n"},
64-
{"127.0.0.1:8080", "/ipfs/this-is-not-a-cid", http.StatusBadRequest, "invalid path \"/ipfs/this-is-not-a-cid\": invalid CID: invalid cid: illegal base32 data at input byte 3\n"},
64+
{"127.0.0.1:8080", "/ipfs/this-is-not-a-cid", http.StatusBadRequest, "invalid path \"/ipfs/this-is-not-a-cid\": invalid cid: illegal base32 data at input byte 3\n"},
6565
{"127.0.0.1:8080", k.String(), http.StatusOK, "fnord"},
6666
{"127.0.0.1:8080", "/ipns/nxdomain.example.com", http.StatusInternalServerError, "failed to resolve /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
6767
{"127.0.0.1:8080", "/ipns/%0D%0A%0D%0Ahello", http.StatusInternalServerError, "failed to resolve /ipns/\\r\\n\\r\\nhello: " + namesys.ErrResolveFailed.Error() + "\n"},

path/error.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
package path
22

33
import (
4+
"errors"
45
"fmt"
56
)
67

8+
var (
9+
ErrExpectedImmutable = errors.New("path was expected to be immutable")
10+
ErrInsufficientComponents = errors.New("path does not have enough components")
11+
ErrUnknownNamespace = errors.New("unknown namespace")
12+
)
13+
714
type ErrInvalidPath struct {
8-
error error
9-
path string
15+
err error
16+
path string
1017
}
1118

1219
func (e ErrInvalidPath) Error() string {
13-
return fmt.Sprintf("invalid path %q: %s", e.path, e.error)
20+
return fmt.Sprintf("invalid path %q: %s", e.path, e.err)
1421
}
1522

1623
func (e ErrInvalidPath) Unwrap() error {
17-
return e.error
24+
return e.err
1825
}
1926

2027
func (e ErrInvalidPath) Is(err error) bool {

path/error_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
)
77

88
func TestErrorIs(t *testing.T) {
9-
if !errors.Is(ErrInvalidPath{path: "foo", error: errors.New("bar")}, ErrInvalidPath{}) {
9+
if !errors.Is(ErrInvalidPath{path: "foo", err: errors.New("bar")}, ErrInvalidPath{}) {
1010
t.Fatal("error must be error")
1111
}
1212

13-
if !errors.Is(&ErrInvalidPath{path: "foo", error: errors.New("bar")}, ErrInvalidPath{}) {
13+
if !errors.Is(&ErrInvalidPath{path: "foo", err: errors.New("bar")}, ErrInvalidPath{}) {
1414
t.Fatal("pointer to error must be error")
1515
}
1616
}

path/path.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ type immutablePath struct {
111111

112112
func NewImmutablePath(p Path) (ImmutablePath, error) {
113113
if p.Namespace().Mutable() {
114-
return nil, fmt.Errorf("path was expected to be immutable: %s", p.String())
114+
return nil, ErrInvalidPath{err: ErrExpectedImmutable, path: p.String()}
115115
}
116116

117117
segments := p.Segments()
118118
cid, err := cid.Decode(segments[1])
119119
if err != nil {
120-
return nil, &ErrInvalidPath{error: fmt.Errorf("invalid CID: %w", err), path: p.String()}
120+
return nil, &ErrInvalidPath{err: err, path: p.String()}
121121
}
122122

123123
return immutablePath{path: p, cid: cid}, nil
@@ -149,7 +149,7 @@ func (ip immutablePath) Remainder() string {
149149

150150
// NewIPFSPath returns a new "/ipfs" path with the provided CID.
151151
func NewIPFSPath(cid cid.Cid) ImmutablePath {
152-
return &immutablePath{
152+
return immutablePath{
153153
path: path{
154154
str: fmt.Sprintf("/%s/%s", IPFSNamespace, cid.String()),
155155
namespace: IPFSNamespace,
@@ -160,7 +160,7 @@ func NewIPFSPath(cid cid.Cid) ImmutablePath {
160160

161161
// NewIPLDPath returns a new "/ipld" path with the provided CID.
162162
func NewIPLDPath(cid cid.Cid) ImmutablePath {
163-
return &immutablePath{
163+
return immutablePath{
164164
path: path{
165165
str: fmt.Sprintf("/%s/%s", IPLDNamespace, cid.String()),
166166
namespace: IPLDNamespace,
@@ -169,10 +169,10 @@ func NewIPLDPath(cid cid.Cid) ImmutablePath {
169169
}
170170
}
171171

172-
// NewPath takes the given string and returns a well-forme and sanitized [Path].
172+
// NewPath takes the given string and returns a well-formed and sanitized [Path].
173173
// The given string is cleaned through [gopath.Clean], but preserving the final
174174
// trailing slash. This function returns an error when the given string is not
175-
// a valid path.
175+
// a valid content path.
176176
func NewPath(str string) (Path, error) {
177177
cleaned := gopath.Clean(str)
178178
components := strings.Split(cleaned, "/")
@@ -186,18 +186,18 @@ func NewPath(str string) (Path, error) {
186186
// components: [" " "{namespace}" "{element}"]. The first component must therefore
187187
// be empty.
188188
if len(components) < 3 || components[0] != "" {
189-
return nil, &ErrInvalidPath{error: fmt.Errorf("not enough path components"), path: str}
189+
return nil, &ErrInvalidPath{err: ErrInsufficientComponents, path: str}
190190
}
191191

192192
switch components[1] {
193193
case "ipfs", "ipld":
194194
if components[2] == "" {
195-
return nil, &ErrInvalidPath{error: fmt.Errorf("not enough path components"), path: str}
195+
return nil, &ErrInvalidPath{err: ErrInsufficientComponents, path: str}
196196
}
197197

198198
cid, err := cid.Decode(components[2])
199199
if err != nil {
200-
return nil, &ErrInvalidPath{error: fmt.Errorf("invalid CID: %w", err), path: str}
200+
return nil, &ErrInvalidPath{err: err, path: str}
201201
}
202202

203203
ns := IPFSNamespace
@@ -214,15 +214,15 @@ func NewPath(str string) (Path, error) {
214214
}, nil
215215
case "ipns":
216216
if components[2] == "" {
217-
return nil, &ErrInvalidPath{error: fmt.Errorf("not enough path components"), path: str}
217+
return nil, &ErrInvalidPath{err: ErrInsufficientComponents, path: str}
218218
}
219219

220220
return path{
221221
str: cleaned,
222222
namespace: IPNSNamespace,
223223
}, nil
224224
default:
225-
return nil, &ErrInvalidPath{error: fmt.Errorf("unknown namespace %q", components[1]), path: str}
225+
return nil, &ErrInvalidPath{err: fmt.Errorf("%w: %q", ErrUnknownNamespace, components[1]), path: str}
226226
}
227227
}
228228

0 commit comments

Comments
 (0)