-
Notifications
You must be signed in to change notification settings - Fork 7
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
Question: intercept request and response #14
Comments
Hi @chakatumi, Yes, there are ways to intercept a request, make the original call, and then intercept the response. // 1. define your mock configuration
mocker.mock({
url: '//jsonplaceholder.typicode.com/',
remote: '$url', // $url points to the original URL which is matched by mock
body: async function(remoteResponse, requestInfo) {
console.log('original url:', requestInfo.url);
console.log('response content:', remoteResponse.responseText);
return { data: 'your fake data' };
}
});
// 2. issue a request
axios
.get('https://jsonplaceholder.typicode.com/photos/1?some=x')
.then(res => console.log('photo', res.data)) |
I think I have not been clear enough. I want to intercept a request, do something, then make the original call and capture the response and do something again. In your example, it seems to me that you only intercept the response, but you call also see the original URL. |
I think, the following codes may be what you want. // mock configuration
mocker.mock({
url: '//jsonplaceholder.typicode.com/',
body: async function(requestInfo) {
// 1. intercept a request
const originalUrl = requestInfo.url;
// 2. do something (here, output the original request information)
console.log('original request info: ', requestInfo);
// 3. then make the original call and capture the response
this.disable = 'YES'; // disable this mock to avoid recursion
const res = await axios.get(requestInfo.url);
this.disable = 'NO';
// 4. and do something again (here, output the response)
console.log('response content: ', res.data);
return res.data;
}
});
// issue a request
axios.get('https://jsonplaceholder.typicode.com/photos/1'); If the above codes is still not what you want, please let me know and show me your case. |
A similar discussion was held in #13. @huturen made some modifs to support that use case. However, a pure interceptor is not possible. We forked the project, and we'll propose a modification. In order to support such interception, 4 modifications need to be made. For:
We'll first do the modif for fetch and ask the author if our proposal is OK. If yes, we'll propagate the modifs for the other cases as well. NOTE: the above workaround may be OK for some use cases. But in our case, we need to be compatible w/ all the 4 methods above. Because the code may execute in all 4 modes |
As of version mocker.mock({
url: '//jsonplaceholder.typicode.com/',
body: async function(requestInfo) {
// 1. intercept a request, do something (here, output the original request information)
console.log('original request info: ', requestInfo);
// 2. then make the original call and capture the response
const res = await requestInfo.doOriginalCall();
// 3. and do something again.
console.log('original response:', res);
return { code: 0, msg: 'ok', data: res.responseJson };
}
}); |
I think we can close this issue now. Let me know if you need further assistance. |
Hi,
Is there a way to intercept a request, make the original call and then intercept the response?
I saw that you can intercept the request, then make the original call or intercept the request. But can you do both? And I want to intercept any type of request,
The text was updated successfully, but these errors were encountered: