Skip to content

Commit b27e5b1

Browse files
committed
Fix 404 behaviour
1 parent 75d95b1 commit b27e5b1

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/http-handler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ function resolveEndpointChain(initialPath: string, hostnamePrefix: string | unde
1515

1616
while (path && entries.length <= MAX_CHAIN_DEPTH) {
1717
const endpoint = httpEndpoints.find(ep => ep.matchPath(path!, hostnamePrefix));
18-
if (!endpoint) break;
18+
if (!endpoint) {
19+
throw new StatusError(404, `Could not match endpoint for ${initialPath}`);
20+
}
1921

2022
entries.push({ endpoint, path });
2123
needsRawData ||= !!endpoint.needsRawData;

test/root-page.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as net from 'net';
2+
import { expect } from 'chai';
3+
import { DestroyableServer, makeDestroyable } from 'destroyable-server';
4+
5+
import { createServer } from '../src/server.js';
6+
7+
describe("Root page endpoint", () => {
8+
9+
let server: DestroyableServer;
10+
let serverPort: number;
11+
12+
beforeEach(async () => {
13+
server = makeDestroyable(await createServer());
14+
await new Promise<void>((resolve) => server.listen(resolve));
15+
serverPort = (server.address() as net.AddressInfo).port;
16+
});
17+
18+
afterEach(async () => {
19+
await server.destroy();
20+
});
21+
22+
it("redirects to the source for direct requests", async () => {
23+
const address = `http://localhost:${serverPort}/`;
24+
const response = await fetch(address, {
25+
redirect: 'manual'
26+
});
27+
28+
expect(response.status).to.equal(307);
29+
expect(response.headers.get('location')).to.equal('https://github.com/httptoolkit/testserver/');
30+
});
31+
32+
it("just returns a 404 for any other hostnames", async () => {
33+
const address = `http://http1.localhost:${serverPort}/`;
34+
const response = await fetch(address, {
35+
redirect: 'manual'
36+
});
37+
38+
expect(response.status).to.equal(404);
39+
expect(await response.text()).to.equal('Could not match endpoint for / (http1)');
40+
});
41+
42+
});

0 commit comments

Comments
 (0)