Skip to content

Commit 819b383

Browse files
committed
Stop Node from blocking & hiding unexpected 'expect' requests
1 parent 75dc38c commit 819b383

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/server/http-combo-server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,12 @@ export async function createComboServer(options: ComboServerOptions): Promise<De
322322
// our case, we want to handle the garbage requests too, so we disable it:
323323
(server as any)._httpServer.requireHostHeader = false;
324324

325+
// Disable auto 417 on unknown expect, and map this back to
326+
// normal request behaviour instead.
327+
(server as any)._httpServer.on('checkExpectation', (req: http.IncomingMessage, res: http.ServerResponse) => {
328+
(server as any)._httpServer.emit('request', req, res);
329+
});
330+
325331
server.on('connection', (socket: net.Socket | http2.ServerHttp2Stream) => {
326332
socket[SocketTimingInfo] ||= buildSocketTimingInfo();
327333

test/integration/handlers/informational-response.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,38 @@ describe("Informational response steps", () => {
256256
});
257257
});
258258

259+
describe("with non-standard Expect headers", () => {
260+
261+
const server = getLocal();
262+
263+
beforeEach(() => server.start());
264+
afterEach(() => server.stop());
265+
266+
it("routes a non-standard Expect through to the matching rule", async () => {
267+
const endpoint = await server.forGet('/x').thenReply(200, 'rule ran');
268+
269+
const sock = await openRawSocket(server);
270+
const raw = await new Promise<string>((resolve, reject) => {
271+
let buf = '';
272+
sock.on('data', d => { buf += d.toString('utf8'); });
273+
sock.on('end', () => resolve(buf));
274+
sock.on('error', reject);
275+
sock.write(
276+
`GET /x HTTP/1.1\r\nHost: localhost\r\n` +
277+
`Expect: x-weird-thing\r\nConnection: close\r\n\r\n`
278+
);
279+
});
280+
281+
expect(raw).to.contain('HTTP/1.1 200');
282+
expect(raw).to.contain('rule ran');
283+
expect(raw).not.to.contain('417');
284+
285+
const seen = await endpoint.getSeenRequests();
286+
expect(seen.length).to.equal(1);
287+
expect(seen[0].headers['expect']).to.equal('x-weird-thing');
288+
});
289+
});
290+
259291
describe("over HTTP/2", () => {
260292

261293
if (nodeSatisfies(BROKEN_H2_OVER_H2_TUNNELLING)) return;

0 commit comments

Comments
 (0)