@@ -198,14 +198,20 @@ func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, *mock
198
198
}
199
199
200
200
func newTestServer (t * testing.T , api IPFSBackend ) * httptest.Server {
201
- config := Config {Headers : map [string ][]string {}}
201
+ return newTestServerWithConfig (t , api , Config {
202
+ Headers : map [string ][]string {},
203
+ DeserializedResponses : true ,
204
+ })
205
+ }
206
+
207
+ func newTestServerWithConfig (t * testing.T , api IPFSBackend , config Config ) * httptest.Server {
202
208
AddAccessControlHeaders (config .Headers )
203
209
204
210
handler := NewHandler (config , api )
205
211
mux := http .NewServeMux ()
206
212
mux .Handle ("/ipfs/" , handler )
207
213
mux .Handle ("/ipns/" , handler )
208
- handler = WithHostname (mux , api , map [ string ] * Specification {}, false )
214
+ handler = WithHostname (config , api , mux )
209
215
210
216
ts := httptest .NewServer (handler )
211
217
t .Cleanup (func () { ts .Close () })
@@ -573,3 +579,128 @@ func TestIpnsBase58MultihashRedirect(t *testing.T) {
573
579
assert .Equal (t , "/ipns/k2k4r8ol4m8kkcqz509c1rcjwunebj02gcnm5excpx842u736nja8ger?keep=query" , res .Header .Get ("Location" ))
574
580
})
575
581
}
582
+
583
+ func TestIpfsTrustlessMode (t * testing.T ) {
584
+ api , root := newMockAPI (t )
585
+
586
+ ts := newTestServerWithConfig (t , api , Config {
587
+ Headers : map [string ][]string {},
588
+ NoDNSLink : false ,
589
+ PublicGateways : map [string ]* Specification {
590
+ "trustless.com" : {
591
+ Paths : []string {"/ipfs" , "/ipns" },
592
+ },
593
+ "trusted.com" : {
594
+ Paths : []string {"/ipfs" , "/ipns" },
595
+ DeserializedResponses : true ,
596
+ },
597
+ },
598
+ })
599
+ t .Logf ("test server url: %s" , ts .URL )
600
+
601
+ trustedFormats := []string {"" , "dag-json" , "dag-cbor" , "tar" , "json" , "cbor" }
602
+ trustlessFormats := []string {"raw" , "car" }
603
+
604
+ doRequest := func (t * testing.T , path , host string , expectedStatus int ) {
605
+ req , err := http .NewRequest (http .MethodGet , ts .URL + path , nil )
606
+ assert .Nil (t , err )
607
+
608
+ if host != "" {
609
+ req .Host = host
610
+ }
611
+
612
+ res , err := doWithoutRedirect (req )
613
+ assert .Nil (t , err )
614
+ defer res .Body .Close ()
615
+ assert .Equal (t , expectedStatus , res .StatusCode )
616
+ }
617
+
618
+ doIpfsCidRequests := func (t * testing.T , formats []string , host string , expectedStatus int ) {
619
+ for _ , format := range formats {
620
+ doRequest (t , "/ipfs/" + root .String ()+ "/?format=" + format , host , expectedStatus )
621
+ }
622
+ }
623
+
624
+ doIpfsCidPathRequests := func (t * testing.T , formats []string , host string , expectedStatus int ) {
625
+ for _ , format := range formats {
626
+ doRequest (t , "/ipfs/" + root .String ()+ "/EmptyDir/?format=" + format , host , expectedStatus )
627
+ }
628
+ }
629
+
630
+ trustedTests := func (t * testing.T , host string ) {
631
+ doIpfsCidRequests (t , trustlessFormats , host , http .StatusOK )
632
+ doIpfsCidRequests (t , trustedFormats , host , http .StatusOK )
633
+ doIpfsCidPathRequests (t , trustlessFormats , host , http .StatusOK )
634
+ doIpfsCidPathRequests (t , trustedFormats , host , http .StatusOK )
635
+ }
636
+
637
+ trustlessTests := func (t * testing.T , host string ) {
638
+ doIpfsCidRequests (t , trustlessFormats , host , http .StatusOK )
639
+ doIpfsCidRequests (t , trustedFormats , host , http .StatusNotAcceptable )
640
+ doIpfsCidPathRequests (t , trustlessFormats , host , http .StatusNotAcceptable )
641
+ doIpfsCidPathRequests (t , trustedFormats , host , http .StatusNotAcceptable )
642
+ }
643
+
644
+ t .Run ("Explicit Trustless Gateway" , func (t * testing.T ) {
645
+ t .Parallel ()
646
+ trustlessTests (t , "trustless.com" )
647
+ })
648
+
649
+ t .Run ("Explicit Trusted Gateway" , func (t * testing.T ) {
650
+ t .Parallel ()
651
+ trustedTests (t , "trusted.com" )
652
+ })
653
+
654
+ t .Run ("Implicit Default Trustless Gateway" , func (t * testing.T ) {
655
+ t .Parallel ()
656
+ trustlessTests (t , "not.configured.com" )
657
+ trustlessTests (t , "localhost" )
658
+ trustlessTests (t , "127.0.0.1" )
659
+ trustlessTests (t , "::1" )
660
+ })
661
+ }
662
+
663
+ func TestIpnsTrustlessMode (t * testing.T ) {
664
+ api , root := newMockAPI (t )
665
+ api .namesys ["/ipns/trustless.com" ] = path .FromCid (root )
666
+ api .namesys ["/ipns/trusted.com" ] = path .FromCid (root )
667
+
668
+ ts := newTestServerWithConfig (t , api , Config {
669
+ Headers : map [string ][]string {},
670
+ NoDNSLink : false ,
671
+ PublicGateways : map [string ]* Specification {
672
+ "trustless.com" : {
673
+ Paths : []string {"/ipfs" , "/ipns" },
674
+ },
675
+ "trusted.com" : {
676
+ Paths : []string {"/ipfs" , "/ipns" },
677
+ DeserializedResponses : true ,
678
+ },
679
+ },
680
+ })
681
+ t .Logf ("test server url: %s" , ts .URL )
682
+
683
+ doRequest := func (t * testing.T , path , host string , expectedStatus int ) {
684
+ req , err := http .NewRequest (http .MethodGet , ts .URL + path , nil )
685
+ assert .Nil (t , err )
686
+
687
+ if host != "" {
688
+ req .Host = host
689
+ }
690
+
691
+ res , err := doWithoutRedirect (req )
692
+ assert .Nil (t , err )
693
+ defer res .Body .Close ()
694
+ assert .Equal (t , expectedStatus , res .StatusCode )
695
+ }
696
+
697
+ // DNSLink only. Not supported for trustless. Supported for trusted, except
698
+ // format=ipns-record which is unavailable for DNSLink.
699
+ doRequest (t , "/" , "trustless.com" , http .StatusNotAcceptable )
700
+ doRequest (t , "/EmptyDir/" , "trustless.com" , http .StatusNotAcceptable )
701
+ doRequest (t , "/?format=ipns-record" , "trustless.com" , http .StatusNotAcceptable )
702
+
703
+ doRequest (t , "/" , "trusted.com" , http .StatusOK )
704
+ doRequest (t , "/EmptyDir/" , "trusted.com" , http .StatusOK )
705
+ doRequest (t , "/?format=ipns-record" , "trusted.com" , http .StatusBadRequest )
706
+ }
0 commit comments