Skip to content

Commit 97c44d0

Browse files
fixed a bug where a single slash between two components was removed from paths
1 parent b1ec796 commit 97c44d0

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/parsers/paw/Parser.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,25 @@ methods.isPartOfBaseUrl = (defaultUrl, defaultSecureUrl, urlPart) => {
421421
return defaultUrl.indexOf(urlPart) >= 0 || defaultSecureUrl.indexOf(urlPart) >= 0
422422
}
423423

424-
// NOTE: we assume that the urlPart is after the protocol
425-
methods.findIntersection = (defaultUrl, urlPart) => {
426-
const match = (defaultUrl + '####' + urlPart).match(/^.*?(.*)####\1(.*)$/)
424+
/**
425+
* finds the intersection between two strings, and returns the intersection, as well as the right-
426+
* most exclusion. This is used to find the overlap between a host url and a part of a url
427+
* associated with that host.
428+
* @param {string} defaultUrl: the default url to test against.
429+
* @param {string} defaultSecureUrl: the default secure url to test against.
430+
* @param {string} urlPart: the part of url to test
431+
* @returns {{ inside: string, outside: string }}
432+
*
433+
* Note: this assumes Paw only supports http and https.
434+
* Note: this may work incorrectly if url is as follow: http://example.com/example.com/ (not tested)
435+
*/
436+
methods.findIntersection = (defaultUrl, defaultSecureUrl, urlPart) => {
437+
let baseUrl = defaultUrl
438+
if (urlPart.match(/^[^:]*s:\/\//)) {
439+
baseUrl = defaultSecureUrl
440+
}
427441

442+
const match = (baseUrl + '####' + urlPart).match(/^.*?(.*)####\1(.*)$/)
428443
// always matches
429444
return { inside: match[1], outside: match[2] }
430445
}
@@ -452,15 +467,18 @@ methods.addComponentToBaseOrPath = (
452467
{ baseComponents, pathComponents },
453468
{ key: urlPart, value: component }
454469
) => {
455-
if (methods.isPartOfBaseUrl(defaultUrl, defaultSecureUrl, urlPart)) {
470+
if (
471+
pathComponents.length === 0 &&
472+
methods.isPartOfBaseUrl(defaultUrl, defaultSecureUrl, urlPart)
473+
) {
456474
// component is member of base url
457475
baseComponents.push({ key: urlPart, value: component })
458476
return { baseComponents, pathComponents }
459477
}
460478

461479
if (pathComponents.length === 0) {
462480
// component may be split between base url and path
463-
const { inside, outside } = methods.findIntersection(defaultUrl, urlPart)
481+
const { inside, outside } = methods.findIntersection(defaultUrl, defaultSecureUrl, urlPart)
464482
baseComponents.push({ key: inside, value: inside })
465483
pathComponents.push({ key: outside, value: outside })
466484
}

src/parsers/paw/__tests__/Parser.spec.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,28 @@ describe('parsers/paw/Parser.js', () => {
595595
describe('@findIntersection', () => {
596596
it('should work', () => {
597597
const defaultUrl = 'http://echo.paw.cloud/pets'
598-
const inputs = [ '/pets', '/pets/234', '/234' ]
598+
const defaultSecureUrl = 'https://echo.paw.cloud/pets'
599+
const inputs = [
600+
'/pets',
601+
'/pets/234',
602+
'/234',
603+
// this one should be outside, because our intersection method tries to place strings so
604+
// that they overlap from the right
605+
'http://echo.paw.cloud',
606+
'http://echo.paw.cloud/pets',
607+
'https://echo.paw.cloud/pets'
608+
]
599609
const expected = [
600610
{ inside: '/pets', outside: '' },
601611
{ inside: '/pets', outside: '/234' },
602-
{ inside: '', outside: '/234' }
612+
{ inside: '', outside: '/234' },
613+
{ inside: '', outside: 'http://echo.paw.cloud' },
614+
{ inside: 'http://echo.paw.cloud/pets', outside: '' },
615+
{ inside: 'https://echo.paw.cloud/pets', outside: '' }
603616
]
604-
const actual = inputs.map(input => __internals__.findIntersection(defaultUrl, input))
617+
const actual = inputs.map(
618+
input => __internals__.findIntersection(defaultUrl, defaultSecureUrl, input)
619+
)
605620
expect(actual).toEqual(expected)
606621
})
607622
})

0 commit comments

Comments
 (0)