Merge remote-tracking branch 'origin/main' into fix/places-no-store-on-errors

This commit is contained in:
German Khokhlov committed 2026-11-19 18:29:27 +04:00
commit 19542baf61
3271 files changed
+457829 -51689

No files matched your search

+125 -13
View File
@@ -11,7 +11,11 @@ import {
_routeInflightCountForTest,
_setRouteUpstreamIntervalForTest,
} from '../../server/providers/places/routes.js';
import { ROUTE_STEPS_MAX } from '../../src/data/routeSteps.js';
import {
ROUTE_STEPS_MAX,
normalizeOsrmSteps,
} from '../../src/data/routeSteps.js';
import * as placePayloads from '../../src/data/placeProviderPayloads.js';
import {
projectNearbyPlaces,
projectTextSearchPlaces,
@@ -331,23 +335,108 @@ test('OSRM routing preserves aliases, cache, span guards and upstream failure be
assert.equal((await request(route, url)).body.error, error);
assert.equal(calls, 1);
now += 600001;
t.mock.method(
globalThis,
'fetch',
async () => new Response('offline', { status: 503 }),
t.mock.method(globalThis, 'fetch', async () =>
Response.json(
{ code: 'NoRoute', message: 'Impossible route between points' },
{ status: 400 },
),
);
assert.deepEqual((await request(route, query)).body, {
ok: false,
error: 'no route found',
});
t.mock.method(
globalThis,
'fetch',
async () => new Response('x', { headers: { 'content-type': 'text/html' } }),
const noRoute = await request(route, query);
assert.equal(noRoute.statusCode, 200);
assert.deepEqual(noRoute.body, { ok: false, error: 'no route found' });
t.mock.method(globalThis, 'fetch', async () =>
Response.json({ code: 'NoRoute', routes: [] }),
);
assert.equal((await request(route, query)).body.error, 'no route found');
});
test('a routing outage is reported as one, not as "no route found"', async (t) => {
const route = '/api/route';
const query = '?profile=car&coords=-97,30;-97.01,30.01';
for (const response of [
() => new Response('offline', { status: 503 }),
() => new Response('blocked', { status: 403 }),
() => new Response('x', { headers: { 'content-type': 'text/html' } }),
() =>
new Response('<html>', {
headers: { 'content-type': 'application/json' },
}),
() => Promise.reject(new TypeError('fetch failed')),
() => Promise.reject(new DOMException('aborted', 'AbortError')),
]) {
_resetRouteUpstreamForTest();
const request = install(installRouteMiddleware);
t.mock.method(globalThis, 'fetch', async () => response());
const result = await request(route, query);
assert.equal(result.statusCode, 502);
assert.deepEqual(result.body, {
ok: false,
error: 'routing service unavailable',
});
}
});
test('callers sharing one failed upstream call all hear the outage, and it is not cached', async (t) => {
_resetRouteUpstreamForTest();
_setRouteUpstreamIntervalForTest(0);
t.after(() => _resetRouteUpstreamForTest());
const request = install(installRouteMiddleware);
let calls = 0;
let release;
const gate = new Promise((resolve) => {
release = resolve;
});
t.mock.method(globalThis, 'fetch', async () => {
calls += 1;
await gate;
return new Response('offline', { status: 503 });
});
const route = '/api/route';
const query = '?profile=car&coords=-97,30;-97.01,30.01';
const all = [
request(route, `${query}&steps=1`),
request(route, `${query}&steps=1`),
request(route, query),
];
await Promise.resolve();
release();
for (const result of await Promise.all(all)) {
assert.equal(result.statusCode, 502);
assert.equal(result.body.error, 'routing service unavailable');
}
assert.equal(calls, 1);
assert.equal((await request(route, query)).statusCode, 502);
assert.equal(calls, 2, 'the next request asks the routing service again');
});
test('a budget spent in the outbound queue is reported as busy, not as an outage', async (t) => {
_resetRouteUpstreamForTest();
// The second request waits 13 s for its slot, past the 12 s budget.
_setRouteUpstreamIntervalForTest(13_000);
t.after(() => _resetRouteUpstreamForTest());
t.mock.timers.enable({ apis: ['setTimeout', 'Date'], now: Date.now() });
const request = install(installRouteMiddleware);
let calls = 0;
t.mock.method(globalThis, 'fetch', async () => {
calls += 1;
return Response.json(osrmRouteWithSteps(2));
});
const first = request(
'/api/route',
'?profile=car&coords=-97,30;-97.01,30.01',
);
const second = request(
'/api/route',
'?profile=foot&coords=-97,30;-97.01,30.01',
);
assert.equal((await first).body.ok, true);
t.mock.timers.tick(13_000);
const result = await second;
assert.equal(result.statusCode, 429);
assert.match(result.body.error, /routing busy/);
assert.equal(calls, 1, 'the spent request never reaches the routing service');
});
/** One OSRM route with `count` maneuvers, for the step tests. */
function osrmRouteWithSteps(count) {
return {
@@ -475,6 +564,10 @@ test('identical concurrent route requests make one upstream call', async (t) =>
);
});
test('the place payloads entry serves the same route steps the dev proxy does', () => {
assert.equal(placePayloads.normalizeOsrmSteps, normalizeOsrmSteps);
});
test('a pathological maneuver count is bounded before it reaches the browser', async (t) => {
_resetRouteUpstreamForTest();
const request = install(installRouteMiddleware);
@@ -596,6 +689,25 @@ test('the upstream host is pinned: a redirect is refused, not followed', async (
assert.deepEqual(seen, ['error'], 'a redirect would escape the pinned host');
});
test('the routing proxy declines a non-GET before rate limiting or upstream', async (t) => {
// A fetch that throws proves the refusal comes before any upstream call.
const request = install(installRouteMiddleware);
t.mock.method(globalThis, 'fetch', async () => {
throw new Error('upstream must not be reached');
});
for (const method of ['POST', 'PUT', 'DELETE', 'PATCH']) {
const response = await request(
'/api/route',
'?profile=car&coords=-97,30;-97.01,30.01',
method,
);
assert.equal(response.statusCode, 405, method);
// RFC 9110 section 15.5.6: a 405 names the methods it accepts.
assert.equal(response.headers.allow, 'GET', method);
assert.deepEqual(response.body, { error: 'Method Not Allowed' });
}
});
test('both paid Google routes refuse cross-site requests before calling Google', async (t) => {
const plugin = googlePlacesContextProxy({ resolveApiKey: () => 'key' });
const routes = new Map();