Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: forward 405 Allow header #138

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,17 @@ internals.handler = function (route, handlerOptions) {
return settings.onResponse.call(bind, null, res, request, h, settings, ttl);
}

return h.response(res)
const response = h.response(res)
.ttl(ttl)
.code(res.statusCode)
.passThrough(!!settings.passThrough);

if (!settings.passThrough && res.statusCode === 405 && 'allow' in res.headers) {
response.header('allow', res.headers.allow);
}

return response;

};
};

Expand Down
84 changes: 84 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2215,4 +2215,88 @@ describe('h2o2', () => {
await server.stop();
await upstream.stop();
});

it('propagates the allow header in case of 405 (with passthrough)', async () => {

const upstream = Hapi.server();
upstream.route({
method: 'POST',
path: '/item',
handler(request, h) {

throw Boom.methodNotAllowed('Not allowed', {}, ['GET']);
}
});

await upstream.start();

const server = Hapi.server();
await server.register(H2o2);

server.route({ method: 'POST', path: '/item', handler: { proxy: { host: upstream.info.address, port: upstream.info.port, passThrough: true } } });
await server.start();

const res = await server.inject({ method: 'POST', url: '/item' });
expect(res.statusCode).to.equal(405);
expect(res.headers.allow).to.equal('GET');

await server.stop();
await upstream.stop();
});

it('propagates the allow header in case of 405 (without passthrough)', async () => {

const upstream = Hapi.server();
upstream.route({
method: 'POST',
path: '/item',
handler(request, h) {

throw Boom.methodNotAllowed('Not allowed', {}, ['GET']);
}
});

await upstream.start();

const server = Hapi.server();
await server.register(H2o2);

server.route({ method: 'POST', path: '/item', handler: { proxy: { host: upstream.info.address, port: upstream.info.port } } });
await server.start();

const res = await server.inject({ method: 'POST', url: '/item' });
expect(res.statusCode).to.equal(405);
expect(res.headers.allow).to.equal('GET');

await server.stop();
await upstream.stop();
});

it('does not propagate a missing allow header in case of malformed 405', async () => {

const upstream = Hapi.server();
upstream.route({
method: 'POST',
path: '/item',
handler(request, h) {

throw Boom.methodNotAllowed('Not allowed');
}
});

await upstream.start();

const server = Hapi.server();
await server.register(H2o2);

server.route({ method: 'POST', path: '/item', handler: { proxy: { host: upstream.info.address, port: upstream.info.port } } });
await server.start();

const res = await server.inject({ method: 'POST', url: '/item' });
expect(res.statusCode).to.equal(405);
expect(res.headers.allow).to.not.exist();

await server.stop();
await upstream.stop();
});
});