@@ -20,6 +20,7 @@ import (
20
20
"github.com/ipfs/go-cid"
21
21
"github.com/libp2p/go-libp2p/core/crypto"
22
22
"github.com/libp2p/go-libp2p/core/peer"
23
+ "github.com/libp2p/go-libp2p/core/routing"
23
24
"github.com/multiformats/go-multiaddr"
24
25
"github.com/multiformats/go-multibase"
25
26
"github.com/multiformats/go-multihash"
@@ -317,6 +318,19 @@ func TestClient_FindProviders(t *testing.T) {
317
318
httpStatusCode : 404 ,
318
319
expResult : nil ,
319
320
},
321
+ {
322
+ name : "returns no providers if the HTTP server returns 200 with empty JSON array" ,
323
+ routerResult : []iter.Result [types.Record ]{},
324
+ expResult : nil ,
325
+ serverStreamingDisabled : true ,
326
+ expJSONResponse : true ,
327
+ },
328
+ {
329
+ name : "returns no providers if the HTTP server returns 200 with empty NDJSON" ,
330
+ routerResult : []iter.Result [types.Record ]{},
331
+ expResult : nil ,
332
+ expStreamingResponse : true ,
333
+ },
320
334
}
321
335
for _ , c := range cases {
322
336
t .Run (c .name , func (t * testing.T ) {
@@ -604,6 +618,19 @@ func TestClient_FindPeers(t *testing.T) {
604
618
httpStatusCode : 404 ,
605
619
expResult : nil ,
606
620
},
621
+ {
622
+ name : "returns no providers if the HTTP server returns 200 with empty JSON array" ,
623
+ routerResult : []iter.Result [* types.PeerRecord ]{},
624
+ expResult : nil ,
625
+ serverStreamingDisabled : true ,
626
+ expJSONResponse : true ,
627
+ },
628
+ {
629
+ name : "returns no providers if the HTTP server returns 200 with empty NDJSON" ,
630
+ routerResult : []iter.Result [* types.PeerRecord ]{},
631
+ expResult : nil ,
632
+ expStreamingResponse : true ,
633
+ },
607
634
}
608
635
for _ , c := range cases {
609
636
t .Run (c .name , func (t * testing.T ) {
@@ -676,6 +703,139 @@ func TestClient_FindPeers(t *testing.T) {
676
703
}
677
704
}
678
705
706
+ func TestClient_EmptyResponses (t * testing.T ) {
707
+ // Test that client handles various empty response formats correctly
708
+ cid := makeCID ()
709
+ pid := peer .ID ("12D3KooWD3eckifWpRn9wQpMG9R9hX3sD158z7EqHWmweQAJU5SA" )
710
+ _ , name := makeName (t )
711
+
712
+ // Helper to create test server with given response
713
+ makeTestServer := func (handler http.HandlerFunc ) * httptest.Server {
714
+ return httptest .NewServer (handler )
715
+ }
716
+
717
+ // Helper to verify empty results for providers
718
+ verifyEmptyProviders := func (t * testing.T , serverURL string ) {
719
+ client , err := New (serverURL , WithHTTPClient ((& http.Client {})))
720
+ require .NoError (t , err )
721
+
722
+ resultsIter , err := client .FindProviders (context .Background (), cid )
723
+ require .NoError (t , err )
724
+
725
+ results , err := iter .ReadAllResults (resultsIter )
726
+ require .NoError (t , err )
727
+ require .Empty (t , results )
728
+ }
729
+
730
+ // Helper to verify empty results for peers
731
+ verifyEmptyPeers := func (t * testing.T , serverURL string ) {
732
+ client , err := New (serverURL , WithHTTPClient ((& http.Client {})))
733
+ require .NoError (t , err )
734
+
735
+ resultsIter , err := client .FindPeers (context .Background (), pid )
736
+ require .NoError (t , err )
737
+
738
+ results , err := iter .ReadAllResults (resultsIter )
739
+ require .NoError (t , err )
740
+ require .Empty (t , results )
741
+ }
742
+
743
+ // Helper to verify IPNS not found
744
+ verifyIPNSNotFound := func (t * testing.T , serverURL string ) {
745
+ client , err := New (serverURL , WithHTTPClient ((& http.Client {})))
746
+ require .NoError (t , err )
747
+
748
+ record , err := client .GetIPNS (context .Background (), name )
749
+ require .ErrorIs (t , err , routing .ErrNotFound )
750
+ require .Nil (t , record )
751
+ }
752
+
753
+ testCases := []struct {
754
+ name string
755
+ handler http.HandlerFunc
756
+ testFunc func (* testing.T , string )
757
+ }{
758
+ // FindProviders tests
759
+ {
760
+ name : "404 Not Found (legacy server)" ,
761
+ handler : func (w http.ResponseWriter , r * http.Request ) {
762
+ w .WriteHeader (http .StatusNotFound )
763
+ w .Write ([]byte ("404 page not found" ))
764
+ },
765
+ testFunc : verifyEmptyProviders ,
766
+ },
767
+ {
768
+ name : "200 with null providers in JSON" ,
769
+ handler : func (w http.ResponseWriter , r * http.Request ) {
770
+ w .Header ().Set ("Content-Type" , "application/json" )
771
+ // Old server behavior - returns null instead of empty array
772
+ w .Write ([]byte (`{"Providers":null}` ))
773
+ },
774
+ testFunc : verifyEmptyProviders ,
775
+ },
776
+ {
777
+ name : "200 with empty array in JSON" ,
778
+ handler : func (w http.ResponseWriter , r * http.Request ) {
779
+ w .Header ().Set ("Content-Type" , "application/json" )
780
+ // New server behavior - returns empty array
781
+ w .Write ([]byte (`{"Providers":[]}` ))
782
+ },
783
+ testFunc : verifyEmptyProviders ,
784
+ },
785
+ {
786
+ name : "200 with empty NDJSON stream" ,
787
+ handler : func (w http.ResponseWriter , r * http.Request ) {
788
+ w .Header ().Set ("Content-Type" , "application/x-ndjson" )
789
+ // Empty body - no NDJSON lines
790
+ },
791
+ testFunc : verifyEmptyProviders ,
792
+ },
793
+ // FindPeers tests
794
+ {
795
+ name : "FindPeers: 404 Not Found (legacy server)" ,
796
+ handler : func (w http.ResponseWriter , r * http.Request ) {
797
+ w .WriteHeader (http .StatusNotFound )
798
+ w .Write ([]byte ("404 page not found" ))
799
+ },
800
+ testFunc : verifyEmptyPeers ,
801
+ },
802
+ {
803
+ name : "FindPeers: 200 with null peers in JSON" ,
804
+ handler : func (w http.ResponseWriter , r * http.Request ) {
805
+ w .Header ().Set ("Content-Type" , "application/json" )
806
+ // Old server behavior - returns null instead of empty array
807
+ w .Write ([]byte (`{"Peers":null}` ))
808
+ },
809
+ testFunc : verifyEmptyPeers ,
810
+ },
811
+ // IPNS tests
812
+ {
813
+ name : "IPNS: 404 Not Found (legacy server)" ,
814
+ handler : func (w http.ResponseWriter , r * http.Request ) {
815
+ w .WriteHeader (http .StatusNotFound )
816
+ w .Write ([]byte ("404 page not found" ))
817
+ },
818
+ testFunc : verifyIPNSNotFound ,
819
+ },
820
+ {
821
+ name : "IPNS: 200 with text/plain (no record found)" ,
822
+ handler : func (w http.ResponseWriter , r * http.Request ) {
823
+ w .Header ().Set ("Content-Type" , "text/plain; charset=utf-8" )
824
+ w .Write ([]byte ("delegate error: routing: not found" ))
825
+ },
826
+ testFunc : verifyIPNSNotFound ,
827
+ },
828
+ }
829
+
830
+ for _ , tc := range testCases {
831
+ t .Run (tc .name , func (t * testing.T ) {
832
+ server := makeTestServer (tc .handler )
833
+ defer server .Close ()
834
+ tc .testFunc (t , server .URL )
835
+ })
836
+ }
837
+ }
838
+
679
839
func TestNormalizeBaseURL (t * testing.T ) {
680
840
cases := []struct {
681
841
name string
0 commit comments