Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 553a65b

Browse files
authored
test: stub out ipfs interactions in http injection tests (#2990)
* test: stub out ipfs interactions in http injection tests This makes running the tests faster as we do no network operations, nor do we wait for ports to open, etc. Functionality covered by the tests is also covered by the interface tests so there is no testing gap.
1 parent fbb5ef9 commit 553a65b

39 files changed

+3443
-2959
lines changed

packages/ipfs/package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"test:node": "cross-env ECHO_SERVER_PORT=37481 aegir test -t node",
4545
"test:browser": "cross-env ECHO_SERVER_PORT=37482 aegir test -t browser",
4646
"test:browser:http": "cross-env ECHO_SERVER_PORT=37489 aegir test -t browser -f test/http-api/index.js",
47-
"test:browser:interface:http": "cross-env ECHO_SERVER_PORT=37489 aegir test -t browser -f test/http-api/interface.js",
4847
"test:webworker": "cross-env ECHO_SERVER_PORT=37483 aegir test -t webworker",
4948
"test:electron": "cross-env ECHO_SERVER_PORT=37484 aegir test -t electron-main -t electron-renderer",
5049
"test:electron-main": "cross-env ECHO_SERVER_PORT=37485 aegir test -t electron-main",
@@ -53,7 +52,8 @@
5352
"test:node:core": "cross-env ECHO_SERVER_PORT=37488 aegir test -t node -f test/core/**/*.js",
5453
"test:node:http": "cross-env ECHO_SERVER_PORT=37489 aegir test -t node -f test/http-api/index.js",
5554
"test:node:gateway": "cross-env ECHO_SERVER_PORT=37490 aegir test -t node -f test/gateway/index.js",
56-
"test:node:interface": "cross-env ECHO_SERVER_PORT=37491 aegir test -t node -f test/core/interface.spec.js",
55+
"test:interface": "cross-env ECHO_SERVER_PORT=37491 aegir test -f test/core/interface.spec.js",
56+
"test:interface:http": "cross-env ECHO_SERVER_PORT=37489 aegir test -f test/http-api/interface.js",
5757
"test:bootstrapers": "cross-env ECHO_SERVER_PORT=37492 IPFS_TEST=bootstrapers aegir test -t browser -f test/bootstrapers.js",
5858
"test:interop": "cross-env IPFS_JS_EXEC=$PWD/src/cli/bin.js IPFS_JS_MODULE=$PWD IPFS_REUSEPORT=false ipfs-interop",
5959
"test:interop:node": "cross-env IPFS_JS_EXEC=$PWD/src/cli/bin.js IPFS_JS_MODULE=$PWD IPFS_REUSEPORT=false ipfs-interop -- -t node",
@@ -145,7 +145,7 @@
145145
"libp2p-record": "^0.7.0",
146146
"libp2p-secio": "^0.12.2",
147147
"libp2p-tcp": "^0.14.3",
148-
"libp2p-webrtc-star": "^0.17.6",
148+
"libp2p-webrtc-star": "^0.17.9",
149149
"libp2p-websockets": "^0.13.3",
150150
"mafmt": "^7.0.0",
151151
"merge-options": "^2.0.0",
@@ -185,10 +185,11 @@
185185
"form-data": "^3.0.0",
186186
"go-ipfs-dep": "0.4.23-3",
187187
"interface-ipfs-core": "^0.134.0",
188-
"ipfs-interop": "ipfs/interop#fix/name-pubsub",
188+
"ipfs-interop": "^1.0.1",
189189
"ipfsd-ctl": "^3.0.0",
190190
"iso-random-stream": "^1.1.1",
191191
"it-first": "^1.0.1",
192+
"it-to-buffer": "^1.0.0",
192193
"nanoid": "^3.0.2",
193194
"ncp": "^2.0.0",
194195
"p-event": "^4.1.0",

packages/ipfs/src/http/api/resources/dag.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ exports.put = {
173173
query: Joi.object().keys({
174174
format: Joi.string().default('cbor'),
175175
'input-enc': Joi.string().default('json'),
176-
pin: Joi.boolean(),
176+
pin: Joi.boolean().default(false),
177177
hash: Joi.string().valid(...Object.keys(mh.names)).default('sha2-256'),
178178
'cid-base': Joi.string().valid(...multibase.names)
179179
}).unknown()
@@ -245,16 +245,13 @@ exports.put = {
245245
try {
246246
cid = await ipfs.dag.put(node, {
247247
format: format,
248-
hashAlg: hashAlg
248+
hashAlg: hashAlg,
249+
pin: request.query.pin
249250
})
250251
} catch (err) {
251252
throw Boom.boomify(err, { message: 'Failed to put node' })
252253
}
253254

254-
if (request.query.pin) {
255-
await ipfs.pin.add(cid)
256-
}
257-
258255
return h.response({
259256
Cid: {
260257
'/': cidToString(cid, {
@@ -287,15 +284,13 @@ exports.resolve = {
287284
let lastRemainderPath = path
288285

289286
if (path) {
290-
const result = ipfs.dag.resolve(lastCid, path)
291-
while (true) {
292-
const resolveResult = (await result.next()).value
293-
if (!CID.isCID(resolveResult.value)) {
287+
for await (const { value, remainderPath } of ipfs.dag.resolve(lastCid, path)) {
288+
if (!CID.isCID(value)) {
294289
break
295290
}
296291

297-
lastRemainderPath = resolveResult.remainderPath
298-
lastCid = resolveResult.value
292+
lastRemainderPath = remainderPath
293+
lastCid = value
299294
}
300295
}
301296

packages/ipfs/src/http/api/resources/dht.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ exports.findPeer = {
2121
let res
2222

2323
try {
24-
res = await ipfs.dht.findPeer(arg)
24+
res = await ipfs.dht.findPeer(new CID(arg))
2525
} catch (err) {
2626
if (err.code === 'ERR_LOOKUP_FAILED') {
2727
throw Boom.notFound(err.toString())
@@ -52,9 +52,9 @@ exports.findProvs = {
5252
const ipfs = request.server.app.ipfs
5353
const { arg } = request.query
5454

55-
request.query.maxNumProviders = request.query['num-providers']
56-
57-
const res = await all(ipfs.dht.findProvs(arg, { numProviders: request.query['num-providers'] }))
55+
const res = await all(ipfs.dht.findProvs(new CID(arg), {
56+
numProviders: request.query['num-providers']
57+
}))
5858

5959
return h.response({
6060
Responses: res.map(({ id, addrs }) => ({
+31-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
'use strict'
22

3-
const Boom = require('@hapi/boom')
3+
const Joi = require('@hapi/joi')
44

5-
module.exports = async (request, h) => {
6-
const domain = request.query.arg
5+
module.exports = {
6+
validate: {
7+
options: {
8+
allowUnknown: true,
9+
stripUnknown: true
10+
},
11+
query: Joi.object().keys({
12+
arg: Joi.string().required(),
13+
format: Joi.string(),
14+
recursive: Joi.boolean().default(false)
15+
})
16+
.rename('r', 'recursive', {
17+
override: true,
18+
ignoreUndefined: true
19+
})
20+
},
21+
async handler (request, h) {
22+
const {
23+
arg,
24+
format,
25+
recursive
26+
} = request.query
727

8-
if (!domain) {
9-
throw Boom.badRequest("Argument 'domain' is required")
10-
}
11-
12-
const format = request.query.format
28+
const path = await request.server.app.ipfs.dns(arg, {
29+
recursive,
30+
format
31+
})
1332

14-
// query parameters are passed as strings and need to be parsed to expected type
15-
let recursive = request.query.recursive || request.query.r
16-
recursive = !(recursive && recursive === 'false')
17-
18-
const path = await request.server.app.ipfs.dns(domain, { recursive, format })
19-
return h.response({
20-
Path: path
21-
})
33+
return h.response({
34+
Path: path
35+
})
36+
}
2237
}

packages/ipfs/src/http/api/resources/files-regular.js

+32-20
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,13 @@ exports.add = {
249249

250250
exports.ls = {
251251
validate: {
252-
query: Joi.object().keys({
253-
'cid-base': Joi.string().valid(...multibase.names),
254-
stream: Joi.boolean()
255-
}).unknown()
252+
query: Joi.object()
253+
.keys({
254+
arg: Joi.string().required(),
255+
'cid-base': Joi.string().valid(...multibase.names),
256+
stream: Joi.boolean().default(false),
257+
recursive: Joi.boolean().default(false)
258+
}).unknown()
256259
},
257260

258261
// uses common parseKey method that returns a `key`
@@ -262,8 +265,11 @@ exports.ls = {
262265
async handler (request, h) {
263266
const { ipfs } = request.server.app
264267
const { key } = request.pre.args
265-
const recursive = request.query && request.query.recursive === 'true'
266-
const cidBase = request.query['cid-base']
268+
const {
269+
recursive,
270+
stream,
271+
'cid-base': cidBase
272+
} = request.query
267273

268274
const mapLink = link => {
269275
const output = {
@@ -286,15 +292,16 @@ exports.ls = {
286292
return output
287293
}
288294

289-
if (!request.query.stream) {
290-
let links
295+
if (!stream) {
291296
try {
292-
links = await all(ipfs.ls(key, { recursive }))
297+
const links = await all(ipfs.ls(key, {
298+
recursive
299+
}))
300+
301+
return h.response({ Objects: [{ Hash: key, Links: links.map(mapLink) }] })
293302
} catch (err) {
294303
throw Boom.boomify(err, { message: 'Failed to list dir' })
295304
}
296-
297-
return h.response({ Objects: [{ Hash: key, Links: links.map(mapLink) }] })
298305
}
299306

300307
return streamResponse(request, h, () => pipe(
@@ -334,17 +341,22 @@ exports.refs = {
334341
handler (request, h) {
335342
const { ipfs } = request.server.app
336343
const { key } = request.pre.args
337-
338-
const options = {
339-
recursive: request.query.recursive,
340-
format: request.query.format,
341-
edges: request.query.edges,
342-
unique: request.query.unique,
343-
maxDepth: request.query['max-depth']
344-
}
344+
const {
345+
recursive,
346+
format,
347+
edges,
348+
unique,
349+
'max-depth': maxDepth
350+
} = request.query
345351

346352
return streamResponse(request, h, () => pipe(
347-
ipfs.refs(key, options),
353+
ipfs.refs(key, {
354+
recursive,
355+
format,
356+
edges,
357+
unique,
358+
maxDepth
359+
}),
348360
map(({ ref, err }) => ({ Ref: ref, Err: err })),
349361
ndjson.stringify
350362
))

packages/ipfs/src/http/api/resources/files/flush.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const mfsFlush = {
1212
cidBase
1313
} = request.query
1414

15-
let cid = await ipfs.files.flush(arg || '/', {})
15+
let cid = await ipfs.files.flush(arg || '/')
1616

1717
if (cidBase && cidBase !== 'base58btc' && cid.version === 0) {
1818
cid = cid.toV1()

packages/ipfs/src/http/api/resources/key.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ exports.rename = async (request, h) => {
3535
exports.gen = async (request, h) => {
3636
const { ipfs } = request.server.app
3737
const { arg, type, size } = request.query
38-
const key = await ipfs.key.gen(arg, { type, size: parseInt(size) })
38+
const key = await ipfs.key.gen(arg, {
39+
type,
40+
size: parseInt(size)
41+
})
3942
return h.response(toKeyInfo(key))
4043
}
4144

packages/ipfs/src/http/api/resources/name.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ exports.resolve = {
1818
},
1919
async handler (request, h) {
2020
const { ipfs } = request.server.app
21-
const { arg, stream } = request.query
21+
const {
22+
arg,
23+
nocache,
24+
recursive,
25+
stream
26+
} = request.query
2227

2328
if (!stream) {
24-
const value = await last(ipfs.name.resolve(arg, request.query))
29+
const value = await last(ipfs.name.resolve(arg, {
30+
nocache,
31+
recursive
32+
}))
2533
return h.response({ Path: value })
2634
}
2735

@@ -39,14 +47,29 @@ exports.publish = {
3947
arg: Joi.string().required(),
4048
resolve: Joi.boolean().default(true),
4149
lifetime: Joi.string().default('24h'),
42-
key: Joi.string().default('self')
50+
ttl: Joi.string(),
51+
key: Joi.string().default('self'),
52+
'allow-offline': Joi.boolean()
4353
}).unknown()
4454
},
4555
async handler (request, h) {
4656
const { ipfs } = request.server.app
47-
const { arg } = request.query
57+
const {
58+
arg,
59+
resolve,
60+
lifetime,
61+
ttl,
62+
key,
63+
'allow-offline': allowOffline
64+
} = request.query
4865

49-
const res = await ipfs.name.publish(arg, request.query)
66+
const res = await ipfs.name.publish(arg, {
67+
resolve,
68+
lifetime,
69+
ttl,
70+
key,
71+
allowOffline
72+
})
5073

5174
return h.response({
5275
Name: res.name,

packages/ipfs/src/http/api/resources/object.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ exports.get = {
8383

8484
let node, cid
8585
try {
86-
node = await ipfs.object.get(key, { enc })
86+
node = await ipfs.object.get(key, {
87+
enc
88+
})
8789
cid = await dagPB.util.cid(dagPB.util.serialize(node))
8890
} catch (err) {
8991
throw Boom.boomify(err, { message: 'Failed to get object' })
@@ -490,7 +492,9 @@ exports.patchRmLink = {
490492

491493
let cid, node
492494
try {
493-
cid = await ipfs.object.patch.rmLink(root, { name: link })
495+
cid = await ipfs.object.patch.rmLink(root, {
496+
name: link
497+
})
494498
node = await ipfs.object.get(cid)
495499
} catch (err) {
496500
throw Boom.boomify(err, { message: 'Failed to remove link from object' })

packages/ipfs/src/http/api/resources/pin.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ exports.ls = {
6060

6161
if (!request.query.stream) {
6262
const res = await pipe(
63-
ipfs.pin.ls(path, { type }),
63+
ipfs.pin.ls(path, {
64+
type
65+
}),
6466
reduce((res, { type, cid }) => {
6567
res.Keys[cidToString(cid, { base: request.query['cid-base'] })] = { Type: type }
6668
return res
@@ -93,7 +95,9 @@ exports.add = {
9395

9496
let result
9597
try {
96-
result = await ipfs.pin.add(path, { recursive })
98+
result = await ipfs.pin.add(path, {
99+
recursive
100+
})
97101
} catch (err) {
98102
if (err.message.includes('already pinned recursively')) {
99103
throw Boom.boomify(err, { statusCode: 400 })
@@ -122,7 +126,9 @@ exports.rm = {
122126

123127
let result
124128
try {
125-
result = await ipfs.pin.rm(path, { recursive })
129+
result = await ipfs.pin.rm(path, {
130+
recursive
131+
})
126132
} catch (err) {
127133
throw Boom.boomify(err, { message: 'Failed to remove pin' })
128134
}

packages/ipfs/src/http/api/resources/ping.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ module.exports = {
2727
const count = request.query.n || request.query.count || 10
2828

2929
return streamResponse(request, h, () => pipe(
30-
ipfs.ping(peerId, { count }),
30+
ipfs.ping(peerId, {
31+
count
32+
}),
3133
map(pong => ({ Success: pong.success, Time: pong.time, Text: pong.text })),
3234
ndjson.stringify
3335
))

0 commit comments

Comments
 (0)