diff --git a/API.md b/API.md index 19bb722..945d2f2 100755 --- a/API.md +++ b/API.md @@ -47,6 +47,7 @@ The proxy handler object has the following properties: * `request` - is the incoming [request object](http://hapijs.com/api#request-object). The response from this function should be an object with the following properties: * `uri` - the absolute proxy URI. * `headers` - optional object where each key is an HTTP request header and the value is the header content. + * `method` - optional HTTP method to use when making request to upstream server. * `onRequest` - a custom function which is passed the upstream request. Function signature is `function (req)` where: * `req` - the [wreck] (https://github.com/hapijs/wreck) request to the upstream server. * `onResponse` - a custom function for processing the response from the upstream service before sending to the client. Useful for custom error handling of responses from the proxied endpoint or other payload manipulation. Function signature is `function (err, res, request, h, settings, ttl)` where: diff --git a/lib/index.js b/lib/index.js index 02631e4..735b5d9 100755 --- a/lib/index.js +++ b/lib/index.js @@ -92,7 +92,7 @@ internals.handler = function (route, handlerOptions) { return async function (request, h) { - const { uri, headers } = await settings.mapUri(request); + let { uri, headers, method } = await settings.mapUri(request); const protocol = uri.split(':', 1)[0]; @@ -168,7 +168,8 @@ internals.handler = function (route, handlerOptions) { downstreamStartTime = process.hrtime.bigint(); } - const promise = settings.httpClient.request(request.method, uri, options); + method = method || request.method; + const promise = settings.httpClient.request(method, uri, options); request.events.once('disconnect', () => { diff --git a/test/index.js b/test/index.js index 1f78914..3c0c69e 100755 --- a/test/index.js +++ b/test/index.js @@ -1044,6 +1044,38 @@ describe('h2o2', () => { await upstream.stop(); }); + it('uses the method returned from mapUri', 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': '@' }, + method: 'PUT' + }; + }; + + 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: 'POST', path: '/echo', handler: { proxy: { mapUri } } }); + + const res = await server.inject({ url: '/echo', method: 'POST', 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) {