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

Commit 86c3d81

Browse files
authored
fix: stub out call to fetch for ipfs.dns test in browser (#1512)
Stubs self.fetch to return a static CID for calls to https://ipfs.io/api/v0/dns?arg=ipfs.io. Removes dependency on external service. License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent afd3255 commit 86c3d81

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

test/core/interface.spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
/* eslint-env mocha */
1+
/* eslint-env mocha, browser */
22
'use strict'
33

44
const tests = require('interface-ipfs-core')
55
const CommonFactory = require('../utils/interface-common-factory')
66
const isNode = require('detect-node')
7+
const dnsFetchStub = require('../utils/dns-fetch-stub')
78

89
describe('interface-ipfs-core tests', () => {
10+
// ipfs.dns in the browser calls out to https://ipfs.io/api/v0/dns.
11+
// The following code stubs self.fetch to return a static CID for calls
12+
// to https://ipfs.io/api/v0/dns?arg=ipfs.io.
13+
if (!isNode) {
14+
const fetch = self.fetch
15+
16+
before(() => {
17+
self.fetch = dnsFetchStub(fetch)
18+
})
19+
20+
after(() => {
21+
self.fetch = fetch
22+
})
23+
}
24+
925
const defaultCommonFactory = CommonFactory.create()
1026

1127
tests.bitswap(defaultCommonFactory, { skip: !isNode })

test/utils/dns-fetch-stub.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
// Create a fetch stub with a fall through to the provided fetch implementation
4+
// if the URL doesn't match https://ipfs.io/api/v0/dns?arg=ipfs.io.
5+
module.exports = (fetch) => {
6+
return function () {
7+
if (arguments[0].startsWith('https://ipfs.io/api/v0/dns?arg=ipfs.io')) {
8+
return Promise.resolve({
9+
json: () => Promise.resolve({
10+
Path: '/ipfs/QmYNQJoKGNHTpPxCBPh9KkDpaExgd2duMa3aF6ytMpHdao'
11+
})
12+
})
13+
}
14+
return fetch.apply(this, arguments)
15+
}
16+
}

0 commit comments

Comments
 (0)