-
-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: preserve trailing optional path parameters (#2169)
Co-authored-by: Kai Spencer <51139521+KaiSpencer@users.noreply.github.com>
- Loading branch information
1 parent
540e0ac
commit e69bbd6
Showing
9 changed files
with
94 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
import { cleanUrl } from './cleanUrl' | ||
|
||
test('removes query parameters from a URL string', () => { | ||
it('removes query parameters from a URL string', () => { | ||
expect(cleanUrl('/user?id=123')).toEqual('/user') | ||
expect(cleanUrl('/user?id=123&id=456')).toEqual('/user') | ||
expect(cleanUrl('/user?id=123&role=admin')).toEqual('/user') | ||
}) | ||
|
||
test('removes hashes from a URL string', () => { | ||
it('removes hashes from a URL string', () => { | ||
expect(cleanUrl('/user#hash')).toEqual('/user') | ||
expect(cleanUrl('/user#hash-with-dashes')).toEqual('/user') | ||
}) | ||
|
||
test('removes both query parameters and hashes from a URL string', () => { | ||
it('removes both query parameters and hashes from a URL string', () => { | ||
expect(cleanUrl('/user?id=123#some')).toEqual('/user') | ||
expect(cleanUrl('/user?id=123&role=admin#some')).toEqual('/user') | ||
}) | ||
|
||
it('preserves optional path parameters', () => { | ||
expect(cleanUrl('/user/:id?')).toEqual('/user/:id?') | ||
expect(cleanUrl('/user/:id?/:messageId?')).toEqual('/user/:id?/:messageId?') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/node/rest-api/request/matching/path-params-optional.node.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @vitest-environment node | ||
*/ | ||
import { HttpResponse, http } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
|
||
const server = setupServer() | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
it('intercepts the request that fully matches the path', async () => { | ||
server.use( | ||
http.get('http://localhost/user/:id?', () => | ||
HttpResponse.json({ mocked: true }), | ||
), | ||
) | ||
|
||
const response = await fetch('http://localhost/user/123') | ||
expect(response.status).toBe(200) | ||
expect(await response.json()).toEqual({ mocked: true }) | ||
}) | ||
|
||
it('intercepts the request that partially matches the path', async () => { | ||
server.use( | ||
http.get('http://localhost/user/:id?', () => | ||
HttpResponse.json({ mocked: true }), | ||
), | ||
) | ||
|
||
const response = await fetch('http://localhost/user') | ||
expect(response.status).toBe(200) | ||
expect(await response.json()).toEqual({ mocked: true }) | ||
}) |