From bea40f1683d93bebfe4e96e012815472f17dcb74 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 13 May 2013 16:15:16 -0700 Subject: [PATCH] cache bug and 100% --- lib/client.js | 2 +- lib/proxy.js | 5 ++-- test/integration/proxy.js | 51 ++++++++++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/lib/client.js b/lib/client.js index 3651b52be..0e5e2b8eb 100755 --- a/lib/client.js +++ b/lib/client.js @@ -139,7 +139,7 @@ exports.parse = function (res, callback) { res.once('error', function (err) { - return finish(Boom.internal('Proxy response error', err)); + return finish(Boom.internal('Client response error', err)); }); res.once('close', function () { diff --git a/lib/proxy.js b/lib/proxy.js index c260812cc..539ea7ab6 100755 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -103,7 +103,7 @@ internals.Proxy.prototype.handler = function () { return request.reply(err); } - return self.settings.postResponse(request, self.settings, res, buffer); + return self.settings.postResponse(request, self.settings, res, buffer.toString()); }); }); }); @@ -136,4 +136,5 @@ internals.postResponse = function (request, settings, res, payload) { if (contentType) { response.type(contentType); } -}; \ No newline at end of file +}; + diff --git a/test/integration/proxy.js b/test/integration/proxy.js index 0ac49f7c5..06c78e963 100755 --- a/test/integration/proxy.js +++ b/test/integration/proxy.js @@ -5,6 +5,7 @@ var Fs = require('fs'); var Zlib = require('zlib'); var Request = require('request'); var Hapi = require('../..'); +var Client = require('../../lib/client'); // Declare internals @@ -39,26 +40,28 @@ describe('Proxy', function () { } var profile = { - 'id': 'fa0dbda9b1b', - 'name': 'John Doe' + id: 'fa0dbda9b1b', + name: 'John Doe' }; this.reply(profile).state('test', '123'); }; + var activeCount = 0; var activeItem = function () { this.reply({ - 'id': '55cf687663', - 'name': 'Active Item' + id: '55cf687663', + name: 'Active Item', + count: activeCount++ }); }; var item = function () { this.reply({ - 'id': '55cf687663', - 'name': 'Item' + id: '55cf687663', + name: 'Item' }).created('http://example.com'); }; @@ -134,7 +137,8 @@ describe('Proxy', function () { { method: 'GET', path: '/gzipstream', handler: gzipStreamHandler }, { method: 'GET', path: '/redirect', handler: redirectHandler }, { method: 'POST', path: '/post1', handler: function () { this.reply.redirect('/post2').rewritable(false); } }, - { method: 'POST', path: '/post2', handler: function () { this.reply(this.payload); } } + { method: 'POST', path: '/post2', handler: function () { this.reply(this.payload); } }, + { method: 'GET', path: '/cached', handler: profile } ]); var mapUri = function (request, callback) { @@ -169,7 +173,8 @@ describe('Proxy', function () { { method: 'GET', path: '/googler', handler: { proxy: { mapUri: function (request, callback) { callback(null, 'http://google.com'); }, redirects: 1 } } }, { method: 'GET', path: '/redirect', handler: { proxy: { host: 'localhost', port: backendPort, passThrough: true, redirects: 2 } } }, { method: 'POST', path: '/post1', handler: { proxy: { host: 'localhost', port: backendPort, redirects: 3 } }, config: { payload: 'stream' } }, - { method: 'GET', path: '/nowhere', handler: { proxy: { host: 'no.such.domain.x8' } } } + { method: 'GET', path: '/nowhere', handler: { proxy: { host: 'no.such.domain.x8' } } }, + { method: 'GET', path: '/cached', handler: { proxy: { host: 'localhost', port: backendPort } }, config: { cache: routeCache } } ]); server.state('auto', { autoValue: 'xyz' }); @@ -296,13 +301,20 @@ describe('Proxy', function () { }); }); - it('forwards on the response when making a GET request to a route that also accepts a POST', function (done) { + it('request a cached proxy route', function (done) { server.inject('/item', function (res) { expect(res.statusCode).to.equal(200); expect(res.payload).to.contain('Active Item'); - done(); + var counter = res.result.count; + + server.inject('/item', function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.result.count).to.equal(counter); + done(); + }); }); }); @@ -438,4 +450,21 @@ describe('Proxy', function () { done(); }); }); -}); \ No newline at end of file + + it('errors on invalid response stream', function (done) { + + var orig = Client.parse; + Client.parse = function (res, callback) { + + Client.parse = orig; + callback(Hapi.error.internal('Fake error')); + }; + + server.inject('/cached', function (res) { + + expect(res.statusCode).to.equal(500); + done(); + }); + }); +}); +