Skip to content

Commit 8551dea

Browse files
committed
add test for 4973
License: MIT Signed-off-by: Steven Allen <[email protected]>
1 parent 681d59b commit 8551dea

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

core/corehttp/gateway_test.go

+30-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"io/ioutil"
7+
"math"
78
"net/http"
89
"net/http/httptest"
910
"strings"
@@ -31,11 +32,25 @@ var emptyDir = "/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
3132
type mockNamesys map[string]path.Path
3233

3334
func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.ResolveOpt) (value path.Path, err error) {
34-
p, ok := m[name]
35-
if !ok {
36-
return "", namesys.ErrResolveFailed
35+
cfg := nsopts.DefaultResolveOpts()
36+
for _, o := range opts {
37+
o(cfg)
3738
}
38-
return p, nil
39+
depth := cfg.Depth
40+
if depth == nsopts.UnlimitedDepth {
41+
depth = math.MaxUint64
42+
}
43+
for depth > 0 && strings.HasPrefix(name, "/ipns/") {
44+
depth--
45+
46+
var ok bool
47+
value, ok = m[name]
48+
if !ok {
49+
return "", namesys.ErrResolveFailed
50+
}
51+
name = value.String()
52+
}
53+
return value, nil
3954
}
4055

4156
func (m mockNamesys) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
@@ -130,6 +145,10 @@ func TestGatewayGet(t *testing.T) {
130145
t.Fatal(err)
131146
}
132147
ns["/ipns/example.com"] = path.FromString("/ipfs/" + k)
148+
ns["/ipns/working.example.com"] = path.FromString("/ipfs/" + k)
149+
ns["/ipns/double.example.com"] = path.FromString("/ipns/working.example.com")
150+
ns["/ipns/triple.example.com"] = path.FromString("/ipns/double.example.com")
151+
ns["/ipns/broken.example.com"] = path.FromString("/ipns/" + k)
133152

134153
t.Log(ts.URL)
135154
for _, test := range []struct {
@@ -145,6 +164,13 @@ func TestGatewayGet(t *testing.T) {
145164
{"localhost:5001", "/ipns/%0D%0A%0D%0Ahello", http.StatusNotFound, "ipfs resolve -r /ipns/%0D%0A%0D%0Ahello: " + namesys.ErrResolveFailed.Error() + "\n"},
146165
{"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"},
147166
{"example.com", "/", http.StatusOK, "fnord"},
167+
168+
{"working.example.com", "/", http.StatusOK, "fnord"},
169+
{"double.example.com", "/", http.StatusOK, "fnord"},
170+
{"triple.example.com", "/", http.StatusOK, "fnord"},
171+
{"working.example.com", "/ipfs/" + k, http.StatusNotFound, "ipfs resolve -r /ipns/working.example.com/ipfs/" + k + ": no link named \"ipfs\" under " + k + "\n"},
172+
{"broken.example.com", "/", http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
173+
{"broken.example.com", "/ipfs/" + k, http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/ipfs/" + k + ": " + namesys.ErrResolveFailed.Error() + "\n"},
148174
} {
149175
var c http.Client
150176
r, err := http.NewRequest("GET", ts.URL+test.path, nil)

0 commit comments

Comments
 (0)