Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#437] dynamic value generation of option parseReqBody #457

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,19 @@ app.use('/proxy', proxy('www.google.com', {
parseReqBody: false
}));
```
You can use function instead of boolean value for dynamic value generation based on request

```js
app.use('/proxy', proxy('www.google.com', {
parseReqBody: function (proxyReq) {
if (proxyReq.headers["content-type"] === "application/json") {
return true;
} else {
return false;
}
}
v-electrolux marked this conversation as resolved.
Show resolved Hide resolved
}));
```

#### reqAsBuffer

Expand Down
3 changes: 2 additions & 1 deletion app/steps/buildProxyReq.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function buildProxyReq(Container) {
var options = Container.options;
var host = Container.proxy.host;

var parseBody = (!options.parseReqBody) ? Promise.resolve(null) : requestOptions.bodyContent(req, res, options);
var parseReqBody = (typeof options.parseReqBody === 'function') ? options.parseReqBody(req) : options.parseReqBody;
v-electrolux marked this conversation as resolved.
Show resolved Hide resolved
var parseBody = (!parseReqBody) ? Promise.resolve(null) : requestOptions.bodyContent(req, res, options);
var createReqOptions = requestOptions.create(req, res, options, host);

return Promise
Expand Down
3 changes: 2 additions & 1 deletion app/steps/sendProxyRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ function sendProxyRequest(Container) {

proxyReq.on('error', reject);

var parseReqBody = (typeof options.parseReqBody === 'function') ? options.parseReqBody(req) : options.parseReqBody;
// this guy should go elsewhere, down the chain
if (options.parseReqBody) {
if (parseReqBody) {
// We are parsing the body ourselves so we need to write the body content
// and then manually end the request.

Expand Down
1 change: 1 addition & 0 deletions lib/requestOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function createRequestOptions(req, res, options) {

function bodyContent(req, res, options) {
var parseReqBody = isUnset(options.parseReqBody) ? true : options.parseReqBody;
parseReqBody = (typeof parseReqBody === 'function') ? parseReqBody(req) : options.parseReqBody;

function maybeParseBody(req, limit) {
if (req.body) {
Expand Down
46 changes: 45 additions & 1 deletion test/bodyEncoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('body encoding', function () {

});

describe('when user sets parseReqBody', function () {
describe('when user sets parseReqBody as bool', function () {
it('should not parse body', function (done) {
var filename = os.tmpdir() + '/express-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png';
var app = express();
Expand Down Expand Up @@ -140,6 +140,50 @@ describe('body encoding', function () {
});


describe('when user sets parseReqBody as function', function () {
it('should not parse body with form-data content', function (done) {
var filename = os.tmpdir() + '/express-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png';
var app = express();
app.use(proxy('localhost:8109', {
parseReqBody: (proxyReq) => proxyReq.headers['content-type'].includes('application/json'),
proxyReqBodyDecorator: function (bodyContent) {
assert(!bodyContent, 'body content should not be parsed.');
return bodyContent;
}
}));

fs.writeFile(filename, pngData, function (err) {
if (err) { throw err; }
request(app)
.post('/post')
.attach('image', filename)
.end(function (err) {
fs.unlinkSync(filename);

done(err);
});
});
});
it('should parse body with json content', function (done) {
var app = express();
app.use(proxy('localhost:8109', {
parseReqBody: (proxyReq) => proxyReq.headers['content-type'].includes('application/json'),
proxyReqBodyDecorator: function (bodyContent) {
assert(bodyContent, 'body content should be parsed.');
return bodyContent;
}
}));

request(app)
.post('/post')
.send({ some: 'json' })
.end(function (err) {
done(err);
});
});
});


describe('when user sets reqBodyEncoding', function () {
it('should set the accepts-charset header', function (done) {
var app = express();
Expand Down