Open alxndrsn opened 3 months ago
Hi @alxndrsn thanks for reporting this issue.
Indeed, Express.js by default uses qs
instead of querystring
.
I agree that for better compatibility node-mocks-http
should also use qs
.
Since this project relies a lot on contributors, we'd really appreciate it if you could come up with a fix. Thanks a bunch!
I agree that for better compatibility
node-mocks-http
should also useqs
.
That's not what I'm suggesting - I think doing any parsing of query strings is going to give misleading results for some users in some situations.
Here are the parsers used by the frameworks listed in https://github.com/eugef/node-mocks-http/blob/master/README.md:
library | parser | docs |
---|---|---|
expressjs ("extended"/default) | qs |
https://expressjs.com/en/api.html#app.set |
expressjs ("simple") | querystring |
https://expressjs.com/en/api.html#app.set |
NextJS | URLSearchParams |
https://nextjs.org/docs/app/api-reference/functions/use-search-params |
Koa (default) | querystring |
https://github.com/koajs/koa/blob/master/lib/request.js#L13 |
Safe approaches might be to:
Option 2 would seem the simplest & safest, with obvious inconvenience for anyone using this feature.
Stale issue message
To keep library backwards compatible we will continue using built-in querystring
but add a configuration option to allow using other parsing libs
From the README:
Query string parsing was added in https://github.com/eugef/node-mocks-http/pull/30, but the results are potentially misleading.
node-mocks-http
uses node's built-inquerystring
to parse query strings at https://github.com/eugef/node-mocks-http/blob/dd1fb05fdd36a391600ae0392b690cbd9f40ebee/lib/mockRequest.js#L89ExpressJS's default query string parsing (
'extended'
mode) does not usequerystring
. Seequery parser
at https://expressjs.com/en/api.html#app.set:This may lead to misleading results for tests written for an ExpressJS application with default
query parser
configuration which usenode-mocks-http
's implicit query string parsing (as opposed to thequery
option increateRequest()
).Example
Test code
Output