Repository navigation
Expand file tree
/
Copy pathnews-api.test.mjs
More file actions
285 lines (262 loc) · 8.56 KB
/
Copy pathnews-api.test.mjs
File metadata and controls
285 lines (262 loc) · 8.56 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import assert from "node:assert/strict";
import test from "node:test";
import { handleApiRequest } from "../server/api-router.js";
import { BUILT_IN_RSS_FEEDS } from "../server/news-feeds.js";
import { handleNewsRequest } from "../server/news.js";
const FIXED_FEEDS = new Map(
BUILT_IN_RSS_FEEDS.map(({ id, url }) => [id, url]),
);
const FEED_URL = FIXED_FEEDS.get("sbs-latest");
test("uses unique fixed feed IDs and explicit shared fallback URLs", () => {
assert.equal(BUILT_IN_RSS_FEEDS.length, 540);
assert.equal(FIXED_FEEDS.size, BUILT_IN_RSS_FEEDS.length);
const localized = BUILT_IN_RSS_FEEDS.filter((feed) => !feed.fallbackLocale);
assert.equal(
new Set(localized.map(({ url }) => url)).size,
localized.length,
);
const englishUrls = new Set(
BUILT_IN_RSS_FEEDS
.filter((feed) => feed.locale === "en")
.map(({ url }) => url),
);
for (const feed of BUILT_IN_RSS_FEEDS.filter((item) => item.fallbackLocale)) {
assert.equal(feed.fallbackLocale, "en");
assert.equal(englishUrls.has(feed.url), true);
}
});
function rssResponse(body = "<rss><channel /></rss>", init = {}) {
return new Response(body, {
status: 200,
headers: { "content-type": "application/rss+xml" },
...init,
});
}
test("proxies only the fixed SBS latest feed with hardened options", async () => {
const calls = [];
const response = await handleApiRequest(
new Request("https://example.test/api/news?feed=sbs-latest"),
{},
{
fetchImpl: async (url, options) => {
calls.push({ url, options });
return rssResponse("<rss>ok</rss>");
},
},
);
assert.equal(response.status, 200);
assert.equal(await response.text(), "<rss>ok</rss>");
assert.equal(
response.headers.get("content-type"),
"application/rss+xml; charset=utf-8",
);
assert.equal(
response.headers.get("cache-control"),
"public, max-age=300, s-maxage=300, stale-while-revalidate=600",
);
assert.equal(response.headers.get("x-content-type-options"), "nosniff");
assert.equal(calls.length, 1);
assert.equal(calls[0].url, FEED_URL);
assert.equal(calls[0].options.redirect, "manual");
assert.deepEqual(calls[0].options.headers, {
accept: "application/rss+xml, application/xml, text/xml",
});
assert.ok(calls[0].options.signal instanceof AbortSignal);
});
for (const [feed, url] of FIXED_FEEDS) {
test(`proxies the fixed ${feed} feed`, async () => {
const calls = [];
const response = await handleNewsRequest(
new Request(`https://example.test/api/news?feed=${feed}`),
{},
{
fetchImpl: async (input) => {
calls.push(input);
return rssResponse();
},
},
);
assert.equal(response.status, 200);
assert.deepEqual(calls, [url]);
});
}
test("proxies one validated custom HTTPS RSS feed without shared caching", async () => {
const calls = [];
const source = "https://feeds.example.com/latest.xml?edition=kr";
const response = await handleApiRequest(
new Request(
`https://example.test/api/news?url=${encodeURIComponent(source)}`,
),
{},
{
fetchImpl: async (url, options) => {
calls.push({ url, options });
return rssResponse("<feed xmlns=\"http://www.w3.org/2005/Atom\" />", {
headers: { "content-type": "application/atom+xml" },
});
},
},
);
assert.equal(response.status, 200);
assert.equal(calls.length, 1);
assert.equal(calls[0].url, source);
assert.equal(calls[0].options.redirect, "manual");
assert.equal(response.headers.get("cache-control"), "no-store");
assert.equal(
response.headers.get("content-type"),
"application/xml; charset=utf-8",
);
});
for (const unsafe of [
"http://feeds.example.com/rss.xml",
"https://user:pass@feeds.example.com/rss.xml",
"https://feeds.example.com:8443/rss.xml",
"https://127.0.0.1/rss.xml",
"https://[::1]/rss.xml",
"https://localhost/rss.xml",
"https://localhost./rss.xml",
"https://reader.local./rss.xml",
"https://printer/rss.xml",
"https://reader.local/rss.xml",
"https://feeds.internal/rss.xml",
"https://feeds.example.com/rss.xml#private",
]) {
test(`rejects unsafe custom RSS URL ${unsafe}`, async () => {
let calls = 0;
const response = await handleNewsRequest(
new Request(
`https://example.test/api/news?url=${encodeURIComponent(unsafe)}`,
),
{},
{ fetchImpl: async () => {
calls += 1;
return rssResponse();
} },
);
assert.equal(response.status, 400);
assert.equal(calls, 0);
assert.equal((await response.json()).error.code, "UNSAFE_FEED_URL");
});
}
test("rejects custom redirects, non-XML content, and non-feed XML", async () => {
const source = encodeURIComponent("https://feeds.example.com/rss.xml");
const redirect = await handleNewsRequest(
new Request(`https://example.test/api/news?url=${source}`),
{},
{ fetchImpl: async () => new Response(null, {
status: 302,
headers: { location: "https://other.example.com/rss.xml" },
}) },
);
const html = await handleNewsRequest(
new Request(`https://example.test/api/news?url=${source}`),
{},
{ fetchImpl: async () => new Response("<rss />", {
headers: { "content-type": "text/html" },
}) },
);
const nonFeed = await handleNewsRequest(
new Request(`https://example.test/api/news?url=${source}`),
{},
{ fetchImpl: async () => new Response("<document />", {
headers: { "content-type": "application/xml" },
}) },
);
assert.equal(redirect.status, 502);
assert.equal((await redirect.json()).error.code, "NEWS_REDIRECT");
assert.equal(html.status, 502);
assert.equal((await html.json()).error.code, "NEWS_CONTENT_TYPE");
assert.equal(nonFeed.status, 502);
assert.equal((await nonFeed.json()).error.code, "NEWS_INVALID_FEED");
});
test("rejects unsupported feeds without calling upstream", async () => {
let calls = 0;
const response = await handleNewsRequest(
new Request("https://example.test/api/news?feed=unknown"),
{},
{ fetchImpl: async () => {
calls += 1;
return rssResponse();
} },
);
assert.equal(response.status, 400);
assert.equal(calls, 0);
assert.deepEqual(await response.json(), {
error: {
code: "UNSUPPORTED_FEED",
message: "Unsupported news feed",
},
});
});
test("leaves non-GET news requests to the API 404", async () => {
let calls = 0;
const response = await handleApiRequest(
new Request("https://example.test/api/news?feed=sbs-latest", {
method: "POST",
}),
{},
{ fetchImpl: async () => {
calls += 1;
return rssResponse();
} },
);
assert.equal(response.status, 404);
assert.equal(calls, 0);
assert.equal((await response.json()).error.code, "API_NOT_FOUND");
});
test("maps an upstream non-2xx response to a stable JSON error", async () => {
const response = await handleNewsRequest(
new Request("https://example.test/api/news?feed=sbs-latest"),
{},
{ fetchImpl: async () => new Response("bad", { status: 503 }) },
);
assert.equal(response.status, 502);
assert.equal((await response.json()).error.code, "NEWS_UPSTREAM_ERROR");
});
test("cancels and rejects a response larger than one million bytes", async () => {
let cancelled = false;
const body = new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array(600_000));
controller.enqueue(new Uint8Array(600_000));
},
cancel() {
cancelled = true;
},
});
const response = await handleNewsRequest(
new Request("https://example.test/api/news?feed=sbs-latest"),
{},
{
fetchImpl: async () => new Response(body, {
status: 200,
headers: { "content-type": "application/rss+xml" },
}),
},
);
assert.equal(response.status, 502);
assert.equal((await response.json()).error.code, "NEWS_TOO_LARGE");
assert.equal(cancelled, true);
});
test("maps the eight-second abort to NEWS_TIMEOUT and clears its timer", async () => {
const cleared = [];
const response = await handleNewsRequest(
new Request("https://example.test/api/news?feed=sbs-latest"),
{},
{
setTimeoutImpl: (callback, milliseconds) => {
assert.equal(milliseconds, 8_000);
callback();
return 42;
},
clearTimeoutImpl: (timer) => cleared.push(timer),
fetchImpl: async (_url, { signal }) => {
assert.equal(signal.aborted, true);
throw new DOMException("Aborted", "AbortError");
},
},
);
assert.equal(response.status, 504);
assert.equal((await response.json()).error.code, "NEWS_TIMEOUT");
assert.deepEqual(cleared, [42]);
});