summary refs log tree commit diff stats
path: root/test/index.test.ts
blob: 2f682a91c621260445c76850ff640a6619130924 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { type Server } from "bun";
import { expect, test, beforeAll, afterAll } from "bun:test";

import app from "../src/index";

const port = 33000;
const base = `http://localhost:${port}/`;
let server: Server;

beforeAll(async function () {
  server = Bun.serve(Object.assign({}, app, { port }));
});

afterAll(function () {
  server.stop();
});

test("/ returns 200", async function () {
  const res = await fetch(base);
  expect(res.status).toBe(200);
});

test("/asdf returns 404", async function () {
  const res = await fetch(`${base}asdf`);
  expect(res.status).toBe(404);
});

test("/ returns 304 with newer if-modified-since header", async function () {
  const res = await fetch(base, {
    headers: {
      "if-modified-since": new Date().toUTCString(),
    },
  });
  expect(res.status).toBe(304);
  expect(res.headers.get("vary")).toBe("Accept-Encoding");
});

test("/ returns 200 with older if-modified-since header", async function () {
  const res = await fetch(base, {
    headers: {
      "if-modified-since": new Date(0).toUTCString(),
    },
  });
  expect(res.status).toBe(200);
});

test("/ returns gzipped content with accept-encoding: gzip", async function () {
  const res = await fetch(base, {
    headers: {
      "accept-encoding": "gzip",
    },
  });
  expect(res.status).toBe(200);
  // Bun 0.8.1 this doesn't work, but `verbose` shows it's there
  // expect(res.headers.get("content-encoding")).toBe("gzip");
  // response is automatically gunzipped
  const body = await res.text();
  expect(body.length).toBeGreaterThan(
    Number(res.headers.get("content-length")),
  );
});

test("/ returns uncompressed content with accept-encoding: identity", async function () {
  const res = await fetch(base, {
    headers: {
      "accept-encoding": "identity",
    },
  });
  expect(res.status).toBe(200);
  const body = await res.text();
  expect(body.length).toBe(Number(res.headers.get("content-length")));
});

test("/ returns brotli-compressed content with accept-encoding: br", async function () {
  const res = await fetch(base, {
    headers: {
      "accept-encoding": "br",
    },
  });
  expect(res.status).toBe(200);
  expect(res.headers.get("content-encoding")).toBe("br");
  const body = await res.text();
  expect(body.length).toBeLessThan(Number(res.headers.get("content-length")));
});

test("/ returns zstd-compressed content with accept-encoding: zstd", async function () {
  const res = await fetch(base, {
    headers: {
      "accept-encoding": "zstd",
    },
  });
  expect(res.status).toBe(200);
  expect(res.headers.get("content-encoding")).toBe("zstd");
  expect(res.headers.get("vary")).toBe("Accept-Encoding");
  const body = await res.text();
  expect(body.length).toBeLessThan(Number(res.headers.get("content-length")));
});