Repository navigation
Expand file tree
/
Copy pathhttpapi-compression.test.ts
More file actions
151 lines (138 loc) · 6.01 KB
/
Copy pathhttpapi-compression.test.ts
File metadata and controls
151 lines (138 loc) · 6.01 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { afterEach, describe, expect, test } from "bun:test"
import { gunzipSync, inflateSync } from "node:zlib"
import { Server } from "../../src/server/server"
import { resetDatabase } from "../fixture/db"
import { disposeAllInstances, tmpdir } from "../fixture/fixture"
afterEach(async () => {
await disposeAllInstances()
await resetDatabase()
})
function app() {
return Server.Default().app
}
// /config echoes the config back. Padding the config pushes the response body
// well past the 1024 B threshold so we can observe compression behavior.
function fatConfig() {
const instructions: string[] = []
for (let i = 0; i < 50; i++) {
instructions.push(`padding-instruction-${i}-${"x".repeat(40)}`)
}
return {
formatter: false,
lsp: false,
username: "compression-test-user",
instructions,
}
}
describe("HttpApi compression", () => {
describe("encodes responses", () => {
test("gzips JSON when Accept-Encoding includes gzip and body exceeds threshold", async () => {
await using tmp = await tmpdir({ config: fatConfig() })
const response = await app().request("/config", {
headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip" },
})
expect(response.status).toBe(200)
expect(response.headers.get("content-encoding")).toBe("gzip")
const compressed = new Uint8Array(await response.arrayBuffer())
const decompressed = gunzipSync(compressed)
const json = JSON.parse(new TextDecoder().decode(decompressed))
expect(json).toMatchObject({ username: "compression-test-user" })
expect(compressed.byteLength).toBeLessThan(decompressed.byteLength)
})
test("uses deflate when only deflate is acceptable", async () => {
await using tmp = await tmpdir({ config: fatConfig() })
const response = await app().request("/config", {
headers: { "x-opencode-directory": tmp.path, "accept-encoding": "deflate" },
})
expect(response.status).toBe(200)
expect(response.headers.get("content-encoding")).toBe("deflate")
const compressed = new Uint8Array(await response.arrayBuffer())
const decompressed = inflateSync(compressed)
const json = JSON.parse(new TextDecoder().decode(decompressed))
expect(json).toMatchObject({ username: "compression-test-user" })
})
test("prefers gzip when both gzip and deflate are acceptable", async () => {
await using tmp = await tmpdir({ config: fatConfig() })
const response = await app().request("/config", {
headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip, deflate" },
})
expect(response.headers.get("content-encoding")).toBe("gzip")
})
test("does not include the original Content-Length when compressed", async () => {
await using tmp = await tmpdir({ config: fatConfig() })
const response = await app().request("/config", {
headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip" },
})
const compressed = new Uint8Array(await response.arrayBuffer())
const declared = response.headers.get("content-length")
// Either absent (transfer-encoding chunked) or matches the compressed length.
if (declared !== null) expect(Number(declared)).toBe(compressed.byteLength)
})
})
describe("skips", () => {
test("when no Accept-Encoding header is present", async () => {
await using tmp = await tmpdir({ config: fatConfig() })
const response = await app().request("/config", {
headers: { "x-opencode-directory": tmp.path },
})
expect(response.headers.get("content-encoding")).toBeNull()
})
test("when Accept-Encoding only allows unsupported encodings", async () => {
await using tmp = await tmpdir({ config: fatConfig() })
const response = await app().request("/config", {
headers: { "x-opencode-directory": tmp.path, "accept-encoding": "br" },
})
expect(response.headers.get("content-encoding")).toBeNull()
})
test("when the response body is below the 1024-byte threshold", async () => {
// A bare config produces a tiny response (~few hundred bytes).
await using tmp = await tmpdir({ config: { formatter: false, lsp: false } })
const response = await app().request("/config", {
headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip" },
})
expect(response.status).toBe(200)
const body = new Uint8Array(await response.arrayBuffer())
expect(body.byteLength).toBeLessThan(1024)
expect(response.headers.get("content-encoding")).toBeNull()
})
test("HEAD requests", async () => {
await using tmp = await tmpdir({ config: fatConfig() })
const response = await app().request("/config", {
method: "HEAD",
headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip" },
})
expect(response.headers.get("content-encoding")).toBeNull()
})
})
describe("streaming exclusions", () => {
test("/event SSE is not compressed", async () => {
await using tmp = await tmpdir({ config: { formatter: false, lsp: false } })
const controller = new AbortController()
const response = await app().request("/event", {
headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip" },
signal: controller.signal,
})
try {
expect(response.status).toBe(200)
expect(response.headers.get("content-encoding")).toBeNull()
} finally {
controller.abort()
await response.body?.cancel().catch(() => {})
}
})
test("/global/event SSE is not compressed", async () => {
const controller = new AbortController()
const response = await app().request("/global/event", {
headers: { "accept-encoding": "gzip" },
signal: controller.signal,
})
try {
expect(response.status).toBe(200)
expect(response.headers.get("content-encoding")).toBeNull()
} finally {
controller.abort()
await response.body?.cancel().catch(() => {})
}
})
})
})