Skip to content

Commit

Permalink
Decode path components in "req.params"
Browse files Browse the repository at this point in the history
  • Loading branch information
VanTanev committed Nov 11, 2021
1 parent fe6751f commit e4f957c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/utils/matching/matchRequestUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ test('returns request parameters when matched', () => {
})
})

test('decodes request parameters', () => {
const url = 'http://example.com:5001/example'
const match = matchRequestUrl(
new URL(`https://test.mswjs.io/reflect-url/${encodeURIComponent(url)}`),
'https://test.mswjs.io/reflect-url/:url',
)
expect(match).toHaveProperty('matches', true)
expect(match).toHaveProperty('params', {
url,
})
})

test('returns false when does not match against the request URL', () => {
const match = matchRequestUrl(
new URL('https://test.mswjs.io'),
Expand Down
2 changes: 1 addition & 1 deletion src/utils/matching/matchRequestUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function matchRequestUrl(url: URL, path: Path, baseUrl?: string): Match {
? coercePath(normalizedPath)
: normalizedPath
const cleanUrl = getCleanUrl(url)
const result = match(cleanPath)(cleanUrl)
const result = match(cleanPath, { decode: decodeURIComponent })(cleanUrl)
const params = (result && (result.params as PathParams)) || {}

return {
Expand Down
11 changes: 11 additions & 0 deletions test/rest-api/request/matching/path-params-decode.mocks.ts
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 test/rest-api/request/matching/path-params-decode.node.test.ts
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,
})
})
24 changes: 24 additions & 0 deletions test/rest-api/request/matching/path-params-decode.test.ts
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,
})
})

0 comments on commit e4f957c

Please sign in to comment.