Skip to content

Commit

Permalink
Support delete payload forwarding (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
geek authored Oct 16, 2022
1 parent de60f2e commit 3e695df
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const Wreck = require('@hapi/wreck');


const internals = {
NS_PER_SEC: 1e9
NS_PER_SEC: 1e9,
CHUNKABLE: ['delete']
};


Expand Down Expand Up @@ -154,6 +155,12 @@ internals.handler = function (route, handlerOptions) {
options.headers['content-type'] = contentType;
}


const encoding = options.headers['transfer-encoding'];
if (!encoding && options.payload && internals.CHUNKABLE.includes(request.method)) {
options.headers['transfer-encoding'] = 'chunked';
}

let ttl = null;

let downstreamStartTime;
Expand Down
124 changes: 124 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,37 @@ describe('h2o2', () => {
await upstream.stop();
});

it('does not clobber existing transfer-encoding header', async () => {

const echoDeleteBody = function (request, h) {

return h.response(request.raw.req.headers['transfer-encoding']);
};

const mapUri = function (request) {

return {
uri: `http://127.0.0.1:${upstream.info.port}${request.path}${(request.url.search || '')}`,
headers: { 'transfer-encoding': 'gzip,chunked' }
};
};

const upstream = Hapi.server();
upstream.route({ method: 'DELETE', path: '/echo', handler: echoDeleteBody });
await upstream.start();

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

server.route({ method: 'DELETE', path: '/echo', handler: { proxy: { mapUri } } });

const res = await server.inject({ url: '/echo', method: 'DELETE' });
expect(res.statusCode).to.equal(200);
expect(res.payload).to.equal('gzip,chunked');

await upstream.stop();
});

it('forwards on a POST body', async () => {

const echoPostBody = function (request, h) {
Expand Down Expand Up @@ -920,6 +951,99 @@ describe('h2o2', () => {
await upstream.stop();
});

it('forwards on a DELETE body', async () => {

const echoDeleteBody = function (request, h) {

return h.response(request.payload.echo + request.raw.req.headers['x-super-special']);
};

const mapUri = function (request) {

return {
uri: `http://127.0.0.1:${upstream.info.port}${request.path}${(request.url.search || '')}`,
headers: { 'x-super-special': '@' }
};
};

const upstream = Hapi.server();
upstream.route({ method: 'DELETE', path: '/echo', handler: echoDeleteBody });
await upstream.start();

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

server.route({ method: 'DELETE', path: '/echo', handler: { proxy: { mapUri } } });

const res = await server.inject({ url: '/echo', method: 'DELETE', payload: '{"echo":true}' });
expect(res.statusCode).to.equal(200);
expect(res.payload).to.equal('true@');

await upstream.stop();
});

it('forwards on a PUT body', async () => {

const echoPutBody = function (request, h) {

return h.response(request.payload.echo + request.raw.req.headers['x-super-special']);
};

const mapUri = function (request) {

return {
uri: `http://127.0.0.1:${upstream.info.port}${request.path}${(request.url.search || '')}`,
headers: { 'x-super-special': '@' }
};
};

const upstream = Hapi.server();
upstream.route({ method: 'PUT', path: '/echo', handler: echoPutBody });
await upstream.start();

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

server.route({ method: 'PUT', path: '/echo', handler: { proxy: { mapUri } } });

const res = await server.inject({ url: '/echo', method: 'PUT', payload: '{"echo":true}' });
expect(res.statusCode).to.equal(200);
expect(res.payload).to.equal('true@');

await upstream.stop();
});

it('forwards on a PATCH body', async () => {

const echoPatchBody = function (request, h) {

return h.response(request.payload.echo + request.raw.req.headers['x-super-special']);
};

const mapUri = function (request) {

return {
uri: `http://127.0.0.1:${upstream.info.port}${request.path}${(request.url.search || '')}`,
headers: { 'x-super-special': '@' }
};
};

const upstream = Hapi.server();
upstream.route({ method: 'PATCH', path: '/echo', handler: echoPatchBody });
await upstream.start();

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

server.route({ method: 'PATCH', path: '/echo', handler: { proxy: { mapUri } } });

const res = await server.inject({ url: '/echo', method: 'PATCH', payload: '{"echo":true}' });
expect(res.statusCode).to.equal(200);
expect(res.payload).to.equal('true@');

await upstream.stop();
});

it('replies with an error when it occurs in mapUri', async () => {

const mapUriWithError = function (request) {
Expand Down

0 comments on commit 3e695df

Please sign in to comment.