Skip to content

Commit 4a37175

Browse files
jcrugzzindexzero
authored andcommitted
[test] add test for selfHandleRequest and remove modifyResponse as selfHandleRequest is the only way that functionality works
1 parent e5c02b8 commit 4a37175

6 files changed

+120
-4
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
sudo: false
22
language: node_js
33
node_js:
4+
- "4"
45
- "6"
56
- "8"
67
script:

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ proxyServer.listen(8015);
377377
* **timeout**: timeout (in millis) for incoming requests
378378
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
379379
* **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
380-
* **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
381380
* **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:
382381
383382
```
@@ -487,13 +486,16 @@ proxy.close();
487486

488487
### Miscellaneous
489488

489+
If you want to handle your own response after receiving the proxyRes, you can do
490+
so with `selfHandleResponse`
491+
490492
### Modify response
491493

492494
```
493495
494496
var option = {
495-
target: target,
496-
modifyResponse : true
497+
target: target,
498+
selfHandleResponse : true
497499
};
498500
proxy.on('proxyRes', function (proxyRes, req, res) {
499501
var body = new Buffer('');

lib/http-proxy/passes/web-incoming.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ module.exports = {
182182
if (server) server.emit('end', req, res, proxyRes);
183183
});
184184
// We pipe to the response unless its expected to be handled by the user
185-
if (!options.selfHandleResponse && !options.modifyResponse) proxyRes.pipe(res);
185+
if (!options.selfHandleResponse) proxyRes.pipe(res);
186186
} else {
187187
if (server) server.emit('end', req, res, proxyRes);
188188
}

package-lock.json

+72
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"devDependencies": {
2020
"async": "^2.0.0",
21+
"concat-stream": "^1.6.2",
2122
"expect.js": "~0.3.1",
2223
"mocha": "^3.5.3",
2324
"nyc": "^11.7.1",

test/lib-http-proxy-passes-web-incoming-test.js

+40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var webPasses = require('../lib/http-proxy/passes/web-incoming'),
22
httpProxy = require('../lib/http-proxy'),
33
expect = require('expect.js'),
4+
concat = require('concat-stream'),
5+
async = require('async'),
46
url = require('url'),
57
http = require('http');
68

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

321+
it('should proxy the request and provide and respond to manual user response when using modifyResponse', function(done) {
322+
var proxy = httpProxy.createProxyServer({
323+
target: 'http://127.0.0.1:8080',
324+
selfHandleResponse: true
325+
});
326+
327+
function requestHandler(req, res) {
328+
proxy.once('proxyRes', function (proxyRes, pReq, pRes) {
329+
proxyRes.pipe(concat(function (body) {
330+
expect(body.toString('utf8')).eql('Response');
331+
pRes.end(Buffer.from('my-custom-response'));
332+
}))
333+
});
334+
335+
proxy.web(req, res);
336+
}
337+
338+
var proxyServer = http.createServer(requestHandler);
339+
340+
var source = http.createServer(function(req, res) {
341+
res.end('Response');
342+
});
343+
344+
async.parallel([
345+
next => proxyServer.listen(8086, next),
346+
next => source.listen(8080, next)
347+
], function (err) {
348+
http.get('http://127.0.0.1:8086', function(res) {
349+
res.pipe(concat(function(body) {
350+
expect(body.toString('utf8')).eql('my-custom-response');
351+
source.close();
352+
proxyServer.close();
353+
done();
354+
}));
355+
}).once('error', done);
356+
})
357+
});
358+
319359
it('should proxy the request and handle changeOrigin option', function (done) {
320360
var proxy = httpProxy.createProxyServer({
321361
target: 'http://127.0.0.1:8080',

0 commit comments

Comments
 (0)