Comparing version 0.0.7 to 0.1.0
136
lib/echo.js
@@ -8,77 +8,101 @@ /* | ||
var scheme = require('./scheme'); | ||
var parse = require('url').parse; | ||
var path = require('path'); | ||
var BASE = {}; | ||
exports.scheme = scheme; | ||
/* | ||
//Load more route listeners | ||
var load = function (obj) { | ||
Object.keys(obj).forEach(function (key) { | ||
scheme[key] = obj[key]; | ||
}); | ||
}; | ||
EchoEcho({ | ||
paths: [] //Array of paths | ||
all: true //Turn on all paths with 'echo' in the url | ||
}) | ||
exports.load = load; | ||
*/ | ||
var EchoEcho = function(options) { | ||
this.BASE = {}; | ||
this.options = options || {}; | ||
var serve = function (req, res) { | ||
var url = req.url.split('?')[0], | ||
route, | ||
base; | ||
this.scheme = require('./scheme'); | ||
Object.keys(BASE).forEach(function (b) { | ||
if (url.indexOf(b) === 0) { | ||
route = url.replace((b + '/echo/'), ''); | ||
} | ||
}); | ||
if (route) { | ||
base = route.split('/'); | ||
if (base[0] === 'status') { | ||
scheme.status(base[1], res); | ||
} else if (scheme[base[0]]) { | ||
scheme[base[0]](req, res); | ||
} else { | ||
scheme.status(404, res); | ||
} | ||
} else { | ||
scheme.status(404, res); | ||
if (this.options.paths && !this.options.all) { | ||
this.paths(this.options.paths); | ||
} | ||
}; | ||
exports.serve = serve; | ||
EchoEcho.prototype = { | ||
load: function (obj) { | ||
var self = this; | ||
Object.keys(obj).forEach(function (key) { | ||
self.scheme[key] = obj[key]; | ||
}); | ||
}, | ||
validate: function(req, lax) { | ||
if (typeof req === 'object') { | ||
req = req.url; | ||
} | ||
var handle = function (req) { | ||
var url = req.url.split('?')[0], | ||
ret = false; | ||
var url = req.split('?')[0], | ||
ret = false, | ||
route, | ||
base; | ||
Object.keys(BASE).forEach(function (b) { | ||
if (url.indexOf(b) === 0) { | ||
ret = true; | ||
if (this.options.all) { | ||
//Catch all (non-registered) echo urls. | ||
if (!ret && url.indexOf('/echo/') > -1) { | ||
route = url.split('/echo/')[1]; | ||
ret = true; | ||
} | ||
} else { | ||
Object.keys(this.BASE).forEach(function (b) { | ||
if (url.indexOf(b) === 0) { | ||
route = url.replace((b + '/echo/'), ''); | ||
ret = true; | ||
} | ||
}); | ||
} | ||
}); | ||
return ret; | ||
}; | ||
exports.handle = handle; | ||
var paths = function (p) { | ||
p.forEach(function (dir) { | ||
var base = dir; | ||
if (path.basename(dir).indexOf('.') > -1) { | ||
base = path.dirname(dir); | ||
if (route && !lax) { | ||
ret = false; | ||
base = route.split('/'); | ||
if (this.scheme[base[0]]) { | ||
ret = base[0]; | ||
if (base[0] === 'status') { | ||
ret = base[1]; | ||
} | ||
} | ||
} | ||
if (base.substr(-1) === '/') { | ||
base = base.substring(0, base.length - 1); | ||
return ret; | ||
}, | ||
handle: function (req) { | ||
return !!this.validate(req, true); | ||
}, | ||
serve: function (req, res) { | ||
var base = this.validate(req); | ||
if (base && base !== true) { | ||
if (this.scheme[base]) { | ||
this.scheme[base](req, res); | ||
} else { | ||
this.scheme.status(base, res); | ||
} | ||
} else { | ||
this.scheme.status(404, res); | ||
} | ||
BASE[parse(base).path] = true; | ||
}); | ||
}, | ||
paths: function (p) { | ||
p.forEach(function (dir) { | ||
var base = dir; | ||
if (path.basename(dir).indexOf('.') > -1) { | ||
base = path.dirname(dir); | ||
} | ||
if (base.substr(-1) === '/') { | ||
base = base.substring(0, base.length - 1); | ||
} | ||
this.BASE[parse(base).path] = true; | ||
}, this); | ||
} | ||
}; | ||
exports.BASE = BASE; | ||
module.exports = new EchoEcho(); | ||
exports.paths = paths; | ||
module.exports.EchoEcho = EchoEcho; |
@@ -5,3 +5,3 @@ { | ||
"author": "Dav Glass <davglass@gmail.com>", | ||
"version": "0.0.7", | ||
"version": "0.1.0", | ||
"devDependencies": { | ||
@@ -11,7 +11,7 @@ "vows": "*", | ||
"jshint": "~0.9.0", | ||
"istanbul": "*" | ||
"istanbul": "~0.1.8" | ||
}, | ||
"scripts": { | ||
"pretest": "jshint --config ./node_modules/yui-lint/jshint.json ./lib/*.js", | ||
"test": "istanbul test vows -- --spec ./tests/*.js" | ||
"test": "istanbul cover --print both -- vows --spec ./tests/*.js" | ||
}, | ||
@@ -18,0 +18,0 @@ "main": "./lib/echo.js", |
@@ -63,5 +63,30 @@ EchoEcho | ||
} | ||
``` | ||
Instantiate an EchoEcho object: | ||
``` | ||
var ee = new echoecho.EchoEcho({ | ||
paths: [] //base paths | ||
}); | ||
//Like above | ||
ee.handle(); | ||
ee.serve(); | ||
``` | ||
Handling all requests with /echo/ in the URL: | ||
``` | ||
var ee = new require('echoecho').EchoEcho({ | ||
all: true; | ||
}); | ||
//Like above | ||
ee.handle(); | ||
ee.serve(); | ||
``` | ||
Using in Your Tests | ||
@@ -68,0 +93,0 @@ ------------------- |
@@ -96,2 +96,5 @@ var vows = require('vows'), | ||
}, | ||
'and have EchoEcho Function': function (topic) { | ||
assert.isFunction(topic.EchoEcho); | ||
}, | ||
'and should load paths': { | ||
@@ -118,2 +121,38 @@ topic: function() { | ||
}, | ||
'and use handler with object': { | ||
topic: function() { | ||
return echoecho.handle({ | ||
url: '/foo/bar/baz/echo/status/500' | ||
}); | ||
}, | ||
'should return true': function(topic) { | ||
assert.isTrue(topic); | ||
} | ||
}, | ||
'and use handler with string': { | ||
topic: function() { | ||
return echoecho.handle('/foo/bar/baz/echo/jsonp'); | ||
}, | ||
'should return true': function(topic) { | ||
assert.isTrue(topic); | ||
} | ||
}, | ||
'and use validator with object': { | ||
topic: function() { | ||
return echoecho.validate({ | ||
url: '/foo/bar/baz/echo/status/500' | ||
}); | ||
}, | ||
'should return true': function(topic) { | ||
assert.equal(topic, '500'); | ||
} | ||
}, | ||
'and use validator with string': { | ||
topic: function() { | ||
return echoecho.validate('/foo/bar/baz/echo/jsonp'); | ||
}, | ||
'should return true': function(topic) { | ||
assert.equal(topic, 'jsonp'); | ||
} | ||
}, | ||
"and default response": { | ||
@@ -551,2 +590,68 @@ topic: function() { | ||
} | ||
}, | ||
'should be instantitable': { | ||
'should be different objects': { | ||
topic: function() { | ||
var one = new echoecho.EchoEcho({ | ||
paths: ['/one/one.html'] | ||
}); | ||
var two = new echoecho.EchoEcho({ | ||
paths: ['/two/one/', '/two/two/'] | ||
}); | ||
return [one, two] | ||
}, | ||
'different paths': function(topic) { | ||
assert.notDeepEqual(topic[0].BASE, topic[1].BASE); | ||
} | ||
}, | ||
'unknown queries with all option': { | ||
'and use validator with object': { | ||
topic: function() { | ||
return new echoecho.EchoEcho({ all: true }).validate({ | ||
url: '/have/not/used/echo/status/500' | ||
}); | ||
}, | ||
'should return "500"': function(topic) { | ||
assert.equal(topic, '500'); | ||
} | ||
}, | ||
'and use validator with string': { | ||
topic: function() { | ||
return new echoecho.EchoEcho({ all: true }).validate('/have/not/used/this/yet/echo/jsonp'); | ||
}, | ||
'should return "jsonp"': function(topic) { | ||
assert.equal(topic, 'jsonp'); | ||
} | ||
}, | ||
'and use validator with no echo': { | ||
topic: function() { | ||
return new echoecho.EchoEcho({ all: true }).validate('/have/not/used/this/yet/ech/jsonp'); | ||
}, | ||
'should return false': function(topic) { | ||
assert.isFalse(topic); | ||
} | ||
} | ||
}, | ||
'unknown queries without all option': { | ||
'and use validator with object': { | ||
topic: function() { | ||
return new echoecho.EchoEcho().validate({ | ||
url: '/have/not/used/echo/status/500' | ||
}); | ||
}, | ||
'should return false': function(topic) { | ||
assert.isFalse(topic); | ||
} | ||
}, | ||
'and use validator with string': { | ||
topic: function() { | ||
return new echoecho.EchoEcho().validate('/have/not/used/this/yet/echo/jsonp'); | ||
}, | ||
'should return false': function(topic) { | ||
assert.isFalse(topic); | ||
} | ||
} | ||
} | ||
} | ||
@@ -553,0 +658,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
38502
907
120