diff --git a/packages/service-worker/worker/testing/fetch.ts b/packages/service-worker/worker/testing/fetch.ts index acd1ba91d1..41605a3b20 100644 --- a/packages/service-worker/worker/testing/fetch.ts +++ b/packages/service-worker/worker/testing/fetch.ts @@ -13,46 +13,34 @@ export class MockBody implements Body { constructor(public _body: string|null) {} async arrayBuffer(): Promise { - this.markBodyUsed(); - if (this._body !== null) { - const buffer = new ArrayBuffer(this._body.length); - const access = new Uint8Array(buffer); - for (let i = 0; i < this._body.length; i++) { - access[i] = this._body.charCodeAt(i); - } - return buffer; - } else { - throw new Error('No body'); + const body = this.getBody(); + const buffer = new ArrayBuffer(body.length); + const view = new Uint8Array(buffer); + + for (let i = 0; i < body.length; i++) { + view[i] = body.charCodeAt(i); } + + return buffer; } async blob(): Promise { throw 'Not implemented'; } - async json(): Promise { - this.markBodyUsed(); - if (this._body !== null) { - return JSON.parse(this._body); - } else { - throw new Error('No body'); - } - } + async json(): Promise { return JSON.parse(this.getBody()); } - async text(): Promise { - this.markBodyUsed(); - if (this._body !== null) { - return this._body; - } else { - throw new Error('No body'); - } - } + async text(): Promise { return this.getBody(); } async formData(): Promise { throw 'Not implemented'; } - private markBodyUsed(): void { + private getBody(): string { if (this.bodyUsed === true) { throw new Error('Cannot reuse body without cloning.'); } this.bodyUsed = true; + + // According to the spec, a `null` body results in an empty `ReadableStream` (which for our + // needs is equivalent to `''`). See https://fetch.spec.whatwg.org/#concept-body-consume-body. + return this._body || ''; } } diff --git a/packages/service-worker/worker/testing/mock.ts b/packages/service-worker/worker/testing/mock.ts index 907e375614..66577d475b 100644 --- a/packages/service-worker/worker/testing/mock.ts +++ b/packages/service-worker/worker/testing/mock.ts @@ -120,11 +120,12 @@ export class MockServerState { throw new Error('Offline.'); } - if (req.credentials === 'include') { + this.requests.push(req); + + if ((req.credentials === 'include') || (req.mode === 'no-cors')) { return new MockResponse(null, {status: 0, statusText: '', type: 'opaque'}); } const url = req.url.split('?')[0]; - this.requests.push(req); if (this.resources.has(url)) { return this.resources.get(url) !.clone(); }