New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
req.params
includes port number, regression 0.35.0
-0.36.0
#1036
Comments
Debugged the request on browser and built minimal repro for unit testing: import { match } from 'path-to-regexp'
test('match should not include port number in params', () => {
const out = match('http\\://localhost:3000/example/:id', {
decode: decodeURIComponent,
})('http://localhost:3000/example/someid')
expect(out).toHaveProperty('params', { id: 'someid' })
})
I got these input arguments logged out from these lines in browser: msw/src/handlers/RestHandler.ts Lines 153 to 159 in b3c34e1
msw/src/utils/matching/matchRequestUrl.ts Line 64 in b3c34e1
Do these argument look alright or is this a bug in |
Hey, @AriPerkkio. Thank you for reporting and investigating this! This is a regression likely related to our internal migration to Here's where we're escaping the port number: msw/src/utils/matching/matchRequestUrl.ts Lines 39 to 43 in 527add2
So a path string like
Which gets matched correctly given to msw/src/utils/matching/matchRequestUrl.test.ts Lines 77 to 90 in 527add2
|
There must be something else at play as I cannot reproduce the issue following the steps you've mentioned. Here's what I got: // src/mocks/handlers.js
import { rest } from 'msw'
export const handlers = [
rest.get('/example/:id', (req, res, ctx) => {
return res(ctx.text(req.params.id))
}),
] The actual request: useEffect(() => {
fetch('/example/abc-123')
}, []) The response: 200 OK
text/plain
abc-123 @AriPerkkio, may you please double-check that you're indeed using the latest $ npm remove msw
$ npm install msw -D If the issue still persists, may I ask you to isolate it to a standalone repo (you can use CRA or any other starter)? Thanks. |
As the current handler is not returning all parameters could you try with following changes: export const handlers = [
rest.get('/example/:id', (req, res, ctx) => {
- return res(ctx.text(req.params.id))
+ return res(ctx.json(req.params))
}),
]
useEffect(() => {
- fetch('/example/abc-123')
+ fetch('/example/abc-123').then(r => r.json()).then(console.log)
}, []) Now the handler should also be returning the port number in the response. I quickly verified the version of MSW with following logging using the same rest.get('/example/:id', (req, res, ctx) => {
console.log('msw/package.json version', require('msw/package.json').version)
// ^^ msw/package.json version 0.36.0
console.log('req.params', req.params)
// ^^ req.params {3001: ':3001', id: 'someid'}
return res(ctx.json(req.params))
}), I'm happy to build a separate minimal repro repository if needed but I think these will be enough - let me know if following changes help. |
Oh, that's a mistake on my side. Thanks for pointing it out 👍 |
I was comparing the bundled Below is comparison between coercePath msw@0.36.3function coercePath(path) {
return (
path
/**
* Replace wildcards ("*") with unnamed capturing groups
* because "path-to-regexp" doesn't support wildcards.
* Ignore path parameter' modifiers (i.e. ":name*").
*/
.replace(
/([:a-zA-Z_-]*)(\*{1,2})+/g,
(_, parameterName, wildcard) => {
const expression = '(.*)'
if (!parameterName) {
return expression
}
return parameterName.startsWith(':')
? `${parameterName}${wildcard}`
: `${parameterName}${expression}`
},
)
/**
* Escape the protocol so that "path-to-regexp" could match
* absolute URL.
* @see https://github.com/pillarjs/path-to-regexp/issues/259
*/
.replace(/^([^\/]+)(:)(?=\/\/)/g, '$1\\$2')
)
}
> coercePath('http://localhost:3000/example/:id')
> 'http\\://localhost:3000/example/:id'
// ^^ Incorrect coercePath msw@main, unreleasedfunction coercePath(path) {
return (
path
/**
* Replace wildcards ("*") with unnamed capturing groups
* because "path-to-regexp" doesn't support wildcards.
* Ignore path parameter' modifiers (i.e. ":name*").
*/
.replace(
/([:a-zA-Z_-]*)(\*{1,2})+/g,
(_, parameterName, wildcard) => {
const expression = '(.*)'
if (!parameterName) {
return expression
}
return parameterName.startsWith(':')
? `${parameterName}${wildcard}`
: `${parameterName}${expression}`
},
)
/**
* Escape the port so that "path-to-regexp" can match
* absolute URLs including port numbers.
*/
.replace(/([^\/])(:)(?=\d+)/, '$1\\$2')
/**
* Escape the protocol so that "path-to-regexp" could match
* absolute URL.
* @see https://github.com/pillarjs/path-to-regexp/issues/259
*/
.replace(/^([^\/]+)(:)(?=\/\/)/, '$1\\$2')
)
}
> coercePath('http://localhost:3000/example/:id')
> 'http\\://localhost\\:3000/example/:id'
// ^^ Correct So it seems |
That's a great find, @AriPerkkio! I'll publish the latest Update: published in 0.36.4. Could you please update to that version and let me know if the issue is gone? Thanks. |
Fix verified with version |
* generate client with new generator * passify tests and types * actually it's npm install in oxide.ts/generator * update omicron to bring in full snakeification * automatically update packer-id * regen with latest generator version * serialize mock api responses as snake case (ugh doesn't work) * upgrade msw for bugfix: port was showing up in request params mswjs/msw#1036 * fix all problems in universe * just use never as empty response body type * use API JSON types, which are cool except they clutter up the generated client * do the same thing with a Json helper instead of generated *JSON types * get Snakify from type-fest. why not * tests for Json type Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Describe the bug
When upgrading
msw@0.35.0
to latest I noticed one of my utility method started to fail. I narrowed this issue tomsw@0.36.0
.Some background. Not necessary for the bug but gives some insight.
I have a utility method which validates that all
req.params
values are encrypted. I have a specific pattern which can be utilized to identify such fields. For now lets call this pattern/somepattern/
.This validation is applied to all my MSW handlers. It is now failing due to
:3000
not matching/somepattern/
.Environment
msw: 0.36.3
nodejs: 16.13.1
npm: 8.3.0
Google Chrome 96
To Reproduce
Steps to reproduce the behavior:
fetch("/example/someid");
.Expected behavior
Below is logs from different MSW versions. Version
0.35.0
works as expected.The text was updated successfully, but these errors were encountered: