A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

dbbasic-browser/apps/browser/test/intercept.test.ts
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
intercept.test.ts · 2.1 KB · 56 lines
1import { describe, expect, it } from "vitest";
2import type { X402Engine } from "@dbbasic/x402-engine";
3import { bypassingFetch, createPaymentHandler, topOriginOf } from "../src/intercept.js";
4
5describe("topOriginOf", () => {
6 it("uses the referrer origin so subresources are billed to their page", () => {
7 const req = new Request("https://cdn.paid.example/img.png", {
8 headers: { referer: "https://reader.example/article?x=1" },
9 });
10 expect(topOriginOf(req)).toBe("https://reader.example");
11 });
12
13 it("returns empty for a top-level navigation (its own budget scope)", () => {
14 const req = new Request("https://reader.example/");
15 expect(topOriginOf(req)).toBe("");
16 });
17
18 it("ignores about:client and malformed referrers", () => {
19 const req = new Request("https://x.example/", { headers: { referer: "not-a-url" } });
20 expect(topOriginOf(req)).toBe("");
21 });
22});
23
24describe("createPaymentHandler", () => {
25 it("routes the request through the engine with the derived topOrigin", async () => {
26 const seen: { url: string; topOrigin: string }[] = [];
27 const fakeEngine = {
28 fetch: async (request: Request, topOrigin: string) => {
29 seen.push({ url: request.url, topOrigin });
30 return { response: new Response("paid body", { status: 200 }) };
31 },
32 } as unknown as X402Engine;
33
34 const handler = createPaymentHandler(fakeEngine);
35 const req = new Request("https://api.paid.example/data", {
36 headers: { referer: "https://app.example/dashboard" },
37 });
38 const res = await handler(req);
39
40 expect(await res.text()).toBe("paid body");
41 expect(seen).toEqual([{ url: "https://api.paid.example/data", topOrigin: "https://app.example" }]);
42 });
43});
44
45describe("bypassingFetch", () => {
46 it("delegates to the provided bypassing fetch (recursion guard contract)", async () => {
47 let called = 0;
48 const raw = bypassingFetch(async (input) => {
49 called++;
50 return new Response(`fetched ${input.url}`);
51 });
52 const res = await raw(new Request("https://origin.example/x"));
53 expect(called).toBe(1);
54 expect(await res.text()).toBe("fetched https://origin.example/x");
55 });
56});