@@ -26,7 +26,7 @@ func TestGatewayGet(t *testing.T) {
26
26
ctx , cancel := context .WithCancel (context .Background ())
27
27
defer cancel ()
28
28
29
- k , err := backend .resolvePathNoRootsReturned (ctx , ipath .Join (ipath .IpfsPath (root ), t . Name () , "fnord" ))
29
+ k , err := backend .resolvePathNoRootsReturned (ctx , ipath .Join (ipath .IpfsPath (root ), "subdir" , "fnord" ))
30
30
require .NoError (t , err )
31
31
32
32
backend .namesys ["/ipns/example.com" ] = path .FromCid (k .Cid ())
@@ -92,7 +92,7 @@ func TestPretty404(t *testing.T) {
92
92
ctx , cancel := context .WithCancel (context .Background ())
93
93
defer cancel ()
94
94
95
- k , err := backend .resolvePathNoRootsReturned (ctx , ipath .Join (ipath .IpfsPath (root ), t . Name () ))
95
+ k , err := backend .resolvePathNoRootsReturned (ctx , ipath .Join (ipath .IpfsPath (root ), "subdir-404" ))
96
96
assert .NoError (t , err )
97
97
98
98
host := "example.net"
@@ -131,19 +131,171 @@ func TestPretty404(t *testing.T) {
131
131
}
132
132
}
133
133
134
- func TestCacheControlImmutable (t * testing.T ) {
134
+ func TestHeaders (t * testing.T ) {
135
+ t .Parallel ()
136
+
135
137
ts , _ , root := newTestServerAndNode (t , nil )
136
138
137
- req := mustNewRequest (t , http .MethodGet , ts .URL + "/ipfs/" + root .String ()+ "/" , nil )
138
- res := mustDoWithoutRedirect (t , req )
139
+ var (
140
+ dirCID = "bafybeihta5xfgxcmyxyq6druvidc7es6ogffdd6zel22l3y4wddju5xxsu"
141
+ dirPath = "/ipfs/" + root .String () + "/subdir/"
142
+ dirRoots = "bafybeifhvgr4ufgwpoj2iyrymrigjhpexvsyfm2elafebkmrei4skunihe," + dirCID
143
+
144
+ fileCID = "bafkreiba3vpkcqpc6xtp3hsatzcod6iwneouzjoq7ymy4m2js6gc3czt6i"
145
+ filePath = "/ipfs/" + root .String () + "/subdir/fnord"
146
+ fileRoots = dirRoots + "," + fileCID
139
147
140
- // check the immutable tag isn't set
141
- hdrs , ok := res .Header ["Cache-Control" ]
142
- if ok {
143
- for _ , hdr := range hdrs {
144
- assert .NotContains (t , hdr , "immutable" , "unexpected Cache-Control: immutable on directory listing" )
148
+ dagCborCID = "bafyreiaocls5bt2ha5vszv5pwz34zzcdf3axk3uqa56bgsgvlkbezw67hq"
149
+ dagCborPath = "/ipfs/" + root .String () + "/subdir/dag-cbor-document"
150
+ dagCborRoots = dirRoots + "," + dagCborCID
151
+ )
152
+
153
+ t .Run ("Control-Cache-Immutable is not immutable for directories" , func (t * testing.T ) {
154
+ req := mustNewRequest (t , http .MethodGet , ts .URL + "/ipfs/" + root .String ()+ "/" , nil )
155
+ res := mustDoWithoutRedirect (t , req )
156
+
157
+ // check the immutable tag isn't set
158
+ hdrs , ok := res .Header ["Cache-Control" ]
159
+ if ok {
160
+ for _ , hdr := range hdrs {
161
+ assert .NotContains (t , hdr , "immutable" , "unexpected Cache-Control: immutable on directory listing" )
162
+ }
145
163
}
146
- }
164
+ })
165
+
166
+ t .Run ("ETag contains expected values" , func (t * testing.T ) {
167
+ test := func (responseFormat string , path string , format string , args ... any ) {
168
+ t .Run (responseFormat , func (t * testing.T ) {
169
+ url := ts .URL + path
170
+ req := mustNewRequest (t , http .MethodGet , url , nil )
171
+ req .Header .Add ("Accept" , responseFormat )
172
+ res := mustDoWithoutRedirect (t , req )
173
+ require .Equal (t , http .StatusOK , res .StatusCode )
174
+ require .Regexp (t , `^` + fmt .Sprintf (format , args ... )+ `$` , res .Header .Get ("Etag" ))
175
+ })
176
+ }
177
+ test ("" , dirPath , `"DirIndex-(.*)_CID-%s"` , dirCID )
178
+ test ("text/html" , dirPath , `"DirIndex-(.*)_CID-%s"` , dirCID )
179
+ test (carResponseFormat , dirPath , `W/"%s.car.5ovg7dign8ug"` , root .String ()) // ETags of CARs on a Path have the root CID in the Etag and hashed information to derive the correct Etag of the full request.
180
+ test (rawResponseFormat , dirPath , `"%s.raw"` , dirCID )
181
+ test (tarResponseFormat , dirPath , `W/"%s.x-tar"` , dirCID )
182
+
183
+ test ("" , filePath , `"%s"` , fileCID )
184
+ test ("text/html" , filePath , `"%s"` , fileCID )
185
+ test (carResponseFormat , filePath , `W/"%s.car.fivdlu5uk7ab6"` , root .String ())
186
+ test (rawResponseFormat , filePath , `"%s.raw"` , fileCID )
187
+ test (tarResponseFormat , filePath , `W/"%s.x-tar"` , fileCID )
188
+
189
+ test ("" , dagCborPath , `"%s.dag-cbor"` , dagCborCID )
190
+ test ("text/html" , dagCborPath + "/" , `"DagIndex-(.*)_CID-%s"` , dagCborCID )
191
+ test (carResponseFormat , dagCborPath , `W/"%s.car.5b4vl0vt6odpt"` , root .String ())
192
+ test (rawResponseFormat , dagCborPath , `"%s.raw"` , dagCborCID )
193
+ test (dagJsonResponseFormat , dagCborPath , `"%s.dag-json"` , dagCborCID )
194
+ test (dagCborResponseFormat , dagCborPath , `"%s.dag-cbor"` , dagCborCID )
195
+ })
196
+
197
+ t .Run ("If-None-Match with previous Etag returns Not Modified" , func (t * testing.T ) {
198
+ test := func (responseFormat string , path string ) {
199
+ t .Run (responseFormat , func (t * testing.T ) {
200
+ url := ts .URL + path
201
+ req := mustNewRequest (t , http .MethodGet , url , nil )
202
+ req .Header .Add ("Accept" , responseFormat )
203
+ res := mustDoWithoutRedirect (t , req )
204
+ require .Equal (t , http .StatusOK , res .StatusCode )
205
+ etag := res .Header .Get ("Etag" )
206
+ require .NotEmpty (t , etag )
207
+ req = mustNewRequest (t , http .MethodGet , url , nil )
208
+ req .Header .Add ("Accept" , responseFormat )
209
+ req .Header .Add ("If-None-Match" , etag )
210
+ res = mustDoWithoutRedirect (t , req )
211
+ require .Equal (t , http .StatusNotModified , res .StatusCode )
212
+ })
213
+ }
214
+
215
+ test ("" , dirPath )
216
+ test ("text/html" , dirPath )
217
+ test (carResponseFormat , dirPath )
218
+ test (rawResponseFormat , dirPath )
219
+ test (tarResponseFormat , dirPath )
220
+
221
+ test ("" , filePath )
222
+ test ("text/html" , filePath )
223
+ test (carResponseFormat , filePath )
224
+ test (rawResponseFormat , filePath )
225
+ test (tarResponseFormat , filePath )
226
+
227
+ test ("" , dagCborPath )
228
+ test ("text/html" , dagCborPath + "/" )
229
+ test (carResponseFormat , dagCborPath )
230
+ test (rawResponseFormat , dagCborPath )
231
+ test (dagJsonResponseFormat , dagCborPath )
232
+ test (dagCborResponseFormat , dagCborPath )
233
+ })
234
+
235
+ t .Run ("X-Ipfs-Roots contains expected values" , func (t * testing.T ) {
236
+ test := func (responseFormat string , path string , roots string ) {
237
+ t .Run (responseFormat , func (t * testing.T ) {
238
+ url := ts .URL + path
239
+ req := mustNewRequest (t , http .MethodGet , url , nil )
240
+ req .Header .Add ("Accept" , responseFormat )
241
+ res := mustDoWithoutRedirect (t , req )
242
+ require .Equal (t , http .StatusOK , res .StatusCode )
243
+ require .Equal (t , roots , res .Header .Get ("X-Ipfs-Roots" ))
244
+ })
245
+ }
246
+
247
+ test ("" , dirPath , dirRoots )
248
+ test ("text/html" , dirPath , dirRoots )
249
+ test (carResponseFormat , dirPath , dirRoots )
250
+ test (rawResponseFormat , dirPath , dirRoots )
251
+ test (tarResponseFormat , dirPath , dirRoots )
252
+
253
+ test ("" , filePath , fileRoots )
254
+ test ("text/html" , filePath , fileRoots )
255
+ test (carResponseFormat , filePath , fileRoots )
256
+ test (rawResponseFormat , filePath , fileRoots )
257
+ test (tarResponseFormat , filePath , fileRoots )
258
+
259
+ test ("" , dagCborPath , dagCborRoots )
260
+ test ("text/html" , dagCborPath + "/" , dagCborRoots )
261
+ test (carResponseFormat , dagCborPath , dagCborRoots )
262
+ test (rawResponseFormat , dagCborPath , dagCborRoots )
263
+ test (dagJsonResponseFormat , dagCborPath , dagCborRoots )
264
+ test (dagCborResponseFormat , dagCborPath , dagCborRoots )
265
+ })
266
+
267
+ t .Run ("If-None-Match with wrong value forces path resolution, but X-Ipfs-Roots is correct (regression)" , func (t * testing.T ) {
268
+ test := func (responseFormat string , path string , roots string ) {
269
+ t .Run (responseFormat , func (t * testing.T ) {
270
+ url := ts .URL + path
271
+ req := mustNewRequest (t , http .MethodGet , url , nil )
272
+ req .Header .Add ("Accept" , responseFormat )
273
+ req .Header .Add ("If-None-Match" , "just-some-gibberish" )
274
+ res := mustDoWithoutRedirect (t , req )
275
+ require .Equal (t , http .StatusOK , res .StatusCode )
276
+ require .Equal (t , roots , res .Header .Get ("X-Ipfs-Roots" ))
277
+ })
278
+ }
279
+
280
+ test ("" , dirPath , dirRoots )
281
+ test ("text/html" , dirPath , dirRoots )
282
+ test (carResponseFormat , dirPath , dirRoots )
283
+ test (rawResponseFormat , dirPath , dirRoots )
284
+ test (tarResponseFormat , dirPath , dirRoots )
285
+
286
+ test ("" , filePath , fileRoots )
287
+ test ("text/html" , filePath , fileRoots )
288
+ test (carResponseFormat , filePath , fileRoots )
289
+ test (rawResponseFormat , filePath , fileRoots )
290
+ test (tarResponseFormat , filePath , fileRoots )
291
+
292
+ test ("" , dagCborPath , dagCborRoots )
293
+ test ("text/html" , dagCborPath + "/" , dagCborRoots )
294
+ test (carResponseFormat , dagCborPath , dagCborRoots )
295
+ test (rawResponseFormat , dagCborPath , dagCborRoots )
296
+ test (dagJsonResponseFormat , dagCborPath , dagCborRoots )
297
+ test (dagCborResponseFormat , dagCborPath , dagCborRoots )
298
+ })
147
299
}
148
300
149
301
func TestGoGetSupport (t * testing.T ) {
@@ -157,7 +309,6 @@ func TestGoGetSupport(t *testing.T) {
157
309
158
310
func TestRedirects (t * testing.T ) {
159
311
t .Parallel ()
160
- // Move here?
161
312
162
313
t .Run ("IPNS Base58 Multihash Redirect" , func (t * testing.T ) {
163
314
ts , _ , _ := newTestServerAndNode (t , nil )
@@ -282,7 +433,6 @@ func TestDeserializedResponses(t *testing.T) {
282
433
},
283
434
},
284
435
})
285
- t .Logf ("test server url: %s" , ts .URL )
286
436
287
437
trustedFormats := []string {"" , "dag-json" , "dag-cbor" , "tar" , "json" , "cbor" }
288
438
trustlessFormats := []string {"raw" , "car" }
@@ -305,7 +455,7 @@ func TestDeserializedResponses(t *testing.T) {
305
455
306
456
doIpfsCidPathRequests := func (t * testing.T , formats []string , host string , expectedStatus int ) {
307
457
for _ , format := range formats {
308
- doRequest (t , "/ipfs/" + root .String ()+ "/EmptyDir /?format=" + format , host , expectedStatus )
458
+ doRequest (t , "/ipfs/" + root .String ()+ "/empty-dir /?format=" + format , host , expectedStatus )
309
459
}
310
460
}
311
461
@@ -363,7 +513,6 @@ func TestDeserializedResponses(t *testing.T) {
363
513
},
364
514
},
365
515
})
366
- t .Logf ("test server url: %s" , ts .URL )
367
516
368
517
doRequest := func (t * testing.T , path , host string , expectedStatus int ) {
369
518
req := mustNewRequest (t , http .MethodGet , ts .URL + path , nil )
@@ -378,11 +527,11 @@ func TestDeserializedResponses(t *testing.T) {
378
527
// DNSLink only. Not supported for trustless. Supported for trusted, except
379
528
// format=ipns-record which is unavailable for DNSLink.
380
529
doRequest (t , "/" , "trustless.com" , http .StatusNotAcceptable )
381
- doRequest (t , "/EmptyDir /" , "trustless.com" , http .StatusNotAcceptable )
530
+ doRequest (t , "/empty-dir /" , "trustless.com" , http .StatusNotAcceptable )
382
531
doRequest (t , "/?format=ipns-record" , "trustless.com" , http .StatusNotAcceptable )
383
532
384
533
doRequest (t , "/" , "trusted.com" , http .StatusOK )
385
- doRequest (t , "/EmptyDir /" , "trusted.com" , http .StatusOK )
534
+ doRequest (t , "/empty-dir /" , "trusted.com" , http .StatusOK )
386
535
doRequest (t , "/?format=ipns-record" , "trusted.com" , http .StatusBadRequest )
387
536
})
388
537
}
0 commit comments