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
80import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, tempDirWithFiles } from "harness";
describe("--user-agent flag", () => {
test("custom user agent is sent in HTTP requests", async () => {
const customUserAgent = "MyCustomUserAgent/1.0";
const testScript = `
const server = Bun.serve({
port: 0,
async fetch(request) {
const userAgent = request.headers.get("User-Agent");
if (userAgent === "${customUserAgent}") {
process.exit(0); // SUCCESS
} else {
process.exit(1); // FAIL
}
},
});
// Make request to self
try {
await fetch(\`http://localhost:\${server.port}/test\`);
} catch (error) {
process.exit(1);
}
`;
const dir = tempDirWithFiles("user-agent-test", {
"test.js": testScript,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "--user-agent", customUserAgent, "test.js"],
env: bunEnv,
cwd: dir,
});
const exitCode = await proc.exited;
expect(exitCode).toBe(0);
});
test("default user agent is used when --user-agent is not specified", async () => {
const testScript = `
const server = Bun.serve({
port: 0,
async fetch(request) {
const userAgent = request.headers.get("User-Agent");
// Default Bun user agent should contain "Bun/"
if (userAgent && userAgent.includes("Bun/")) {
process.exit(0); // SUCCESS
} else {
process.exit(1); // FAIL
}
},
});
// Make request to self
try {
await fetch(\`http://localhost:\${server.port}/test\`);
} catch (error) {
process.exit(1);
}
`;
const dir = tempDirWithFiles("user-agent-default-test", {
"test.js": testScript,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "test.js"],
env: bunEnv,
cwd: dir,
});
const exitCode = await proc.exited;
expect(exitCode).toBe(0);
});
});