Skip to content

Commit 3920b7e

Browse files
committed
test(cli): cover managed service credentials with proxy bypass
1 parent f922ed0 commit 3920b7e

1 file changed

Lines changed: 37 additions & 21 deletions

File tree

‎packages/cli/test/server-connection.test.ts‎

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -212,26 +212,32 @@ test("local config commands do not start or require a server", async () => {
212212
}
213213
})
214214

215-
test("CLI loopback requests bypass environment proxies", async () => {
215+
test.each(["session", "plugin"])("managed %s list bypasses environment proxies", async (command) => {
216216
const root = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-loopback-proxy-"))
217-
const proxied: string[] = []
217+
const proxied: Array<{ url: string; authorization: string | null }> = []
218+
const received: Array<{ path: string; authorization: string | null }> = []
218219
const server = Bun.serve({
219220
hostname: "127.0.0.1",
220221
port: 0,
221-
fetch() {
222-
return Response.json({ version: OPENCODE_VERSION, pid: process.pid, urls: [] })
222+
fetch(request) {
223+
const pathname = new URL(request.url).pathname
224+
received.push({ path: pathname, authorization: request.headers.get("authorization") })
225+
if (pathname === "/api/info") return Response.json({ version: OPENCODE_VERSION, pid: process.pid, urls: [] })
226+
if (pathname === "/api/location") return Response.json({ project: { id: "loopback-proxy-test" } })
227+
if (pathname === "/api/session" || pathname === "/api/plugin") return Response.json({ data: [] })
228+
return new Response(null, { status: 404 })
223229
},
224230
})
225231
const proxy = Bun.serve({
226232
hostname: "127.0.0.1",
227233
port: 0,
228234
fetch(request) {
229-
proxied.push(request.url)
230-
return new Response(null, { status: 502 })
235+
proxied.push({ url: request.url, authorization: request.headers.get("authorization") })
236+
return new Response(null, { status: 404 })
231237
},
232238
})
233239
const env = isolatedEnv(root, {
234-
OPENCODE_PASSWORD: "fixture-password",
240+
OPENCODE_PASSWORD: undefined,
235241
OPENCODE_SERVER_PASSWORD: undefined,
236242
HTTP_PROXY: proxy.url.href,
237243
http_proxy: proxy.url.href,
@@ -243,27 +249,37 @@ test("CLI loopback requests bypass environment proxies", async () => {
243249
no_proxy: "",
244250
})
245251
try {
246-
const child = Bun.spawn(
247-
[
248-
process.execPath,
249-
"run",
250-
path.join(import.meta.dir, "../src/index.ts"),
251-
"api",
252-
"get",
253-
"/api/info",
254-
"--server",
255-
server.url.href,
256-
],
257-
{ cwd: path.join(import.meta.dir, ".."), env, stdout: "pipe", stderr: "pipe" },
252+
const registration = path.join(root, "state", "opencode", ServiceConfig.filename())
253+
await fs.mkdir(path.dirname(registration), { recursive: true })
254+
await fs.writeFile(
255+
registration,
256+
JSON.stringify({
257+
id: "loopback-proxy-test",
258+
version: OPENCODE_VERSION,
259+
url: server.url.href,
260+
pid: process.pid,
261+
password: "fixture-password",
262+
}),
258263
)
264+
const child = Bun.spawn([process.execPath, "run", path.join(import.meta.dir, "../src/index.ts"), command, "list"], {
265+
cwd: path.join(import.meta.dir, ".."),
266+
env,
267+
stdout: "pipe",
268+
stderr: "pipe",
269+
})
259270
const [stdout, stderr, code] = await Promise.all([
260271
new Response(child.stdout).text(),
261272
new Response(child.stderr).text(),
262273
child.exited,
263274
])
264-
expect({ stderr, code }).toEqual({ stderr: "", code: 0 })
265-
expect(JSON.parse(stdout)).toMatchObject({ version: OPENCODE_VERSION, pid: process.pid })
266275
expect(proxied).toEqual([])
276+
expect({ stderr, code }).toEqual({ stderr: "", code: 0 })
277+
expect(stdout).toBe(command === "session" ? "" : "No plugins found\n")
278+
expect(received.map((request) => request.path)).toContain("/api/info")
279+
expect(received.map((request) => request.path)).toContain(`/api/${command}`)
280+
expect(received.every((request) => request.authorization === "Basic " + btoa("opencode:fixture-password"))).toBe(
281+
true,
282+
)
267283
} finally {
268284
await server.stop(true)
269285
await proxy.stop(true)

0 commit comments

Comments
 (0)