Skip to content

Commit

Permalink
check body length instead of 304 code to determine there is no body
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-arad committed Jan 9, 2019
1 parent bf44d2b commit e419334
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/steps/decorateUserRes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion test/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 25 additions & 18 deletions test/userResDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,45 @@ 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);
});
});
});

Expand Down

0 comments on commit e419334

Please sign in to comment.