diff --git a/app/steps/decorateUserRes.js b/app/steps/decorateUserRes.js index 87874699..9f4efd34 100644 --- a/app/steps/decorateUserRes.js +++ b/app/steps/decorateUserRes.js @@ -45,8 +45,8 @@ function decorateProxyResBody(container) { var req = container.user.req; var res = container.user.res; - if (res.statusCode === 304) { - debug('Skipping userResDecorator on response 304'); + if (proxyResData.length === 0) { + debug('Skipping userResDecorator on response with empty proxyResData'); return Promise.resolve(container); } diff --git a/test/status.js b/test/status.js index 872d64cf..06d233cc 100644 --- a/test/status.js +++ b/test/status.js @@ -21,7 +21,7 @@ describe('proxies status code', function () { server.close(); }); - [304, 404, 200, 401, 500].forEach(function (status) { + [301, 302, 304, 404, 200, 401, 500].forEach(function (status) { it('on ' + status, function (done) { request(proxyServer) .get('/status/' + status) diff --git a/test/userResDecorator.js b/test/userResDecorator.js index 150b6a3e..77ea14af 100644 --- a/test/userResDecorator.js +++ b/test/userResDecorator.js @@ -4,38 +4,46 @@ var assert = require('assert'); var express = require('express'); var request = require('supertest'); var proxy = require('../'); +var http = require('http'); describe('userResDecorator', function () { - describe('when handling a 304', function () { + describe('when handling no body', function () { this.timeout(10000); var app; - var slowTarget; - var serverReference; + var noBodyTarget; + var serverReference; + var responseCode; beforeEach(function () { app = express(); - slowTarget = express(); - slowTarget.use(function (req, res) { res.sendStatus(304); }); - serverReference = slowTarget.listen(12345); + noBodyTarget = new http.Server(); + noBodyTarget.on('request', function (req, res) { + res.writeHead(responseCode, { 'Content-Length': '0' }); + res.end(); + }); + serverReference = noBodyTarget.listen(12345); }); afterEach(function () { serverReference.close(); }); - - it('skips any handling', function (done) { - app.use('/proxy', proxy('http://127.0.0.1:12345', { - userResDecorator: function (/*res*/) { - throw new Error('expected to never get here because this step should be skipped for 304'); - } - })); - - request(app) - .get('/proxy') - .expect(304) - .end(done); + [200, 201, 204, 205, 301, 302, 304, 400, 500].forEach(function (status) { + it('skips any handling for ' + status, function (done) { + responseCode = status; + app.use('/proxy', proxy('http://127.0.0.1:12345', { + userResDecorator: function (/*res*/) { + throw new Error('expected to never get here because this step should be skipped for ' + + status + ' with no body'); + } + })); + + request(app) + .get('/proxy') + .expect(status) + .end(done); + }); }); });