New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

commandeer

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

commandeer - npm Package Compare versions

Comparing version

to
1.0.0

12

example/basic/application.js
'use strict';
var commandeer = require('../..');
var connect = require('connect');
const commandeer = require('../..');
const connect = require('connect');
var app = connect();
const app = connect();

@@ -17,3 +17,3 @@ // Initialise Commandeer

// (Just add another property to the JSON and output it)
app.use(function (request, response) {
app.use((request, response) => {
response.proxyData.commandeered = true;

@@ -24,3 +24,3 @@ response.end(JSON.stringify(response.proxyData));

// Handle errors
app.use(function (error, request, response, next) {
app.use((error, request, response, next) => {
// jshint unused: false

@@ -32,4 +32,4 @@ response.writeHead(500);

// Start the application
app.listen(3000, function () {
app.listen(3000, () => {
console.log('Application running on port %d', 3000);
});
'use strict';
var connect = require('connect');
const connect = require('connect');
var app = connect();
const app = connect();
// Application routes
var routes = {
const routes = {
// Index route
'/': function (request, response) {
'/': (request, response) => {
response.end([

@@ -24,3 +24,3 @@ '<h1>Example Application</h1>',

// HTML page
'/html': function (request, response) {
'/html': (request, response) => {
response.end('<p>Hello World!</p>');

@@ -30,3 +30,3 @@ },

// Plain text
'/text': function (request, response) {
'/text': (request, response) => {
response.end('Hello World!');

@@ -36,3 +36,3 @@ },

// Regular JSON
'/json': function (request, response) {
'/json': (request, response) => {
response.end('{}');

@@ -42,3 +42,3 @@ },

// JSON which will be commandeered
'/jsonc': function (request, response) {
'/jsonc': (request, response) => {
response.writeHead(200, {

@@ -53,3 +53,3 @@ 'Content-Type': 'application/x-commandeer+json'

// Handle application routes
app.use(function (request, response, next) {
app.use((request, response, next) => {
if (routes[request.url]) {

@@ -62,3 +62,3 @@ return routes[request.url](request, response, next);

// Handle 404 errors
app.use(function (request, response) {
app.use((request, response) => {
response.writeHead(404);

@@ -69,3 +69,3 @@ response.end('404');

// Handle 500 errors
app.use(function (error, request, response, next) {
app.use((error, request, response, next) => {
// jshint unused: false

@@ -77,4 +77,4 @@ response.writeHead(500);

// Start the application
app.listen(3001, function () {
app.listen(3001, () => {
console.log('Backend running on port %d', 3001);
});
'use strict';
var commandeer = require('../..');
var connect = require('connect');
var fs = require('fs');
var hogan = require('hogan.js');
const commandeer = require('../..');
const connect = require('connect');
const fs = require('fs');
const hogan = require('hogan.js');
var app = connect();
const app = connect();
// Compile templates
var templates = {
const templates = {
about: loadTemplate('about'),

@@ -19,3 +19,3 @@ error: loadTemplate('error'),

function loadTemplate (name) {
return hogan.compile(fs.readFileSync(__dirname + '/view/' + name + '.mustache', 'utf-8'));
return hogan.compile(fs.readFileSync(`${__dirname}/view/${name}.mustache`, 'utf-8'));
}

@@ -31,5 +31,5 @@

// (Render the requested template)
app.use(function (request, response) {
var template = templates[response.viewData.template];
var output = template.render(response.viewData);
app.use((request, response) => {
const template = templates[response.viewData.template];
const output = template.render(response.viewData);
response.end(output);

@@ -40,3 +40,3 @@ });

// (Render the error page)
app.use(function (error, request, response, next) {
app.use((error, request, response, next) => {
// jshint unused: false

@@ -51,4 +51,4 @@ response.writeHead(500);

// Start the application
app.listen(3000, function () {
app.listen(3000, () => {
console.log('Application running on port %d', 3000);
});
'use strict';
var connect = require('connect');
const connect = require('connect');
var app = connect();
const app = connect();
// Application routes
var routes = {
const routes = {
// Home page
'/': function (request, response) {
'/': (request, response) => {
response.end(JSON.stringify({

@@ -19,3 +19,3 @@ template: 'home',

// About page
'/about': function (request, response) {
'/about': (request, response) => {
response.end(JSON.stringify({

@@ -30,3 +30,3 @@ template: 'about',

// Set the commandeerable content type for all requests
app.use(function (request, response, next) {
app.use((request, response, next) => {
response.setHeader('Content-Type', 'application/x-commandeer+json');

@@ -37,3 +37,3 @@ next();

// Handle application routes
app.use(function (request, response, next) {
app.use((request, response, next) => {
if (routes[request.url]) {

@@ -46,3 +46,3 @@ return routes[request.url](request, response, next);

// Handle 404 errors
app.use(function (request, response) {
app.use((request, response) => {
response.writeHead(404);

@@ -56,3 +56,3 @@ response.end(JSON.stringify({

// Handle 500 errors
app.use(function (error, request, response, next) {
app.use((error, request, response, next) => {
// jshint unused: false

@@ -67,4 +67,4 @@ response.writeHead(500);

// Start the application
app.listen(3001, function () {
app.listen(3001, () => {
console.log('Backend running on port %d', 3001);
});
'use strict';
var commandeer = require('../..');
var connect = require('connect');
const commandeer = require('../..');
const connect = require('connect');
var app = connect();
const app = connect();

@@ -28,3 +28,3 @@ // Initialise Commandeer with `target` set to a function

// (Just add another property to the JSON and output it)
app.use(function (request, response) {
app.use((request, response) => {
response.proxyData.commandeered = true;

@@ -35,3 +35,3 @@ response.end(JSON.stringify(response.proxyData));

// Handle errors
app.use(function (error, request, response, next) {
app.use((error, request, response, next) => {
// jshint unused: false

@@ -43,4 +43,4 @@ response.writeHead(500);

// Start the application
app.listen(3000, function () {
app.listen(3000, () => {
console.log('Application running on port %d', 3000);
});
'use strict';
var connect = require('connect');
const connect = require('connect');
var app = connect();
const app = connect();
// Application routes
var routes = {
const routes = {
// Index route
'/': function (request, response) {
'/': (request, response) => {
response.end([

@@ -24,3 +24,3 @@ '<h1>Example Backend 1</h1>',

// JSON which will be commandeered
'/jsonc': function (request, response) {
'/jsonc': (request, response) => {
response.writeHead(200, {

@@ -35,3 +35,3 @@ 'Content-Type': 'application/x-commandeer+json'

// Handle application routes
app.use(function (request, response, next) {
app.use((request, response, next) => {
if (routes[request.url]) {

@@ -44,3 +44,3 @@ return routes[request.url](request, response, next);

// Handle 404 errors
app.use(function (request, response) {
app.use((request, response) => {
response.writeHead(404);

@@ -51,3 +51,3 @@ response.end('404');

// Handle 500 errors
app.use(function (error, request, response, next) {
app.use((error, request, response, next) => {
// jshint unused: false

@@ -59,4 +59,4 @@ response.writeHead(500);

// Start the application
app.listen(3001, function () {
app.listen(3001, () => {
console.log('Backend running on port %d', 3001);
});
'use strict';
var connect = require('connect');
const connect = require('connect');
var app = connect();
const app = connect();
// Application routes
var routes = {
const routes = {
// Index route
'/backend2': function (request, response) {
'/backend2': (request, response) => {
response.end([

@@ -24,3 +24,3 @@ '<h1>Example Backend 2</h1>',

// JSON which will be commandeered
'/backend2/jsonc': function (request, response) {
'/backend2/jsonc': (request, response) => {
response.writeHead(200, {

@@ -35,3 +35,3 @@ 'Content-Type': 'application/x-commandeer+json'

// Handle application routes
app.use(function (request, response, next) {
app.use((request, response, next) => {
if (routes[request.url]) {

@@ -44,3 +44,3 @@ return routes[request.url](request, response, next);

// Handle 404 errors
app.use(function (request, response) {
app.use((request, response) => {
response.writeHead(404);

@@ -51,3 +51,3 @@ response.end('404');

// Handle 500 errors
app.use(function (error, request, response, next) {
app.use((error, request, response, next) => {
// jshint unused: false

@@ -59,4 +59,4 @@ response.writeHead(500);

// Start the application
app.listen(3002, function () {
app.listen(3002, () => {
console.log('Backend running on port %d', 3002);
});
# History
## 1.0.0 (2016-03-10)
* First stable release
* Support Node.js 4+ only
* Update dependencies
## 0.5.5 pre-release (2016-01-09)

@@ -5,0 +11,0 @@

'use strict';
var _ = require('underscore');
var createProxyServer = require('http-proxy').createProxyServer;
var createResponseInterceptor = require('./response-interceptor');
var parseUrl = require('url').parse;
const _ = require('underscore');
const createProxyServer = require('http-proxy').createProxyServer;
const createResponseInterceptor = require('./response-interceptor');
const parseUrl = require('url').parse;

@@ -13,4 +13,4 @@ module.exports = commandeer;

log: {
error: /* istanbul ignore next */ function () {},
info: /* istanbul ignore next */ function () {}
error: /* istanbul ignore next */ () => {},
info: /* istanbul ignore next */ () => {}
},

@@ -22,15 +22,15 @@ rewriteHostHeader: true,

function commandeer (options) {
var proxy = createProxyServer();
const proxy = createProxyServer();
options = defaultOptions(options);
proxy.on('proxyReq', handleProxyRequest.bind(null, options));
proxy.on('proxyRes', handleProxyResponse.bind(null, options));
return function (request, response, next) {
var jsonData = '';
return (request, response, next) => {
let jsonData = '';
createResponseInterceptor(response, {
condition: function () {
return responseHasContentType(response, options.contentType);
},
condition: () => responseHasContentType(response, options.contentType),
writeHead: function (statusCode) {
writeHead: statusCode => {
response.statusCode = statusCode;

@@ -41,7 +41,7 @@ response.removeHeader('Content-Type');

write: function (data) {
write: data => {
jsonData += data.toString();
},
end: function () {
end: () => {
try {

@@ -57,11 +57,10 @@ response[options.dataProperty] = JSON.parse(jsonData);

var target = resolveTarget(options.target, request);
options.log.info(
'Proxying "' + request.url + '" ' +
'to "' + target.replace(/\/+$/, '') + request.url + '"'
);
const target = resolveTarget(options.target, request);
const targetUrl = target.replace(/\/+$/, '') + request.url;
options.log.info(`Proxying "${request.url}" to "${targetUrl}"`);
proxy.web(request, response, {
target: target
}, function (error) {
options.log.error('Failed to proxy "' + request.url + '"');
}, error => {
options.log.error(`Failed to proxy "${request.url}"`);
next(error);

@@ -79,7 +78,7 @@ });

function handleProxyResponse (options, proxyResponse, request) {
options.log.info('Proxied "' + request.url + '" successfully');
options.log.info(`Proxied "${request.url}" successfully`);
}
function responseHasContentType (response, expectedContentTypes) {
var contentType = response.getHeader('content-type');
let contentType = response.getHeader('content-type');
if (typeof contentType !== 'string') {

@@ -86,0 +85,0 @@ return false;

'use strict';
var _ = require('underscore');
const _ = require('underscore');

@@ -8,5 +8,5 @@ module.exports = createResponseInterceptor;

function createResponseInterceptor (response, options) {
var condition = _.memoize(options.condition);
var originals = backUpResponseMethods(response);
['writeHead', 'write', 'end'].forEach(function (methodName) {
const condition = _.memoize(options.condition);
const originals = backUpResponseMethods(response);
['writeHead', 'write', 'end'].forEach(methodName => {
response[methodName] = function () {

@@ -13,0 +13,0 @@ if (methodName === 'end') {

{
"name": "commandeer",
"version": "0.5.5",
"version": "1.0.0",

@@ -18,10 +18,10 @@ "description": "Proxy requests through connect and capture JSON responses before they are output",

"engines": {
"node": ">=0.10"
"node": ">=4"
},
"dependencies": {
"http-proxy": "~1.12",
"http-proxy": "~1.13",
"underscore": "~1.8"
},
"devDependencies": {
"istanbul": "~0.3",
"istanbul": "~0.4",
"jscs": "^2",

@@ -32,3 +32,3 @@ "connect": "^3",

"mocha": "^2",
"mockery": "~1.4",
"mockery": "^1",
"proclaim": "^3",

@@ -35,0 +35,0 @@ "request": "^2",

@@ -235,4 +235,4 @@

[shield-license]: https://img.shields.io/badge/license-MIT-blue.svg
[shield-node]: https://img.shields.io/badge/node.js%20support-0.10–5-brightgreen.svg
[shield-node]: https://img.shields.io/badge/node.js%20support-4–5-brightgreen.svg
[shield-npm]: https://img.shields.io/npm/v/commandeer.svg
[shield-build]: https://img.shields.io/travis/rowanmanning/commandeer/master.svg

@@ -1,6 +0,5 @@

// jshint maxstatements: false
// jscs:disable disallowMultipleVarDecl, maximumLineLength
// jscs:disable maximumLineLength, requireArrowFunctions
'use strict';
var assert = require('proclaim');
const assert = require('proclaim');

@@ -7,0 +6,0 @@ describe('Commandeerable JSON Routes', function () {

@@ -1,6 +0,5 @@

// jshint maxstatements: false
// jscs:disable disallowMultipleVarDecl, maximumLineLength
// jscs:disable maximumLineLength, requireArrowFunctions
'use strict';
var assert = require('proclaim');
const assert = require('proclaim');

@@ -7,0 +6,0 @@ describe('JSON Routes', function () {

@@ -1,6 +0,5 @@

// jshint maxstatements: false
// jscs:disable disallowMultipleVarDecl, maximumLineLength
// jscs:disable maximumLineLength, requireArrowFunctions
'use strict';
var assert = require('proclaim');
const assert = require('proclaim');

@@ -7,0 +6,0 @@ describe('Text routes', function () {

@@ -1,22 +0,21 @@

// jshint maxstatements: false
// jscs:disable disallowMultipleVarDecl, maximumLineLength
// jscs:disable maximumLineLength, requireArrowFunctions
'use strict';
var createTestApplication = require('./test-application');
var createTestBackend = require('./test-backend');
var request = require('request');
const createTestApplication = require('./test-application');
const createTestBackend = require('./test-backend');
const request = require('request');
before(function (done) {
var self = this;
const self = this;
var applicationPort = process.env.PORT || 5052;
var backendPort = process.env.BACKEND_PORT || 5053;
const applicationPort = process.env.PORT || 5052;
const backendPort = process.env.BACKEND_PORT || 5053;
self.request = function (method, path, headers, done) {
self.request = (method, path, headers, done) => {
request({
method: method.toUpperCase(),
url: 'http://localhost:' + applicationPort + path,
url: `http://localhost:${applicationPort}${path}`,
headers: headers,
json: true
}, function (error, response, body) {
}, (error, response, body) => {
self.response = response;

@@ -28,4 +27,4 @@ self.body = body;

createTestApplication(applicationPort, backendPort, function () {
createTestBackend(backendPort, function () {
createTestApplication(applicationPort, backendPort, () => {
createTestBackend(backendPort, () => {
done();

@@ -32,0 +31,0 @@ });

'use strict';
var commandeer = require('../..');
var connect = require('connect');
const commandeer = require('../..');
const connect = require('connect');

@@ -13,3 +13,3 @@ module.exports = createTestApplication;

dataProperty: 'proxyDataIntegration',
target: 'http://localhost:' + backendPort
target: `http://localhost:${backendPort}`
}))

@@ -16,0 +16,0 @@ .use(handleProxyData)

'use strict';
var connect = require('connect');
const connect = require('connect');

@@ -13,5 +13,5 @@ module.exports = createTestBackend;

var routes = {
const routes = {
'/text': function (req, res) {
'/text': (req, res) => {
res.writeHead(200, {

@@ -23,3 +23,3 @@ 'Content-Type': 'text/plain'

'/text-with-status': function (req, res) {
'/text-with-status': (req, res) => {
res.writeHead(400, {

@@ -31,3 +31,3 @@ 'Content-Type': 'text/plain'

'/text-with-header': function (req, res) {
'/text-with-header': (req, res) => {
res.writeHead(200, {

@@ -40,3 +40,3 @@ 'Content-Type': 'text/plain',

'/json': function (req, res) {
'/json': (req, res) => {
res.writeHead(200, {

@@ -48,3 +48,3 @@ 'Content-Type': 'application/json'

'/json-with-status': function (req, res) {
'/json-with-status': (req, res) => {
res.writeHead(400, {

@@ -56,3 +56,3 @@ 'Content-Type': 'application/json'

'/json-with-header': function (req, res) {
'/json-with-header': (req, res) => {
res.writeHead(200, {

@@ -65,3 +65,3 @@ 'Content-Type': 'application/json',

'/json-commandeer': function (req, res) {
'/json-commandeer': (req, res) => {
res.writeHead(200, {

@@ -73,3 +73,3 @@ 'Content-Type': 'application/x-commandeer-integration+json'

'/json-commandeer-with-status': function (req, res) {
'/json-commandeer-with-status': (req, res) => {
res.writeHead(400, {

@@ -81,3 +81,3 @@ 'Content-Type': 'application/x-commandeer-integration+json'

'/json-commandeer-with-header': function (req, res) {
'/json-commandeer-with-header': (req, res) => {
res.writeHead(200, {

@@ -90,3 +90,3 @@ 'Content-Type': 'application/x-commandeer-integration+json',

default: function (req, res) {
default: (req, res) => {
res.writeHead(404, {

@@ -93,0 +93,0 @@ 'Content-Type': 'text/plain'

@@ -1,13 +0,16 @@

// jshint maxstatements: false
// jscs:disable disallowMultipleVarDecl, maximumLineLength
// jscs:disable maximumLineLength
'use strict';
var assert = require('proclaim');
var mockery = require('mockery');
var sinon = require('sinon');
const assert = require('proclaim');
const mockery = require('mockery');
const sinon = require('sinon');
describe('lib/commandeer', function () {
var commandeer, http, httpProxy, responseInterceptor, underscore;
describe('lib/commandeer', () => {
let commandeer;
let http;
let httpProxy;
let responseInterceptor;
let underscore;
beforeEach(function () {
beforeEach(() => {

@@ -29,42 +32,42 @@ http = require('../mock/http');

it('should be a function', function () {
it('should be a function', () => {
assert.isFunction(commandeer);
});
it('should have a `defaults` property', function () {
it('should have a `defaults` property', () => {
assert.isObject(commandeer.defaults);
});
describe('.defaults', function () {
var defaults;
describe('.defaults', () => {
let defaults;
beforeEach(function () {
beforeEach(() => {
defaults = commandeer.defaults;
});
it('should have a `contentType` property', function () {
it('should have a `contentType` property', () => {
assert.strictEqual(defaults.contentType, 'application/x-commandeer+json');
});
it('should have a `dataProperty` property', function () {
it('should have a `dataProperty` property', () => {
assert.strictEqual(defaults.dataProperty, 'proxyData');
});
it('should have a `log` property', function () {
it('should have a `log` property', () => {
assert.isObject(defaults.log);
});
it('should have a `log.error` method', function () {
it('should have a `log.error` method', () => {
assert.isFunction(defaults.log.error);
});
it('should have a `log.info` method', function () {
it('should have a `log.info` method', () => {
assert.isFunction(defaults.log.info);
});
it('should have a `rewriteHostHeader` property', function () {
it('should have a `rewriteHostHeader` property', () => {
assert.isTrue(defaults.rewriteHostHeader);
});
it('should have a `target` property', function () {
it('should have a `target` property', () => {
assert.strictEqual(defaults.target, 'http://localhost');

@@ -75,6 +78,8 @@ });

describe('commandeer()', function () {
var options, middleware, proxyServer;
describe('commandeer()', () => {
let middleware;
let options;
let proxyServer;
beforeEach(function () {
beforeEach(() => {
options = {

@@ -94,3 +99,3 @@ contentType: 'application/x-commandeer-unit+json',

it('should default the options', function () {
it('should default the options', () => {
assert.isTrue(underscore.defaults.calledOnce);

@@ -102,7 +107,7 @@ assert.deepEqual(underscore.defaults.firstCall.args[0], {});

it('should create a proxy server', function () {
it('should create a proxy server', () => {
assert.isTrue(httpProxy.createProxyServer.calledOnce);
});
it('should handle the proxy server "proxyReq" event', function () {
it('should handle the proxy server "proxyReq" event', () => {
assert.isTrue(proxyServer.on.withArgs('proxyReq').calledOnce);

@@ -112,6 +117,8 @@ assert.isFunction(proxyServer.on.withArgs('proxyReq').firstCall.args[1]);

describe('proxy server "proxyReq" handler', function () {
var proxyOptions, proxyReqHandler, proxyRequest;
describe('proxy server "proxyReq" handler', () => {
let proxyOptions;
let proxyReqHandler;
let proxyRequest;
beforeEach(function () {
beforeEach(() => {
proxyOptions = {

@@ -125,7 +132,7 @@ target: 'http://localhost:1234'

it('should rewrite the host header of the proxy request', function () {
it('should rewrite the host header of the proxy request', () => {
assert.isTrue(proxyRequest.setHeader.withArgs('Host', 'localhost:1234').calledOnce);
});
it('should not rewrite the host header of the proxy request if `options.rewriteHostHeader` is `false`', function () {
it('should not rewrite the host header of the proxy request if `options.rewriteHostHeader` is `false`', () => {
proxyServer.on.reset();

@@ -142,3 +149,3 @@ proxyRequest.setHeader.reset();

it('should handle the proxy server "proxyRes" event', function () {
it('should handle the proxy server "proxyRes" event', () => {
assert.isTrue(proxyServer.on.withArgs('proxyRes').calledOnce);

@@ -148,6 +155,8 @@ assert.isFunction(proxyServer.on.withArgs('proxyRes').firstCall.args[1]);

describe('proxy server "proxyRes" handler', function () {
var proxyResHandler, proxyResponse, request;
describe('proxy server "proxyRes" handler', () => {
let proxyResHandler;
let proxyResponse;
let request;
beforeEach(function () {
beforeEach(() => {
proxyResponse = new http.ServerResponse();

@@ -160,3 +169,3 @@ request = new http.ClientRequest();

it('should log the successful proxying of the request', function () {
it('should log the successful proxying of the request', () => {
assert.isTrue(options.log.info.withArgs('Proxied "/foo" successfully').calledOnce);

@@ -167,10 +176,12 @@ });

it('should return a function', function () {
it('should return a function', () => {
assert.isFunction(middleware);
});
describe('returnedFunction()', function () {
var request, response, next;
describe('returnedFunction()', () => {
let next;
let request;
let response;
beforeEach(function () {
beforeEach(() => {
request = new http.ClientRequest();

@@ -183,30 +194,30 @@ request.url = '/foo';

it('should call `responseInterceptor` with the response', function () {
it('should call `responseInterceptor` with the response', () => {
assert.isTrue(responseInterceptor.withArgs(response).calledOnce);
});
it('should log that the request is being proxied', function () {
it('should log that the request is being proxied', () => {
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 () {
it('should call `proxyServer.web` with the request and response', () => {
assert.isTrue(proxyServer.web.withArgs(request, response).calledOnce);
});
it('should call `proxyServer.web` with a target of `options.target`', function () {
it('should call `proxyServer.web` with a target of `options.target`', () => {
assert.strictEqual(proxyServer.web.firstCall.args[2].target, options.target);
});
it('should call `proxyServer.web` with an error handler', function () {
it('should call `proxyServer.web` with an error handler', () => {
assert.isFunction(proxyServer.web.firstCall.args[3]);
});
describe('responseInterceptor `options.condition`', function () {
var condition;
describe('responseInterceptor `options.condition`', () => {
let condition;
beforeEach(function () {
beforeEach(() => {
condition = responseInterceptor.firstCall.args[1].condition;
});
it('should return `true` if response content-type is `options.contentType`', function () {
it('should return `true` if response content-type is `options.contentType`', () => {
response.getHeader.withArgs('content-type').returns('application/x-commandeer-unit+json');

@@ -220,3 +231,3 @@ assert.isTrue(condition());

it('should return `false` if response content-type is not `options.contentType`', function () {
it('should return `false` if response content-type is not `options.contentType`', () => {
response.getHeader.withArgs('content-type').returns('text/html');

@@ -226,9 +237,9 @@ assert.isFalse(condition());

it('should return `false` if response content-type is not set', function () {
it('should return `false` if response content-type is not set', () => {
assert.isFalse(condition());
});
describe('when `options.contentType` is an array', function () {
describe('when `options.contentType` is an array', () => {
beforeEach(function () {
beforeEach(() => {
options = {

@@ -248,3 +259,3 @@ contentType: [

it('should return `true` if response content-type is in `options.contentType`', function () {
it('should return `true` if response content-type is in `options.contentType`', () => {
response.getHeader.withArgs('content-type').returns('application/x-commandeer-unit1+json');

@@ -264,3 +275,3 @@ assert.isTrue(condition());

it('should return `false` if response content-type is not in `options.contentType`', function () {
it('should return `false` if response content-type is not in `options.contentType`', () => {
response.getHeader.withArgs('content-type').returns('application/x-commandeer-unit+json');

@@ -276,10 +287,10 @@ assert.isFalse(condition());

describe('responseInterceptor `options.writeHead`', function () {
var writeHead;
describe('responseInterceptor `options.writeHead`', () => {
let writeHead;
beforeEach(function () {
beforeEach(() => {
writeHead = responseInterceptor.firstCall.args[1].writeHead;
});
it('should set the response status code', function () {
it('should set the response status code', () => {
writeHead(200);

@@ -291,3 +302,3 @@ assert.strictEqual(response.statusCode, 200);

it('should remove the response content-type header', function () {
it('should remove the response content-type header', () => {
writeHead();

@@ -297,3 +308,3 @@ assert.isTrue(response.removeHeader.withArgs('Content-Type').calledOnce);

it('should remove the response content-length header', function () {
it('should remove the response content-length header', () => {
writeHead();

@@ -305,6 +316,7 @@ assert.isTrue(response.removeHeader.withArgs('Content-Length').calledOnce);

describe('responseInterceptor `options.end`', function () {
var end, write;
describe('responseInterceptor `options.end`', () => {
let end;
let write;
beforeEach(function () {
beforeEach(() => {
end = responseInterceptor.firstCall.args[1].end;

@@ -314,3 +326,3 @@ write = responseInterceptor.firstCall.args[1].write;

it('parse the concatenated result of all `write` calls as JSON and add to response under `options.dataProperty`', function () {
it('parse the concatenated result of all `write` calls as JSON and add to response under `options.dataProperty`', () => {
write(new Buffer('{'));

@@ -325,3 +337,3 @@ write(new Buffer('"foo": "bar"'));

it('should call `next` with no error if the JSON is valid', function () {
it('should call `next` with no error if the JSON is valid', () => {
write(new Buffer('{}'));

@@ -333,3 +345,3 @@ end();

it('call `next` with an error if the JSON is invalid', function () {
it('call `next` with an error if the JSON is invalid', () => {
write(new Buffer('{"foo"}'));

@@ -345,6 +357,7 @@ end();

describe('`proxyServer.web` error handler', function () {
var error, errorHandler;
describe('`proxyServer.web` error handler', () => {
let error;
let errorHandler;
beforeEach(function () {
beforeEach(() => {
error = new Error('...');

@@ -355,7 +368,7 @@ errorHandler = proxyServer.web.firstCall.args[3];

it('should log that the proxy failed', function () {
it('should log that the proxy failed', () => {
assert.isTrue(options.log.error.withArgs('Failed to proxy "/foo"').calledOnce);
});
it('should call `next` with the handled error', function () {
it('should call `next` with the handled error', () => {
assert.isTrue(next.withArgs(error).calledOnce);

@@ -370,6 +383,8 @@ });

describe('commandeer() with a function `target` option', function () {
var options, middleware, proxyServer;
describe('commandeer() with a function `target` option', () => {
let middleware;
let options;
let proxyServer;
beforeEach(function () {
beforeEach(() => {
options = {

@@ -389,6 +404,8 @@ contentType: 'application/x-commandeer-unit+json',

describe('returnedFunction()', function () {
var request, response, next;
describe('returnedFunction()', () => {
let next;
let request;
let response;
beforeEach(function () {
beforeEach(() => {
request = new http.ClientRequest();

@@ -400,7 +417,7 @@ response = new http.ServerResponse();

it('should call `options.target` with the request object', function () {
it('should call `options.target` with the request object', () => {
assert.isTrue(options.target.withArgs(request).calledOnce);
});
it('should call `proxyServer.web` with a target set to the return value of `options.target`', function () {
it('should call `proxyServer.web` with a target set to the return value of `options.target`', () => {
assert.strictEqual(proxyServer.web.firstCall.args[2].target, options.target.firstCall.returnValue);

@@ -407,0 +424,0 @@ });

@@ -1,13 +0,14 @@

// jshint maxstatements: false
// jscs:disable disallowMultipleVarDecl, maximumLineLength
// jscs:disable maximumLineLength
'use strict';
var assert = require('proclaim');
var mockery = require('mockery');
var sinon = require('sinon');
const assert = require('proclaim');
const mockery = require('mockery');
const sinon = require('sinon');
describe('lib/response-interceptor', function () {
var http, responseInterceptor, underscore;
describe('lib/response-interceptor', () => {
let http;
let responseInterceptor;
let underscore;
beforeEach(function () {
beforeEach(() => {

@@ -23,10 +24,12 @@ http = require('../mock/http');

it('should be a function', function () {
it('should be a function', () => {
assert.isFunction(responseInterceptor);
});
describe('responseInterceptor()', function () {
var response, responseBackup, options;
describe('responseInterceptor()', () => {
let options;
let response;
let responseBackup;
beforeEach(function () {
beforeEach(() => {
response = new http.ServerResponse();

@@ -47,25 +50,25 @@ responseBackup = {

it('should replace the `response.write` method', function () {
it('should replace the `response.write` method', () => {
assert.notStrictEqual(response.write, responseBackup.write);
});
it('should replace the `response.writeHead` method', function () {
it('should replace the `response.writeHead` method', () => {
assert.notStrictEqual(response.writeHead, responseBackup.writeHead);
});
it('should replace the `response.end` method', function () {
it('should replace the `response.end` method', () => {
assert.notStrictEqual(response.end, responseBackup.end);
});
it('should memoize `options.condition`', function () {
it('should memoize `options.condition`', () => {
assert.isTrue(underscore.memoize.withArgs(options.condition).calledOnce);
});
describe('when `options.condition` returns `false`', function () {
describe('when `options.condition` returns `false`', () => {
beforeEach(function () {
beforeEach(() => {
options.condition.returns(false);
});
it('should call the original `response.write`', function () {
it('should call the original `response.write`', () => {
response.write('foo', 'bar');

@@ -77,3 +80,3 @@ assert.isTrue(responseBackup.write.withArgs('foo', 'bar').calledOnce, 'Called once');

it('should call the original `response.writeHead`', function () {
it('should call the original `response.writeHead`', () => {
response.writeHead('foo', 'bar');

@@ -85,3 +88,3 @@ assert.isTrue(responseBackup.writeHead.withArgs('foo', 'bar').calledOnce, 'Called once');

it('should restore the original methods and call the original `response.end`', function () {
it('should restore the original methods and call the original `response.end`', () => {
response.end('foo', 'bar');

@@ -97,9 +100,9 @@ assert.isTrue(responseBackup.end.withArgs('foo', 'bar').calledOnce, 'Called once');

describe('when `options.condition` returns `true`', function () {
describe('when `options.condition` returns `true`', () => {
beforeEach(function () {
beforeEach(() => {
options.condition.returns(true);
});
it('should call `options.write`', function () {
it('should call `options.write`', () => {
response.write('foo', 'bar');

@@ -111,3 +114,3 @@ assert.isTrue(options.write.withArgs('foo', 'bar').calledOnce, 'Called once');

it('should call `options.writeHead`', function () {
it('should call `options.writeHead`', () => {
response.writeHead('foo', 'bar');

@@ -119,3 +122,3 @@ assert.isTrue(options.writeHead.withArgs('foo', 'bar').calledOnce, 'Called once');

it('should restore the original methods and call `options.end`', function () {
it('should restore the original methods and call `options.end`', () => {
response.end('foo', 'bar');

@@ -122,0 +125,0 @@ assert.isTrue(options.end.withArgs('foo', 'bar').calledOnce, 'Called once');

'use strict';
var sinon = require('sinon');
const sinon = require('sinon');

@@ -5,0 +5,0 @@ module.exports = {

'use strict';
var sinon = require('sinon');
const sinon = require('sinon');

@@ -5,0 +5,0 @@ module.exports = {

'use strict';
var _ = require('underscore');
var sinon = require('sinon');
const _ = require('underscore');
const sinon = require('sinon');

@@ -6,0 +6,0 @@ module.exports = {

@@ -1,8 +0,7 @@

// jshint maxstatements: false
// jscs:disable disallowMultipleVarDecl, maximumLineLength
// jscs:disable maximumLineLength
'use strict';
var mockery = require('mockery');
const mockery = require('mockery');
beforeEach(function () {
beforeEach(() => {
mockery.enable({

@@ -15,5 +14,5 @@ useCleanCache: true,

afterEach(function () {
afterEach(() => {
mockery.deregisterAll();
mockery.disable();
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet