commandeer
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -52,9 +52,22 @@ 'use strict'; | ||
function responseHasContentType (response, expectedContentType) { | ||
function responseHasContentType (response, expectedContentTypes) { | ||
var contentType = response.getHeader('content-type'); | ||
return !!(contentType && contentType.indexOf(expectedContentType) === 0); | ||
if (typeof contentType !== 'string') { | ||
return false; | ||
} | ||
contentType = contentType.split(';')[0].trim().toLowerCase(); | ||
return (expectedContentTypes.indexOf(contentType) !== -1); | ||
} | ||
function defaultOptions (options) { | ||
return _.defaults({}, options, commandeer.defaults); | ||
options = _.defaults({}, options, commandeer.defaults); | ||
if (!Array.isArray(options.contentType)) { | ||
options.contentType = [options.contentType]; | ||
} | ||
options.contentType.map(lowerCase); | ||
return options; | ||
} | ||
function lowerCase (string) { | ||
return string.toLowerCase(); | ||
} |
{ | ||
"name": "commandeer", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
@@ -5,0 +5,0 @@ "description": "Proxy requests through connect and capture JSON responses before they are output", |
@@ -7,3 +7,3 @@ | ||
**Current Version:** *0.1.0* | ||
**Current Version:** *0.2.0* | ||
**Node Support:** *0.10.x, 0.12.x* | ||
@@ -89,3 +89,3 @@ **License:** [MIT][mit] | ||
#### `contentType` (string) | ||
#### `contentType` (string|array) | ||
@@ -103,2 +103,16 @@ Any responses with a matching `Content-Type` header will not be proxied directly. Instead, the response body will be parsed as JSON and sent to the next middleware in the stack. E.g: | ||
If an array of strings is passed in, the response `Content-Type` will be checked against each of them: | ||
```js | ||
app.use(commandeer({ | ||
contentType: [ | ||
'application/x-foo', | ||
'application/x-bar' | ||
] | ||
})); | ||
app.use(function (request, response) { | ||
// Only responses from with a Content-Type of 'application/x-foo' or 'application/x-bar' will reach this middleware | ||
}); | ||
``` | ||
Defaults to `'application/x-commandeer+json'`. | ||
@@ -105,0 +119,0 @@ |
@@ -118,2 +118,6 @@ /* jshint maxstatements: false, maxlen: false */ | ||
assert.isTrue(condition()); | ||
response.getHeader.withArgs('content-type').returns('application/X-Commandeer-Unit+json'); | ||
assert.isTrue(condition()); | ||
response.getHeader.withArgs('content-type').returns('application/x-commandeer-unit+json; charset=utf-8'); | ||
assert.isTrue(condition()); | ||
}); | ||
@@ -130,2 +134,42 @@ | ||
describe('when `options.contentType` is an array', function () { | ||
beforeEach(function () { | ||
options = { | ||
contentType: [ | ||
'application/x-commandeer-unit1+json', | ||
'application/x-commandeer-unit2+json' | ||
], | ||
dataProperty: options.dataProperty, | ||
target: options.target | ||
}; | ||
middleware = commandeer(options); | ||
middleware(request, response, next); | ||
condition = responseInterceptor.secondCall.args[1].condition; | ||
}); | ||
it('should return `true` if response content-type is in `options.contentType`', function () { | ||
response.getHeader.withArgs('content-type').returns('application/x-commandeer-unit1+json'); | ||
assert.isTrue(condition()); | ||
response.getHeader.withArgs('content-type').returns('application/X-Commandeer-Unit1+json'); | ||
assert.isTrue(condition()); | ||
response.getHeader.withArgs('content-type').returns('application/x-commandeer-unit1+json; charset=utf-8'); | ||
assert.isTrue(condition()); | ||
response.getHeader.withArgs('content-type').returns('application/x-commandeer-unit2+json'); | ||
assert.isTrue(condition()); | ||
response.getHeader.withArgs('content-type').returns('application/X-Commandeer-Unit2+json'); | ||
assert.isTrue(condition()); | ||
response.getHeader.withArgs('content-type').returns('application/x-commandeer-unit2+json; charset=utf-8'); | ||
assert.isTrue(condition()); | ||
}); | ||
it('should return `false` if response content-type is not in `options.contentType`', function () { | ||
response.getHeader.withArgs('content-type').returns('application/x-commandeer-unit+json'); | ||
assert.isFalse(condition()); | ||
response.getHeader.withArgs('content-type').returns('text/html'); | ||
assert.isFalse(condition()); | ||
}); | ||
}); | ||
}); | ||
@@ -132,0 +176,0 @@ |
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
45330
926
188