-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decode path components in "req.params"
- Loading branch information
Showing
5 changed files
with
83 additions
and
1 deletion.
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
11 changes: 11 additions & 0 deletions
11
test/rest-api/request/matching/path-params-decode.mocks.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,11 @@ | ||
import { setupWorker, rest } from 'msw' | ||
|
||
const worker = setupWorker( | ||
rest.get('https://test.mswjs.io/reflect-url/:url', (req, res, ctx) => { | ||
const { url } = req.params | ||
|
||
return res(ctx.json({ url })) | ||
}), | ||
) | ||
|
||
worker.start() |
35 changes: 35 additions & 0 deletions
35
test/rest-api/request/matching/path-params-decode.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,35 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import fetch from 'node-fetch' | ||
import { rest } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
|
||
const server = setupServer( | ||
rest.get('https://test.mswjs.io/reflect-url/:url', (req, res, ctx) => { | ||
const { url } = req.params | ||
|
||
return res(ctx.json({ url })) | ||
}), | ||
) | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
test('decodes url componets', async () => { | ||
const url = 'http://example.com:5001/example' | ||
|
||
const res = await fetch( | ||
`https://test.mswjs.io/reflect-url/${encodeURIComponent(url)}`, | ||
) | ||
|
||
expect(res.status).toEqual(200) | ||
expect(await res.json()).toEqual({ | ||
url, | ||
}) | ||
}) |
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,24 @@ | ||
import * as path from 'path' | ||
import { pageWith } from 'page-with' | ||
|
||
function createRuntime() { | ||
return pageWith({ | ||
example: path.resolve(__dirname, 'path-params-decode.mocks.ts'), | ||
}) | ||
} | ||
|
||
test('decodes url componets', async () => { | ||
const runtime = await createRuntime() | ||
|
||
const url = 'http://example.com:5001/example' | ||
|
||
const res = await runtime.request( | ||
`https://test.mswjs.io/reflect-url/${encodeURIComponent(url)}`, | ||
) | ||
|
||
expect(res.status()).toEqual(200) | ||
expect(await res.allHeaders()).toHaveProperty('x-powered-by', 'msw') | ||
expect(await res.json()).toEqual({ | ||
url, | ||
}) | ||
}) |