@@ -198,14 +198,20 @@ func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, *mock
198198}
199199
200200func 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 {
202208 AddAccessControlHeaders (config .Headers )
203209
204210 handler := NewHandler (config , api )
205211 mux := http .NewServeMux ()
206212 mux .Handle ("/ipfs/" , handler )
207213 mux .Handle ("/ipns/" , handler )
208- handler = WithHostname (mux , api , map [ string ] * Specification {}, false )
214+ handler = WithHostname (config , api , mux )
209215
210216 ts := httptest .NewServer (handler )
211217 t .Cleanup (func () { ts .Close () })
@@ -546,3 +552,128 @@ func TestGoGetSupport(t *testing.T) {
546552 assert .Nil (t , err )
547553 assert .Equal (t , http .StatusOK , res .StatusCode )
548554}
555+
556+ func TestIpfsTrustlessMode (t * testing.T ) {
557+ api , root := newMockAPI (t )
558+
559+ ts := newTestServerWithConfig (t , api , Config {
560+ Headers : map [string ][]string {},
561+ NoDNSLink : false ,
562+ PublicGateways : map [string ]* Specification {
563+ "trustless.com" : {
564+ Paths : []string {"/ipfs" , "/ipns" },
565+ },
566+ "trusted.com" : {
567+ Paths : []string {"/ipfs" , "/ipns" },
568+ TrustedMode : true ,
569+ },
570+ },
571+ })
572+ t .Logf ("test server url: %s" , ts .URL )
573+
574+ trustedFormats := []string {"" , "dag-json" , "dag-cbor" , "tar" , "json" , "cbor" }
575+ trustlessFormats := []string {"raw" , "car" }
576+
577+ doRequest := func (t * testing.T , path , host string , expectedStatus int ) {
578+ req , err := http .NewRequest (http .MethodGet , ts .URL + path , nil )
579+ assert .Nil (t , err )
580+
581+ if host != "" {
582+ req .Host = host
583+ }
584+
585+ res , err := doWithoutRedirect (req )
586+ assert .Nil (t , err )
587+ defer res .Body .Close ()
588+ assert .Equal (t , expectedStatus , res .StatusCode )
589+ }
590+
591+ doIpfsCidRequests := func (t * testing.T , formats []string , host string , expectedStatus int ) {
592+ for _ , format := range formats {
593+ doRequest (t , "/ipfs/" + root .String ()+ "/?format=" + format , host , expectedStatus )
594+ }
595+ }
596+
597+ doIpfsCidPathRequests := func (t * testing.T , formats []string , host string , expectedStatus int ) {
598+ for _ , format := range formats {
599+ doRequest (t , "/ipfs/" + root .String ()+ "/EmptyDir/?format=" + format , host , expectedStatus )
600+ }
601+ }
602+
603+ trustedTests := func (t * testing.T , host string ) {
604+ doIpfsCidRequests (t , trustlessFormats , host , http .StatusOK )
605+ doIpfsCidRequests (t , trustedFormats , host , http .StatusOK )
606+ doIpfsCidPathRequests (t , trustlessFormats , host , http .StatusOK )
607+ doIpfsCidPathRequests (t , trustedFormats , host , http .StatusOK )
608+ }
609+
610+ trustlessTests := func (t * testing.T , host string ) {
611+ doIpfsCidRequests (t , trustlessFormats , host , http .StatusOK )
612+ doIpfsCidRequests (t , trustedFormats , host , http .StatusNotImplemented )
613+ doIpfsCidPathRequests (t , trustlessFormats , host , http .StatusNotImplemented )
614+ doIpfsCidPathRequests (t , trustedFormats , host , http .StatusNotImplemented )
615+ }
616+
617+ t .Run ("Explicit Trustless Gateway" , func (t * testing.T ) {
618+ t .Parallel ()
619+ trustlessTests (t , "trustless.com" )
620+ })
621+
622+ t .Run ("Explicit Trusted Gateway" , func (t * testing.T ) {
623+ t .Parallel ()
624+ trustedTests (t , "trusted.com" )
625+ })
626+
627+ t .Run ("Implicit Default Trustless Gateway" , func (t * testing.T ) {
628+ t .Parallel ()
629+ trustlessTests (t , "not.configured.com" )
630+ trustlessTests (t , "localhost" )
631+ trustlessTests (t , "127.0.0.1" )
632+ trustlessTests (t , "::1" )
633+ })
634+ }
635+
636+ func TestIpnsTrustlessMode (t * testing.T ) {
637+ api , root := newMockAPI (t )
638+ api .namesys ["/ipns/trustless.com" ] = path .FromCid (root )
639+ api .namesys ["/ipns/trusted.com" ] = path .FromCid (root )
640+
641+ ts := newTestServerWithConfig (t , api , Config {
642+ Headers : map [string ][]string {},
643+ NoDNSLink : false ,
644+ PublicGateways : map [string ]* Specification {
645+ "trustless.com" : {
646+ Paths : []string {"/ipfs" , "/ipns" },
647+ },
648+ "trusted.com" : {
649+ Paths : []string {"/ipfs" , "/ipns" },
650+ TrustedMode : true ,
651+ },
652+ },
653+ })
654+ t .Logf ("test server url: %s" , ts .URL )
655+
656+ doRequest := func (t * testing.T , path , host string , expectedStatus int ) {
657+ req , err := http .NewRequest (http .MethodGet , ts .URL + path , nil )
658+ assert .Nil (t , err )
659+
660+ if host != "" {
661+ req .Host = host
662+ }
663+
664+ res , err := doWithoutRedirect (req )
665+ assert .Nil (t , err )
666+ defer res .Body .Close ()
667+ assert .Equal (t , expectedStatus , res .StatusCode )
668+ }
669+
670+ // DNSLink only. Not supported for trustless. Supported for trusted, except
671+ // format=ipns-record which is unavailable for DNSLink.
672+ doRequest (t , "/" , "trustless.com" , http .StatusNotImplemented )
673+ doRequest (t , "/EmptyDir/" , "trustless.com" , http .StatusNotImplemented )
674+ doRequest (t , "/?format=ipns-record" , "trustless.com" , http .StatusNotImplemented )
675+
676+ doRequest (t , "/" , "trusted.com" , http .StatusOK )
677+ doRequest (t , "/EmptyDir/" , "trusted.com" , http .StatusOK )
678+ doRequest (t , "/?format=ipns-record" , "trusted.com" , http .StatusBadRequest )
679+ }
0 commit comments