askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
request-kinds.test.ts · 4.4 KB · 57 lines
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { RequestKinds } from "../src/request-kinds"; |
| 3 | import { chromiumRequestHeaders, fetchMetadataFor, requestCredentials } from "../src/compat"; |
| 4 | |
| 5 | const hints = { brands: [{ brand: "Chromium", version: "156" }], platform: "macOS", mobile: false }; |
| 6 | |
| 7 | describe("the kind of each request, as webRequest saw it", () => { |
| 8 | it("is taken once, oldest first, for the same method and address, and forgotten after a few seconds", () => { |
| 9 | let t = 0; |
| 10 | const k = new RequestKinds(() => t); |
| 11 | k.record({ method: "GET", url: "https://a.test/x.js", type: "script", frameUrl: "https://page.test/p" }); |
| 12 | k.record({ method: "GET", url: "https://a.test/x.js", type: "image", frameUrl: "https://other.test/" }); |
| 13 | expect(k.take("GET", "https://a.test/x.js")).toEqual({ type: "script", initiator: "https://page.test" }); |
| 14 | expect(k.take("GET", "https://a.test/x.js")).toEqual({ type: "image", initiator: "https://other.test" }); |
| 15 | expect(k.take("GET", "https://a.test/x.js")).toBeNull(); |
| 16 | k.record({ method: "POST", url: "https://a.test/api", type: "xhr", frameUrl: "https://a.test/" }); |
| 17 | expect(k.take("GET", "https://a.test/api")).toBeNull(); // another method |
| 18 | t += 11_000; |
| 19 | expect(k.take("POST", "https://a.test/api")).toBeNull(); // too old |
| 20 | }); |
| 21 | |
| 22 | it("a frame being loaded was started by its parent; a page by where the link was", () => { |
| 23 | const k = new RequestKinds(); |
| 24 | k.record({ method: "GET", url: "https://challenges.test/f", type: "subFrame", frameUrl: "", parentUrl: "https://form.test/apply", referrer: "https://form.test/" }); |
| 25 | expect(k.take("GET", "https://challenges.test/f")).toEqual({ type: "subFrame", initiator: "https://form.test" }); |
| 26 | k.record({ method: "GET", url: "https://b.test/", type: "mainFrame", frameUrl: "https://a.test/", referrer: "" }); |
| 27 | expect(k.take("GET", "https://b.test/")).toEqual({ type: "mainFrame", initiator: null }); // typed: from nowhere |
| 28 | }); |
| 29 | }); |
| 30 | |
| 31 | describe("fetch metadata from the real request kind (what Cloudflare's check compares)", () => { |
| 32 | const H = (init: Record<string, string> = {}) => new Headers({ accept: "*/*", ...init }); |
| 33 | it("a cross-site script, a frame, the frame's own POST, and a same-origin image", () => { |
| 34 | const s = chromiumRequestHeaders(new URL("https://challenges.test/api.js"), H(), hints, { kind: { type: "script", initiator: "https://form.test" } }); |
| 35 | expect([s.get("sec-fetch-site"), s.get("sec-fetch-mode"), s.get("sec-fetch-dest")]).toEqual(["cross-site", "no-cors", "script"]); |
| 36 | const f = chromiumRequestHeaders(new URL("https://challenges.test/f"), H({ "upgrade-insecure-requests": "1", accept: "text/html" }), hints, { kind: { type: "subFrame", initiator: "https://form.test" } }); |
| 37 | expect([f.get("sec-fetch-site"), f.get("sec-fetch-mode"), f.get("sec-fetch-dest"), f.has("sec-fetch-user")]).toEqual(["cross-site", "navigate", "iframe", false]); |
| 38 | const p = chromiumRequestHeaders(new URL("https://challenges.test/fo/1"), H(), hints, { kind: { type: "xhr", initiator: "https://challenges.test" } }, "POST"); |
| 39 | expect([p.get("sec-fetch-site"), p.get("sec-fetch-mode"), p.get("sec-fetch-dest"), p.get("origin")]).toEqual(["same-origin", "cors", "empty", "https://challenges.test"]); |
| 40 | const i = chromiumRequestHeaders(new URL("https://challenges.test/i.png"), H({ accept: "image/*" }), hints, { kind: { type: "image", initiator: "https://challenges.test" } }); |
| 41 | expect([i.get("sec-fetch-site"), i.get("sec-fetch-mode"), i.get("sec-fetch-dest")]).toEqual(["same-origin", "no-cors", "image"]); |
| 42 | }); |
| 43 | |
| 44 | it("adds no Origin to a cross-origin fetch, so it keeps the page's cookies as before (x.com's calls to api.x.com)", () => { |
| 45 | const h = chromiumRequestHeaders(new URL("https://api.x.test/1"), H(), hints, { kind: { type: "xhr", initiator: "https://x.test" } }, "POST"); |
| 46 | expect(h.has("origin")).toBe(false); |
| 47 | expect(h.get("sec-fetch-site")).toBe("same-site"); |
| 48 | expect(h.get("sec-fetch-mode")).toBe("no-cors"); // as before: "cors" with cookies and no Origin is refused by Chromium |
| 49 | expect(requestCredentials({ url: "https://api.x.test/1", credentials: "same-origin" }, h)).toBe("include"); |
| 50 | }); |
| 51 | |
| 52 | it("knows the kinds Chromium names", () => { |
| 53 | expect(fetchMetadataFor("font", false)).toEqual({ dest: "font", mode: "cors" }); |
| 54 | expect(fetchMetadataFor("stylesheet", true)).toEqual({ dest: "style", mode: "cors" }); |
| 55 | expect(fetchMetadataFor("other", false)).toBeNull(); // unknown: the old guess from Accept |
| 56 | }); |
| 57 | }); |