aboutsummaryrefslogtreecommitdiff
path: root/test/regression/issue/06443.test.ts
blob: 19d8f200e33f00bb3b039e47320eec9d1fa6afda (plain) (blame)
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
import { describe, test, expect } from "bun:test";
import { serve, file } from "bun";

describe("Bun.serve()", () => {
  const tls = {
    cert: file(new URL("../fixtures/cert.pem", import.meta.url)),
    key: file(new URL("../fixtures/cert.key", import.meta.url)),
  };

  const servers = [
    {
      port: 0,
      url: /^http:\/\/localhost:\d+\/$/,
    },
    {
      tls,
      port: 0,
      url: /^https:\/\/localhost:\d+\/$/,
    },
  ];

  test.each(servers)("%j", async ({ url, ...options }) => {
    const server = serve({
      hostname: "localhost",
      ...options,
      fetch(request) {
        return new Response(request.url);
      },
    });
    try {
      const proto = options.tls ? "https" : "http";
      const target = `${proto}://localhost:${server.port}/`;
      const response = await fetch(target);
      expect(response.text()).resolves.toMatch(url);
    } finally {
      server.stop(true);
    }
  });
});