4
4
"context"
5
5
"errors"
6
6
"io/ioutil"
7
+ "math"
7
8
"net/http"
8
9
"net/http/httptest"
9
10
"strings"
@@ -31,11 +32,25 @@ var emptyDir = "/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
31
32
type mockNamesys map [string ]path.Path
32
33
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 )
37
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
39
54
}
40
55
41
56
func (m mockNamesys ) Publish (ctx context.Context , name ci.PrivKey , value path.Path ) error {
@@ -130,6 +145,10 @@ func TestGatewayGet(t *testing.T) {
130
145
t .Fatal (err )
131
146
}
132
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 )
133
152
134
153
t .Log (ts .URL )
135
154
for _ , test := range []struct {
@@ -145,6 +164,13 @@ func TestGatewayGet(t *testing.T) {
145
164
{"localhost:5001" , "/ipns/%0D%0A%0D%0Ahello" , http .StatusNotFound , "ipfs resolve -r /ipns/%0D%0A%0D%0Ahello: " + namesys .ErrResolveFailed .Error () + "\n " },
146
165
{"localhost:5001" , "/ipns/example.com" , http .StatusOK , "fnord" },
147
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 " },
148
174
} {
149
175
var c http.Client
150
176
r , err := http .NewRequest ("GET" , ts .URL + test .path , nil )
0 commit comments