Skip to content

Commit

Permalink
make Requests more readily available
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 21, 2018
1 parent 066e812 commit 0a5d2c6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Replaces `fetch` with a stub which records its calls, grouped by route, and opti
- `glob:http://*.*` to match glob patterns
- `express:/user/:user` to match [express style paths](https://www.npmjs.com/package/path-to-regexp)
- `RegExp`: A regular expression to test the url against
- `Function(url, opts)`: A function (returning a Boolean) that is passed the url and opts `fetch()` is called with (or, if `fetch()` was called with one, the `Request` instance)
- `Function(url, options, [request])`: A function (returning a Boolean) that is passed the url and options `fetch()` is called with. If `fetch()` was called with a `Request` instance, `url` and some basic `options` will be extracted from teh `Request`, but if more fine graned examination of it is needed, it is available as the third parameter

_Note that if using `end:` or an exact url matcher, `fetch-mock` ([for good reason](https://url.spec.whatwg.org/#url-equivalence)) is unable to distinguish whether URLs without a path end in a trailing slash or not i.e. `http://thing` is treated the same as `http://thing/`_

Expand Down
4 changes: 2 additions & 2 deletions src/lib/compile-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ const generateMatcher = route => {
getUrlMatcher(route)
].filter(matcher => !!matcher);

return (url, options = {}) => {
return matchers.every(matcher => matcher(url, options));
return (url, options = {}, request) => {
return matchers.every(matcher => matcher(url, options, request));
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/fetch-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ FetchMock.generateResponse = async function(response, url, opts) {
};

FetchMock.router = function(url, options, request) {
const route = this.routes.find(route => route.matcher(url, options));
const route = this.routes.find(route => route.matcher(url, options, request));

if (route) {
this.push(route.name, { url, options, request });
Expand Down
37 changes: 37 additions & 0 deletions test/specs/routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ module.exports = fetchMock => {
expect(fm.calls(true).length).to.equal(1);
});

it('match using custom function using request body', async () => {
fm.mock((url, opts) => opts.body === 'a string', 200).catch();
await fm.fetchHandler('http://it.at.there/logged-in');
expect(fm.calls(true).length).to.equal(0);
await fm.fetchHandler('http://it.at.there/logged-in', {
method: 'post',
body: 'a string'
});
expect(fm.calls(true).length).to.equal(1);
});

it('match using custom function with Request', async () => {
fm.mock((url, options) => {
return url.indexOf('logged-in') > -1 && options.headers.authorized;
Expand All @@ -170,6 +181,32 @@ module.exports = fetchMock => {
expect(fm.calls(true).length).to.equal(1);
});

it('match using custom function with Request with unusual options', async () => {
// as node-fetch does not try to emulate all the WHATWG standards, we can't check for the
// same properties in the browser and nodejs
const propertyToCheck = new fm.config.Request('http://example.com')
.cache
? 'cache'
: 'timeout';
const valueToSet = propertyToCheck === 'cache' ? 'force-cache' : 2000;

fm.mock(
(url, options, request) => request[propertyToCheck] === valueToSet,
200
).catch();

await fm.fetchHandler(
new fm.config.Request('http://it.at.there/logged-in')
);
expect(fm.calls(true).length).to.equal(0);
await fm.fetchHandler(
new fm.config.Request('http://it.at.there/logged-in', {
[propertyToCheck]: valueToSet
})
);
expect(fm.calls(true).length).to.equal(1);
});

describe('headers', () => {
it('not match when headers not present', async () => {
fm.mock('http://it.at.there/', 200, {
Expand Down

0 comments on commit 0a5d2c6

Please sign in to comment.