Skip to content

Commit

Permalink
test(service-worker): better simulate opaque requests (#30977)
Browse files Browse the repository at this point in the history
Previously, opaque responses where handled a little differently than
other responses from the mock server. More specifically, they were not
tracked (so no assertions could be made for them) and their
[`Body` mixin][1] methods (such as `arrayBuffer()`, `json()`, `text()`)
would throw an error due to `body` being `null`.

This commit ensures opaque responses are also captured on the mock
server and also changes `Body` mixin methods to better simulate the
[spec'd behavior][2].

(These improvements will be necessary to test caching of opaque
responses in a subsequent commit.)

[1]: https://developer.mozilla.org/en-US/docs/Web/API/Body
[2]: https://fetch.spec.whatwg.org/#concept-body-consume-body

PR Close #30977
  • Loading branch information
gkalpak authored and alxhub committed Jun 27, 2019
1 parent 54c171c commit 8b61287
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
42 changes: 15 additions & 27 deletions packages/service-worker/worker/testing/fetch.ts
Expand Up @@ -13,46 +13,34 @@ export class MockBody implements Body {
constructor(public _body: string|null) {}

async arrayBuffer(): Promise<ArrayBuffer> {
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<Blob> { throw 'Not implemented'; }

async json(): Promise<any> {
this.markBodyUsed();
if (this._body !== null) {
return JSON.parse(this._body);
} else {
throw new Error('No body');
}
}
async json(): Promise<any> { return JSON.parse(this.getBody()); }

async text(): Promise<string> {
this.markBodyUsed();
if (this._body !== null) {
return this._body;
} else {
throw new Error('No body');
}
}
async text(): Promise<string> { return this.getBody(); }

async formData(): Promise<FormData> { 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 || '';
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/service-worker/worker/testing/mock.ts
Expand Up @@ -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();
}
Expand Down

0 comments on commit 8b61287

Please sign in to comment.