Skip to content

Commit

Permalink
[#545] Fixes => content-length request header is removed when parse…
Browse files Browse the repository at this point in the history
…ReqBody is false.
  • Loading branch information
monkpow committed Aug 20, 2024
1 parent b05cb04 commit 0f1a486
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ app.use('/', proxy('internalhost.example.com', {
| Release | Notes |
| --- | --- |
| next | Fixes => content-length request header is removed when parseReqBody is false (#549) |
| 2.1.1 | (trivial) Fixes formatting in README.|
| 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. |
| 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. |
Expand Down
2 changes: 1 addition & 1 deletion lib/requestOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function reqHeaders(req, options) {

var headers = options.headers || {};

var skipHdrs = [ 'connection', 'content-length' ];
var skipHdrs = [ 'connection' ];
if (!options.preserveHostHdr) {
skipHdrs.push('host');
}
Expand Down
84 changes: 84 additions & 0 deletions test/setsContentLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';
var assert = require('assert');
var express = require('express');
var request = require('supertest');
var fs = require('fs');
var os = require('os');
var proxy = require('../');
var startProxyTarget = require('./support/proxyTarget');

describe('headers: [content-length]', function () {
var server;

before(function () {
server = startProxyTarget(8109, 1000);
});

after(function () {
server.close();
});

this.timeout(10000);

describe('on proxy request', function () {
it('a `GET` request should have a content-length header, and it should be 0', function (done) {
var app = express();
app.use(proxy('localhost:8109'));
request(app)
.get('/headers')
.end(function (err, res) {
if (err) { throw err; }
assert(res.body.headers['content-length']);
const contentLength = res.body.headers['content-length'];
assert(Number(contentLength) === 0);
done(err);
});
});

describe('a `POST` request should have a content-length header, and it should be accurate', function () {
it('when the author does not modify the proxyRequest', function(done) {
var app = express();
app.use(proxy('localhost:8109'));

request(app)
.post('/headers')
.send({
data: 'random string of words'
})
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.end(function (err, res) {
if (err) { throw err; }
assert(res.body.headers['content-length']);
const contentLength = res.body.headers['content-length'];
assert.equal(contentLength, 33);
done(err);
});
});

it('when using parseReqBody as false', function (done) {
var app = express();
app.use(proxy('localhost:8109', {
parseReqBody: false
}));

request(app)
.post('/headers')
.send({
data: 'random string of words'
})
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.end(function (err, res) {
if (err) { throw err; }
assert(res.body.headers['content-length']);
const contentLength = res.body.headers['content-length'];
assert.equal(contentLength, 33);
done(err);
});
});
});
});


});

0 comments on commit 0f1a486

Please sign in to comment.