@@ -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 () })
@@ -544,3 +550,137 @@ func TestGoGetSupport(t *testing.T) {
544550 assert .Nil (t , err )
545551 assert .Equal (t , http .StatusOK , res .StatusCode )
546552}
553+
554+ func TestIpfsTrustlessMode (t * testing.T ) {
555+ api , root := newMockAPI (t )
556+
557+ ts := newTestServerWithConfig (t , api , Config {
558+ Headers : map [string ][]string {},
559+ NoDNSLink : false ,
560+ PublicGateways : map [string ]* Specification {
561+ "trustless.com" : {
562+ Paths : []string {"/ipfs" , "/ipns" },
563+ },
564+ "trusted.com" : {
565+ Paths : []string {"/ipfs" , "/ipns" },
566+ TrustedMode : true ,
567+ },
568+ },
569+ })
570+ t .Logf ("test server url: %s" , ts .URL )
571+
572+ trustedFormats := []string {"" , "dag-json" , "dag-cbor" , "tar" , "json" , "cbor" }
573+ trustlessFormats := []string {"raw" , "car" }
574+
575+ doRequest := func (t * testing.T , path , host string , expectedStatus int ) {
576+ req , err := http .NewRequest (http .MethodGet , ts .URL + path , 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+ doIpfsCidRequests := func (t * testing.T , formats []string , host string , expectedStatus int ) {
590+ for _ , format := range formats {
591+ doRequest (t , "/ipfs/" + root .String ()+ "/?format=" + format , host , expectedStatus )
592+ }
593+ }
594+
595+ doIpfsCidPathRequests := func (t * testing.T , formats []string , host string , expectedStatus int ) {
596+ for _ , format := range formats {
597+ doRequest (t , "/ipfs/" + root .String ()+ "/EmptyDir/?format=" + format , host , expectedStatus )
598+ }
599+ }
600+
601+ trustedTests := func (t * testing.T , host string ) {
602+ doIpfsCidRequests (t , trustlessFormats , host , http .StatusOK )
603+ doIpfsCidRequests (t , trustedFormats , host , http .StatusOK )
604+ doIpfsCidPathRequests (t , trustlessFormats , host , http .StatusOK )
605+ doIpfsCidPathRequests (t , trustedFormats , host , http .StatusOK )
606+ }
607+
608+ trustlessTests := func (t * testing.T , host string ) {
609+ doIpfsCidRequests (t , trustlessFormats , host , http .StatusOK )
610+ doIpfsCidRequests (t , trustedFormats , host , http .StatusNotImplemented )
611+ doIpfsCidPathRequests (t , trustlessFormats , host , http .StatusNotImplemented )
612+ doIpfsCidPathRequests (t , trustedFormats , host , http .StatusNotImplemented )
613+ }
614+
615+ t .Run ("Explicit Trustless Gateway" , func (t * testing.T ) {
616+ t .Parallel ()
617+ trustlessTests (t , "trustless.com" )
618+ })
619+
620+ t .Run ("Explicit Trusted Gateway" , func (t * testing.T ) {
621+ t .Parallel ()
622+ trustedTests (t , "trusted.com" )
623+ })
624+
625+ t .Run ("Implicit Default Trustless Gateway" , func (t * testing.T ) {
626+ t .Parallel ()
627+ trustlessTests (t , "not.configured.com" )
628+ })
629+
630+ t .Run ("Implicit Default Local Trusted Gateway" , func (t * testing.T ) {
631+ t .Parallel ()
632+ trustedTests (t , "localhost" )
633+ trustedTests (t , "127.0.0.1" )
634+ trustedTests (t , "::1" )
635+
636+ // For this exception, also check if ports would match.
637+ trustedTests (t , "localhost:8080" )
638+ trustedTests (t , "127.0.0.1:8080" )
639+ trustedTests (t , "[::1]:8080" )
640+ })
641+ }
642+
643+ func TestIpnsTrustlessMode (t * testing.T ) {
644+ api , root := newMockAPI (t )
645+ api .namesys ["/ipns/trustless.com" ] = path .FromCid (root )
646+ api .namesys ["/ipns/trusted.com" ] = path .FromCid (root )
647+
648+ ts := newTestServerWithConfig (t , api , Config {
649+ Headers : map [string ][]string {},
650+ NoDNSLink : false ,
651+ PublicGateways : map [string ]* Specification {
652+ "trustless.com" : {
653+ Paths : []string {"/ipfs" , "/ipns" },
654+ },
655+ "trusted.com" : {
656+ Paths : []string {"/ipfs" , "/ipns" },
657+ TrustedMode : true ,
658+ },
659+ },
660+ })
661+ t .Logf ("test server url: %s" , ts .URL )
662+
663+ doRequest := func (t * testing.T , path , host string , expectedStatus int ) {
664+ req , err := http .NewRequest (http .MethodGet , ts .URL + path , nil )
665+ assert .Nil (t , err )
666+
667+ if host != "" {
668+ req .Host = host
669+ }
670+
671+ res , err := doWithoutRedirect (req )
672+ assert .Nil (t , err )
673+ defer res .Body .Close ()
674+ assert .Equal (t , expectedStatus , res .StatusCode )
675+ }
676+
677+ // DNSLink only. Not supported for trustless. Supported for trusted, except
678+ // format=ipns-record which is unavailable for DNSLink.
679+ doRequest (t , "/" , "trustless.com" , http .StatusNotImplemented )
680+ doRequest (t , "/EmptyDir/" , "trustless.com" , http .StatusNotImplemented )
681+ doRequest (t , "/?format=ipns-record" , "trustless.com" , http .StatusNotImplemented )
682+
683+ doRequest (t , "/" , "trusted.com" , http .StatusOK )
684+ doRequest (t , "/EmptyDir/" , "trusted.com" , http .StatusOK )
685+ doRequest (t , "/?format=ipns-record" , "trusted.com" , http .StatusBadRequest )
686+ }
0 commit comments