mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-04 12:07:09 +00:00
test(replay-ui): exercise real URL encoding and non-ok throw in getJson
This commit is contained in:
1 parent
0fae65d4f2
commit
8f817bd8ff
2 files changed
+32
-5
No files matched your search
@@ -1,10 +1,37 @@
|
|||||||
import { describe, it, expect } from "bun:test";
|
import { describe, it, expect } from "bun:test";
|
||||||
import { screenshotUrl } from "../api";
|
import { getJson, screenshotUrl } from "../api";
|
||||||
|
|
||||||
describe("screenshotUrl", () => {
|
describe("screenshotUrl", () => {
|
||||||
it("encodes runId and name", () => {
|
it("percent-encodes runId and name with reserved characters", () => {
|
||||||
expect(screenshotUrl("run-1", "step-00001.png")).toBe(
|
expect(screenshotUrl("run #1/a", "step 00001.png")).toBe(
|
||||||
"/api/runs/run-1/screenshots/step-00001.png",
|
"/api/runs/run%20%231%2Fa/screenshots/step%2000001.png",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("getJson", () => {
|
||||||
|
it("returns the decoded body on a 200 response", async () => {
|
||||||
|
const server = Bun.serve({
|
||||||
|
port: 0,
|
||||||
|
fetch: () => Response.json({ ok: true }),
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const body = await getJson<{ ok: boolean }>(server.url.href);
|
||||||
|
expect(body).toEqual({ ok: true });
|
||||||
|
} finally {
|
||||||
|
server.stop(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws on a non-ok response instead of returning the error body", async () => {
|
||||||
|
const server = Bun.serve({
|
||||||
|
port: 0,
|
||||||
|
fetch: () => new Response("boom", { status: 500 }),
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await expect(getJson(server.url.href)).rejects.toThrow("500");
|
||||||
|
} finally {
|
||||||
|
server.stop(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { Run, RunSummary, Step } from "./types";
|
import type { Run, RunSummary, Step } from "./types";
|
||||||
|
|
||||||
async function getJson<T>(path: string): Promise<T> {
|
export async function getJson<T>(path: string): Promise<T> {
|
||||||
const response = await fetch(path, { headers: { Accept: "application/json" } });
|
const response = await fetch(path, { headers: { Accept: "application/json" } });
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`request failed: ${response.status} ${response.statusText} (${path})`);
|
throw new Error(`request failed: ${response.status} ${response.statusText} (${path})`);
|
||||||
|
|||||||
Reference in new issue
Block a user