From fd05b32f04eb9c20f0a8daddcf7d71c530d85c36 Mon Sep 17 00:00:00 2001 From: Nik 'Fire Eater' Krimm Date: Thu, 7 Mar 2019 09:58:21 -0600 Subject: [PATCH] [#399]: Clarifies behavior on empty response body. --- README.md | 19 +++++++++++++------ test/userResDecorator.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 711f9da3..72b6016f 100644 --- a/README.md +++ b/README.md @@ -142,25 +142,32 @@ Promise form: ```js app.use(proxy('localhost:12346', { - filter: function (req, res) { - return new Promise(function (resolve) { + filter: function (req, res) { + return new Promise(function (resolve) { resolve(req.method === 'GET'); - }); + }); } })); ``` Note that in the previous example, `resolve(false)` will execute the happy path for filter here (skipping the rest of the proxy, and calling `next()`). -`reject()` will also skip the rest of proxy and call `next()`. +`reject()` will also skip the rest of proxy and call `next()`. #### userResDecorator (was: intercept) (supports Promise) You can modify the proxy's response before sending it to the client. +Note: `proxyResData` will be an empty buffer when the proxied response is empty. + ```js app.use('/proxy', proxy('www.google.com', { userResDecorator: function(proxyRes, proxyResData, userReq, userRes) { + if (proxyResData.length == 0) { + // body is empty, don't try to parse it + // return alternate document instead. + return JSON.stringify({}); + } data = JSON.parse(proxyResData.toString('utf8')); data.newProperty = 'exciting data'; return JSON.stringify(data); @@ -562,9 +569,9 @@ app.use('/', proxy('internalhost.example.com', { | --- | --- | | 1.5.1 | Fixes bug in stringifying debug messages. | | 1.5.0 | Fixes bug in `filter` signature. Fix bug in skipToNextHandler, add expressHttpProxy value to user res when skipped. Add tests for host as ip address. | -| 1.4.0 | DEPRECATED. Critical bug in the `filter` api.| +| 1.4.0 | DEPRECATED. Critical bug in the `filter` api.| | 1.3.0 | DEPRECATED. Critical bug in the `filter` api. `filter` now supports Promises. Update linter to eslint. | -| 1.2.0 | Auto-stream when no decorations are made to req/res. Improved docs, fixes issues in maybeSkipToNexthandler, allow authors to manage error handling. | +| 1.2.0 | Auto-stream when no decorations are made to req/res. Improved docs, fixes issues in maybeSkipToNexthandler, allow authors to manage error handling. | | 1.1.0 | Add step to allow response headers to be modified. | 1.0.7 | Update dependencies. Improve docs on promise rejection. Fix promise rejection on body limit. Improve debug output. | | 1.0.6 | Fixes preserveHostHdr not working, skip userResDecorator on 304, add maybeSkipToNext, test improvements and cleanup. | diff --git a/test/userResDecorator.js b/test/userResDecorator.js index 150b6a3e..0332b63e 100644 --- a/test/userResDecorator.js +++ b/test/userResDecorator.js @@ -4,6 +4,7 @@ var assert = require('assert'); var express = require('express'); var request = require('supertest'); var proxy = require('../'); +var http = require('http'); describe('userResDecorator', function () { @@ -39,6 +40,44 @@ describe('userResDecorator', function () { }); }); + describe('when handling a response with no body', function () { + this.timeout(10000); + + var app; + var noBodyTarget; + var serverReference; + var responseCode = 200; + + beforeEach(function () { + app = express(); + noBodyTarget = new http.Server(); + noBodyTarget.on('request', function (req, res) { + res.writeHead(responseCode, { 'Content-Length': '0' }); + res.end(); + }); + serverReference = noBodyTarget.listen(12346); + }); + + afterEach(function () { + serverReference.close(); + }); + + + it('returns an empty Buffer for the proxyResData', function (done) { + app.use('/proxy', proxy('http://127.0.0.1:12346', { + userResDecorator: function (proxyRes, proxyResData /*, userReq, userRes */) { + assert(Buffer.isBuffer(proxyResData)); + assert(proxyResData.length === 0); + } + })); + + request(app) + .get('/proxy') + .expect(200) + .end(done); + }); + }); + it('has access to original response', function (done) { var app = express(); app.use(proxy('httpbin.org', {