1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-08 23:28:18 -05:00

fetch: implement bodyUsed (#2877)

This commit is contained in:
Yoshiya Hinosawa 2019-09-08 01:20:30 +09:00 committed by Ryan Dahl
parent 8e3c879d13
commit a205e8a3c2
3 changed files with 31 additions and 3 deletions

View file

@ -35,7 +35,7 @@ function hasHeaderValueOf(s: string, value: string): boolean {
} }
class Body implements domTypes.Body, domTypes.ReadableStream, io.ReadCloser { class Body implements domTypes.Body, domTypes.ReadableStream, io.ReadCloser {
bodyUsed = false; private _bodyUsed = false;
private _bodyPromise: null | Promise<ArrayBuffer> = null; private _bodyPromise: null | Promise<ArrayBuffer> = null;
private _data: ArrayBuffer | null = null; private _data: ArrayBuffer | null = null;
readonly locked: boolean = false; // TODO readonly locked: boolean = false; // TODO
@ -223,6 +223,7 @@ class Body implements domTypes.Body, domTypes.ReadableStream, io.ReadCloser {
} }
read(p: Uint8Array): Promise<number | io.EOF> { read(p: Uint8Array): Promise<number | io.EOF> {
this._bodyUsed = true;
return read(this.rid, p); return read(this.rid, p);
} }
@ -245,6 +246,10 @@ class Body implements domTypes.Body, domTypes.ReadableStream, io.ReadCloser {
[Symbol.asyncIterator](): AsyncIterableIterator<Uint8Array> { [Symbol.asyncIterator](): AsyncIterableIterator<Uint8Array> {
return io.toAsyncIterator(this); return io.toAsyncIterator(this);
} }
get bodyUsed(): boolean {
return this._bodyUsed;
}
} }
export class Response implements domTypes.Response { export class Response implements domTypes.Response {
@ -252,7 +257,6 @@ export class Response implements domTypes.Response {
readonly redirected: boolean; readonly redirected: boolean;
headers: domTypes.Headers; headers: domTypes.Headers;
readonly trailer: Promise<domTypes.Headers>; readonly trailer: Promise<domTypes.Headers>;
bodyUsed = false;
readonly body: Body; readonly body: Body;
constructor( constructor(
@ -302,6 +306,10 @@ export class Response implements domTypes.Response {
return 200 <= this.status && this.status < 300; return 200 <= this.status && this.status < 300;
} }
get bodyUsed(): boolean {
return this.body.bodyUsed;
}
clone(): domTypes.Response { clone(): domTypes.Response {
if (this.bodyUsed) { if (this.bodyUsed) {
throw new TypeError( throw new TypeError(

View file

@ -1,5 +1,11 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, testPerm, assert, assertEquals } from "./test_util.ts"; import {
test,
testPerm,
assert,
assertEquals,
assertThrows
} from "./test_util.ts";
testPerm({ net: true }, async function fetchJsonSuccess(): Promise<void> { testPerm({ net: true }, async function fetchJsonSuccess(): Promise<void> {
const response = await fetch("http://localhost:4545/package.json"); const response = await fetch("http://localhost:4545/package.json");
@ -38,6 +44,19 @@ testPerm({ net: true }, async function fetchBlob(): Promise<void> {
assertEquals(blob.size, Number(headers.get("Content-Length"))); assertEquals(blob.size, Number(headers.get("Content-Length")));
}); });
testPerm({ net: true }, async function fetchBodyUsed(): Promise<void> {
const response = await fetch("http://localhost:4545/package.json");
assertEquals(response.bodyUsed, false);
assertThrows(
(): void => {
// Assigning to read-only property throws in the strict mode.
response.bodyUsed = true;
}
);
await response.blob();
assertEquals(response.bodyUsed, true);
});
testPerm({ net: true }, async function fetchAsyncIterator(): Promise<void> { testPerm({ net: true }, async function fetchAsyncIterator(): Promise<void> {
const response = await fetch("http://localhost:4545/package.json"); const response = await fetch("http://localhost:4545/package.json");
const headers = response.headers; const headers = response.headers;

View file

@ -14,6 +14,7 @@ import {
} from "./deps/https/deno.land/std/testing/asserts.ts"; } from "./deps/https/deno.land/std/testing/asserts.ts";
export { export {
assert, assert,
assertThrows,
assertEquals, assertEquals,
assertMatch, assertMatch,
assertNotEquals, assertNotEquals,