sinonjs/sinon

SinonFakeServer.respondWith not working with query params on 17.0.2

1bberto opened this issue ยท 5 comments

Describe the bug
the respondWith seems to not work when the URL has a ?

To Reproduce
Steps to reproduce the behavior:

I have this

const url =  `/api/FakeApi?hash=10101050&fakeData=true&isGet=true`;

const method = 'GET';

const response = [
  status,
  { 'Content-type': 'application/json', ...headers },
  JSON.stringify(body),
];

server.respondWith(method, url, response);

the code is breaking when server.respondWith is being called

the error is

TypeError: Unexpected MODIFIER at 12, expected END
at mustConsume (webpack://my-company/./node_modules/sinon/pkg/sinon-esm.js?:19288:15)
at parse (webpack://sensei/./node_modules/sinon/pkg/sinon-esm.js?:19346:9)
at stringToRegexp (webpack://my-company/./node_modules/sinon/pkg/sinon-esm.js?:19503:27)
at pathToRegexp (webpack://my-company/./node_modules/sinon/pkg/sinon-esm.js?:19581:12)
at Object.respondWith (webpack://my-company/./node_modules/sinon/pkg/sinon-esm.js?:17830:57)

Context (please complete the following information):

  • Sinon version : 17.0.2
  • Runtime: Node v20.11.1, Chrome Headless 124.0.6367.119 (Windows 10)

We've been blocked by this issue, too. I noticed that the issue is caused by sinonjs/nise#216. Rewriting all the usages of respondWith is a pain for us, but reading sinonjs/nise#216, this seems to be an intended breaking change.

Thanks for providing this. This bug is totally on me. I am not sure how I missed James' very detailed explanations in his PR, which spells out how this is a breaking change and actually asks for input (which I also missed, must have been late).

My suggestion:

  • Update the NPM registry to point to 17.0.1 as latest and 17.0.2 as deprecated
  • Add support to Nise for re-writing expressions including query parameters to the new form through a compatibility flag
  • Make a patch version of Sinon 17 use that flag by default to restore current behavior
  • Release Sinon 18 with that flag disabled and note the requirement in the docs, but with the possibility of enabling it manually to avoid rewriting expressions

Thoughts, @43081j or @mroderick ?

An interim "fix" is that I rolled the latest tag to point to 17.0.1. Wiping node_modules should be sufficient.

i think that makes sense. let me know if you want any help sorting the new flag out etc

doesn't sound too difficult. basically default it to legacy behaviour which auto-escapes ? under the hood

The code repro is unfortunately not runnable (suggest using RunKit next time), so I have no 100% assurance this is fixed, but it should be fixed in the changes James shipped to Nise (enabling the legacy routes flag by default) should mean this is gone. Please test Sinon 18 and report back if this is not fixed.