-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.test.ts
121 lines (102 loc) · 3.48 KB
/
server.test.ts
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { HandleMap } from "./providers/map";
import { handleRequest, HttpConfiguration, defaultFallbackUrl } from "./server";
import {
createRequest,
createResponse,
RequestOptions,
MockResponse,
} from "node-mocks-http";
const testHandleMap = new HandleMap(
new Map([["registered.at.example.com", "did:plc:example1"]]),
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Response = MockResponse<any>;
async function performRequest(
options: RequestOptions,
configuration: HttpConfiguration = {
fallbackUrl: defaultFallbackUrl,
},
): Promise<Response> {
const req = createRequest(options);
const res = createResponse();
await handleRequest(req, res, testHandleMap, configuration);
return res;
}
describe("Handles Server", () => {
test("Responds to healthcheck request", async () => {
const res = await performRequest({ method: "GET", url: "/healthz" });
expect(res._getData()).toBe("OK");
expect(res._getStatusCode()).toBe(200);
});
test("Request to verify registered handle returns did.", async () => {
const res = await performRequest({
method: "GET",
url: "/.well-known/atproto-did",
headers: { Host: "registered.at.example.com" },
});
expect(res._getStatusCode()).toBe(200);
expect(res._getData()).toBe("did:plc:example1");
});
test("Request to verify unregistered handle returns error", async () => {
const res = await performRequest({
method: "GET",
url: "/.well-known/atproto-did",
headers: { Host: "not-registered.at.example.com" },
});
expect(res._getStatusCode()).toBe(404);
});
test("Request to visit registered handle redirects to profile", async () => {
const res = await performRequest({
method: "GET",
url: "/",
headers: { Host: "registered.at.example.com" },
});
expect(res._getStatusCode()).toBe(307);
expect(res._getHeaders().location).toBe(
"https://bsky.app/profile/registered.at.example.com",
);
});
test("Request to visit unregistered handle without fallback URL redirects to domain", async () => {
const res = await performRequest({
method: "GET",
url: "/",
headers: { Host: "not-registered.at.example.com" },
});
expect(res._getStatusCode()).toBe(307);
expect(res._getHeaders().location).toBe(
"https://at.example.com?utm_source=handles-server&utm_medium=http&utm_campaign=redirect&utm_term=not-registered.at.example.com",
);
});
test("Request to visit unregistered handle redirects to fallback URL", async () => {
const res = await performRequest(
{
method: "GET",
url: "/",
headers: { Host: "not-registered.at.example.com" },
},
{
fallbackUrl: "https://example.com/domains/{domain}?handle={handle}",
},
);
expect(res._getStatusCode()).toBe(307);
expect(res._getHeaders().location).toBe(
"https://example.com/domains/at.example.com?handle=not-registered.at.example.com",
);
});
test("Replaces multiple instances of token in fallback URL", async () => {
const res = await performRequest(
{
method: "GET",
url: "/",
headers: { Host: "not-registered.at.example.com" },
},
{
fallbackUrl: "https://example.com/{handle}?handle={handle}",
},
);
expect(res._getStatusCode()).toBe(307);
expect(res._getHeaders().location).toBe(
"https://example.com/not-registered.at.example.com?handle=not-registered.at.example.com",
);
});
});