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
Example of mocking response with interceptor #40
Example of mocking response with interceptor #40
Comments
From what I can tell, this isn't possible with the current version of axios. An interceptor needs some way of preempting dispatchRequest. |
Is this to accommodate a test scenario? |
Yes, that and caching On Mon, Feb 2, 2015 at 9:05 PM, Matt Zabriskie notifications@github.com
|
With version 0.6.0 you will be able to specify a custom adapter, which facilitates testing. There will also be built in caching of GET requests. There's also this neat lib that is inspired by pretender that you could use https://github.com/aaronshaf/xhr-interceptor :) |
@mzabriskie How do you specify a custom adapter now? |
@resistdesign functionality didn't make the cut for 0.6.0. Will work on this for a future release. |
Any update on this? I much prefer axios's Promise api than superagent's callback api but I would need a way of mocking to adopt it. Thanks |
You can do this with axios' interceptors by failing each request, but resolving the promise in the response error handler. // Add request interceptor to force the request to fail
axios.interceptors.request.use((config) => {
// Mangle URL to skip network request
config.url = 'http://localhost:0/' + config.url;
return config;
});
instance.interceptors.response.use((response) => {
// Shouldn't really be called, ever
return response;
}, (error) => {
/* Returning a resolved promise from error handler
will make final promise resolve as though the request
would have returned successfully */
return Promise.resolve({
data: {/* response data */},
status: 200,
statusText: 'OK',
headers: {},
config: error.config
});
}); |
For anyone looking to address mocking for a test scenario I have created moxios. |
I would like to intercept a request and then pre-emptively resolve with a response (compare https://github.com/trek/pretender). Is there an example of this being done elsewhere?
The text was updated successfully, but these errors were encountered: