From f5d99fb0e998fb881bbc769aa5ba596d0f27bf05 Mon Sep 17 00:00:00 2001 From: kyo_ago Date: Tue, 21 Mar 2017 23:33:39 +0900 Subject: [PATCH] feat(proxy): add proxy events to config --- docs/config/01-configuration-file.md | 37 ++++++++++++++++++++++++++++ lib/middleware/proxy.js | 7 ++++++ test/unit/middleware/proxy.spec.js | 9 +++++++ 3 files changed, 53 insertions(+) diff --git a/docs/config/01-configuration-file.md b/docs/config/01-configuration-file.md index bd3f8198e..9df6b6a84 100644 --- a/docs/config/01-configuration-file.md +++ b/docs/config/01-configuration-file.md @@ -702,6 +702,43 @@ is handed off to [socket.io](http://socket.io/) (which manages the communication between browsers and the testing server). +## proxyReq +**Type:** Function + +**Default:** `undefined` + +**Description:** Called when requesting Proxy. + +Details about this can be found in the [node-http-proxy](https://github.com/nodejitsu/node-http-proxy). An example of overwriting the HTTP header is shown below. + +**Example:** +```javascript +proxyReq: function(proxyReq, req, res, options) { + proxyReq.setHeader('Referer', 'https://www.example.com/'); +} +``` + +## proxyRes +**Type:** Function + +**Default:** `undefined` + +**Description:** Called when respnsing Proxy. + +Details about this can be found in the [node-http-proxy](https://github.com/nodejitsu/node-http-proxy). An example of overwriting the HTTP header is shown below. + +**Example:** +```javascript +proxyRes: function(proxyRes, req, res) { + if (proxyRes.headers['set-cookie']) { + proxyRes.headers['set-cookie'] = proxyRes.headers['set-cookie'].map(function (cookie) { + return cookie.replace(/\s*secure;?/i, ''); + }) + } +} +``` + + ## upstreamProxy **Type:** Object diff --git a/lib/middleware/proxy.js b/lib/middleware/proxy.js index e835dbf69..80ea0c895 100644 --- a/lib/middleware/proxy.js +++ b/lib/middleware/proxy.js @@ -62,6 +62,13 @@ var parseProxyConfig = function (proxies, config) { secure: config.proxyValidateSSL }) + ;['proxyReq', 'proxyRes'].forEach(function (name) { + var callback = proxyDetails[name] || config[name] + if (callback) { + proxy.on(name, callback) + } + }) + proxy.on('error', function proxyError (err, req, res) { if (err.code === 'ECONNRESET' && req.socket.destroyed) { log.debug('failed to proxy %s (browser hung up the socket)', req.url) diff --git a/test/unit/middleware/proxy.spec.js b/test/unit/middleware/proxy.spec.js index ba5b8dd70..d9384641d 100644 --- a/test/unit/middleware/proxy.spec.js +++ b/test/unit/middleware/proxy.spec.js @@ -338,6 +338,15 @@ describe('middleware.proxy', () => { expect(parsedProxyConfig[0].proxy).to.exist }) + it('should bind proxy event', () => { + var proxy = {'/base/': 'http://localhost:8000/'} + var config = {proxyReq: function proxyReq() {}, proxyRes: function proxyRes() {}} + var parsedProxyConfig = m.parseProxyConfig(proxy, config) + expect(parsedProxyConfig).to.have.length(1) + expect(parsedProxyConfig[0].proxy.listeners('proxyReq', true)).to.equal(true) + expect(parsedProxyConfig[0].proxy.listeners('proxyRes', true)).to.equal(true) + }) + it('should handle empty proxy config', () => { expect(m.parseProxyConfig({})).to.deep.equal([]) })