~james/core

core/web/src/lib/api/_request.test.ts -rw-r--r-- 2.8 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { afterEach, describe, expect, it, vi } from "vitest";
import { hostForKnot, tryHostForKnot } from "#lib/api/_request.ts";

describe("hostForKnot", () => {
	it.each([
		["did:web:knot2.example.com", "knot2.example.com"],
		["did:web:KNOT2.EXAMPLE.COM", "knot2.example.com"],
		["did:web:knot.example.com%3A8443", "knot.example.com:8443"],
		["did:web:knot.example.com%3a8443", "knot.example.com:8443"],
		["did:web:localhost%3A3000", "localhost:3000"],
		["knot2.example.com", "knot2.example.com"],
		["knot.example.com:8443", "knot.example.com:8443"],
		["localhost", "localhost"],
		["localhost:3000", "localhost:3000"],
		["127.0.0.1:8000", "127.0.0.1:8000"]
	])("normalizes %s to %s", (input, expected) => {
		expect(hostForKnot(input)).toBe(expected);
	});

	it.each([
		"did:plc:iz6r437h6kndu44y7u7f7rly",
		"https://knot2.example.com",
		"knot2.example.com/path",
		"did:web:",
		"did:web:foo/bar",
		"did:web:knot.dev:8443",
		"garbage text",
		":::",
		"",
		"   ",
		"knot..dev",
		"-knot.dev",
		"knot.dev:99999",
		"knot.dev:0",
		"foo@bar.com"
	])("rejects garbage %j", (input) => {
		expect(() => hostForKnot(input)).toThrow();
	});
});

describe("hosts outside dev", () => {
	afterEach(() => vi.unstubAllEnvs());

	it.each([
		"localhost",
		"localhost:6555",
		"LOCALHOST:6555",
		"foo.localhost",
		"foo.local",
		"metadata.internal",
		"intranet",
		"127.0.0.1",
		"127.0.0.1:8000",
		"10.0.0.5",
		"169.254.169.254",
		"192.168.1.1:80",
		"2130706433",
		"0x7f000001",
		"0x7f.1",
		"017700000001",
		"127.1",
		"did:web:localhost%3A6555",
		"did:web:169.254.169.254",
		"[::1]",
		"[::1]:80",
		"::1"
	])("refuses %s", (input) => {
		vi.stubEnv("DEV", false);
		expect(() => hostForKnot(input)).toThrow();
		expect(tryHostForKnot(input)).toBeUndefined();
	});

	it.each([
		["spindle.example.com", "spindle.example.com"],
		["spindle.tngl.boltless.dev:8443", "spindle.tngl.boltless.dev:8443"],
		["did:web:knot1.tangled.sh", "knot1.tangled.sh"],
		["a.b1", "a.b1"]
	])("keeps %s", (input, expected) => {
		vi.stubEnv("DEV", false);
		expect(hostForKnot(input)).toBe(expected);
	});

	it("allows loopback in dev", () => {
		vi.stubEnv("DEV", true);
		expect(hostForKnot("localhost:6555")).toBe("localhost:6555");
		expect(hostForKnot("127.0.0.1:8000")).toBe("127.0.0.1:8000");
	});
});

describe("tryHostForKnot", () => {
	it.each([
		["did:web:spindle.example.com", "spindle.example.com"],
		["did:web:localhost%3A6555", "localhost:6555"],
		["spindle.example.com", "spindle.example.com"],
		["did:plc:abcdefghijklmnopqrstuvwx", undefined],
		["did:web:bad%zz", undefined],
		["did:web:a%3A1:path", undefined],
		["evil.com/x", undefined],
		["a@b", undefined],
		["http://x", undefined],
		["a b", undefined],
		["", undefined],
		[undefined, undefined]
	])("%s -> %s", (input, expected) => {
		expect(tryHostForKnot(input)).toBe(expected);
	});
});