@@ -29,19 +29,19 @@ func TestGatewayGet(t *testing.T) {
29
29
k , err := backend .resolvePathNoRootsReturned (ctx , ipath .Join (ipath .IpfsPath (root ), "subdir" , "fnord" ))
30
30
require .NoError (t , err )
31
31
32
- backend .namesys ["/ipns/example.com" ] = path .FromCid (k .Cid ())
33
- backend .namesys ["/ipns/working.example.com" ] = path .FromString (k .String ())
34
- backend .namesys ["/ipns/double.example.com" ] = path .FromString ("/ipns/working.example.com" )
35
- backend .namesys ["/ipns/triple.example.com" ] = path .FromString ("/ipns/double.example.com" )
36
- backend .namesys ["/ipns/broken.example.com" ] = path .FromString ("/ipns/" + k .Cid ().String ())
32
+ backend .namesys ["/ipns/example.com" ] = newMockNamesysItem ( path .FromCid (k .Cid ()), 0 )
33
+ backend .namesys ["/ipns/working.example.com" ] = newMockNamesysItem ( path .FromString (k .String ()), 0 )
34
+ backend .namesys ["/ipns/double.example.com" ] = newMockNamesysItem ( path .FromString ("/ipns/working.example.com" ), 0 )
35
+ backend .namesys ["/ipns/triple.example.com" ] = newMockNamesysItem ( path .FromString ("/ipns/double.example.com" ), 0 )
36
+ backend .namesys ["/ipns/broken.example.com" ] = newMockNamesysItem ( path .FromString ("/ipns/" + k .Cid ().String ()), 0 )
37
37
// We picked .man because:
38
38
// 1. It's a valid TLD.
39
39
// 2. Go treats it as the file extension for "man" files (even though
40
40
// nobody actually *uses* this extension, AFAIK).
41
41
//
42
42
// Unfortunately, this may not work on all platforms as file type
43
43
// detection is platform dependent.
44
- backend .namesys ["/ipns/example.man" ] = path .FromString (k .String ())
44
+ backend .namesys ["/ipns/example.man" ] = newMockNamesysItem ( path .FromString (k .String ()), 0 )
45
45
46
46
for _ , test := range []struct {
47
47
host string
@@ -90,7 +90,7 @@ func TestPretty404(t *testing.T) {
90
90
t .Logf ("test server url: %s" , ts .URL )
91
91
92
92
host := "example.net"
93
- backend .namesys ["/ipns/" + host ] = path .FromCid (root )
93
+ backend .namesys ["/ipns/" + host ] = newMockNamesysItem ( path .FromCid (root ), 0 )
94
94
95
95
for _ , test := range []struct {
96
96
path string
@@ -150,7 +150,56 @@ func TestHeaders(t *testing.T) {
150
150
dagCborRoots = dirRoots + "," + dagCborCID
151
151
)
152
152
153
- t .Run ("Cache-Control is not immutable on generated /ipfs/ HTML dir listings" , func (t * testing.T ) {
153
+ t .Run ("Cache-Control uses TTL for /ipns/ when it is known" , func (t * testing.T ) {
154
+ t .Parallel ()
155
+
156
+ ts , backend , root := newTestServerAndNode (t , nil , "ipns-hostname-redirects.car" )
157
+ backend .namesys ["/ipns/example.net" ] = newMockNamesysItem (path .FromCid (root ), time .Second * 30 )
158
+
159
+ t .Run ("UnixFS generated directory listing without index.html has no Cache-Control" , func (t * testing.T ) {
160
+ req := mustNewRequest (t , http .MethodGet , ts .URL + "/ipns/example.net/" , nil )
161
+ res := mustDoWithoutRedirect (t , req )
162
+ require .Empty (t , res .Header ["Cache-Control" ])
163
+ })
164
+
165
+ t .Run ("UnixFS directory with index.html has Cache-Control" , func (t * testing.T ) {
166
+ req := mustNewRequest (t , http .MethodGet , ts .URL + "/ipns/example.net/foo/" , nil )
167
+ res := mustDoWithoutRedirect (t , req )
168
+ require .Equal (t , "public, max-age=30" , res .Header .Get ("Cache-Control" ))
169
+ })
170
+
171
+ t .Run ("UnixFS file has Cache-Control" , func (t * testing.T ) {
172
+ req := mustNewRequest (t , http .MethodGet , ts .URL + "/ipns/example.net/foo/index.html" , nil )
173
+ res := mustDoWithoutRedirect (t , req )
174
+ require .Equal (t , "public, max-age=30" , res .Header .Get ("Cache-Control" ))
175
+ })
176
+
177
+ t .Run ("Raw block has Cache-Control" , func (t * testing.T ) {
178
+ req := mustNewRequest (t , http .MethodGet , ts .URL + "/ipns/example.net?format=raw" , nil )
179
+ res := mustDoWithoutRedirect (t , req )
180
+ require .Equal (t , "public, max-age=30" , res .Header .Get ("Cache-Control" ))
181
+ })
182
+
183
+ t .Run ("DAG-JSON block has Cache-Control" , func (t * testing.T ) {
184
+ req := mustNewRequest (t , http .MethodGet , ts .URL + "/ipns/example.net?format=dag-json" , nil )
185
+ res := mustDoWithoutRedirect (t , req )
186
+ require .Equal (t , "public, max-age=30" , res .Header .Get ("Cache-Control" ))
187
+ })
188
+
189
+ t .Run ("DAG-CBOR block has Cache-Control" , func (t * testing.T ) {
190
+ req := mustNewRequest (t , http .MethodGet , ts .URL + "/ipns/example.net?format=dag-cbor" , nil )
191
+ res := mustDoWithoutRedirect (t , req )
192
+ require .Equal (t , "public, max-age=30" , res .Header .Get ("Cache-Control" ))
193
+ })
194
+
195
+ t .Run ("CAR block has Cache-Control" , func (t * testing.T ) {
196
+ req := mustNewRequest (t , http .MethodGet , ts .URL + "/ipns/example.net?format=car" , nil )
197
+ res := mustDoWithoutRedirect (t , req )
198
+ require .Equal (t , "public, max-age=30" , res .Header .Get ("Cache-Control" ))
199
+ })
200
+ })
201
+
202
+ t .Run ("Cache-Control is not immutable on generated /ipfs/ HTML dir listings" , func (t * testing.T ) {
154
203
req := mustNewRequest (t , http .MethodGet , ts .URL + "/ipfs/" + rootCID + "/" , nil )
155
204
res := mustDoWithoutRedirect (t , req )
156
205
@@ -492,7 +541,7 @@ func TestRedirects(t *testing.T) {
492
541
t .Parallel ()
493
542
494
543
ts , backend , root := newTestServerAndNode (t , nil , "ipns-hostname-redirects.car" )
495
- backend .namesys ["/ipns/example.net" ] = path .FromCid (root )
544
+ backend .namesys ["/ipns/example.net" ] = newMockNamesysItem ( path .FromCid (root ), 0 )
496
545
497
546
// make request to directory containing index.html
498
547
req := mustNewRequest (t , http .MethodGet , ts .URL + "/foo" , nil )
@@ -527,7 +576,7 @@ func TestRedirects(t *testing.T) {
527
576
t .Parallel ()
528
577
529
578
backend , root := newMockBackend (t , "redirects-spa.car" )
530
- backend .namesys ["/ipns/example.com" ] = path .FromCid (root )
579
+ backend .namesys ["/ipns/example.com" ] = newMockNamesysItem ( path .FromCid (root ), 0 )
531
580
532
581
ts := newTestServerWithConfig (t , backend , Config {
533
582
Headers : map [string ][]string {},
@@ -664,8 +713,8 @@ func TestDeserializedResponses(t *testing.T) {
664
713
t .Parallel ()
665
714
666
715
backend , root := newMockBackend (t , "fixtures.car" )
667
- backend .namesys ["/ipns/trustless.com" ] = path .FromCid (root )
668
- backend .namesys ["/ipns/trusted.com" ] = path .FromCid (root )
716
+ backend .namesys ["/ipns/trustless.com" ] = newMockNamesysItem ( path .FromCid (root ), 0 )
717
+ backend .namesys ["/ipns/trusted.com" ] = newMockNamesysItem ( path .FromCid (root ), 0 )
669
718
670
719
ts := newTestServerWithConfig (t , backend , Config {
671
720
Headers : map [string ][]string {},
@@ -727,8 +776,8 @@ func (mb *errorMockBackend) GetCAR(ctx context.Context, path ImmutablePath, para
727
776
return ContentPathMetadata {}, nil , mb .err
728
777
}
729
778
730
- func (mb * errorMockBackend ) ResolveMutable (ctx context.Context , path ipath.Path ) (ImmutablePath , error ) {
731
- return ImmutablePath {}, mb .err
779
+ func (mb * errorMockBackend ) ResolveMutable (ctx context.Context , path ipath.Path ) (ImmutablePath , time. Duration , error ) {
780
+ return ImmutablePath {}, 0 , mb .err
732
781
}
733
782
734
783
func (mb * errorMockBackend ) GetIPNSRecord (ctx context.Context , c cid.Cid ) ([]byte , error ) {
@@ -811,7 +860,7 @@ func (mb *panicMockBackend) GetCAR(ctx context.Context, immutablePath ImmutableP
811
860
panic ("i am panicking" )
812
861
}
813
862
814
- func (mb * panicMockBackend ) ResolveMutable (ctx context.Context , p ipath.Path ) (ImmutablePath , error ) {
863
+ func (mb * panicMockBackend ) ResolveMutable (ctx context.Context , p ipath.Path ) (ImmutablePath , time. Duration , error ) {
815
864
panic ("i am panicking" )
816
865
}
817
866
0 commit comments