summaryrefslogtreecommitdiff
path: root/cli/tests
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tests')
-rw-r--r--cli/tests/unit/http_test.ts161
1 files changed, 134 insertions, 27 deletions
diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts
index f48f314db..5fabd40fe 100644
--- a/cli/tests/unit/http_test.ts
+++ b/cli/tests/unit/http_test.ts
@@ -1224,26 +1224,25 @@ Deno.test(
const decoder = new TextDecoder();
Deno.test({
- name: "http server compresses body",
+ name: "http server compresses body - check headers",
permissions: { net: true, run: true },
async fn() {
const hostname = "localhost";
const port = 4501;
+ const listener = Deno.listen({ hostname, port });
+
+ const data = { hello: "deno", now: "with", compressed: "body" };
async function server() {
- const listener = Deno.listen({ hostname, port });
const tcpConn = await listener.accept();
const httpConn = Deno.serveHttp(tcpConn);
const e = await httpConn.nextRequest();
assert(e);
const { request, respondWith } = e;
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
- const response = new Response(
- JSON.stringify({ hello: "deno", now: "with", compressed: "body" }),
- {
- headers: { "content-type": "application/json" },
- },
- );
+ const response = new Response(JSON.stringify(data), {
+ headers: { "content-type": "application/json" },
+ });
await respondWith(response);
httpConn.close();
listener.close();
@@ -1275,6 +1274,60 @@ Deno.test({
});
Deno.test({
+ name: "http server compresses body - check body",
+ permissions: { net: true, run: true },
+ async fn() {
+ const hostname = "localhost";
+ const port = 4501;
+ const listener = Deno.listen({ hostname, port });
+
+ const data = { hello: "deno", now: "with", compressed: "body" };
+
+ async function server() {
+ const tcpConn = await listener.accept();
+ const httpConn = Deno.serveHttp(tcpConn);
+ const e = await httpConn.nextRequest();
+ assert(e);
+ const { request, respondWith } = e;
+ assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
+ const response = new Response(JSON.stringify(data), {
+ headers: { "content-type": "application/json" },
+ });
+ await respondWith(response);
+ httpConn.close();
+ listener.close();
+ }
+
+ async function client() {
+ const url = `http://${hostname}:${port}/`;
+ const cmd = [
+ "curl",
+ "--request",
+ "GET",
+ "--url",
+ url,
+ "--header",
+ "Accept-Encoding: gzip, deflate, br",
+ ];
+ const proc = Deno.run({ cmd, stdout: "piped", stderr: "null" });
+ const status = await proc.status();
+ assert(status.success);
+ const stdout = proc.stdout!.readable
+ .pipeThrough(new DecompressionStream("gzip"))
+ .pipeThrough(new TextDecoderStream());
+ let body = "";
+ for await (const chunk of stdout) {
+ body += chunk;
+ }
+ assertEquals(JSON.parse(body), data);
+ proc.close();
+ }
+
+ await Promise.all([server(), client()]);
+ },
+});
+
+Deno.test({
name: "http server doesn't compress small body",
permissions: { net: true, run: true },
async fn() {
@@ -1653,15 +1706,18 @@ Deno.test({
});
Deno.test({
- name: "http server doesn't compress streamed bodies",
+ name: "http server compresses streamed bodies - check headers",
permissions: { net: true, run: true },
async fn() {
const hostname = "localhost";
const port = 4501;
+ const encoder = new TextEncoder();
+ const listener = Deno.listen({ hostname, port });
+
+ const data = { hello: "deno", now: "with", compressed: "body" };
+
async function server() {
- const encoder = new TextEncoder();
- const listener = Deno.listen({ hostname, port });
const tcpConn = await listener.accept();
const httpConn = Deno.serveHttp(tcpConn);
const e = await httpConn.nextRequest();
@@ -1670,23 +1726,13 @@ Deno.test({
assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
const bodyInit = new ReadableStream({
start(controller) {
- controller.enqueue(
- encoder.encode(
- JSON.stringify({
- hello: "deno",
- now: "with",
- compressed: "body",
- }),
- ),
- );
+ controller.enqueue(encoder.encode(JSON.stringify(data)));
controller.close();
},
});
const response = new Response(
bodyInit,
- {
- headers: { "content-type": "application/json", vary: "Accept" },
- },
+ { headers: { "content-type": "application/json" } },
);
await respondWith(response);
httpConn.close();
@@ -1709,8 +1755,71 @@ Deno.test({
const status = await proc.status();
assert(status.success);
const output = decoder.decode(await proc.output());
- assert(output.includes("vary: Accept\r\n"));
- assert(!output.includes("content-encoding: "));
+ assert(output.includes("vary: Accept-Encoding\r\n"));
+ assert(output.includes("content-encoding: gzip\r\n"));
+ proc.close();
+ }
+
+ await Promise.all([server(), client()]);
+ },
+});
+
+Deno.test({
+ name: "http server compresses streamed bodies - check body",
+ permissions: { net: true, run: true },
+ async fn() {
+ const hostname = "localhost";
+ const port = 4501;
+
+ const encoder = new TextEncoder();
+ const listener = Deno.listen({ hostname, port });
+
+ const data = { hello: "deno", now: "with", compressed: "body" };
+
+ async function server() {
+ const tcpConn = await listener.accept();
+ const httpConn = Deno.serveHttp(tcpConn);
+ const e = await httpConn.nextRequest();
+ assert(e);
+ const { request, respondWith } = e;
+ assertEquals(request.headers.get("Accept-Encoding"), "gzip, deflate, br");
+ const bodyInit = new ReadableStream({
+ start(controller) {
+ controller.enqueue(encoder.encode(JSON.stringify(data)));
+ controller.close();
+ },
+ });
+ const response = new Response(
+ bodyInit,
+ { headers: { "content-type": "application/json" } },
+ );
+ await respondWith(response);
+ httpConn.close();
+ listener.close();
+ }
+
+ async function client() {
+ const url = `http://${hostname}:${port}/`;
+ const cmd = [
+ "curl",
+ "--request",
+ "GET",
+ "--url",
+ url,
+ "--header",
+ "Accept-Encoding: gzip, deflate, br",
+ ];
+ const proc = Deno.run({ cmd, stdout: "piped", stderr: "null" });
+ const status = await proc.status();
+ assert(status.success);
+ const stdout = proc.stdout.readable
+ .pipeThrough(new DecompressionStream("gzip"))
+ .pipeThrough(new TextDecoderStream());
+ let body = "";
+ for await (const chunk of stdout) {
+ body += chunk;
+ }
+ assertEquals(JSON.parse(body), data);
proc.close();
}
@@ -1775,8 +1884,6 @@ Deno.test({
// Ensure the content-length header is updated.
assert(!output.includes(`content-length: ${contentLength}\r\n`));
assert(output.includes("content-length: 72\r\n"));
- console.log(output);
-
proc.close();
}