Public: anyone can read this; the crew can change it.

/explore.json answers with an ETag and a 304 on a matching If-None-Match #16058

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/16053-explore-json-answers-with-an-etag-and-a → main 0 commits fork

  • Merged. torque/16053-explore-json-answers-with-an-etag-and-a was merged into main as 49abd2e.

Review

Sign in to review this change.
1 file changed +73 −0
test/explore-json-etag.test.ts added +73
@@ -0,0 +1,73 @@
1 /**
2 * /explore.json carries a weak ETag and answers a matching If-None-Match with a 304 and no body
3 * (#16053); a changed ranking (a like on a listed app moves the write clock) changes the tag.
4 * The behaviour was already in src/http/explore_json.ts; this pins it for the .json route.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { createRepo } from "../src/actions/repos.ts";
12 import { createApp as insertApp } from "../src/core/apps.ts";
13 import { createUser, getUserByName } from "../src/core/users.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import { promote } from "./helpers/members.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17
18 quietLogs();
19 let dir = "";
20 let app: App;
21 let servers: Servers;
22 let repoId = "";
23 let userId = "";
24
25 beforeAll(async () => {
26 dir = await mkdtemp(join(tmpdir(), "forge-explore-json-etag-"));
27 app = createApp({ dataDir: dir, port: 0 });
28 servers = await serve(app);
29 const founder = await createUser(app.db, { origin: "seed", trusted: true, username: "founder", email: "[email protected]", password: "a-long-words-passphrase", adminIfFirst: true });
30 if (!founder.ok) throw new Error("founder");
31 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "olive", email: "[email protected]", password: "a-long-words-passphrase" });
32 if (!made.ok) throw new Error("user");
33 promote(app.db, made.value);
34 const olive = getUserByName(app.db, "olive")!;
35 userId = olive.id;
36 const repo = await createRepo(app, olive, { name: "garden", visibility: "public" });
37 if (!repo.ok) throw new Error("repo");
38 repoId = repo.value.id;
39 const rec = insertApp(app.db, { repoId, slug: "olive-garden", access: "public" });
40 if (!rec.ok) throw new Error("app");
41 }, 30_000);
42 afterAll(async () => {
43 await servers.stop();
44 app.close();
45 await rm(dir, { recursive: true, force: true });
46 });
47
48 const get = (etag?: string) =>
49 fetch(`${servers.url}/explore.json?kind=app`, { headers: etag === undefined ? {} : { "if-none-match": etag } });
50
51 test("a repeat with the ETag is a 304 with no body", async () => {
52 const first = await get();
53 await first.arrayBuffer();
54 expect(first.status).toBe(200);
55 const tag = first.headers.get("etag")!;
56 expect(tag).toMatch(/^W\/"[0-9a-z]+"$/);
57 const again = await get(tag);
58 expect(again.status).toBe(304);
59 expect(await again.text()).toBe("");
60 expect(again.headers.get("etag")).toBe(tag);
61 expect((await get('W/"other"')).status).toBe(200);
62 });
63
64 test("a changed ranking changes the ETag", async () => {
65 const first = await get();
66 await first.arrayBuffer();
67 const tag = first.headers.get("etag")!;
68 app.db.run("INSERT INTO feed_like (viewer_id, item_id, repo_id) VALUES (?1, ?2, ?3)", [userId, "x-1", repoId]);
69 const next = await get(tag);
70 expect(next.status).toBe(200);
71 await next.arrayBuffer();
72 expect(next.headers.get("etag")).not.toBe(tag);
73 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs