Skip to content

Commit

Permalink
more tidying of compile route
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 2, 2018
1 parent 33f5c0f commit 48ee354
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 51 deletions.
71 changes: 22 additions & 49 deletions src/lib/compile-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ const stringMatchers = {
};

function getHeaderMatcher({ headers: expectedHeaders }) {
if (!expectedHeaders) {
return () => true;
}
const expectation = headerUtils.toLowerCase(expectedHeaders);
return (url, { headers = {} }) => {
const lowerCaseHeaders = headerUtils.toLowerCase(
Expand All @@ -37,58 +34,44 @@ function getHeaderMatcher({ headers: expectedHeaders }) {
};
}

const getMethodMatcher = route => {
return (url, { method }) => {
return (
!route.method || route.method === (method ? method.toLowerCase() : 'get')
);
};
const getMethodMatcher = ({ method: expectedMethod }) => {
return (url, { method }) =>
expectedMethod === (method ? method.toLowerCase() : 'get');
};

const getQueryStringMatcher = route => {
if (!route.query) {
return () => true;
}
const keys = Object.keys(route.query);
const getQueryStringMatcher = ({ query: expectedQuery }) => {
const keys = Object.keys(expectedQuery);
return url => {
const query = querystring.parse(URL.parse(url).query);
return keys.every(key => query[key] === route.query[key]);
return keys.every(key => query[key] === expectedQuery[key]);
};
};

const getUrlMatcher = route => {
// When the matcher is a function it should not be compared with the url
// in the normal way
if (typeof route.matcher === 'function') {
return () => true;
const getUrlMatcher = ({ matcher, query }) => {
if (typeof matcher === 'function') {
return matcher;
}

if (route.matcher instanceof RegExp) {
const urlRX = route.matcher;
return url => urlRX.test(url);
if (matcher instanceof RegExp) {
return url => matcher.test(url);
}

if (route.matcher === '*') {
if (matcher === '*') {
return () => true;
}

if (route.matcher.indexOf('^') === 0) {
throw new Error(
"Using '^' to denote the start of a url is deprecated. Use 'begin:' instead"
);
}

for (const shorthand in stringMatchers) {
if (route.matcher.indexOf(shorthand + ':') === 0) {
const url = route.matcher.replace(new RegExp(`^${shorthand}:`), '');
if (matcher.indexOf(shorthand + ':') === 0) {
const url = matcher.replace(new RegExp(`^${shorthand}:`), '');
return stringMatchers[shorthand](url);
}
}

// if none of the special syntaxes apply, it's just a simple string match
const expectedUrl = route.matcher;
const expectedUrl = matcher;

return url => {
if (route.query && expectedUrl.indexOf('?')) {
if (query && expectedUrl.indexOf('?')) {
return url.indexOf(expectedUrl) === 0;
}
return url === expectedUrl;
Expand Down Expand Up @@ -120,23 +103,13 @@ const sanitizeRoute = route => {
return route;
};

const getFunctionMatcher = route => {
if (typeof route.matcher === 'function') {
const matcher = route.matcher;
return (url, options) => matcher(url, options);
} else {
return () => true;
}
};

const generateMatcher = route => {
const matchers = [
getQueryStringMatcher(route),
getMethodMatcher(route),
getHeaderMatcher(route),
getUrlMatcher(route),
getFunctionMatcher(route)
];
route.query && getQueryStringMatcher(route),
route.method && getMethodMatcher(route),
route.headers && getHeaderMatcher(route),
getUrlMatcher(route)
].filter(matcher => !!matcher);

return (url, options = {}) => {
return matchers.every(matcher => matcher(url, options));
Expand Down
3 changes: 1 addition & 2 deletions src/lib/inspecting.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ FetchMock.calls = function(name, options = {}) {
}

let calls = this.callsFilteredByName(name);

if (options.method) {
const testMethod = options.method.toLowerCase();
calls = calls.filter(([url, opts = {}]) => {
calls = calls.filter(([, opts = {}]) => {
const method = (opts.method || 'get').toLowerCase();
return method === testMethod;
});
Expand Down

0 comments on commit 48ee354

Please sign in to comment.