50 lines
3.0 KiB
TypeScript
50 lines
3.0 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { fetchBoundedEvoCatalog, fetchBoundedEvoModules } from "./evobgp-client.js"
|
|
|
|
test("connectivity uses only the fixed modules GET and a smaller body bound", async () => {
|
|
assert.deepEqual(await fetchBoundedEvoModules("https://evo.invalid", "fixture", async (url, init) => {
|
|
assert.equal(url, "https://evo.invalid/v1/modules?limit=1")
|
|
assert.equal(init?.method, "GET"); assert.equal(init?.redirect, "error")
|
|
return new Response('{"items":[]}')
|
|
}), { items: [] })
|
|
await assert.rejects(fetchBoundedEvoModules("https://evo.invalid", "fixture", async () => new Response("{}", { headers: { "content-length": String(1024 * 1024 + 1) } }), { maxBytes: 8000000 }))
|
|
})
|
|
|
|
test("fixed authenticated catalog GET forbids redirects and accepts known empty JSON", async () => {
|
|
const result = await fetchBoundedEvoCatalog("https://evo.invalid", "private-fixture", async (url, init) => {
|
|
assert.equal(url, "https://evo.invalid/v1/router-lists/catalog")
|
|
assert.equal(init?.method, "GET"); assert.equal(init?.redirect, "error")
|
|
assert.deepEqual(init?.headers, { Authorization: "Bearer private-fixture", Accept: "application/json" })
|
|
return new Response("{}")
|
|
})
|
|
assert.deepEqual(result, {})
|
|
})
|
|
|
|
test("HTTP errors and malformed JSON never echo upstream credential text", async () => {
|
|
for (const response of [new Response("private-fixture", { status: 502 }), new Response("not-json-private-fixture")]) {
|
|
await assert.rejects(fetchBoundedEvoCatalog("https://evo.invalid", "private-fixture", async () => response), error => error instanceof Error && !error.message.includes("private-fixture"))
|
|
}
|
|
})
|
|
|
|
test("announced and streamed sizes are bounded and cancel their bodies", async () => {
|
|
for (const announced of [true, false]) {
|
|
let canceled = false
|
|
const response = new Response(new ReadableStream({ start(controller) { controller.enqueue(new Uint8Array(20)) }, cancel() { canceled = true } }), { headers: announced ? { "content-length": "20" } : {} })
|
|
await assert.rejects(fetchBoundedEvoCatalog("https://evo.invalid", "fixture", async () => response, { maxBytes: 10 }))
|
|
assert.equal(canceled, true)
|
|
}
|
|
})
|
|
|
|
test("the deadline also aborts a stalled streaming body", async () => {
|
|
const fetcher: typeof fetch = async (_url, init) => new Response(new ReadableStream({ start(controller) { init?.signal?.addEventListener("abort", () => controller.error(new Error("aborted-private-fixture")), { once: true }) } }))
|
|
await assert.rejects(fetchBoundedEvoCatalog("https://evo.invalid", "private-fixture", fetcher, { timeoutMs: 20 }), error => error instanceof Error && !error.message.includes("private-fixture"))
|
|
})
|
|
|
|
test("an already canceled MCP call dispatches no upstream request", async () => {
|
|
const controller = new AbortController(); controller.abort()
|
|
let calls = 0
|
|
await assert.rejects(fetchBoundedEvoCatalog("https://evo.invalid", "fixture", async () => { calls++; return new Response("{}") }, { signal: controller.signal }))
|
|
assert.equal(calls, 0)
|
|
})
|