Skip to content

Commit

Permalink
solve the signal problem
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Dec 4, 2019
1 parent b384567 commit ee581ff
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 29 deletions.
9 changes: 6 additions & 3 deletions src/lib/fetch-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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());
Expand Down
9 changes: 4 additions & 5 deletions src/lib/request-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 2 additions & 20 deletions test/specs/inspecting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ee581ff

Please sign in to comment.