commandeer
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -12,2 +12,6 @@ 'use strict'; | ||
dataProperty: 'proxyData', | ||
log: { | ||
error: function () {}, | ||
info: function () {} | ||
}, | ||
rewriteHostHeader: true, | ||
@@ -21,2 +25,3 @@ target: 'http://localhost' | ||
proxy.on('proxyReq', handleProxyRequest.bind(null, options)); | ||
proxy.on('proxyRes', handleProxyResponse.bind(null, options)); | ||
return function (request, response, next) { | ||
@@ -51,4 +56,12 @@ var jsonData = ''; | ||
var target = resolveTarget(options.target, request); | ||
options.log.info( | ||
'Proxying "' + request.url + '" ' + | ||
'to "' + target.replace(/\/+$/, '') + request.url + '"' | ||
); | ||
proxy.web(request, response, { | ||
target: resolveTarget(options.target, request) | ||
target: target | ||
}, function (error) { | ||
options.log.error('Failed to proxy "' + request.url + '"'); | ||
next(error); | ||
}); | ||
@@ -64,2 +77,6 @@ }; | ||
function handleProxyResponse (options, proxyResponse, request) { | ||
options.log.info('Proxied "' + request.url + '" successfully'); | ||
} | ||
function responseHasContentType (response, expectedContentTypes) { | ||
@@ -66,0 +83,0 @@ var contentType = response.getHeader('content-type'); |
{ | ||
"name": "commandeer", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
@@ -5,0 +5,0 @@ "description": "Proxy requests through connect and capture JSON responses before they are output", |
@@ -144,2 +144,14 @@ | ||
#### `log` (object) | ||
An object which implments the methods `error` and `info` which will be used to report errors and request information. | ||
```js | ||
app.use(commandeer({ | ||
log: console | ||
})); | ||
``` | ||
Defaults to a mock object which doesn't output anything. | ||
#### `rewriteHostHeader` (boolean) | ||
@@ -146,0 +158,0 @@ |
@@ -52,2 +52,14 @@ /* jshint maxstatements: false, maxlen: false */ | ||
it('should have a `log` property', function () { | ||
assert.isObject(defaults.log); | ||
}); | ||
it('should have a `log.error` method', function () { | ||
assert.isFunction(defaults.log.error); | ||
}); | ||
it('should have a `log.info` method', function () { | ||
assert.isFunction(defaults.log.info); | ||
}); | ||
it('should have a `rewriteHostHeader` property', function () { | ||
@@ -70,2 +82,6 @@ assert.isTrue(defaults.rewriteHostHeader); | ||
dataProperty: 'proxyDataUnit', | ||
log: { | ||
error: sinon.spy(), | ||
info: sinon.spy() | ||
}, | ||
rewriteHostHeader: true, | ||
@@ -122,2 +138,24 @@ target: 'http://localhost:1234' | ||
it('should handle the proxy server "proxyRes" event', function () { | ||
assert.isTrue(proxyServer.on.withArgs('proxyRes').calledOnce); | ||
assert.isFunction(proxyServer.on.withArgs('proxyRes').firstCall.args[1]); | ||
}); | ||
describe('proxy server "proxyRes" handler', function () { | ||
var proxyResHandler, proxyResponse, request; | ||
beforeEach(function () { | ||
proxyResponse = new http.ServerResponse(); | ||
request = new http.ClientRequest(); | ||
request.url = '/foo'; | ||
proxyResHandler = proxyServer.on.withArgs('proxyRes').firstCall.args[1]; | ||
proxyResHandler(proxyResponse, request); | ||
}); | ||
it('should log the successful proxying of the request', function () { | ||
assert.isTrue(options.log.info.withArgs('Proxied "/foo" successfully').calledOnce); | ||
}); | ||
}); | ||
it('should return a function', function () { | ||
@@ -132,2 +170,3 @@ assert.isFunction(middleware); | ||
request = new http.ClientRequest(); | ||
request.url = '/foo'; | ||
response = new http.ServerResponse(); | ||
@@ -142,2 +181,6 @@ next = sinon.spy(); | ||
it('should log that the request is being proxied', function () { | ||
assert.isTrue(options.log.info.withArgs('Proxying "/foo" to "http://localhost:1234/foo"').calledOnce); | ||
}); | ||
it('should call `proxyServer.web` with the request and response', function () { | ||
@@ -151,2 +194,6 @@ assert.isTrue(proxyServer.web.withArgs(request, response).calledOnce); | ||
it('should call `proxyServer.web` with an error handler', function () { | ||
assert.isFunction(proxyServer.web.firstCall.args[3]); | ||
}); | ||
describe('responseInterceptor `options.condition`', function () { | ||
@@ -185,2 +232,3 @@ var condition; | ||
], | ||
log: options.log, | ||
dataProperty: options.dataProperty, | ||
@@ -282,2 +330,21 @@ target: options.target | ||
describe('`proxyServer.web` error handler', function () { | ||
var error, errorHandler; | ||
beforeEach(function () { | ||
error = new Error('...'); | ||
errorHandler = proxyServer.web.firstCall.args[3]; | ||
errorHandler(error); | ||
}); | ||
it('should log that the proxy failed', function () { | ||
assert.isTrue(options.log.error.withArgs('Failed to proxy "/foo"').calledOnce); | ||
}); | ||
it('should call `next` with the handled error', function () { | ||
assert.isTrue(next.withArgs(error).calledOnce); | ||
}); | ||
}); | ||
}); | ||
@@ -294,2 +361,6 @@ | ||
dataProperty: 'proxyDataUnit', | ||
log: { | ||
error: sinon.spy(), | ||
info: sinon.spy() | ||
}, | ||
rewriteHostHeader: true, | ||
@@ -296,0 +367,0 @@ target: sinon.stub().returns('http://localhost:1234') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58709
35
1201
238