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

When a matcher fails because a header matches the key but not the value, other matchers aren't considered #636

Open
julienw opened this issue Apr 28, 2022 · 0 comments

Comments

@julienw
Copy link

julienw commented Apr 28, 2022

Hi, thanks for this easy to use library.

I'm using fetch-mock in Jest (using fetch-mock-jest). I'm using fetch-mock v9.11.0.

The code I'm using looks like this:

      const endpointUrl = 'https://api.profiler.firefox.com/profile/0123456789';
      fetch.config.overwriteRoutes = true;
      fetch
        .catch(404) // Catchall
        .mock(endpointUrl, 405) // method not allowed for all methods but delete
        .delete(endpointUrl, 406) // Not acceptable for other Accept values
        .delete(
          {
            url: endpointUrl,
            headers: {
              'Content-Type': 'application/json',
              Accept: 'application/vnd.firefox-profiler+json;version=1.0',
            },
          },
          401  // Unauthorized when no Bearer or no valid Bearer
        )
        .delete(
          {
            url: endpointUrl,
            headers: {
              'Content-Type': 'application/json',
              Accept: 'application/vnd.firefox-profiler+json;version=1.0',
              Authorization: `Bearer NOT_A_TOKEN`,
            },
          },
          200
        );

Then the call looks like this:

  const response = await fetch(ENDPOINT, {
    method: 'DELETE',
    headers: {
      Accept: 'application/vnd.firefox-profiler+json;version=1.0',
      'Content-Type': 'application/json',
      Authorization: `Bearer VALID_TOKEN`,
    },
  });

I would have expected that the last matcher doesn't match, but then the previous one would match. But here the catch matcher is used instead.

I believe this is because the last matcher overwrites previous ones.

When I use fetch.config.overwriteRoutes = false instead, then the matcher returning 405 is used.

Finally, if I add name values for all matchers, without setting fetch.config.overwriteRoutes, then the matcher returning 405 is also used.

Any idea how to make this use case work declaratively?

In the mean time I'll use a function to select the right response.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant