(req: Request)
| 147 | } |
| 148 | |
| 149 | async fetch(req: Request): Promise<Response> { |
| 150 | this.resolveNextRequest?.(req); |
| 151 | this.nextRequest = new Promise((resolve) => { |
| 152 | this.resolveNextRequest = resolve; |
| 153 | }); |
| 154 | |
| 155 | await this.gate; |
| 156 | |
| 157 | if (!this.online) { |
| 158 | throw new Error('Offline.'); |
| 159 | } |
| 160 | |
| 161 | this.requests.push(req); |
| 162 | |
| 163 | if (req.credentials === 'include' || req.mode === 'no-cors') { |
| 164 | return new MockResponse(null, {status: 0, statusText: '', type: 'opaque'}); |
| 165 | } |
| 166 | const url = req.url.split('?')[0]; |
| 167 | if (this.resources.has(url)) { |
| 168 | const response = this.resources.get(url)!.clone(); |
| 169 | if (response.redirected && req.redirect === 'error') { |
| 170 | throw new Error('Redirect disallowed by request policy.'); |
| 171 | } |
| 172 | return response; |
| 173 | } |
| 174 | if (this.errors.has(url)) { |
| 175 | throw new Error('Intentional failure!'); |
| 176 | } |
| 177 | return new MockResponse(null, {status: 404, statusText: 'Not Found'}); |
| 178 | } |
| 179 | |
| 180 | pause(): void { |
| 181 | this.gate = new Promise((resolve) => { |
no test coverage detected