@cryptotaxi247 / kubo / commits / 65b8d530b

test: add streaming delegated routing test (#9874)

Henrique Dias committed May 16, 2023 at 14:05 UTC 65b8d530b0bb6f0608264b7e9e6d1aaa2378bd0a
1 file changed +41 -9
test/cli/delegated_routing_http_test.go
+41 -9
@@ -15,18 +15,20 @@ func TestHTTPDelegatedRouting(t *testing.T) {
15 t.Parallel()
16 node := harness.NewT(t).NewNode().Init().StartDaemon()
17
18 - fakeServer := func(resp string) *httptest.Server {
18 + fakeServer := func(contentType string, resp ...string) *httptest.Server {
19 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20 - w.Header().Set("Content-Type", "application/json")
21 - _, err := w.Write([]byte(resp))
22 - if err != nil {
23 - panic(err)
20 + w.Header().Set("Content-Type", contentType)
21 + for _, r := range resp {
22 + _, err := w.Write([]byte(r))
23 + if err != nil {
24 + panic(err)
25 + }
26 }
27 }))
28 }
29
30 findProvsCID := "baeabep4vu3ceru7nerjjbk37sxb7wmftteve4hcosmyolsbsiubw2vr6pqzj6mw7kv6tbn6nqkkldnklbjgm5tzbi4hkpkled4xlcr7xz4bq"
29 - prov := "12D3KooWARYacCc6eoCqvsS9RW9MA2vo51CV75deoiqssx3YgyYJ"
31 + provs := []string{"12D3KooWAobjw92XDcnQ1rRmRJDA3zAQpdPYUpZKrJxH6yccSpje", "12D3KooWARYacCc6eoCqvsS9RW9MA2vo51CV75deoiqssx3YgyYJ"}
32
33 t.Run("default routing config has no routers defined", func(t *testing.T) {
34 assert.Nil(t, node.ReadConfig().Routing.Routers)
@@ -85,11 +87,11 @@ func TestHTTPDelegatedRouting(t *testing.T) {
87 })
88
89 t.Run("adding HTTP delegated routing endpoint to Routing.Routers config works", func(t *testing.T) {
88 - server := fakeServer(ToJSONStr(JSONObj{
90 + server := fakeServer("application/json", ToJSONStr(JSONObj{
91 "Providers": []JSONObj{{
92 "Protocol": "transport-bitswap",
93 "Schema": "bitswap",
92 - "ID": prov,
94 + "ID": provs[0],
95 "Addrs": []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/tcp/4002"},
96 }},
97 }))
@@ -114,9 +116,39 @@ func TestHTTPDelegatedRouting(t *testing.T) {
116 assert.Equal(t, res.Stdout.Trimmed(), server.URL)
117
118 node.StartDaemon()
119 + res = node.IPFS("routing", "findprovs", findProvsCID)
120 + assert.Equal(t, provs[0], res.Stdout.Trimmed())
121 + })
122 +
123 + node.StopDaemon()
124 +
125 + t.Run("adding HTTP delegated routing endpoint to Routing.Routers config works (streaming)", func(t *testing.T) {
126 + server := fakeServer("application/x-ndjson", ToJSONStr(JSONObj{
127 + "Protocol": "transport-bitswap",
128 + "Schema": "bitswap",
129 + "ID": provs[1],
130 + "Addrs": []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/tcp/4002"},
131 + }), ToJSONStr(JSONObj{
132 + "Protocol": "transport-bitswap",
133 + "Schema": "bitswap",
134 + "ID": provs[0],
135 + "Addrs": []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/tcp/4002"},
136 + }))
137 + t.Cleanup(server.Close)
138 +
139 + node.IPFS("config", "Routing.Routers.TestDelegatedRouter", "--json", ToJSONStr(JSONObj{
140 + "Type": "http",
141 + "Parameters": JSONObj{
142 + "Endpoint": server.URL,
143 + },
144 + }))
145
146 + res := node.IPFS("config", "Routing.Routers.TestDelegatedRouter.Parameters.Endpoint")
147 + assert.Equal(t, res.Stdout.Trimmed(), server.URL)
148 +
149 + node.StartDaemon()
150 res = node.IPFS("routing", "findprovs", findProvsCID)
119 - assert.Equal(t, prov, res.Stdout.Trimmed())
151 + assert.Equal(t, provs[1]+"\n"+provs[0], res.Stdout.Trimmed())
152 })
153
154 t.Run("HTTP client should emit OpenCensus metrics", func(t *testing.T) {