@@ -2,6 +2,7 @@ package server
2
2
3
3
import (
4
4
"context"
5
+ "io"
5
6
"net/http"
6
7
"net/http/httptest"
7
8
"testing"
@@ -10,6 +11,7 @@ import (
10
11
"github.com/ipfs/boxo/routing/http/types"
11
12
"github.com/ipfs/boxo/routing/http/types/iter"
12
13
"github.com/ipfs/go-cid"
14
+ "github.com/libp2p/go-libp2p/core/peer"
13
15
"github.com/stretchr/testify/mock"
14
16
"github.com/stretchr/testify/require"
15
17
)
@@ -48,6 +50,60 @@ func TestHeaders(t *testing.T) {
48
50
require .Equal (t , "text/plain; charset=utf-8" , header )
49
51
}
50
52
53
+ func TestResponse (t * testing.T ) {
54
+ pidStr := "12D3KooWM8sovaEGU1bmiWGWAzvs47DEcXKZZTuJnpQyVTkRs2Vn"
55
+ cidStr := "bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4"
56
+
57
+ pid , err := peer .Decode (pidStr )
58
+ require .NoError (t , err )
59
+
60
+ cid , err := cid .Decode (cidStr )
61
+ require .NoError (t , err )
62
+
63
+ runTest := func (t * testing.T , contentType string , expected string ) {
64
+ t .Parallel ()
65
+
66
+ results := iter .FromSlice ([]iter.Result [types.ProviderResponse ]{
67
+ {Val : & types.ReadBitswapProviderRecord {
68
+ Protocol : "transport-bitswap" ,
69
+ Schema : types .SchemaBitswap ,
70
+ ID : & pid ,
71
+ Addrs : []types.Multiaddr {},
72
+ }}},
73
+ )
74
+
75
+ router := & mockContentRouter {}
76
+ server := httptest .NewServer (Handler (router ))
77
+ t .Cleanup (server .Close )
78
+ serverAddr := "http://" + server .Listener .Addr ().String ()
79
+ router .On ("FindProviders" , mock .Anything , cid ).Return (results , nil )
80
+ urlStr := serverAddr + ProvidePath + cidStr
81
+
82
+ req , err := http .NewRequest (http .MethodGet , urlStr , nil )
83
+ require .NoError (t , err )
84
+ req .Header .Set ("Accept" , contentType )
85
+
86
+ resp , err := http .DefaultClient .Do (req )
87
+ require .NoError (t , err )
88
+ require .Equal (t , 200 , resp .StatusCode )
89
+ header := resp .Header .Get ("Content-Type" )
90
+ require .Equal (t , contentType , header )
91
+
92
+ body , err := io .ReadAll (resp .Body )
93
+ require .NoError (t , err )
94
+
95
+ require .Equal (t , string (body ), expected )
96
+ }
97
+
98
+ t .Run ("JSON Response" , func (t * testing.T ) {
99
+ runTest (t , mediaTypeJSON , `{"Providers":[{"Protocol":"transport-bitswap","Schema":"bitswap","ID":"12D3KooWM8sovaEGU1bmiWGWAzvs47DEcXKZZTuJnpQyVTkRs2Vn","Addrs":[]}]}` )
100
+ })
101
+
102
+ t .Run ("NDJSON Response" , func (t * testing.T ) {
103
+ runTest (t , mediaTypeNDJSON , `{"Protocol":"transport-bitswap","Schema":"bitswap","ID":"12D3KooWM8sovaEGU1bmiWGWAzvs47DEcXKZZTuJnpQyVTkRs2Vn","Addrs":[]}` + "\n " )
104
+ })
105
+ }
106
+
51
107
type mockContentRouter struct { mock.Mock }
52
108
53
109
func (m * mockContentRouter ) FindProviders (ctx context.Context , key cid.Cid ) (iter.ResultIter [types.ProviderResponse ], error ) {
0 commit comments