Skip to content

Commit

Permalink
fix(core): Handle trailing slashes when generating route names (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored Nov 26, 2018
1 parent 79e04b8 commit 19147f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
17 changes: 8 additions & 9 deletions packages/@pollyjs/core/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import RouteRecognizer from 'route-recognizer';
import castArray from 'lodash-es/castArray';
import { URL, assert, timeout, buildUrl } from '@pollyjs/utils';

import removeHostFromUrl from '../utils/remove-host-from-url';

import Route from './route';
import Handler from './handler';
import Middleware from './middleware';
Expand All @@ -25,15 +23,15 @@ const METHODS = ['GET', 'PUT', 'POST', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
const { keys } = Object;

function parseUrl(url) {
const path = new URL(url);
const parsedUrl = new URL(url);
/*
Use the full origin (http://hostname:port) if the host exists. If there
is no host, URL.origin returns "null" (null as a string) so set host to '/'
*/
const host = path.host ? path.origin : CHARS.SLASH;
const href = removeHostFromUrl(path).href;
const host = parsedUrl.host ? parsedUrl.origin : CHARS.SLASH;
const path = parsedUrl.pathname || CHARS.SLASH;

return { host, path: href };
return { host, path };
}

export default class Server {
Expand Down Expand Up @@ -178,7 +176,7 @@ export default class Server {
* @returns {String}
*/
_nameForPath(path = '') {
return path
const name = path
.split(CHARS.SLASH)
.map(segment => {
switch (segment.charAt(0)) {
Expand All @@ -195,11 +193,12 @@ export default class Server {
}
})
.join(CHARS.SLASH);

// Remove trailing slash, if we result with an empty string, return a slash
return name.replace(/\/$/, '') || CHARS.SLASH;
}

_registryForHost(host) {
host = host || CHARS.SLASH;

if (!this[REGISTRY][host]) {
this[REGISTRY][host] = METHODS.reduce((acc, method) => {
acc[method] = new RouteRecognizer();
Expand Down
22 changes: 22 additions & 0 deletions packages/@pollyjs/core/tests/unit/server/server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ describe('Unit | Server', function() {
});
});

it('should concat handlers for same paths with a trailing slash', async function() {
addHandlers('/ping');
expect(server.lookup('GET', '/ping').handlers).to.have.lengthOf(3);

addHandlers('/ping/');
expect(server.lookup('GET', '/ping').handlers).to.have.lengthOf(6);
expect(server.lookup('GET', '/ping/').handlers).to.have.lengthOf(6);

addHandlers('http://ping.com');
expect(server.lookup('GET', 'http://ping.com').handlers).to.have.lengthOf(
3
);

addHandlers('http://ping.com/');
expect(server.lookup('GET', 'http://ping.com').handlers).to.have.lengthOf(
6
);
expect(
server.lookup('GET', 'http://ping.com/').handlers
).to.have.lengthOf(6);
});

it('should concat handlers for same paths with different dynamic segment names', async function() {
addHandlers('/ping/:id');
expect(server.lookup('GET', '/ping/:id').handlers).to.have.lengthOf(3);
Expand Down

0 comments on commit 19147f7

Please sign in to comment.