Skip to content

Commit

Permalink
test: test filtering in findPeers
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Oct 1, 2024
1 parent 4789262 commit 77f19d7
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions routing/http/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,13 @@ func TestClient_Provide(t *testing.T) {
}

func TestClient_FindPeers(t *testing.T) {
peerRecord := makePeerRecord([]string{"transport-bitswap"})
peerRecord1 := makePeerRecord([]string{"transport-bitswap"})
peerRecord2 := makePeerRecord([]string{"transport-ipfs-gateway-http"})
peerRecords := []iter.Result[*types.PeerRecord]{
{Val: &peerRecord},
{Val: &peerRecord1},
{Val: &peerRecord2},
}
pid := *peerRecord.ID
pid := *peerRecord1.ID

cases := []struct {
name string
Expand All @@ -510,6 +512,7 @@ func TestClient_FindPeers(t *testing.T) {
routerErr error
clientRequiresStreaming bool
serverStreamingDisabled bool
filterProtocols []string

expErrContains osErrContains
expResult []iter.Result[*types.PeerRecord]
Expand All @@ -522,6 +525,13 @@ func TestClient_FindPeers(t *testing.T) {
expResult: peerRecords,
expStreamingResponse: true,
},
{
name: "happy case with protocol filter",
filterProtocols: []string{"transport-bitswap"},
routerResult: peerRecords,
expResult: []iter.Result[*types.PeerRecord]{{Val: &peerRecord1}},
expStreamingResponse: true,
},
{
name: "server doesn't support streaming",
routerResult: peerRecords,
Expand Down Expand Up @@ -556,12 +566,10 @@ func TestClient_FindPeers(t *testing.T) {
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var (
clientOpts []Option
serverOpts []server.Option
onRespReceived []func(*http.Response)
onReqReceived []func(*http.Request)
)
var clientOpts []Option
var serverOpts []server.Option
var onRespReceived []func(*http.Response)
var onReqReceived []func(*http.Request)

if c.serverStreamingDisabled {
serverOpts = append(serverOpts, server.WithStreamingResultsDisabled())
Expand All @@ -574,6 +582,10 @@ func TestClient_FindPeers(t *testing.T) {
})
}

if c.filterProtocols != nil {
clientOpts = append(clientOpts, WithProtocolFilter(c.filterProtocols))
}

if c.expStreamingResponse {
onRespReceived = append(onRespReceived, func(r *http.Response) {
assert.Equal(t, mediaTypeNDJSON, r.Header.Get("Content-Type"))
Expand Down Expand Up @@ -617,7 +629,7 @@ func TestClient_FindPeers(t *testing.T) {
resultIter, err := client.FindPeers(ctx, pid)
c.expErrContains.errContains(t, err)

results := iter.ReadAll[iter.Result[*types.PeerRecord]](resultIter)
results := iter.ReadAll(resultIter)
assert.Equal(t, c.expResult, results)
})
}
Expand Down

0 comments on commit 77f19d7

Please sign in to comment.