From bd041bfd6f6fad8c3e96ee10b6dcd84eb3051a76 Mon Sep 17 00:00:00 2001 From: Menno Pruijssers Date: Wed, 19 Oct 2016 16:10:10 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20stringify=20buffers=20in=20tran?= =?UTF-8?q?sports=20Due=20to=20changes=20in=20hammock,=20request-proxy=20i?= =?UTF-8?q?s=20now=20serializing=20buffers=20as=20json.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/middleware/transport.js | 2 +- test/integration/proxy_test.js | 25 +++++++++++++++++++++++++ test/unit/transport_test.js | 13 +++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/middleware/transport.js b/lib/middleware/transport.js index 78e421d3..f7ad4210 100644 --- a/lib/middleware/transport.js +++ b/lib/middleware/transport.js @@ -41,7 +41,7 @@ var transportServerMiddleware = { callback(arg2, arg3); }, 'response': function(req, err, res1, res2, callback) { - if (res2 !== undefined && typeof res2 !== 'string') { + if (res2 !== undefined && typeof res2 !== 'string' && !Buffer.isBuffer(res2)) { res2 = JSON.stringify(res2); } callback(err, res1, res2); diff --git a/test/integration/proxy_test.js b/test/integration/proxy_test.js index 02cb1ced..48730dda 100644 --- a/test/integration/proxy_test.js +++ b/test/integration/proxy_test.js @@ -911,6 +911,31 @@ test('can serialize response body', function t(assert) { }); }); +test('proxies buffer responses', function t(assert) { + var cluster = allocCluster({ + createHandler: function createHandler() { + return function handle(req, res) { + res.end(new Buffer('hello')); + }; + } + }, function onReady() { + cluster.request({ + host: 'one', key: cluster.keys.two + }, function onResponse(err, resp) { + assert.ifError(err); + + assert.equal(resp.statusCode, 200); + assert.equal( + resp.body.toString('hex'), + new Buffer('hello').toString('hex') + ); + + cluster.destroy(); + assert.end(); + }); + }); +}); + // new features test('can handle errors differently'); test('adds forwarding header'); diff --git a/test/unit/transport_test.js b/test/unit/transport_test.js index 9a51e733..7dcaaf7d 100644 --- a/test/unit/transport_test.js +++ b/test/unit/transport_test.js @@ -85,3 +85,16 @@ test('res2 is not JSON encoded if string', function t(assert) { assert.equals(res2, 'string'); }); }); + +test('buffer-res2 is not JSON encoded if string', function t(assert) { + assert.plan(3); + var req = {}; + var error = null; + var res1 = null; + var res2 = new Buffer('string'); + transportMiddleware.response(req, error, res1, res2, function(err, res1, res2) { + assert.equals(err, null); + assert.equals(res1, null); + assert.deepEquals(res2, new Buffer('string')); + }); +});