diff options
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/unit/serve_test.ts | 216 |
1 files changed, 143 insertions, 73 deletions
diff --git a/cli/tests/unit/serve_test.ts b/cli/tests/unit/serve_test.ts index ce7267f58..2bd2314b7 100644 --- a/cli/tests/unit/serve_test.ts +++ b/cli/tests/unit/serve_test.ts @@ -1425,41 +1425,6 @@ Deno.test( }, ); -Deno.test( - { permissions: { net: true, write: true, read: true } }, - async function httpServerCorrectSizeResponse() { - const promise = deferred(); - const listeningPromise = deferred(); - const ac = new AbortController(); - - const tmpFile = await Deno.makeTempFile(); - const file = await Deno.open(tmpFile, { write: true, read: true }); - await file.write(new Uint8Array(70 * 1024).fill(1)); // 70kb sent in 64kb + 6kb chunks - file.close(); - - const server = Deno.serve({ - handler: async (request) => { - const f = await Deno.open(tmpFile, { read: true }); - promise.resolve(); - return new Response(f.readable); - }, - port: 4503, - signal: ac.signal, - onListen: onListen(listeningPromise), - onError: createOnErrorCb(ac), - }); - - await listeningPromise; - const resp = await fetch("http://127.0.0.1:4503/"); - await promise; - const body = await resp.arrayBuffer(); - - assertEquals(body.byteLength, 70 * 1024); - ac.abort(); - await server; - }, -); - // https://github.com/denoland/deno/issues/12741 // https://github.com/denoland/deno/pull/12746 // https://github.com/denoland/deno/pull/12798 @@ -2012,38 +1977,146 @@ Deno.test( }, ); -Deno.test( - { permissions: { net: true, write: true, read: true } }, - async function httpServerSendFile() { - const promise = deferred(); - const ac = new AbortController(); - const listeningPromise = deferred(); - const tmpFile = await Deno.makeTempFile(); - const file = await Deno.open(tmpFile, { write: true, read: true }); - const data = new Uint8Array(70 * 1024).fill(1); - await file.write(data); - file.close(); - const server = Deno.serve({ - handler: async () => { - const f = await Deno.open(tmpFile, { read: true }); - promise.resolve(); - return new Response(f.readable, { status: 200 }); - }, - port: 4503, - signal: ac.signal, - onListen: onListen(listeningPromise), - onError: createOnErrorCb(ac), - }); +function makeTempData(size: number) { + return new Uint8Array(size).fill(1); +} - await listeningPromise; - const response = await fetch(`http://localhost:4503/`); - assertEquals(response.status, 200); - await promise; - assertEquals(new Uint8Array(await response.arrayBuffer()), data); - ac.abort(); - await server; +async function makeTempFile(size: number) { + const tmpFile = await Deno.makeTempFile(); + const file = await Deno.open(tmpFile, { write: true, read: true }); + const data = makeTempData(size); + await file.write(data); + file.close(); + + return await Deno.open(tmpFile, { write: true, read: true }); +} + +const compressionTestCases = [ + { name: "Empty", length: 0, in: {}, out: {}, expect: null }, + { + name: "EmptyAcceptGzip", + length: 0, + in: { "Accept-Encoding": "gzip" }, + out: {}, + expect: null, }, -); + // This technically would be compressible if not for the size, however the size_hint is not implemented + // for FileResource and we don't currently peek ahead on resources. + // { + // name: "EmptyAcceptGzip2", + // length: 0, + // in: { "Accept-Encoding": "gzip" }, + // out: { "Content-Type": "text/plain" }, + // expect: null, + // }, + { name: "Uncompressible", length: 1024, in: {}, out: {}, expect: null }, + { + name: "UncompressibleAcceptGzip", + length: 1024, + in: { "Accept-Encoding": "gzip" }, + out: {}, + expect: null, + }, + { + name: "UncompressibleType", + length: 1024, + in: { "Accept-Encoding": "gzip" }, + out: { "Content-Type": "text/fake" }, + expect: null, + }, + { + name: "CompressibleType", + length: 1024, + in: { "Accept-Encoding": "gzip" }, + out: { "Content-Type": "text/plain" }, + expect: "gzip", + }, + { + name: "CompressibleType2", + length: 1024, + in: { "Accept-Encoding": "gzip, deflate, br" }, + out: { "Content-Type": "text/plain" }, + expect: "gzip", + }, + { + name: "UncompressibleRange", + length: 1024, + in: { "Accept-Encoding": "gzip" }, + out: { "Content-Type": "text/plain", "Content-Range": "1" }, + expect: null, + }, + { + name: "UncompressibleCE", + length: 1024, + in: { "Accept-Encoding": "gzip" }, + out: { "Content-Type": "text/plain", "Content-Encoding": "random" }, + expect: null, + }, + { + name: "UncompressibleCC", + length: 1024, + in: { "Accept-Encoding": "gzip" }, + out: { "Content-Type": "text/plain", "Cache-Control": "no-transform" }, + expect: null, + }, +]; + +for (const testCase of compressionTestCases) { + const name = `httpServerCompression${testCase.name}`; + Deno.test( + { permissions: { net: true, write: true, read: true } }, + { + [name]: async function () { + const promise = deferred(); + const ac = new AbortController(); + const listeningPromise = deferred(); + const server = Deno.serve({ + handler: async (request) => { + const f = await makeTempFile(testCase.length); + promise.resolve(); + const headers = testCase.out as any; + headers["Content-Length"] = testCase.length.toString(); + return new Response(f.readable, { + headers: headers as HeadersInit, + }); + }, + port: 4503, + signal: ac.signal, + onListen: onListen(listeningPromise), + onError: createOnErrorCb(ac), + }); + try { + await listeningPromise; + const resp = await fetch("http://127.0.0.1:4503/", { + headers: testCase.in as HeadersInit, + }); + await promise; + const body = await resp.arrayBuffer(); + if (testCase.expect == null) { + assertEquals(body.byteLength, testCase.length); + assertEquals( + resp.headers.get("content-length"), + testCase.length.toString(), + ); + assertEquals( + resp.headers.get("content-encoding"), + testCase.out["Content-Encoding"] || null, + ); + } else if (testCase.expect == "gzip") { + // Note the fetch will transparently decompress this response, BUT we can detect that a response + // was compressed by the lack of a content length. + assertEquals(body.byteLength, testCase.length); + assertEquals(resp.headers.get("content-encoding"), null); + assertEquals(resp.headers.get("content-length"), null); + } + } finally { + ac.abort(); + await server; + } + }, + }[name], + ); +} Deno.test( { permissions: { net: true, write: true, read: true } }, @@ -2052,15 +2125,12 @@ Deno.test( const ac = new AbortController(); const listeningPromise = deferred(); - const tmpFile = await Deno.makeTempFile(); - const file = await Deno.open(tmpFile, { write: true, read: true }); - const data = new Uint8Array(70 * 1024).fill(1); - await file.write(data); - file.close(); - const server = Deno.serve({ handler: async (request) => { - assertEquals(new Uint8Array(await request.arrayBuffer()), data); + assertEquals( + new Uint8Array(await request.arrayBuffer()), + makeTempData(70 * 1024), + ); promise.resolve(); return new Response("ok"); }, @@ -2071,7 +2141,7 @@ Deno.test( }); await listeningPromise; - const f = await Deno.open(tmpFile, { write: true, read: true }); + const f = await makeTempFile(70 * 1024); const response = await fetch(`http://localhost:4503/`, { method: "POST", body: f.readable, |