Skip to content

Commit 235cfd7

Browse files
committed
test(plugins): add bounded response read tests for hosted catalog
Add three test cases for readHostedCatalogResponseText bounded read: - within maxBytes returns hosted feed - exceeding maxBytes falls back to bundled catalog - readResponseWithLimit enforces byte limit before decoding Ref. #101405
1 parent abc0725 commit 235cfd7

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

‎src/plugins/official-external-plugin-catalog.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import os from "node:os";
33
import path from "node:path";
44
import { describe, expect, it, vi } from "vitest";
55
import officialExternalPluginCatalog from "../../scripts/lib/official-external-plugin-catalog.json" with { type: "json" };
6+
import { readResponseWithLimit } from "../infra/http-body.js";
67
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
78
import { createSqliteHostedOfficialExternalPluginCatalogSnapshotStore } from "./official-external-plugin-catalog-snapshot-store.js";
89
import {
@@ -1960,3 +1961,60 @@ describe("official external plugin catalog", () => {
19601961
});
19611962
});
19621963
});
1964+
1965+
describe("readHostedCatalogResponseText bounded read", () => {
1966+
it("returns hosted feed when response body is within maxBytes", async () => {
1967+
const result = await loadHostedOfficialExternalPluginCatalogEntries({
1968+
snapshotStore: null,
1969+
maxBytes: 4096,
1970+
fetchImpl: vi.fn(
1971+
async () =>
1972+
new Response(
1973+
JSON.stringify({
1974+
schemaVersion: 1,
1975+
id: "test",
1976+
generatedAt: "2026-01-01T00:00:00.000Z",
1977+
sequence: 1,
1978+
entries: [],
1979+
}),
1980+
{ status: 200 },
1981+
),
1982+
),
1983+
});
1984+
expect(result.source).toBe("hosted");
1985+
});
1986+
1987+
it("falls back to bundled catalog when response body exceeds maxBytes", async () => {
1988+
const result = await loadHostedOfficialExternalPluginCatalogEntries({
1989+
snapshotStore: null,
1990+
maxBytes: 10,
1991+
fetchImpl: vi.fn(
1992+
async () =>
1993+
new Response(
1994+
JSON.stringify({
1995+
schemaVersion: 1,
1996+
id: "test",
1997+
generatedAt: "2026-01-01T00:00:00.000Z",
1998+
sequence: 1,
1999+
entries: [],
2000+
}),
2001+
{ status: 200 },
2002+
),
2003+
),
2004+
});
2005+
expect(result.source).toBe("bundled-fallback");
2006+
if (result.source === "bundled-fallback") {
2007+
expect(result.error).toContain("exceeds");
2008+
}
2009+
});
2010+
2011+
it("enforces maxBytes at the readResponseWithLimit layer before decoding", async () => {
2012+
let caughtError: string | undefined;
2013+
try {
2014+
await readResponseWithLimit(new Response("this is twenty bytes"), 10);
2015+
} catch (e: unknown) {
2016+
caughtError = e instanceof Error ? e.message : String(e);
2017+
}
2018+
expect(caughtError).toBe("Content too large: 20 bytes (limit: 10 bytes)");
2019+
});
2020+
});

0 commit comments

Comments
 (0)