| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { HOSTED, apiFor, parseOrigin, parseServer, socketUrl } from "./server.ts"; |
| 5 | |
| 6 | test("an origin is all that is kept", () => { |
| 7 | assert.equal(parseOrigin("https://g1t.sh/acme/-/chat?x#y"), "https://g1t.sh"); |
| 8 | assert.equal(parseOrigin(" g1t.example.com "), "https://g1t.example.com"); |
| 9 | assert.equal(parseOrigin("http://192.168.1.20:5173/login"), "http://192.168.1.20:5173"); |
| 10 | assert.equal(parseOrigin("ftp://g1t.sh"), null); |
| 11 | assert.equal(parseOrigin("not a url at all"), null); |
| 12 | assert.equal(parseOrigin(""), null); |
| 13 | }); |
| 14 | |
| 15 | test("hosted g1t's API is api.g1t.sh, and a self-hosted one's is api. on its host", () => { |
| 16 | assert.equal(apiFor("https://g1t.sh"), "https://api.g1t.sh"); |
| 17 | assert.equal(apiFor("https://g1t.example.com"), "https://api.g1t.example.com"); |
| 18 | assert.deepEqual(parseServer("g1t.sh"), HOSTED); |
| 19 | }); |
| 20 | |
| 21 | test("an API given by hand wins", () => { |
| 22 | assert.deepEqual(parseServer("http://localhost:5173", "http://localhost:8789/"), { |
| 23 | site: "http://localhost:5173", |
| 24 | api: "http://localhost:8789", |
| 25 | }); |
| 26 | assert.equal(parseServer("g1t.sh", "javascript:alert(1)"), null); |
| 27 | }); |
| 28 | |
| 29 | test("sockets follow the site's scheme", () => { |
| 30 | assert.equal(socketUrl(HOSTED, "/acme/-/chat/live?channel=chn_1"), "wss://g1t.sh/acme/-/chat/live?channel=chn_1"); |
| 31 | assert.equal(socketUrl({ site: "http://localhost:5173", api: "" }, "/-/live"), "ws://localhost:5173/-/live"); |
| 32 | }); |