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) { diff --git a/test/bodyEncoding.js b/test/bodyEncoding.js index 22360e2d..8cce4d25 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();