axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

onGet params doesn't seem to be working

Open fterh opened this issue 7 years ago • 5 comments

This works:

mock
  .onGet('/mock?address=Singapore')
  .reply(200, {...});

But this returns Request failed with status code 404:

mock
  .onGet('/mock', {params: {address: 'Singapore'}})
  .reply(200, {...});

fterh avatar May 23 '18 08:05 fterh

I don't think you need to explicitly add a params key.

Have you tried the following ?

mock
  .onGet('/mock', {address: 'Singapore'})
  .reply(200, {...});

thk2b avatar Jun 06 '18 22:06 thk2b

@fterh Are you still observing this issue? I believe I am running into a similar issue and was wondering if you ever solved yours.

jd-carroll avatar Jul 17 '18 13:07 jd-carroll

I believe this is a duplicate of #116

Can you please verify and close (Note: See this comment)

jd-carroll avatar Jul 17 '18 14:07 jd-carroll

I believe I'm running into this issue and I'm not convinced it's the same as #116. The suggestion on #116 seems to be that it's a v8 bug but the issue I'm running into seems to be that the url matching is not working correctly in this line:

return (isUrlMatching(url, handler[0]) || isUrlMatching(combineUrls(baseURL, url), handler[0])) && isBodyOrParametersMatching(method, body, parameters, handler[1])  && isRequestHeadersMatching(headers, handler[2]);

That line fails before it even has a chance to match parameters, because isUrlMatching doesn't strip out query params.

For example, I think https://foo?bar=baz and https://foo need to be considered as matching. Right now these urls fail isUrlMatching so the line short-circuits and isBodyOrParametersMatching never gets run.

sanbornhilland avatar Dec 05 '18 19:12 sanbornhilland

Ahh, okay I figured out my problem. I think this is likely @fterh's problem as well.

I did not realize axios takes a params option so I was composing the URL params myself. This leads to the issue I described above, where the isUrlMatching fails.

For example...

mock
  .onGet('/mock', {params: {address: 'Singapore'}})
  .reply(200, {...});

// This fails
axios.get('/mock?address=Singapore')

// This works
axios.get('/mock', { params: {address: 'Singapore'}}) 

In my opinion, this is a bug in axios-mock-adapter. Passing params in axios is a convenience so it shouldn't be assumed that that is the way it will be used. Both of the axios.get requests above should work.

https://github.com/axios/axios#example

sanbornhilland avatar Dec 05 '18 20:12 sanbornhilland