@@ -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
+ TrustedMode : 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 () })
@@ -544,3 +550,75 @@ func TestGoGetSupport(t *testing.T) {
544
550
assert .Nil (t , err )
545
551
assert .Equal (t , http .StatusOK , res .StatusCode )
546
552
}
553
+
554
+ func TestTrustlessMode (t * testing.T ) {
555
+ api , root := newMockAPI (t )
556
+ ts := newTestServerWithConfig (t , api , Config {
557
+ Headers : map [string ][]string {},
558
+ NoDNSLink : false ,
559
+ PublicGateways : map [string ]* Specification {
560
+ "trustless.com" : {
561
+ Paths : []string {"/ipfs" , "/ipns" },
562
+ },
563
+ "trusted.com" : {
564
+ Paths : []string {"/ipfs" , "/ipns" },
565
+ TrustedMode : true ,
566
+ },
567
+ },
568
+ })
569
+ t .Logf ("test server url: %s" , ts .URL )
570
+
571
+ trustedFormats := []string {"" , "dag-json" , "dag-cbor" , "tar" , "json" , "cbor" }
572
+ trustlessFormats := []string {"raw" , "car" }
573
+
574
+ doRequests := func (formats []string , host string , expectedStatus int ) {
575
+ for _ , format := range formats {
576
+ req , err := http .NewRequest (http .MethodGet , ts .URL + "/ipfs/" + root .String ()+ "/?format=" + format , nil )
577
+ assert .Nil (t , err )
578
+
579
+ if host != "" {
580
+ req .Host = host
581
+ }
582
+
583
+ res , err := doWithoutRedirect (req )
584
+ assert .Nil (t , err )
585
+ defer res .Body .Close ()
586
+ assert .Equal (t , expectedStatus , res .StatusCode )
587
+ }
588
+ }
589
+
590
+ t .Run ("Explicit Trustless Gateway" , func (t * testing.T ) {
591
+ t .Parallel ()
592
+ doRequests (trustlessFormats , "trustless.com" , http .StatusOK )
593
+ doRequests (trustedFormats , "trustless.com" , http .StatusNotImplemented )
594
+ })
595
+
596
+ t .Run ("Explicit Trusted Gateway" , func (t * testing.T ) {
597
+ t .Parallel ()
598
+ doRequests (trustlessFormats , "trusted.com" , http .StatusOK )
599
+ doRequests (trustedFormats , "trusted.com" , http .StatusOK )
600
+ })
601
+
602
+ t .Run ("Implicit Default Trustless Gateway" , func (t * testing.T ) {
603
+ t .Parallel ()
604
+ doRequests (trustlessFormats , "not.configured.com" , http .StatusOK )
605
+ doRequests (trustedFormats , "not.configured.com" , http .StatusNotImplemented )
606
+ })
607
+
608
+ t .Run ("Implicit Default Local Trusted Gateway" , func (t * testing.T ) {
609
+ t .Parallel ()
610
+ doRequests (trustlessFormats , "localhost" , http .StatusOK )
611
+ doRequests (trustedFormats , "localhost" , http .StatusOK )
612
+ doRequests (trustlessFormats , "127.0.0.1" , http .StatusOK )
613
+ doRequests (trustedFormats , "127.0.0.1" , http .StatusOK )
614
+ doRequests (trustlessFormats , "::1" , http .StatusOK )
615
+ doRequests (trustedFormats , "::1" , http .StatusOK )
616
+
617
+ doRequests (trustlessFormats , "localhost:8080" , http .StatusOK )
618
+ doRequests (trustedFormats , "localhost:8080" , http .StatusOK )
619
+ doRequests (trustlessFormats , "127.0.0.1:8080" , http .StatusOK )
620
+ doRequests (trustedFormats , "127.0.0.1:8080" , http .StatusOK )
621
+ doRequests (trustlessFormats , "[::1]:8080" , http .StatusOK )
622
+ doRequests (trustedFormats , "[::1]:8080" , http .StatusOK )
623
+ })
624
+ }
0 commit comments