nodejs / undici

An HTTP/1.1 client, written from scratch for Node.js
https://nodejs.github.io/undici
MIT License
6.23k stars 541 forks source link

when user creates less intercepts than is needed, throw error which explicitly states that the user has X intercepts, but Y requests were made #2219

Open capaj opened 1 year ago

capaj commented 1 year ago

This would solve...

MockNotMatchedError: Mock dispatch not matched for method 'POST': subsequent request to origin https://api.adapty.io was not allowed (net.connect disabled)

The implementation should look like...


MockNotMatchedError: Mock dispatch not matched for method 'POST': subsequent request to origin https://api.adapty.io was not allowed (net.connect disabled), only 3 intercepts were registered on this mock pool

``

## I have also considered...

updating the docs, but people will mis that
## Additional context

I just thought that intercepts only need to be defiend once and then they catch any incoming request. I did not realize they get discarded after a single request. 
hojjatg commented 1 year ago

Looking at mock-interceptors.js and mock-util.js classes, the default behavior for intercept().reply() is not to persist and also discard the intercept after matching one request. However, it does support "persist" and "times" options:

mockPool.intercept({ path: '/foo' , }).reply(200, 'foo').persist()   //the intercept can get matched unlimited times
mockPool.intercept({ path: '/foo' , }).reply(200, 'foo').times(10)   //the intercept gets discarded after matching 10 requests

Since there could be various different reasons an intercept match is not found (including discarded earlier matches for path, method, headers etc), it's hard to give a useful quantitative error message. Given that there is no documentation for these reply options, I suggest we add documentation for "times" and "persist" and link to them as part of error message. I'll create a PR.

I will create a PR for updating documentation.