Not able to intercept query string calls against the same domain #2317
-
I'm trying to intercept two API calls from the same domain, but despite setting up the handlers, MSW is giving me a warning that the requests are not being intercepted. Here’s the code I’m using: export const mountS3Provider = ({ callback, method, scope = '' }) => {
const formattedUrl = `https://s3.west.mycomptest.com/*`;
return http[method](formattedUrl, async ({ request }) => {
const url = new URL(request.url);
const matches = url.searchParams.has(scope);
if (matches) {
return callback({ request });
}
// if it doesn't match the query string, it passes through
return passthrough();
});
};
// mounting the S3Provider
const awsAllRegionsApproach = mountS3Provider({
callback: getAwsAllRegionsResponse,
method: 'get',
scope: 'awsAllRegions'
});
const awsCredentialsApproach = mountS3Provider({
callback: getAwsCredentialsResponse,
method: 'get',
scope: 'awsCredentials'
}); The callback functions(Request handlers) for handling the responses look like this: export const getAwsAllRegionsResponse = ({ request }) => {
const url = new URL(request.url);
const isAwsAllRegions = url.searchParams.has('awsAllRegions');
if (isAwsAllRegions) {
return HttpResponse.xml(jsonToXml(regions));
}
};
export const getAwsCredentialsResponse = ({ request }) => {
const url = new URL(request.url);
const hasAwsCredentials = url.searchParams.has('awsCredentials');
if (hasAwsCredentials) {
return HttpResponse.xml(jsonToXml(credentials));
}
}; I'm trying to intercept API calls to:
console.warn
[MSW] Warning: intercepted a request without a matching request handler:
• GET https://s3.west.mycomptest.com/?awsCredentials
If you still wish to intercept this unhandled request, please create a request handler for it.
Read more: https://mswjs.io/docs/getting-started/mocks I'm using server.use(
awsAllRegionsApproach,
awsCredentialsApproach
); I also tried combining both handlers into one, using conditional statements to differentiate between To debug, I added an event listener to track outgoing requests: server.events.on('request:start', ({ request, requestId }) => {
console.log('Outgoing request:', request.method, request.url);
}); The listener shows the request is being triggered: Outgoing request: GET https://s3.west.mycomptest.com/?awsCredentials But for some reason, it’s still not being intercepted by MSW. Does anyone have suggestions for fixing this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi, @erickbelfy. Can you please show the actual request handlers? These: server.use(
+ awsAllRegionsApproach,
+ awsCredentialsApproach
); How do they look like? What URLs do they target? If the |
Beta Was this translation helpful? Give feedback.
If you handle the request in the first handler then yes, of course no other handlers would be able to affect it. You have to do an early
return
in the cases you don't want to be handled.I also think reading this can be helpful: https://mswjs.io/docs/concepts/request-handler#execution-order.