axios-mock-adapter
axios-mock-adapter copied to clipboard
onGet params doesn't seem to be working
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, {...});
I don't think you need to explicitly add a params key.
Have you tried the following ?
mock
.onGet('/mock', {address: 'Singapore'})
.reply(200, {...});
@fterh Are you still observing this issue? I believe I am running into a similar issue and was wondering if you ever solved yours.
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.
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