From b31ea3db8eae9119fff2867f32c0368070ac6179 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 16 Aug 2018 15:42:44 +0100 Subject: [PATCH 1/2] fix: stub out call to fetch for ipfs.dns test in browser 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 --- test/core/interface.spec.js | 18 +++++++++++++++++- test/utils/dns-fetch-stub.js | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/utils/dns-fetch-stub.js diff --git a/test/core/interface.spec.js b/test/core/interface.spec.js index 67b9f974b3..f333ad8d1d 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() + }) + + 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..fc85620b6e --- /dev/null +++ b/test/utils/dns-fetch-stub.js @@ -0,0 +1,17 @@ +/* eslint-env browser */ +'use strict' + +const _fetch = typeof self === 'undefined' ? null : self.fetch + +module.exports = () => { + 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) + } +} From 528ae2a261f509658409627fb7e08894983e6d62 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 16 Aug 2018 15:58:38 +0100 Subject: [PATCH 2/2] fix: switch to providing fetch implementation So no chance of null is not a function error. License: MIT Signed-off-by: Alan Shaw --- test/core/interface.spec.js | 2 +- test/utils/dns-fetch-stub.js | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/test/core/interface.spec.js b/test/core/interface.spec.js index f333ad8d1d..44ccf730dd 100644 --- a/test/core/interface.spec.js +++ b/test/core/interface.spec.js @@ -14,7 +14,7 @@ describe('interface-ipfs-core tests', () => { const fetch = self.fetch before(() => { - self.fetch = dnsFetchStub() + self.fetch = dnsFetchStub(fetch) }) after(() => { diff --git a/test/utils/dns-fetch-stub.js b/test/utils/dns-fetch-stub.js index fc85620b6e..a1e24a122c 100644 --- a/test/utils/dns-fetch-stub.js +++ b/test/utils/dns-fetch-stub.js @@ -1,9 +1,8 @@ -/* eslint-env browser */ 'use strict' -const _fetch = typeof self === 'undefined' ? null : self.fetch - -module.exports = () => { +// 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({ @@ -12,6 +11,6 @@ module.exports = () => { }) }) } - return _fetch.apply(this, arguments) + return fetch.apply(this, arguments) } }