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
|
import { test, expect, describe } from "bun:test";
import got from "got";
import { Readable } from "stream";
describe("got", () => {
test("should work", async () => {
const server = Bun.serve({
port: 0,
fetch(request, server) {
return new Response("Hello World!");
},
});
const response = await got(`http://${server.hostname}:${server.port}/`);
expect(response.statusCode).toBe(200);
expect(response.body).toBe("Hello World!");
expect(response.headers["content-length"]).toBe("12");
expect(response.url).toBe(`http://${server.hostname}:${server.port}/`);
server.stop();
});
test("json response", async () => {
const server = Bun.serve({
port: 0,
async fetch(request, server) {
expect(request.method).toBe("POST");
const data = await request.json();
expect(data).toEqual({ hello: "world" });
return new Response("Hello world");
},
});
const stream = await got.post(`http://${server.hostname}:${server.port}/`, { json: { hello: "world" } });
expect(stream.body).toBe("Hello world");
server.stop();
});
// test("https plain", async () => {
// const stream = await got("https://bun.sh/", {
// headers: {
// "Accept-Encoding": "identity",
// },
// });
// expect(stream.statusCode).toBe(200);
// });
test("https gzip", async () => {
const stream = await got("https://bun.sh/", {
headers: {
"Accept-Encoding": "gzip",
},
});
expect(stream.statusCode).toBe(200);
});
});
|