Skip to content

Commit

Permalink
[test] add test for selfHandleRequest and remove modifyResponse as se…
Browse files Browse the repository at this point in the history
…lfHandleRequest is the only way that functionality works
  • Loading branch information
jcrugzz committed Apr 20, 2018
1 parent bcaae5b commit 44f085a
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,6 +1,7 @@
sudo: false
language: node_js
node_js:
- "4"
- "6"
- "8"
script:
Expand Down
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -377,7 +377,6 @@ proxyServer.listen(8015);
* **timeout**: timeout (in millis) for incoming requests
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
* **selfHandleRequest** true/false, if set to true, none of the webOutgoing passes are called and its your responsibility ro appropriately return the response by listening and acting on the `proxyRes` event
* **modifyResponse**: do not pipe proxyRes to res, so you can respond to client with your own response. It still goes through all out going web passes unlike selfHandleRequest
* **buffer**: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:

```
Expand Down Expand Up @@ -487,13 +486,16 @@ proxy.close();

### Miscellaneous

If you want to handle your own response after receiving the proxyRes, you can do
so with `selfHandleResponse`

### Modify response

```
var option = {
target: target,
modifyResponse : true
target: target,
selfHandleResponse : true
};
proxy.on('proxyRes', function (proxyRes, req, res) {
var body = new Buffer('');
Expand Down
2 changes: 1 addition & 1 deletion lib/http-proxy/passes/web-incoming.js
Expand Up @@ -182,7 +182,7 @@ module.exports = {
if (server) server.emit('end', req, res, proxyRes);
});
// We pipe to the response unless its expected to be handled by the user
if (!options.selfHandleResponse && !options.modifyResponse) proxyRes.pipe(res);
if (!options.selfHandleResponse) proxyRes.pipe(res);
} else {
if (server) server.emit('end', req, res, proxyRes);
}
Expand Down
72 changes: 72 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -18,6 +18,7 @@
},
"devDependencies": {
"async": "^2.0.0",
"concat-stream": "^1.6.2",
"expect.js": "~0.3.1",
"mocha": "^3.5.3",
"nyc": "^11.7.1",
Expand Down
40 changes: 40 additions & 0 deletions test/lib-http-proxy-passes-web-incoming-test.js
@@ -1,6 +1,8 @@
var webPasses = require('../lib/http-proxy/passes/web-incoming'),
httpProxy = require('../lib/http-proxy'),
expect = require('expect.js'),
concat = require('concat-stream'),
async = require('async'),
url = require('url'),
http = require('http');

Expand Down Expand Up @@ -316,6 +318,44 @@ describe('#createProxyServer.web() using own http server', function () {
http.request('http://127.0.0.1:8086', function() {}).end();
});

it('should proxy the request and provide and respond to manual user response when using modifyResponse', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
selfHandleResponse: true
});

function requestHandler(req, res) {
proxy.once('proxyRes', function (proxyRes, pReq, pRes) {
proxyRes.pipe(concat(function (body) {
expect(body.toString('utf8')).eql('Response');
pRes.end(Buffer.from('my-custom-response'));
}))
});

proxy.web(req, res);
}

var proxyServer = http.createServer(requestHandler);

var source = http.createServer(function(req, res) {
res.end('Response');
});

async.parallel([
next => proxyServer.listen(8086, next),
next => source.listen(8080, next)
], function (err) {
http.get('http://127.0.0.1:8086', function(res) {
res.pipe(concat(function(body) {
expect(body.toString('utf8')).eql('my-custom-response');
source.close();
proxyServer.close();
done();
}));
}).once('error', done);
})
});

it('should proxy the request and handle changeOrigin option', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
Expand Down

0 comments on commit 44f085a

Please sign in to comment.