diff --git a/test/core/interface.spec.js b/test/core/interface.spec.js index 67b9f974b3..44ccf730dd 100644 --- a/test/core/interface.spec.js +++ b/test/core/interface.spec.js @@ -1,11 +1,27 @@ -/* eslint-env mocha */ +/* eslint-env mocha, browser */ 'use strict' const tests = require('interface-ipfs-core') const CommonFactory = require('../utils/interface-common-factory') const isNode = require('detect-node') +const dnsFetchStub = require('../utils/dns-fetch-stub') describe('interface-ipfs-core tests', () => { + // ipfs.dns in the browser calls out to https://ipfs.io/api/v0/dns. + // The following code stubs self.fetch to return a static CID for calls + // to https://ipfs.io/api/v0/dns?arg=ipfs.io. + if (!isNode) { + const fetch = self.fetch + + before(() => { + self.fetch = dnsFetchStub(fetch) + }) + + after(() => { + self.fetch = fetch + }) + } + const defaultCommonFactory = CommonFactory.create() tests.bitswap(defaultCommonFactory, { skip: !isNode }) diff --git a/test/utils/dns-fetch-stub.js b/test/utils/dns-fetch-stub.js new file mode 100644 index 0000000000..a1e24a122c --- /dev/null +++ b/test/utils/dns-fetch-stub.js @@ -0,0 +1,16 @@ +'use strict' + +// Create a fetch stub with a fall through to the provided fetch implementation +// if the URL doesn't match https://ipfs.io/api/v0/dns?arg=ipfs.io. +module.exports = (fetch) => { + return function () { + if (arguments[0].startsWith('https://ipfs.io/api/v0/dns?arg=ipfs.io')) { + return Promise.resolve({ + json: () => Promise.resolve({ + Path: '/ipfs/QmYNQJoKGNHTpPxCBPh9KkDpaExgd2duMa3aF6ytMpHdao' + }) + }) + } + return fetch.apply(this, arguments) + } +}