From d73f66ac297598d4f652687bc1233392635975da Mon Sep 17 00:00:00 2001 From: seglo Date: Sat, 7 Jun 2014 13:35:00 -0400 Subject: [PATCH] Force spaces instead of tabs. --- .jshintrc | 4 +- Gruntfile.js | 1 + lib/events.js | 44 ++++---- lib/modes.js | 110 +++++++++--------- lib/proxies.js | 76 ++++++------- lib/utils.js | 16 +-- tasks/prism-task.js | 106 ++++++++--------- test/prism-task-spec.js | 244 ++++++++++++++++++++-------------------- 8 files changed, 302 insertions(+), 299 deletions(-) diff --git a/.jshintrc b/.jshintrc index 22ee206..0d6d793 100644 --- a/.jshintrc +++ b/.jshintrc @@ -17,5 +17,7 @@ "undef": true, // true: Require all non-global variables to be declared (prevents global leaks) "boss": true, // true: Tolerate assignments where comparisons would be expected "eqnull": true, // true: Tolerate use of `== null` - "node": true // Node.js + "node": true, // Node.js + "smarttabs" : true, + "indent": 2 } diff --git a/Gruntfile.js b/Gruntfile.js index 8a2e5cf..c8e1ce0 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -101,6 +101,7 @@ module.exports = function(grunt) { // plugin's task(s), then test the result. grunt.registerTask('test', [ 'clean', + 'jshint', 'prism:proxyTest', 'prism:recordTest', 'prism:mockTest', diff --git a/lib/events.js b/lib/events.js index 79a8c24..91a1f76 100644 --- a/lib/events.js +++ b/lib/events.js @@ -10,29 +10,29 @@ var modes = require('./modes.js'); var proxies = require('./proxies.js'); module.exports = { - handleRequest: function(req, res, next) { - var proxy = proxies.getProxy(req.url); + handleRequest: function(req, res, next) { + var proxy = proxies.getProxy(req.url); - // if we have a proxy configured then handle this request, else pass the buck to the next connect middleware - if (proxy) { - if (proxy.config.mode === 'mock') { - modes.mock(proxy, req, res); - } else if (proxy.config.mode === 'proxy' || proxy.config.mode === 'record') { - modes.proxy(proxy, req, res); - } else { - throw new Error('No such mode: ' + proxy.config.mode); - } - } else { - next(); - } - }, - handleResponse: function(res) { - var proxy = proxies.getProxy(res.req.path); + // if we have a proxy configured then handle this request, else pass the buck to the next connect middleware + if (proxy) { + if (proxy.config.mode === 'mock') { + modes.mock(proxy, req, res); + } else if (proxy.config.mode === 'proxy' || proxy.config.mode === 'record') { + modes.proxy(proxy, req, res); + } else { + throw new Error('No such mode: ' + proxy.config.mode); + } + } else { + next(); + } + }, + handleResponse: function(res) { + var proxy = proxies.getProxy(res.req.path); - if (_.isUndefined(proxy) || proxy.config.mode !== 'record') { - return; - } + if (_.isUndefined(proxy) || proxy.config.mode !== 'record') { + return; + } - modes.record(proxy, res); - } + modes.record(proxy, res); + } }; \ No newline at end of file diff --git a/lib/modes.js b/lib/modes.js index 17beb92..b3d6779 100644 --- a/lib/modes.js +++ b/lib/modes.js @@ -10,79 +10,79 @@ var utils = require('./utils.js'); // TODO: figure out how to buffer file stream into response function writeResponse(path, res) { - var responseStr = fs.readFileSync(path).toString(); - var response = JSON.parse(responseStr); + var responseStr = fs.readFileSync(path).toString(); + var response = JSON.parse(responseStr); - res.writeHead(response.statusCode, { - 'Content-Type': response.responseHeaders['content-type'] - }); - res.write(response.data); - res.end(); + res.writeHead(response.statusCode, { + 'Content-Type': response.responseHeaders['content-type'] + }); + res.write(response.data); + res.end(); } function write404(req, res) { - res.writeHead(404, { - 'Content-Type': 'text/plain' - }); - res.write('No mock exists for ' + req.url); - res.end(); + res.writeHead(404, { + 'Content-Type': 'text/plain' + }); + res.write('No mock exists for ' + req.url); + res.end(); } function serializeResponse(proxy, res, data) { - var response = { - requestUrl: res.req.path, - responseHeaders: res.headers, - statusCode: res.statusCode, - data: data // TODO: if header content-type is JSON then save data as JSON instead of string - }; + var response = { + requestUrl: res.req.path, + responseHeaders: res.headers, + statusCode: res.statusCode, + data: data // TODO: if header content-type is JSON then save data as JSON instead of string + }; - var serializedResponse = JSON.stringify(response, true, 2); + var serializedResponse = JSON.stringify(response, true, 2); - var requestFilename = utils.hashUrl(res.req.path); + var requestFilename = utils.hashUrl(res.req.path); - var finalPath = path.join(proxy.config.mocksPath, requestFilename); + var finalPath = path.join(proxy.config.mocksPath, requestFilename); - // write file async to disk. overwrite if it already exists. prettyprint. - fs.writeFile(finalPath, serializedResponse); + // write file async to disk. overwrite if it already exists. prettyprint. + fs.writeFile(finalPath, serializedResponse); } function logSuccess(modeMsg, proxy, req) { - var target = utils.absoluteUrl(proxy, req.url); - var source = req.originalUrl; - grunt.log.verbose.writeln(modeMsg + ' request: ' + source + ' -> ' + target + '\n' + JSON.stringify(req.headers, true, 2)); + var target = utils.absoluteUrl(proxy, req.url); + var source = req.originalUrl; + grunt.log.verbose.writeln(modeMsg + ' request: ' + source + ' -> ' + target + '\n' + JSON.stringify(req.headers, true, 2)); } module.exports = { - proxy: function(proxy, req, res) { - var target = utils.absoluteUrl(proxy, req.url); + proxy: function(proxy, req, res) { + var target = utils.absoluteUrl(proxy, req.url); - proxy.server.web(req, res, { - target: target - }); + proxy.server.web(req, res, { + target: target + }); - logSuccess('Proxied', proxy, req); - }, - record: function(proxy, res) { - var data = ''; - res.on('data', function(chunk) { - data += chunk; - }); - res.on('end', function() { - serializeResponse(proxy, res, data); - logSuccess('Recorded', proxy, res.req); - }); - }, - mock: function(proxy, req, res) { - var diskResponse = path.join(proxy.config.mocksPath, utils.hashUrl(req.url)); + logSuccess('Proxied', proxy, req); + }, + record: function(proxy, res) { + var data = ''; + res.on('data', function(chunk) { + data += chunk; + }); + res.on('end', function() { + serializeResponse(proxy, res, data); + logSuccess('Recorded', proxy, res.req); + }); + }, + mock: function(proxy, req, res) { + var diskResponse = path.join(proxy.config.mocksPath, utils.hashUrl(req.url)); - fs.exists(diskResponse, function(exists) { - if (exists) { - writeResponse(diskResponse, res); - logSuccess('Mocked', proxy, req); - } else { - write404(req, res); - grunt.log.verbose.writeln('Returned 404 for: ' + req.url); - } - }); - } + fs.exists(diskResponse, function(exists) { + if (exists) { + writeResponse(diskResponse, res); + logSuccess('Mocked', proxy, req); + } else { + write404(req, res); + grunt.log.verbose.writeln('Returned 404 for: ' + req.url); + } + }); + } }; \ No newline at end of file diff --git a/lib/proxies.js b/lib/proxies.js index 9c4ca94..473f4ad 100644 --- a/lib/proxies.js +++ b/lib/proxies.js @@ -12,45 +12,45 @@ var proxies = []; // ripped from grunt-connect-proxy to match contexts function matchContext(context, url) { - var positiveContexts, negativeContexts, positiveMatch, negativeMatch; - var contexts = context; - if (!_.isArray(contexts)) { - contexts = [contexts]; - } - positiveContexts = _.filter(contexts, function(c) { - return c.charAt(0) !== '!'; - }); - negativeContexts = _.filter(contexts, function(c) { - return c.charAt(0) === '!'; - }); - // Remove the '!' character from the contexts - negativeContexts = _.map(negativeContexts, function(c) { - return c.slice(1); - }); - negativeMatch = _.find(negativeContexts, function(c) { - return url.lastIndexOf(c, 0) === 0; - }); - // If any context negates this url, it must not be proxied. - if (negativeMatch) { - return false; - } - positiveMatch = _.find(positiveContexts, function(c) { - return url.lastIndexOf(c, 0) === 0; - }); - // If there is any positive match, lets proxy this url. - return positiveMatch != null; + var positiveContexts, negativeContexts, positiveMatch, negativeMatch; + var contexts = context; + if (!_.isArray(contexts)) { + contexts = [contexts]; + } + positiveContexts = _.filter(contexts, function(c) { + return c.charAt(0) !== '!'; + }); + negativeContexts = _.filter(contexts, function(c) { + return c.charAt(0) === '!'; + }); + // Remove the '!' character from the contexts + negativeContexts = _.map(negativeContexts, function(c) { + return c.slice(1); + }); + negativeMatch = _.find(negativeContexts, function(c) { + return url.lastIndexOf(c, 0) === 0; + }); + // If any context negates this url, it must not be proxied. + if (negativeMatch) { + return false; + } + positiveMatch = _.find(positiveContexts, function(c) { + return url.lastIndexOf(c, 0) === 0; + }); + // If there is any positive match, lets proxy this url. + return positiveMatch != null; } module.exports = { - getProxy: function(path) { - return _.find(proxies, function(proxy) { - return matchContext(proxy.config.context, path); - }); - }, - proxies: function() { - return proxies; - }, - add: function(proxy) { - proxies.push(proxy); - } + getProxy: function(path) { + return _.find(proxies, function(proxy) { + return matchContext(proxy.config.context, path); + }); + }, + proxies: function() { + return proxies; + }, + add: function(proxy) { + proxies.push(proxy); + } }; \ No newline at end of file diff --git a/lib/utils.js b/lib/utils.js index a68836f..691fbb8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -3,12 +3,12 @@ var crypto = require('crypto'); module.exports = { - hashUrl: function(url) { - var shasum = crypto.createHash('sha1'); - shasum.update(url); - return shasum.digest('hex'); - }, - absoluteUrl: function(proxy, url) { - return (proxy.config.https ? 'https://' : 'http://') + proxy.config.host + ':' + proxy.config.port + url; - } + hashUrl: function(url) { + var shasum = crypto.createHash('sha1'); + shasum.update(url); + return shasum.digest('hex'); + }, + absoluteUrl: function(proxy, url) { + return (proxy.config.https ? 'https://' : 'http://') + proxy.config.host + ':' + proxy.config.port + url; + } }; \ No newline at end of file diff --git a/tasks/prism-task.js b/tasks/prism-task.js index 85b542b..1894d3c 100644 --- a/tasks/prism-task.js +++ b/tasks/prism-task.js @@ -5,68 +5,68 @@ var events = require('../lib/events.js'); var proxies = require('../lib/proxies.js'); module.exports = function(grunt) { - function validateProxyConfig (proxyOption) { - if (_.isUndefined(proxyOption)) { - grunt.log.error('Prism config is missing.'); - return false; - } + function validateProxyConfig(proxyOption) { + if (_.isUndefined(proxyOption)) { + grunt.log.error('Prism config is missing.'); + return false; + } - var mode = proxyOption.mode; - if (_.isUndefined(mode) || (mode !== 'record' && mode !== 'mock' && mode !== 'proxy')) { - grunt.log.error('Prism mode: \'' + mode + '\' is invalid.'); - return false; - } + var mode = proxyOption.mode; + if (_.isUndefined(mode) || (mode !== 'record' && mode !== 'mock' && mode !== 'proxy')) { + grunt.log.error('Prism mode: \'' + mode + '\' is invalid.'); + return false; + } - var mocksPath = proxyOption.mocksPath; - if (_.isUndefined(mocksPath) || !grunt.file.isDir(mocksPath)) { - grunt.log.error('Prism mocksPath: \'' + mocksPath + '\' is invalid.'); - return false; - } + var mocksPath = proxyOption.mocksPath; + if (_.isUndefined(mocksPath) || !grunt.file.isDir(mocksPath)) { + grunt.log.error('Prism mocksPath: \'' + mocksPath + '\' is invalid.'); + return false; + } - if (_.isUndefined(proxyOption.host) || _.isUndefined(proxyOption.context)) { - grunt.log.error('Proxy missing host or context configuration'); - return false; - } - if (proxyOption.https && proxyOption.port === 80) { - grunt.log.warn('Proxy for ' + proxyOption.context + ' is using https on port 80. Are you sure this is correct?'); - } - return true; - } + if (_.isUndefined(proxyOption.host) || _.isUndefined(proxyOption.context)) { + grunt.log.error('Proxy missing host or context configuration'); + return false; + } + if (proxyOption.https && proxyOption.port === 80) { + grunt.log.warn('Proxy for ' + proxyOption.context + ' is using https on port 80. Are you sure this is correct?'); + } + return true; + } - grunt.registerTask('prism', 'Configure any specified connect proxies for prism.', function(target) { - // setup proxy - var httpProxy = require('http-proxy'); - var proxyOption; - var proxyOptions = []; + grunt.registerTask('prism', 'Configure any specified connect proxies for prism.', function(target) { + // setup proxy + var httpProxy = require('http-proxy'); + var proxyOption; + var proxyOptions = []; - if (target) { - var prismOptions = grunt.config('prism.' + target + '.options') || []; - proxyOptions = proxyOptions.concat(prismOptions.proxies || []); - } + if (target) { + var prismOptions = grunt.config('prism.' + target + '.options') || []; + proxyOptions = proxyOptions.concat(prismOptions.proxies || []); + } - // TODO: add support to load proxies of each child config if a target isn't provided + // TODO: add support to load proxies of each child config if a target isn't provided - proxyOptions = proxyOptions.concat(grunt.config('prism.options.proxies') || []); + proxyOptions = proxyOptions.concat(grunt.config('prism.options.proxies') || []); - proxyOptions.forEach(function(proxy) { - proxyOption = _.defaults(proxy, { - port: 80, - https: false, - mode: 'proxy', - mocksPath: './mocks' - }); + proxyOptions.forEach(function(proxy) { + proxyOption = _.defaults(proxy, { + port: 80, + https: false, + mode: 'proxy', + mocksPath: './mocks' + }); - if (validateProxyConfig(proxyOption)) { - var proxyServer = httpProxy.createProxyServer() - .on('proxyRes', events.handleResponse); + if (validateProxyConfig(proxyOption)) { + var proxyServer = httpProxy.createProxyServer() + .on('proxyRes', events.handleResponse); - proxies.add({ - server: proxyServer, - config: proxyOption - }); + proxies.add({ + server: proxyServer, + config: proxyOption + }); - grunt.log.writeln('Proxy created for: ' + proxyOption.context + ' to ' + proxyOption.host + ':' + proxyOption.port); - } - }); - }); + grunt.log.writeln('Proxy created for: ' + proxyOption.context + ' to ' + proxyOption.host + ':' + proxyOption.port); + } + }); + }); }; \ No newline at end of file diff --git a/test/prism-task-spec.js b/test/prism-task-spec.js index edb96d3..ac36eb6 100644 --- a/test/prism-task-spec.js +++ b/test/prism-task-spec.js @@ -13,126 +13,126 @@ var utils = require('../lib/utils.js'); var requestTimeout = 5000; // 5 seconds describe('Prism', function() { - describe('task initialization', function() { - it('should have initialized 3 proxies', function() { - assert.equal(3, proxies.proxies().length); - }); - - it('request options should be correctly mapped', function() { - var proxy = proxies.getProxy('/proxyRequest'); - - assert.equal(_.isUndefined(proxy), false); - assert.equal(proxy.config.mode, 'proxy'); - assert.equal(proxy.config.mocksPath, './mocks'); - }); - }); - - describe('proxy modes', function() { - var testServer = http.createServer(function(req, res) { - res.writeHead(200, { - 'Content-Type': 'text/plain' - }); - res.write('a server response'); - res.end(); - }).listen(8090); - - it('can proxy a response', function(done) { - var request = http.request({ - host: 'localhost', - path: '/proxyRequest', - port: 9000 - }, function(res) { - var data = ''; - res.on('data', function(chunk) { - data += chunk; - }); - res.on('end', function() { - assert.equal(data, 'a server response'); - done(); - }); - }); - request.end(); - }); - - it('can record a response', function(done) { - var request = http.request({ - host: 'localhost', - path: '/recordRequest', - port: 9000 - }, function(res) { - var data = ''; - res.on('data', function(chunk) { - data += chunk; - }); - res.on('end', function() { - var proxy = proxies.getProxy(res.req.path); - - assert.equal(_.isUndefined(proxy), false); - - var pathToResponse = path.join(proxy.config.mocksPath, utils.hashUrl(res.req.path)); - - var waitForFile = function() { - if (fs.statSync(pathToResponse).size === 0) { - setTimeout(waitForFile, 20); - return; - } - - var recordedResponse = fs.readFileSync(pathToResponse).toString(); - var deserializedResponse = JSON.parse(recordedResponse); - - assert.equal(_.isUndefined(deserializedResponse), false); - assert.equal(deserializedResponse.requestUrl, '/recordRequest'); - assert.equal(deserializedResponse.data, 'a server response'); - - done(); - }; - - waitForFile(); - }); - }); - request.end(); - }); - - it('can mock a response', function(done) { - var request = http.request({ - host: 'localhost', - path: '/readRequest', - port: 9000 - }, function(res) { - var data = ''; - res.on('data', function(chunk) { - data += chunk; - }); - res.on('end', function() { - assert.equal(res.statusCode, 200); - assert.equal(res.req.path, '/readRequest'); - assert.equal(data, 'a server response'); - done(); - - }); - }); - request.end(); - }); - - it('can handle a 404 in mock mode', function(done) { - var request = http.request({ - host: 'localhost', - path: '/readRequestThatDoesntExist', - port: 9000 - }, function(res) { - var data = ''; - res.on('data', function(chunk) { - data += chunk; - }); - res.on('end', function() { - assert.equal(res.statusCode, 404); - assert.equal(res.req.path, '/readRequestThatDoesntExist'); - assert.equal(data, 'No mock exists for /readRequestThatDoesntExist'); - done(); - - }); - }); - request.end(); - }); - }); + describe('task initialization', function() { + it('should have initialized 3 proxies', function() { + assert.equal(3, proxies.proxies().length); + }); + + it('request options should be correctly mapped', function() { + var proxy = proxies.getProxy('/proxyRequest'); + + assert.equal(_.isUndefined(proxy), false); + assert.equal(proxy.config.mode, 'proxy'); + assert.equal(proxy.config.mocksPath, './mocks'); + }); + }); + + describe('proxy modes', function() { + var testServer = http.createServer(function(req, res) { + res.writeHead(200, { + 'Content-Type': 'text/plain' + }); + res.write('a server response'); + res.end(); + }).listen(8090); + + it('can proxy a response', function(done) { + var request = http.request({ + host: 'localhost', + path: '/proxyRequest', + port: 9000 + }, function(res) { + var data = ''; + res.on('data', function(chunk) { + data += chunk; + }); + res.on('end', function() { + assert.equal(data, 'a server response'); + done(); + }); + }); + request.end(); + }); + + it('can record a response', function(done) { + var request = http.request({ + host: 'localhost', + path: '/recordRequest', + port: 9000 + }, function(res) { + var data = ''; + res.on('data', function(chunk) { + data += chunk; + }); + res.on('end', function() { + var proxy = proxies.getProxy(res.req.path); + + assert.equal(_.isUndefined(proxy), false); + + var pathToResponse = path.join(proxy.config.mocksPath, utils.hashUrl(res.req.path)); + + var waitForFile = function() { + if (fs.statSync(pathToResponse).size === 0) { + setTimeout(waitForFile, 20); + return; + } + + var recordedResponse = fs.readFileSync(pathToResponse).toString(); + var deserializedResponse = JSON.parse(recordedResponse); + + assert.equal(_.isUndefined(deserializedResponse), false); + assert.equal(deserializedResponse.requestUrl, '/recordRequest'); + assert.equal(deserializedResponse.data, 'a server response'); + + done(); + }; + + waitForFile(); + }); + }); + request.end(); + }); + + it('can mock a response', function(done) { + var request = http.request({ + host: 'localhost', + path: '/readRequest', + port: 9000 + }, function(res) { + var data = ''; + res.on('data', function(chunk) { + data += chunk; + }); + res.on('end', function() { + assert.equal(res.statusCode, 200); + assert.equal(res.req.path, '/readRequest'); + assert.equal(data, 'a server response'); + done(); + + }); + }); + request.end(); + }); + + it('can handle a 404 in mock mode', function(done) { + var request = http.request({ + host: 'localhost', + path: '/readRequestThatDoesntExist', + port: 9000 + }, function(res) { + var data = ''; + res.on('data', function(chunk) { + data += chunk; + }); + res.on('end', function() { + assert.equal(res.statusCode, 404); + assert.equal(res.req.path, '/readRequestThatDoesntExist'); + assert.equal(data, 'No mock exists for /readRequestThatDoesntExist'); + done(); + + }); + }); + request.end(); + }); + }); }); \ No newline at end of file