diff --git a/src/lib/fetch-handler.js b/src/lib/fetch-handler.js index 73b9990d..f983e298 100755 --- a/src/lib/fetch-handler.js +++ b/src/lib/fetch-handler.js @@ -47,11 +47,15 @@ const resolve = async ( }; FetchMock.fetchHandler = function(url, options, request) { - ({ url, options, request } = requestUtils.normalizeRequest( + const normalizedRequest = requestUtils.normalizeRequest( url, options, this.config.Request - )); + ); + + ({ url, options, request } = normalizedRequest); + + const { signal } = normalizedRequest; const route = this.executeRouter(url, options, request); @@ -62,7 +66,6 @@ FetchMock.fetchHandler = function(url, options, request) { // wrapped in this promise to make sure we respect custom Promise // constructors defined by the user return new this.config.Promise((res, rej) => { - const signal = options && options.signal; if (signal) { const abort = () => { rej(new AbortError()); diff --git a/src/lib/request-utils.js b/src/lib/request-utils.js index 3a1e7c55..319c00f2 100644 --- a/src/lib/request-utils.js +++ b/src/lib/request-utils.js @@ -42,13 +42,11 @@ module.exports = { const derivedOptions = { method: url.method }; - if (url.signal) { - derivedOptions.signal = url.signal; - } const normalizedRequestObject = { url: normalizeUrl(url.url), options: Object.assign(derivedOptions, options), - request: url + request: url, + signal: (options && options.signal) || url.signal }; const headers = headersToArray(url.headers); @@ -64,7 +62,8 @@ module.exports = { ) { return { url: normalizeUrl(url), - options: options + options: options, + signal: options && options.signal }; } else if (typeof url === 'object') { throw new TypeError( diff --git a/test/runner.js b/test/runner.js index 0511a80b..264d3b27 100644 --- a/test/runner.js +++ b/test/runner.js @@ -5,7 +5,7 @@ module.exports = (fetchMock, theGlobal, fetch, AbortController) => { require('./specs/sandbox.test')(fetchMock, theGlobal); require('./specs/routing.test')(fetchMock); require('./specs/responses.test')(fetchMock); - require('./specs/inspecting.test')(fetchMock, theGlobal, AbortController); + require('./specs/inspecting.test')(fetchMock); require('./specs/repeat.test')(fetchMock); require('./specs/custom-implementations.test')(fetchMock); require('./specs/options.test')(fetchMock, theGlobal, fetch); diff --git a/test/specs/inspecting.test.js b/test/specs/inspecting.test.js index 70cfa661..e14d1508 100644 --- a/test/specs/inspecting.test.js +++ b/test/specs/inspecting.test.js @@ -5,7 +5,7 @@ const chai = require('chai'); const expect = chai.expect; const sinon = require('sinon'); -module.exports = (fetchMock, theGlobal, AbortController) => { +module.exports = fetchMock => { describe('inspecting', () => { let fm; before(() => { @@ -516,27 +516,9 @@ module.exports = (fetchMock, theGlobal, AbortController) => { expect(fm.lastCall().request).to.equal(req); }); - it('Make signal available in options when called with Request instance using signal', () => { - const controller = new AbortController(); - const req = new fm.config.Request('http://it.at.here/', { - method: 'POST', - signal: controller.signal - }); - fm.fetchHandler(req); - const [, callOptions] = fm.lastCall(); - - expect(callOptions).to.eql({ - method: 'POST', - signal: controller.signal - }); - const options = fm.lastOptions(); - expect(options).to.eql({ method: 'POST', signal: controller.signal }); - expect(fm.lastCall().request).to.equal(req); - }); - it('Not make default signal available in options when called with Request instance using signal', () => { const req = new fm.config.Request('http://it.at.here/', { - method: 'POST', + method: 'POST' }); fm.fetchHandler(req); const [, callOptions] = fm.lastCall();