Repository navigation
Expand file tree
/
Copy pathroute-api.test.mjs
More file actions
422 lines (397 loc) · 12.2 KB
/
Copy pathroute-api.test.mjs
File metadata and controls
422 lines (397 loc) · 12.2 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import assert from "node:assert/strict";
import test from "node:test";
import { handleApiRequest } from "../server/api-router.js";
const SECRET = "fake-ors-secret";
const DEVICE_SECRET = "fake-device-ors-secret";
const START = { latitude: 37.5563, longitude: 126.922 };
const DESTINATION = { latitude: 37.5547, longitude: 126.9707 };
function jsonRequest(url, body) {
return new Request(url, {
method: "POST",
headers: { "content-type": "application/json" },
body: typeof body === "string" ? body : JSON.stringify(body),
});
}
function routeRequest(overrides = {}) {
return jsonRequest("https://example.test/api/route", {
start: START,
destination: DESTINATION,
profile: "foot-walking",
...overrides,
});
}
function upstreamJson(body, init = {}) {
return new Response(JSON.stringify(body), {
status: 200,
headers: { "content-type": "application/json" },
...init,
});
}
async function assertNoSecret(response) {
assert.doesNotMatch(
await response.clone().text(),
/fake-ors-secret|fake-device-ors-secret/,
);
}
test("disables every ORS endpoint cleanly when the server secret is absent", async () => {
const status = await handleApiRequest(
new Request("https://example.test/api/routing-status"),
{},
);
const geocode = await handleApiRequest(
new Request("https://example.test/api/geocode?q=서울역"),
{},
);
const route = await handleApiRequest(routeRequest(), {});
for (const response of [status, geocode, route]) {
await assertNoSecret(response);
}
assert.equal(status.status, 200);
assert.deepEqual(await status.json(), { enabled: false });
assert.equal(status.headers.get("cache-control"), "no-store");
for (const response of [geocode, route]) {
assert.equal(response.status, 503);
assert.equal((await response.json()).error.code, "ROUTING_DISABLED");
}
});
test("validates a device key and never returns it", async () => {
const calls = [];
const response = await handleApiRequest(
new Request("https://example.test/api/routing-key-test", {
method: "POST",
headers: { "x-sandevistan-ors-key": DEVICE_SECRET },
}),
{},
{
fetchImpl: async (url, options) => {
calls.push({ url: String(url), options });
return upstreamJson({ features: [] });
},
},
);
assert.equal(response.status, 200);
assert.deepEqual(await response.clone().json(), { valid: true });
await assertNoSecret(response);
assert.equal(response.headers.get("cache-control"), "no-store");
assert.equal(calls.length, 1);
const upstream = new URL(calls[0].url);
assert.equal(upstream.origin, "https://api.openrouteservice.org");
assert.equal(upstream.pathname, "/geocode/search");
assert.equal(upstream.searchParams.get("api_key"), DEVICE_SECRET);
assert.equal(calls[0].options.redirect, "manual");
});
test("prefers the request key over the development fallback", async () => {
const userKey = "device-owned-key-123456";
const calls = [];
const response = await handleApiRequest(
new Request("https://example.test/api/geocode?q=Seoul", {
headers: { "x-sandevistan-ors-key": userKey },
}),
{ ORS_API_KEY: SECRET },
{
fetchImpl: async (url) => {
calls.push(String(url));
return upstreamJson({ features: [] });
},
},
);
assert.equal(response.status, 200);
assert.equal(new URL(calls[0]).searchParams.get("api_key"), userKey);
assert.doesNotMatch(await response.text(), /device-owned-key|fake-ors-secret/);
});
test("normalizes at most five valid Korean geocoding results", async () => {
const calls = [];
const features = [
{
properties: {
id: 101,
name: "서울역",
label: "서울역, 서울특별시, 대한민국",
},
geometry: { coordinates: [126.9707, 37.5547] },
},
{
properties: {
gid: "venue.2",
name: "서울역 광장",
label: "서울역 광장",
},
geometry: { coordinates: [126.971, 37.555] },
},
{
properties: { id: "bad", name: "좌표 오류", label: "좌표 오류" },
geometry: { coordinates: [181, 37.5] },
},
...Array.from({ length: 5 }, (_, index) => ({
properties: {
id: `extra-${index}`,
name: `후보 ${index}`,
label: `후보 ${index}, 서울`,
},
geometry: { coordinates: [126.972 + index / 10_000, 37.556] },
})),
];
const response = await handleApiRequest(
new Request(
"https://example.test/api/geocode?q=%20%EC%84%9C%EC%9A%B8%EC%97%AD%20",
),
{ ORS_API_KEY: SECRET },
{
fetchImpl: async (url, options) => {
calls.push({ url: String(url), options });
return upstreamJson({ features });
},
},
);
assert.equal(response.status, 200);
await assertNoSecret(response);
assert.deepEqual(await response.json(), {
results: [
{
id: "101",
name: "서울역",
label: "서울역, 서울특별시, 대한민국",
coordinate: { latitude: 37.5547, longitude: 126.9707 },
},
{
id: "venue.2",
name: "서울역 광장",
label: "서울역 광장",
coordinate: { latitude: 37.555, longitude: 126.971 },
},
...Array.from({ length: 3 }, (_, index) => ({
id: `extra-${index}`,
name: `후보 ${index}`,
label: `후보 ${index}, 서울`,
coordinate: {
latitude: 37.556,
longitude: 126.972 + index / 10_000,
},
})),
],
});
assert.equal(calls.length, 1);
const upstream = new URL(calls[0].url);
assert.equal(
`${upstream.origin}${upstream.pathname}`,
"https://api.openrouteservice.org/geocode/search",
);
assert.equal(upstream.searchParams.get("api_key"), SECRET);
assert.equal(upstream.searchParams.get("text"), "서울역");
assert.equal(upstream.searchParams.get("boundary.country"), "KR");
assert.equal(upstream.searchParams.get("size"), "5");
assert.equal(calls[0].options.redirect, "manual");
assert.equal(calls[0].options.headers.accept, "application/json");
assert.ok(calls[0].options.signal instanceof AbortSignal);
});
test("normalizes a directions result and sends the exact ORS request", async () => {
const calls = [];
const response = await handleApiRequest(
routeRequest(),
{ ORS_API_KEY: SECRET },
{
fetchImpl: async (url, options) => {
calls.push({ url: String(url), options });
return upstreamJson({
features: [{
geometry: {
type: "LineString",
coordinates: [
[126.922, 37.5563],
[126.95, 37.555],
[126.9707, 37.5547],
],
},
properties: {
summary: { distance: 4380.5, duration: 3120.2 },
segments: [{
steps: [
{
instruction: "양화로 방면으로 직진",
distance: 120.4,
way_points: [0, 1],
},
{
instruction: "오른쪽으로 도세요",
distance: 4260.1,
way_points: [1, 2],
},
],
}],
},
}],
});
},
},
);
assert.equal(response.status, 200);
await assertNoSecret(response);
assert.deepEqual(await response.json(), {
geometry: [
[37.5563, 126.922],
[37.555, 126.95],
[37.5547, 126.9707],
],
distance: 4380.5,
duration: 3120.2,
maneuvers: [
{
instruction: "양화로 방면으로 직진",
distance: 120.4,
wayPoints: [0, 1],
},
{
instruction: "오른쪽으로 도세요",
distance: 4260.1,
wayPoints: [1, 2],
},
],
});
assert.equal(
calls[0].url,
"https://api.openrouteservice.org/v2/directions/foot-walking/geojson",
);
assert.deepEqual(calls[0].options, {
method: "POST",
headers: {
authorization: SECRET,
"content-type": "application/json",
accept: "application/geo+json",
},
body: JSON.stringify({
coordinates: [
[126.922, 37.5563],
[126.9707, 37.5547],
],
instructions: true,
language: "en",
}),
redirect: "manual",
signal: calls[0].options.signal,
});
assert.ok(calls[0].options.signal instanceof AbortSignal);
});
test("validates geocode queries before contacting ORS", async () => {
for (const query of ["", "a", "가".repeat(81)]) {
let calls = 0;
const response = await handleApiRequest(
new Request(
`https://example.test/api/geocode?q=${encodeURIComponent(query)}`,
),
{ ORS_API_KEY: SECRET },
{ fetchImpl: async () => {
calls += 1;
return upstreamJson({ features: [] });
} },
);
assert.equal(response.status, 400);
assert.equal((await response.json()).error.code, "INVALID_QUERY");
assert.equal(calls, 0);
}
});
test("validates route JSON, profile, and coordinates before contacting ORS", async () => {
const cases = [
{
request: jsonRequest("https://example.test/api/route", "{"),
code: "INVALID_JSON",
},
{
request: routeRequest({ profile: "wheelchair" }),
code: "INVALID_PROFILE",
},
{
request: routeRequest({
start: { latitude: 91, longitude: 126.922 },
}),
code: "INVALID_COORDINATE",
},
{
request: routeRequest({
destination: { latitude: 37.5, longitude: -181 },
}),
code: "INVALID_COORDINATE",
},
];
for (const { request, code } of cases) {
let calls = 0;
const response = await handleApiRequest(
request,
{ ORS_API_KEY: SECRET },
{ fetchImpl: async () => {
calls += 1;
return upstreamJson({});
} },
);
assert.equal(response.status, 400);
assert.equal((await response.json()).error.code, code);
assert.equal(calls, 0);
}
});
test("maps ORS direction failures to stable responses without leaking the key", async () => {
const upstream = await handleApiRequest(
routeRequest(),
{ ORS_API_KEY: SECRET },
{ fetchImpl: async () => new Response(SECRET, { status: 429 }) },
);
const malformed = await handleApiRequest(
routeRequest(),
{ ORS_API_KEY: SECRET },
{ fetchImpl: async () => upstreamJson({ features: [] }) },
);
const timeout = await handleApiRequest(
routeRequest(),
{ ORS_API_KEY: SECRET },
{
setTimeoutImpl: (callback, milliseconds) => {
assert.equal(milliseconds, 8_000);
callback();
return 42;
},
clearTimeoutImpl: () => undefined,
fetchImpl: async (_url, { signal }) => {
assert.equal(signal.aborted, true);
throw new DOMException(SECRET, "AbortError");
},
},
);
for (const response of [upstream, malformed]) {
await assertNoSecret(response);
assert.equal(response.status, 502);
assert.equal((await response.json()).error.code, "ROUTE_UPSTREAM_ERROR");
}
await assertNoSecret(timeout);
assert.equal(timeout.status, 504);
assert.equal((await timeout.json()).error.code, "ROUTE_TIMEOUT");
});
test("rejects oversized ORS direction responses", async () => {
let cancelled = false;
const body = new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array(1_200_000));
controller.enqueue(new Uint8Array(1_200_000));
},
cancel() {
cancelled = true;
},
});
const response = await handleApiRequest(
routeRequest(),
{ ORS_API_KEY: SECRET },
{ fetchImpl: async () => new Response(body, { status: 200 }) },
);
await assertNoSecret(response);
assert.equal(response.status, 502);
assert.equal((await response.json()).error.code, "ROUTE_TOO_LARGE");
assert.equal(cancelled, true);
});
test("leaves unsupported ORS methods to the API 404", async () => {
for (const request of [
new Request("https://example.test/api/routing-status", { method: "POST" }),
new Request("https://example.test/api/geocode?q=서울역", { method: "POST" }),
new Request("https://example.test/api/route"),
]) {
const response = await handleApiRequest(request, { ORS_API_KEY: SECRET });
assert.equal(response.status, 404);
assert.equal((await response.json()).error.code, "API_NOT_FOUND");
}
});