diff options
Diffstat (limited to 'cli/tests/unit/fetch_test.ts')
-rw-r--r-- | cli/tests/unit/fetch_test.ts | 74 |
1 files changed, 57 insertions, 17 deletions
diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index 05c04f4ef..d12a93867 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -3,6 +3,7 @@ import { assert, assertEquals, assertRejects, + assertThrows, delay, fail, unimplemented, @@ -523,7 +524,7 @@ Deno.test( ); Deno.test({ permissions: { net: true } }, async function fetchInitBlobBody() { - const data = "const a = 1"; + const data = "const a = 1 🦕"; const blob = new Blob([data], { type: "text/javascript", }); @@ -555,7 +556,32 @@ Deno.test( async function fetchInitFormDataBlobFilenameBody() { const form = new FormData(); form.append("field", "value"); - form.append("file", new Blob([new TextEncoder().encode("deno")])); + form.append( + "file", + new Blob([new TextEncoder().encode("deno")]), + "file name", + ); + const response = await fetch("http://localhost:4545/echo_server", { + method: "POST", + body: form, + }); + const resultForm = await response.formData(); + assertEquals(form.get("field"), resultForm.get("field")); + const file = resultForm.get("file"); + assert(file instanceof File); + assertEquals(file.name, "file name"); + }, +); + +Deno.test( + { permissions: { net: true } }, + async function fetchInitFormDataFileFilenameBody() { + const form = new FormData(); + form.append("field", "value"); + form.append( + "file", + new File([new Blob([new TextEncoder().encode("deno")])], "file name"), + ); const response = await fetch("http://localhost:4545/echo_server", { method: "POST", body: form, @@ -564,7 +590,7 @@ Deno.test( assertEquals(form.get("field"), resultForm.get("field")); const file = resultForm.get("file"); assert(file instanceof File); - assertEquals(file.name, "blob"); + assertEquals(file.name, "file name"); }, ); @@ -1193,10 +1219,8 @@ Deno.test( "accept-encoding: gzip, br\r\n", `host: ${addr}\r\n`, `transfer-encoding: chunked\r\n\r\n`, - "6\r\n", - "hello \r\n", - "5\r\n", - "world\r\n", + "B\r\n", + "hello world\r\n", "0\r\n\r\n", ].join(""); assertEquals(actual, expected); @@ -1259,13 +1283,19 @@ Deno.test( Deno.test( { permissions: { net: true } }, async function fetchNoServerReadableStreamBody() { - const { promise, resolve } = Promise.withResolvers<void>(); + const completed = Promise.withResolvers<void>(); + const failed = Promise.withResolvers<void>(); const body = new ReadableStream({ start(controller) { controller.enqueue(new Uint8Array([1])); - setTimeout(() => { - controller.enqueue(new Uint8Array([2])); - resolve(); + setTimeout(async () => { + // This is technically a race. If the fetch has failed by this point, the enqueue will + // throw. If not, it will succeed. Windows appears to take a while to time out the fetch, + // so we will just wait for that here before we attempt to enqueue so it's consistent + // across platforms. + await failed.promise; + assertThrows(() => controller.enqueue(new Uint8Array([2]))); + completed.resolve(); }, 1000); }, }); @@ -1273,7 +1303,8 @@ Deno.test( await assertRejects(async () => { await fetch(nonExistentHostname, { body, method: "POST" }); }, TypeError); - await promise; + failed.resolve(); + await completed.promise; }, ); @@ -1853,8 +1884,9 @@ Deno.test( async function fetchBlobUrl(): Promise<void> { const blob = new Blob(["ok"], { type: "text/plain" }); const url = URL.createObjectURL(blob); + assert(url.startsWith("blob:"), `URL was ${url}`); const res = await fetch(url); - assert(res.url.startsWith("blob:http://js-unit-tests/")); + assertEquals(res.url, url); assertEquals(res.status, 200); assertEquals(res.headers.get("content-length"), "2"); assertEquals(res.headers.get("content-type"), "text/plain"); @@ -1941,9 +1973,12 @@ Deno.test( }) ); - assert(err instanceof TypeError); - assert(err.cause); - assert(err.cause instanceof Error); + assert(err instanceof TypeError, `err was not a TypeError ${err}`); + assert(err.cause, `err.cause was null ${err}`); + assert( + err.cause instanceof Error, + `err.cause was not an Error ${err.cause}`, + ); assertEquals(err.cause.message, "foo"); await server; @@ -1968,7 +2003,12 @@ Deno.test( method: "POST", signal: controller.signal, }); - controller.abort(); + try { + controller.abort(); + } catch (e) { + console.log(e); + fail("abort should not throw"); + } await promise; }, DOMException, |