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

Question: intercept request and response #14

Closed
chakatumi opened this issue Mar 13, 2023 · 6 comments
Closed

Question: intercept request and response #14

chakatumi opened this issue Mar 13, 2023 · 6 comments

Comments

@chakatumi
Copy link

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,

@huturen
Copy link
Owner

huturen commented Mar 14, 2023

Hi @chakatumi,

Yes, there are ways to intercept a request, make the original call, and then intercept the response.
Try the codes below:

// 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))

image

@chakatumi
Copy link
Author

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.

@huturen
Copy link
Owner

huturen commented Mar 14, 2023

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');

image

If the above codes is still not what you want, please let me know and show me your case.

@cristian-spiescu
Copy link

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:

  • fetch
  • XHR
  • wx
  • node

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

@huturen
Copy link
Owner

huturen commented Mar 20, 2023

@chakatumi

As of version 1.8.14, RequestInfo.doOriginalCall can be used to implement the above mentioned features in a more natural way.

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 };
  }
});

image

@huturen
Copy link
Owner

huturen commented Mar 21, 2023

I think we can close this issue now. Let me know if you need further assistance.

@huturen huturen closed this as completed Mar 21, 2023
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

3 participants