Skip to content

Commit 0f1a486

Browse files
committed
[#545] Fixes => content-length request header is removed when parseReqBody is false.
1 parent b05cb04 commit 0f1a486

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ app.use('/', proxy('internalhost.example.com', {
592592
593593
| Release | Notes |
594594
| --- | --- |
595+
| next | Fixes => content-length request header is removed when parseReqBody is false (#549) |
595596
| 2.1.1 | (trivial) Fixes formatting in README.|
596597
| 2.1.0 | Fixes parsing error in content-types. Improves behavior of proxyReqBodyDecorator when parseReqBody=false. Repairs issue where authors can't use proxy() twice in Express middleware stack. Fix `new Buffer` deprecation warning. |
597598
| 2.0.0 | Update all dependencies; set stage for next iteration. `express-http-proxy` interface has not changed, but the underlying libraries are not guaranteed to be backward compatible. Versions beyond this point are expected to be run in node verions >= 16. |

lib/requestOptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function reqHeaders(req, options) {
5454

5555
var headers = options.headers || {};
5656

57-
var skipHdrs = [ 'connection', 'content-length' ];
57+
var skipHdrs = [ 'connection' ];
5858
if (!options.preserveHostHdr) {
5959
skipHdrs.push('host');
6060
}

test/setsContentLength.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict';
2+
var assert = require('assert');
3+
var express = require('express');
4+
var request = require('supertest');
5+
var fs = require('fs');
6+
var os = require('os');
7+
var proxy = require('../');
8+
var startProxyTarget = require('./support/proxyTarget');
9+
10+
describe('headers: [content-length]', function () {
11+
var server;
12+
13+
before(function () {
14+
server = startProxyTarget(8109, 1000);
15+
});
16+
17+
after(function () {
18+
server.close();
19+
});
20+
21+
this.timeout(10000);
22+
23+
describe('on proxy request', function () {
24+
it('a `GET` request should have a content-length header, and it should be 0', function (done) {
25+
var app = express();
26+
app.use(proxy('localhost:8109'));
27+
request(app)
28+
.get('/headers')
29+
.end(function (err, res) {
30+
if (err) { throw err; }
31+
assert(res.body.headers['content-length']);
32+
const contentLength = res.body.headers['content-length'];
33+
assert(Number(contentLength) === 0);
34+
done(err);
35+
});
36+
});
37+
38+
describe('a `POST` request should have a content-length header, and it should be accurate', function () {
39+
it('when the author does not modify the proxyRequest', function(done) {
40+
var app = express();
41+
app.use(proxy('localhost:8109'));
42+
43+
request(app)
44+
.post('/headers')
45+
.send({
46+
data: 'random string of words'
47+
})
48+
.set('Content-Type', 'application/json')
49+
.set('Accept', 'application/json')
50+
.end(function (err, res) {
51+
if (err) { throw err; }
52+
assert(res.body.headers['content-length']);
53+
const contentLength = res.body.headers['content-length'];
54+
assert.equal(contentLength, 33);
55+
done(err);
56+
});
57+
});
58+
59+
it('when using parseReqBody as false', function (done) {
60+
var app = express();
61+
app.use(proxy('localhost:8109', {
62+
parseReqBody: false
63+
}));
64+
65+
request(app)
66+
.post('/headers')
67+
.send({
68+
data: 'random string of words'
69+
})
70+
.set('Content-Type', 'application/json')
71+
.set('Accept', 'application/json')
72+
.end(function (err, res) {
73+
if (err) { throw err; }
74+
assert(res.body.headers['content-length']);
75+
const contentLength = res.body.headers['content-length'];
76+
assert.equal(contentLength, 33);
77+
done(err);
78+
});
79+
});
80+
});
81+
});
82+
83+
84+
});

0 commit comments

Comments
 (0)