Skip to content

Commit 2ebe536

Browse files
authored
fix(patchUrlMappings): fix double slash when rewriting urls with prefix ending in / (#333)
* fix(patchUrlMappings): fix double slash from patchUrlMappings * fix: .proxy wasn't actually involved
1 parent 176129e commit 2ebe536

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/utils/__tests__/matchAndRewriteRoute.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,37 @@ describe('matchAndRewriteURL', () => {
271271
expect(resultURL?.toString()).toEqual(result);
272272
}
273273
});
274+
275+
it('handles prefixes ending with slash without adding extra slashes', () => {
276+
const prefixHost = '123456789012345678.discordsays.com';
277+
const TEST_CASES: Array<MatchAndRewriteURLInputs & {result: string}> = [
278+
{
279+
originalURL: new URL('https://api.example.com/v1/test'),
280+
prefixHost,
281+
prefix: '/api/',
282+
target: 'api.example.com',
283+
result: 'https://123456789012345678.discordsays.com/api/v1/test',
284+
},
285+
{
286+
originalURL: new URL('https://service.example.com/endpoint/'),
287+
prefixHost,
288+
prefix: '/service/',
289+
target: 'service.example.com',
290+
result: 'https://123456789012345678.discordsays.com/service/endpoint/',
291+
},
292+
{
293+
originalURL: new URL('https://cdn.example.com/assets/image.png'),
294+
prefixHost,
295+
prefix: '/cdn/',
296+
target: 'cdn.example.com',
297+
result: 'https://123456789012345678.discordsays.com/cdn/assets/image.png',
298+
},
299+
];
300+
301+
for (const {result, ...rest} of TEST_CASES) {
302+
const resultURL = matchAndRewriteURL(rest);
303+
expect(resultURL?.toString()).toEqual(result);
304+
}
305+
});
274306
});
275307
});

src/utils/url.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export function matchAndRewriteURL({originalURL, prefix, prefixHost, target}: Ma
4646
});
4747

4848
// Append the original path
49-
newURL.pathname += newURL.pathname === '/' ? originalURL.pathname.slice(1) : originalURL.pathname;
49+
const pathToAppend = originalURL.pathname.startsWith('/') ? originalURL.pathname.slice(1) : originalURL.pathname;
50+
newURL.pathname += newURL.pathname.endsWith('/') ? pathToAppend : '/' + pathToAppend;
5051

5152
// Remove the target's path from the new url path
5253
newURL.pathname = newURL.pathname.replace(targetURL.pathname, '');

0 commit comments

Comments
 (0)