Skip to content

Commit b88cd64

Browse files
committed
chore: reduce number of changes
1 parent ebb9997 commit b88cd64

File tree

3 files changed

+15
-51
lines changed

3 files changed

+15
-51
lines changed

packages/client/.aegir.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,16 @@ const options = {
2222
echo.polka.post('/add-providers/:cid', (req, res) => {
2323
callCount++
2424
try {
25-
if (!req.headers['content-type']?.includes('application/json')) {
26-
res.statusCode = 400
27-
res.end(JSON.stringify({
28-
error: 'Invalid content type. Expected application/json',
29-
code: 'ERR_INVALID_INPUT'
30-
}))
31-
providers.delete(req.params.cid)
32-
return
25+
26+
let data
27+
try {
28+
// when passed data from a test where `body=providers.map(prov => JSON.stringify(prov)).join('\n')`
29+
data = { Providers: req.body.split('\n').map(line => JSON.parse(line)) }
30+
} catch (err) {
31+
// when passed data from a test where `body=JSON.stringify({ Providers: providers })`
32+
data = req.body
3333
}
34-
35-
const data = typeof req.body === 'string'
36-
? { Providers: req.body.split('\n').map(line => JSON.parse(line)) }
37-
: req.body
38-
34+
3935
providers.set(req.params.cid, data)
4036
res.end(JSON.stringify({ success: true }))
4137
} catch (err) {
@@ -53,7 +49,7 @@ const options = {
5349
try {
5450
const providerData = providers.get(req.params.cid) || { Providers: [] }
5551
const acceptHeader = req.headers.accept
56-
52+
5753
if (acceptHeader?.includes('application/x-ndjson')) {
5854
res.setHeader('Content-Type', 'application/x-ndjson')
5955
const providers = Array.isArray(providerData.Providers) ? providerData.Providers : []

packages/client/test/index.spec.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ describe('delegated-routing-v1-http-api-client', () => {
5252
// load providers for the router to fetch
5353
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
5454
method: 'POST',
55-
headers: {
56-
'Content-Type': 'application/json'
57-
},
58-
body: JSON.stringify({ Providers: providers })
55+
body: providers.map(prov => JSON.stringify(prov)).join('\n')
5956
})
6057

6158
const provs = await all(client.getProviders(cid))
@@ -103,26 +100,6 @@ describe('delegated-routing-v1-http-api-client', () => {
103100
}
104101
})
105102

106-
it('should handle non-json input', async () => {
107-
const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
108-
109-
const response = await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
110-
method: 'POST',
111-
headers: {
112-
'Content-Type': 'text/plain'
113-
},
114-
body: 'not json'
115-
})
116-
117-
expect(response.status).to.equal(400)
118-
const errorData = await response.json()
119-
expect(errorData).to.have.property('error')
120-
expect(errorData).to.have.property('code', 'ERR_INVALID_INPUT')
121-
122-
const provs = await all(client.getProviders(cid))
123-
expect(provs).to.be.empty()
124-
})
125-
126103
it('should add filter parameters the query of the request url', async () => {
127104
const providers = [{
128105
Protocol: 'transport-bitswap',
@@ -180,7 +157,7 @@ describe('delegated-routing-v1-http-api-client', () => {
180157
expect(searchParams.get('filter-addrs')).to.equal('tcp,!p2p-circuit')
181158
})
182159

183-
it('should handle non-json input without content-type header', async () => {
160+
it('should handle non-json input', async () => {
184161
const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
185162

186163
// load providers for the router to fetch
@@ -375,10 +352,7 @@ describe('delegated-routing-v1-http-api-client', () => {
375352
// load providers for the router to fetch
376353
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
377354
method: 'POST',
378-
headers: {
379-
'Content-Type': 'application/json'
380-
},
381-
body: JSON.stringify({ Providers: providers })
355+
body: providers.map(prov => JSON.stringify(prov)).join('\n')
382356
})
383357

384358
// Reset call count before our test
@@ -431,10 +405,7 @@ describe('delegated-routing-v1-http-api-client', () => {
431405
// load providers for the router to fetch
432406
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
433407
method: 'POST',
434-
headers: {
435-
'Content-Type': 'application/json'
436-
},
437-
body: JSON.stringify({ Providers: providers })
408+
body: providers.map(prov => JSON.stringify(prov)).join('\n')
438409
})
439410

440411
// Reset call count

packages/client/test/routings.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ describe('libp2p content-routing', () => {
6969
// load providers for the router to fetch
7070
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
7171
method: 'POST',
72-
headers: {
73-
'Content-Type': 'application/json'
74-
},
75-
body: JSON.stringify({ Providers: providers })
72+
body: providers.map(prov => JSON.stringify(prov)).join('\n')
7673
})
7774

7875
const provs = await all(routing.findProviders(cid))

0 commit comments

Comments
 (0)