diff --git a/lib/mock_agent.js b/lib/mock_agent.js index 304f849..8e61655 100644 --- a/lib/mock_agent.js +++ b/lib/mock_agent.js @@ -1,5 +1,23 @@ const { debuglog } = require('util'); -const { MockAgent, setGlobalDispatcher, getGlobalDispatcher } = require('urllib'); +let { MockAgent, setGlobalDispatcher, getGlobalDispatcher } = require('urllib'); +if (typeof getGlobalDispatcher === 'undefined') { + let urllibNext; + // https://github.com/eggjs/egg/blob/3.x/package.json#L59 + try { + // try to use urllib4 + urllibNext = require('urllib4'); + } catch { + // try to use urllib-next + try { + urllibNext = require('urllib-next'); + } catch { + throw new Error('Please install urllib@4'); + } + } + MockAgent = urllibNext.MockAgent; + setGlobalDispatcher = urllibNext.setGlobalDispatcher; + getGlobalDispatcher = urllibNext.getGlobalDispatcher; +} const debug = debuglog('egg-mock:lib:mock_agent'); diff --git a/test/fixtures/apps/no-framework/plugin/a/package.json b/test/fixtures/apps/no-framework/plugin/a/package.json index 7e3daa8..565d744 100644 --- a/test/fixtures/apps/no-framework/plugin/a/package.json +++ b/test/fixtures/apps/no-framework/plugin/a/package.json @@ -1,6 +1,8 @@ { "eggPlugin": { "name": "a", - "dependencies": [ "egg-mock" ] + "dependencies": [ + "egg-mock" + ] } } diff --git a/test/fixtures/server/package.json b/test/fixtures/server/package.json index 69a979e..f9096fc 100644 --- a/test/fixtures/server/package.json +++ b/test/fixtures/server/package.json @@ -1,4 +1,4 @@ { "name": "server", "version": "1.0.0" -} \ No newline at end of file +}