(request: PlaywrightRequest)
| 68 | |
| 69 | // Listen for requests that match the criteria. |
| 70 | const onRequest = async (request: PlaywrightRequest) => { |
| 71 | if (!(await isMatchingRequest(request))) { |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | // If `capturedRequest` is already set, then we've got multiple requests that match the criteria. |
| 76 | // This is currently not supported, though if needed, we could extend the API |
| 77 | // to allow capturing multiple requests and returning them as an array. |
| 78 | if (capturedRequest) { |
| 79 | const criteriaDescription = |
| 80 | typeof requestMatcher === 'function' |
| 81 | ? 'the specified criteria' |
| 82 | : inspect(requestMatcher) |
| 83 | return responseCtrl.reject( |
| 84 | new Error( |
| 85 | [ |
| 86 | `Captured multiple requests that match ${criteriaDescription} during a \`captureResponse\` call:`, |
| 87 | ...[capturedRequest, request].map( |
| 88 | (req) => ` - ${req.method} ${req.url}` |
| 89 | ), |
| 90 | 'This is currently not supported.', |
| 91 | ].join('\n') |
| 92 | ) |
| 93 | ) |
| 94 | } |
| 95 | |
| 96 | // We found a request that matches our criteria. Now we'll wait for a response. |
| 97 | capturedRequest = request |
| 98 | console.log( |
| 99 | `[request-tracker] request: ${request.method()} ${request.url()}` + |
| 100 | (['POST', 'PUT', 'PATCH'].includes(request.method()) |
| 101 | ? ` (content-type: ${request.headers()['content-type']})` |
| 102 | : '') |
| 103 | ) |
| 104 | const onResponse = (response: PlaywrightResponse) => { |
| 105 | if (isSettled) { |
| 106 | return |
| 107 | } |
| 108 | if (response.request() === request) { |
| 109 | // We found a response to our request. We're done. |
| 110 | console.log(`[request-tracker] response: ${response.status()}`) |
| 111 | return responseCtrl.resolve(response) |
| 112 | } |
| 113 | } |
| 114 | browser.on('response', onResponse) |
| 115 | cleanups.push(() => browser.off('response', onResponse)) |
| 116 | } |
| 117 | |
| 118 | // Install the handler before running the action callback to avoid races. |
| 119 | browser.on('request', onRequest) |
no test coverage detected