Merge pull request #679 from RackspaceEmailAndApps/close_proxy

Added functionality to close proxy.
This commit is contained in:
Jarrett Cruger 2014-08-14 17:12:42 -04:00
commit f92f7aea9b
4 changed files with 44 additions and 13 deletions

View File

@ -36,6 +36,7 @@ An object will be returned with four values:
* web `req, res, [options]` (used for proxying regular HTTP(S) requests) * web `req, res, [options]` (used for proxying regular HTTP(S) requests)
* ws `req, socket, head, [options]` (used for proxying WS(S) requests) * ws `req, socket, head, [options]` (used for proxying WS(S) requests)
* listen `port` (a function that wraps the object in a webserver, for your convenience) * listen `port` (a function that wraps the object in a webserver, for your convenience)
* close `[callback]` (a function that closes the inner webserver and stops listening on given port)
Is it then possible to proxy requests by calling these functions Is it then possible to proxy requests by calling these functions
@ -322,6 +323,21 @@ If you are using the `proxyServer.listen` method, the following options are also
* **xfwd**: true/false, adds x-forward headers * **xfwd**: true/false, adds x-forward headers
* **toProxy**: passes the absolute URL as the `path` (useful for proxying to proxies) * **toProxy**: passes the absolute URL as the `path` (useful for proxying to proxies)
### Shutdown
* When testing or running server within another program it may be necessary to close the proxy.
* This will stop the proxy from accepting new connections.
```js
var proxy = new httpProxy.createProxyServer({
target: {
host: 'localhost',
port: 1337
}
});
proxy.close();
```
### Test ### Test

View File

@ -134,6 +134,21 @@ ProxyServer.prototype.listen = function(port, hostname) {
return this; return this;
}; };
ProxyServer.prototype.close = function(callback) {
if (this._server) {
// Wrap callback to nullify server after all open connections are closed.
var callback_wrapper = function() {
this._server = null;
if (callback) {
callback(arguments);
}
};
this._server.close(callback_wrapper);
}
};
ProxyServer.prototype.before = function(type, passName, callback) { ProxyServer.prototype.before = function(type, passName, callback) {
if (type !== 'ws' && type !== 'web') { if (type !== 'ws' && type !== 'web') {
throw new Error('type must be `web` or `ws`'); throw new Error('type must be `web` or `ws`');

View File

@ -52,7 +52,7 @@ describe('lib/http-proxy.js', function() {
expect(req.method).to.eql('GET'); expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy); expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
source.close(); source.close();
proxy._server.close(); proxy.close();
done(); done();
}); });
@ -73,7 +73,7 @@ describe('lib/http-proxy.js', function() {
expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1'); expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy); expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
source.close(); source.close();
proxy._server.close(); proxy.close();
done(); done();
}); });
@ -119,7 +119,7 @@ describe('lib/http-proxy.js', function() {
res.on('end', function () { res.on('end', function () {
source.close(); source.close();
proxy._server.close(); proxy.close();
done(); done();
}); });
}).end(); }).end();
@ -136,7 +136,7 @@ describe('lib/http-proxy.js', function() {
proxy.on('error', function (err) { proxy.on('error', function (err) {
expect(err).to.be.an(Error); expect(err).to.be.an(Error);
expect(err.code).to.be('ECONNREFUSED'); expect(err.code).to.be('ECONNREFUSED');
proxy._server.close(); proxy.close();
done(); done();
}) })
@ -181,7 +181,7 @@ describe('lib/http-proxy.js', function() {
testReq.on('error', function (e) { testReq.on('error', function (e) {
expect(e).to.be.an(Error); expect(e).to.be.an(Error);
expect(e.code).to.be.eql('ECONNRESET'); expect(e.code).to.be.eql('ECONNRESET');
proxy._server.close(); proxy.close();
source.close(); source.close();
done(); done();
}); });
@ -228,7 +228,7 @@ describe('lib/http-proxy.js', function() {
// expect(events).to.contain('http-proxy:outgoing:web:begin'); // expect(events).to.contain('http-proxy:outgoing:web:begin');
// expect(events).to.contain('http-proxy:outgoing:web:end'); // expect(events).to.contain('http-proxy:outgoing:web:end');
// source.close(); // source.close();
// proxyServer._server.close(); // proxyServer.close();
// done(); // done();
// }); // });
// }).end(); // }).end();
@ -253,7 +253,7 @@ describe('lib/http-proxy.js', function() {
client.on('message', function (msg) { client.on('message', function (msg) {
expect(msg).to.be('Hello over websockets'); expect(msg).to.be('Hello over websockets');
client.close(); client.close();
proxyServer._server.close(); proxyServer.close();
destiny.close(); destiny.close();
done(); done();
}); });
@ -284,7 +284,7 @@ describe('lib/http-proxy.js', function() {
proxy.on('error', function (err) { proxy.on('error', function (err) {
expect(err).to.be.an(Error); expect(err).to.be.an(Error);
expect(err.code).to.be('ECONNREFUSED'); expect(err.code).to.be('ECONNREFUSED');
proxyServer._server.close(); proxyServer.close();
done(); done();
}); });
}); });
@ -307,7 +307,7 @@ describe('lib/http-proxy.js', function() {
client.on('outgoing', function (data) { client.on('outgoing', function (data) {
expect(data).to.be('Hello over websockets'); expect(data).to.be('Hello over websockets');
proxyServer._server.close(); proxyServer.close();
server.close(); server.close();
done(); done();
}); });

View File

@ -53,7 +53,7 @@ describe('lib/http-proxy.js', function() {
res.on('end', function () { res.on('end', function () {
source.close(); source.close();
proxy._server.close(); proxy.close();
done(); done();
}) })
}).end(); }).end();
@ -93,7 +93,7 @@ describe('lib/http-proxy.js', function() {
res.on('end', function () { res.on('end', function () {
source.close(); source.close();
proxy._server.close(); proxy.close();
done(); done();
}); });
}).end(); }).end();
@ -138,7 +138,7 @@ describe('lib/http-proxy.js', function() {
res.on('end', function () { res.on('end', function () {
source.close(); source.close();
proxy._server.close(); proxy.close();
done(); done();
}) })
}).end(); }).end();
@ -219,4 +219,4 @@ describe('lib/http-proxy.js', function() {
}) })
}) })
}); });
}); });