From f7d4573949e4bd6f5552b943ff6b4bc3cb796d0a Mon Sep 17 00:00:00 2001 From: Wyatt Preul Date: Wed, 31 Oct 2012 16:19:26 -0500 Subject: [PATCH] Fixing issue with error responses being cached + test --- lib/request.js | 2 +- test/integration/cache.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/request.js b/lib/request.js index aae6efde7..ae2e2ba1d 100755 --- a/lib/request.js +++ b/lib/request.js @@ -448,7 +448,7 @@ internals.Request.prototype._generateResponse = function () { // Check for Error result - if (response instanceof Error) { + if (response.result instanceof Error) { self.log(['handler', 'result', 'error'], { msec: timer.elapsed() }); return callback(response); } diff --git a/test/integration/cache.js b/test/integration/cache.js index cf0fe293f..53e6800da 100755 --- a/test/integration/cache.js +++ b/test/integration/cache.js @@ -30,6 +30,14 @@ describe('Cache', function() { request.reply.stream(new Stream); }; + var errorHandler = function (request) { + + var error = new Error('myerror'); + error.code = 500; + + request.reply(error); + }; + function setupServer(done) { _server = new Hapi.Server('0.0.0.0', 18085, { cache: 'memory' }); _server.addRoutes([ @@ -37,7 +45,8 @@ describe('Cache', function() { { method: 'GET', path: '/item', config: { handler: activeItemHandler, cache: { mode: 'client', expiresIn: 120000 } } }, { method: 'GET', path: '/item2', config: { handler: activeItemHandler, cache: { mode: 'none' } } }, { method: 'GET', path: '/item3', config: { handler: activeItemHandler, cache: { mode: 'client', expiresIn: 120000 } } }, - { method: 'GET', path: '/bad', config: { handler: badHandler, cache: { expiresIn: 120000 } } } + { method: 'GET', path: '/bad', config: { handler: badHandler, cache: { expiresIn: 120000 } } }, + { method: 'GET', path: '/error', config: { handler: errorHandler, cache: { expiresIn: 120000 } } } ]); _server.listener.on('listening', function() { done(); @@ -104,4 +113,16 @@ describe('Cache', function() { expect(test).to.throw(Error); done(); }); + + it('doesn\'t cache error responses', function(done) { + + makeRequest('/error', function() { + + _server.cache.get({ segment: '/error', id: '/error' }, function(err, cached) { + + expect(cached).to.not.exist; + done(); + }); + }); + }); }); \ No newline at end of file