From 8f2d8013cb34656a6d3816fc7c1f5eae0c5b5d4d Mon Sep 17 00:00:00 2001 From: Marcos Casagrande Date: Mon, 25 May 2020 21:03:25 +0200 Subject: [PATCH 1/3] Add tests to detect consumed body --- cli/tests/unit/fetch_test.ts | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index e829ddc60ac088..afaecb08b15d02 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -581,3 +581,48 @@ unitTest(function responseRedirect(): void { assertEquals(redir.headers.get("Location"), "example.com/newLocation"); assertEquals(redir.type, "default"); }); + +unitTest({ perms: { net: true } }, async function fetchBodyReadTwice(): Promise< + void +> { + const response = await fetch("http://localhost:4545/cli/tests/fixture.json"); + assertEquals(response.url, "http://localhost:4545/cli/tests/fixture.json"); + + // Read body + const _json = await response.json(); + + // All calls after the body was consumed, should fail + const methods = ["json", "text", "formData", "arrayBuffer"]; + for (const method of methods) { + try { + // @ts-ignore + await response[method](); + fail( + "Reading body multiple times should failed, the stream should've been locked." + ); + } catch {} + } +}); + +unitTest( + { perms: { net: true } }, + async function fetchBodyReaderAfterRead(): Promise { + const response = await fetch( + "http://localhost:4545/cli/tests/fixture.json" + ); + const headers = response.headers; + assert(response.body !== null); + const reader = await response.body.getReader(); + let total = 0; + while (true) { + const { done, value } = await reader.read(); + if (done) break; + assert(value); + } + + try { + response.body.getReader(); + fail("The stream should've been locked."); + } catch {} + } +); From 727d90043298e78e9176ce8aa657bcd7f27a67c1 Mon Sep 17 00:00:00 2001 From: Marcos Casagrande Date: Mon, 25 May 2020 21:23:17 +0200 Subject: [PATCH 2/3] Add fetch response body tests --- cli/tests/unit/fetch_test.ts | 67 ++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index afaecb08b15d02..5ebd1bc02b7890 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -590,6 +590,7 @@ unitTest({ perms: { net: true } }, async function fetchBodyReadTwice(): Promise< // Read body const _json = await response.json(); + assert(_json); // All calls after the body was consumed, should fail const methods = ["json", "text", "formData", "arrayBuffer"]; @@ -610,10 +611,8 @@ unitTest( const response = await fetch( "http://localhost:4545/cli/tests/fixture.json" ); - const headers = response.headers; assert(response.body !== null); const reader = await response.body.getReader(); - let total = 0; while (true) { const { done, value } = await reader.read(); if (done) break; @@ -626,3 +625,67 @@ unitTest( } catch {} } ); + +unitTest( + { perms: { net: true } }, + async function fetchBodyReaderWithCancelAndNewReader(): Promise { + const data = "a".repeat(1 << 10); + const response = await fetch( + "http://localhost:4545/cli/tests/echo_server", + { + method: "POST", + body: data, + } + ); + assert(response.body !== null); + const firstReader = await response.body.getReader(); + + // Acquire reader without reading & release + await firstReader.releaseLock(); + + const reader = await response.body.getReader(); + + let total = 0; + while (true) { + const { done, value } = await reader.read(); + if (done) break; + assert(value); + total += value.length; + } + + assertEquals(total, data.length); + } +); + +unitTest( + { perms: { net: true } }, + async function fetchBodyReaderWithReadCancelAndNewReader(): Promise { + const data = "a".repeat(1 << 10); + + const response = await fetch( + "http://localhost:4545/cli/tests/echo_server", + { + method: "POST", + body: data, + } + ); + assert(response.body !== null); + const firstReader = await response.body.getReader(); + + // Do one single read with first reader + const { value: firstValue } = await firstReader.read(); + assert(firstValue); + await firstReader.releaseLock(); + + // Continue read with second reader + const reader = await response.body.getReader(); + let total = firstValue.length || 0; + while (true) { + const { done, value } = await reader.read(); + if (done) break; + assert(value); + total += value.length; + } + assertEquals(total, data.length); + } +); From 8ca43bdf9aaa8c32dc9b83ef09834a0564c42696 Mon Sep 17 00:00:00 2001 From: Marcos Casagrande Date: Mon, 25 May 2020 21:30:12 +0200 Subject: [PATCH 3/3] Remove assertion --- cli/tests/unit/fetch_test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index 5ebd1bc02b7890..ed17c869a3b13c 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -586,7 +586,6 @@ unitTest({ perms: { net: true } }, async function fetchBodyReadTwice(): Promise< void > { const response = await fetch("http://localhost:4545/cli/tests/fixture.json"); - assertEquals(response.url, "http://localhost:4545/cli/tests/fixture.json"); // Read body const _json = await response.json();