From facb3abfaf9c7b281925d4d77424af206ea24470 Mon Sep 17 00:00:00 2001 From: v-electrolux Date: Thu, 2 Jul 2020 17:13:59 +0300 Subject: [PATCH 1/3] [#437] dynamic value generation of option parseReqBody --- README.md | 13 +++++++++++++ app/steps/buildProxyReq.js | 3 ++- app/steps/sendProxyRequest.js | 3 ++- lib/requestOptions.js | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b9c2c08d..8134ceac 100644 --- a/README.md +++ b/README.md @@ -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; + } + } +})); +``` #### reqAsBuffer diff --git a/app/steps/buildProxyReq.js b/app/steps/buildProxyReq.js index 1bb67afa..b8fa06f1 100644 --- a/app/steps/buildProxyReq.js +++ b/app/steps/buildProxyReq.js @@ -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; + var parseBody = (!parseReqBody) ? Promise.resolve(null) : requestOptions.bodyContent(req, res, options); var createReqOptions = requestOptions.create(req, res, options, host); return Promise diff --git a/app/steps/sendProxyRequest.js b/app/steps/sendProxyRequest.js index 6764ecfb..0f7a1b4c 100644 --- a/app/steps/sendProxyRequest.js +++ b/app/steps/sendProxyRequest.js @@ -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. diff --git a/lib/requestOptions.js b/lib/requestOptions.js index 59b8af60..24a668f9 100644 --- a/lib/requestOptions.js +++ b/lib/requestOptions.js @@ -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) { From b865387c359f37a9c88e50220c628d658f368428 Mon Sep 17 00:00:00 2001 From: v-electrolux Date: Wed, 29 Jul 2020 19:22:32 +0300 Subject: [PATCH 2/3] [#437] add tests for option parseReqBody as function --- test/bodyEncoding.js | 46 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/test/bodyEncoding.js b/test/bodyEncoding.js index 22360e2d..4907da46 100644 --- a/test/bodyEncoding.js +++ b/test/bodyEncoding.js @@ -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(); @@ -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(); From 5b5b872313583c2158a39fe635648403d475a1c9 Mon Sep 17 00:00:00 2001 From: v-electrolux Date: Wed, 29 Jul 2020 19:25:40 +0300 Subject: [PATCH 3/3] [#437] fix style in test for option parseReqBody as function --- test/bodyEncoding.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bodyEncoding.js b/test/bodyEncoding.js index 4907da46..8cce4d25 100644 --- a/test/bodyEncoding.js +++ b/test/bodyEncoding.js @@ -173,7 +173,7 @@ describe('body encoding', function () { return bodyContent; } })); - + request(app) .post('/post') .send({ some: 'json' })