Skip to content

Commit

Permalink
Added close method to proxy server.
Browse files Browse the repository at this point in the history
Ensured server exists before closing.
Updated tests to use new close function.
Added documentation to README.
  • Loading branch information
unilaterus committed Aug 14, 2014
1 parent 63c53a1 commit a3d0219
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -36,6 +36,7 @@ An object will be returned with four values:
* web `req, res, [options]` (used for proxying regular HTTP(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)
* 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

Expand Down Expand Up @@ -322,6 +323,21 @@ If you are using the `proxyServer.listen` method, the following options are also
* **xfwd**: true/false, adds x-forward headers
* **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

Expand Down
7 changes: 7 additions & 0 deletions lib/http-proxy/index.js
Expand Up @@ -132,6 +132,13 @@ ProxyServer.prototype.listen = function(port, hostname) {
return this;
};

ProxyServer.prototype.close = function(callback) {
if (this._server) {
this._server.close(callback);
this._server = null;
}
};

ProxyServer.prototype.before = function(type, passName, callback) {
if (type !== 'ws' && type !== 'web') {
throw new Error('type must be `web` or `ws`');
Expand Down
18 changes: 9 additions & 9 deletions test/lib-http-proxy-test.js
Expand Up @@ -52,7 +52,7 @@ describe('lib/http-proxy.js', function() {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
source.close();
proxy._server.close();
proxy.close();
done();
});

Expand All @@ -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.host.split(':')[1]).to.eql(ports.proxy);
source.close();
proxy._server.close();
proxy.close();
done();
});

Expand Down Expand Up @@ -119,7 +119,7 @@ describe('lib/http-proxy.js', function() {

res.on('end', function () {
source.close();
proxy._server.close();
proxy.close();
done();
});
}).end();
Expand All @@ -136,7 +136,7 @@ describe('lib/http-proxy.js', function() {
proxy.on('error', function (err) {
expect(err).to.be.an(Error);
expect(err.code).to.be('ECONNREFUSED');
proxy._server.close();
proxy.close();
done();
})

Expand Down Expand Up @@ -181,7 +181,7 @@ describe('lib/http-proxy.js', function() {
testReq.on('error', function (e) {
expect(e).to.be.an(Error);
expect(e.code).to.be.eql('ECONNRESET');
proxy._server.close();
proxy.close();
source.close();
done();
});
Expand Down Expand Up @@ -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:end');
// source.close();
// proxyServer._server.close();
// proxyServer.close();
// done();
// });
// }).end();
Expand All @@ -253,7 +253,7 @@ describe('lib/http-proxy.js', function() {
client.on('message', function (msg) {
expect(msg).to.be('Hello over websockets');
client.close();
proxyServer._server.close();
proxyServer.close();
destiny.close();
done();
});
Expand Down Expand Up @@ -284,7 +284,7 @@ describe('lib/http-proxy.js', function() {
proxy.on('error', function (err) {
expect(err).to.be.an(Error);
expect(err.code).to.be('ECONNREFUSED');
proxyServer._server.close();
proxyServer.close();
done();
});
});
Expand All @@ -307,7 +307,7 @@ describe('lib/http-proxy.js', function() {

client.on('outgoing', function (data) {
expect(data).to.be('Hello over websockets');
proxyServer._server.close();
proxyServer.close();
server.close();
done();
});
Expand Down
8 changes: 4 additions & 4 deletions test/lib-https-proxy-test.js
Expand Up @@ -53,7 +53,7 @@ describe('lib/http-proxy.js', function() {

res.on('end', function () {
source.close();
proxy._server.close();
proxy.close();
done();
})
}).end();
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('lib/http-proxy.js', function() {

res.on('end', function () {
source.close();
proxy._server.close();
proxy.close();
done();
});
}).end();
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('lib/http-proxy.js', function() {

res.on('end', function () {
source.close();
proxy._server.close();
proxy.close();
done();
})
}).end();
Expand Down Expand Up @@ -219,4 +219,4 @@ describe('lib/http-proxy.js', function() {
})
})
});
});
});

0 comments on commit a3d0219

Please sign in to comment.