add test for 4973
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Apr 25, 2018 at 10:53 UTC
e9928f76916023d5a8372e2635c46e2e746dcfd1
1 file changed
+30
-4
core/corehttp/gateway_test.go
+30
-4
@@ -4,6 +4,7 @@ import (
4
"context"
5
"errors"
6
"io/ioutil"
7
+ "math"
8
"net/http"
9
"net/http/httptest"
10
"strings"
@@ -31,11 +32,25 @@ var emptyDir = "/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
32
type mockNamesys map[string]path.Path
33
34
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)
38
}
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
54
}
55
56
func (m mockNamesys) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
@@ -130,6 +145,10 @@ func TestGatewayGet(t *testing.T) {
145
t.Fatal(err)
146
}
147
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)
152
153
t.Log(ts.URL)
154
for _, test := range []struct {
@@ -145,6 +164,13 @@ func TestGatewayGet(t *testing.T) {
164
{"localhost:5001", "/ipns/%0D%0A%0D%0Ahello", http.StatusNotFound, "ipfs resolve -r /ipns/%0D%0A%0D%0Ahello: " + namesys.ErrResolveFailed.Error() + "\n"},
165
{"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"},
166
{"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"},
174
} {
175
var c http.Client
176
r, err := http.NewRequest("GET", ts.URL+test.path, nil)