Skip to content
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

removeInterceptor able to remove specified POST interceptor only #2115

Closed
simondelany opened this issue Nov 18, 2020 · 3 comments
Closed

removeInterceptor able to remove specified POST interceptor only #2115

simondelany opened this issue Nov 18, 2020 · 3 comments
Labels

Comments

@simondelany
Copy link

Context

When you have multiple intercepts with method: 'POST' and the same basePath and _key but different values for _requestBody.

If you call removeInterceptor with a specific interceptor instance.

Then the first interceptor whose _key matches that of the interceptor instance will be removed.

The interceptor that is removed may or may not have the same _requestBody.
This depends on the order of the interceptors.

Possible Improvement

Have a unique ID field on the Interceptor class and using that to compare interceptors within the removeInterceptor method.

or

Include interceptor._requestBody in the comparison of interceptors within the removeInterceptor method.

Alternatives

It's currently possible to mitigate this issue by setting your 'POST' interceptors for a specific endpoint in the order that you will need to remove them in.

e.g. If you have interceptors A, B and C and in your tests you need to be able to remove interceptor B without removing the other interceptors:

A = nock('http://www.example.com').post('/', requestBodyA);
B = nock('http://www.example.com').post('/', requestBodyB);
C = nock('http://www.example.com').post('/', requestBodyC);

trying to remove interceptor B:

nock.removeInterceptor(B);

Will actually remove interceptor A.

By changing the order you create the interceptors

B = nock('http://www.example.com').post('/', requestBodyB);
A = nock('http://www.example.com').post('/', requestBodyA);
C = nock('http://www.example.com').post('/', requestBodyC);

trying to remove interceptor B:

nock.removeInterceptor(B);

Will then remove the correct interceptor.

Has the feature been requested before?

Not that I can see

If the feature request is accepted, would you be willing to submit a PR?

Yes

one possible change in lib/intercept.js:

  200  if (options instanceof Interceptor) {
  201      baseUrl = options.basePath
  202      key = options._key
+ 203      requestBody = options._requestBody
  217   for (let i = 0; i < allInterceptors[baseUrl].interceptors.length; i++) {
  218     const interceptor = allInterceptors[baseUrl].interceptors[i]
+ 219     if (interceptor._key === key && (!requestBody || interceptor._requestBody === requestBody)) {
  220        allInterceptors[baseUrl].interceptors.splice(i, 1)
  221        interceptor.scope.remove(key, interceptor)
  222        break
  223     }
  224   }
@ghost
Copy link

ghost commented Dec 16, 2020

Very useful feature, should be present in library.

@pillsilly
Copy link

pillsilly commented Feb 11, 2021

#2141

I've created above pr which gives user almost full control about removal logics.

Not sure if it'd fullfill your needs. @simondelany

@stale
Copy link

stale bot commented Jul 1, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We try to do our best, but nock is maintained by volunteers and there is only so much we can do at a time. Thank you for your contributions.

@stale stale bot added the stale label Jul 1, 2021
@stale stale bot closed this as completed Jul 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants