Skip to content

Commit e419334

Browse files
author
amir-arad
committed
check body length instead of 304 code to determine there is no body
1 parent bf44d2b commit e419334

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

app/steps/decorateUserRes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ function decorateProxyResBody(container) {
4545
var req = container.user.req;
4646
var res = container.user.res;
4747

48-
if (res.statusCode === 304) {
49-
debug('Skipping userResDecorator on response 304');
48+
if (proxyResData.length === 0) {
49+
debug('Skipping userResDecorator on response with empty proxyResData');
5050
return Promise.resolve(container);
5151
}
5252

test/status.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('proxies status code', function () {
2121
server.close();
2222
});
2323

24-
[304, 404, 200, 401, 500].forEach(function (status) {
24+
[301, 302, 304, 404, 200, 401, 500].forEach(function (status) {
2525
it('on ' + status, function (done) {
2626
request(proxyServer)
2727
.get('/status/' + status)

test/userResDecorator.js

+25-18
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,45 @@ var assert = require('assert');
44
var express = require('express');
55
var request = require('supertest');
66
var proxy = require('../');
7+
var http = require('http');
78

89
describe('userResDecorator', function () {
910

10-
describe('when handling a 304', function () {
11+
describe('when handling no body', function () {
1112
this.timeout(10000);
1213

1314
var app;
14-
var slowTarget;
15-
var serverReference;
15+
var noBodyTarget;
16+
var serverReference;
17+
var responseCode;
1618

1719
beforeEach(function () {
1820
app = express();
19-
slowTarget = express();
20-
slowTarget.use(function (req, res) { res.sendStatus(304); });
21-
serverReference = slowTarget.listen(12345);
21+
noBodyTarget = new http.Server();
22+
noBodyTarget.on('request', function (req, res) {
23+
res.writeHead(responseCode, { 'Content-Length': '0' });
24+
res.end();
25+
});
26+
serverReference = noBodyTarget.listen(12345);
2227
});
2328

2429
afterEach(function () {
2530
serverReference.close();
2631
});
27-
28-
it('skips any handling', function (done) {
29-
app.use('/proxy', proxy('http://127.0.0.1:12345', {
30-
userResDecorator: function (/*res*/) {
31-
throw new Error('expected to never get here because this step should be skipped for 304');
32-
}
33-
}));
34-
35-
request(app)
36-
.get('/proxy')
37-
.expect(304)
38-
.end(done);
32+
[200, 201, 204, 205, 301, 302, 304, 400, 500].forEach(function (status) {
33+
it('skips any handling for ' + status, function (done) {
34+
responseCode = status;
35+
app.use('/proxy', proxy('http://127.0.0.1:12345', {
36+
userResDecorator: function (/*res*/) {
37+
throw new Error('expected to never get here because this step should be skipped for ' + status + ' with no body');
38+
}
39+
}));
40+
41+
request(app)
42+
.get('/proxy')
43+
.expect(status)
44+
.end(done);
45+
});
3946
});
4047
});
4148

0 commit comments

Comments
 (0)