Skip to content
Browse files

fixed getCustom and related tests

  • Loading branch information...
1 parent 111a450 commit 35da5577ac4a62fa6d84a9e35e59e2fc3e337445 @kilianc committed Jun 1, 2012
Showing with 132 additions and 127 deletions.
  1. +119 −116 lib-cov/router.js
  2. +2 −1 lib/router.js
  3. +2 −2 test/fixtures/router-module-class.js
  4. +2 −2 test/fixtures/router-module.js
  5. +7 −6 test/router-test.js
View
235 lib-cov/router.js
@@ -51,18 +51,18 @@ if (! _$jscoverage['router.js']) {
_$jscoverage['router.js'][85] = 0;
_$jscoverage['router.js'][86] = 0;
_$jscoverage['router.js'][87] = 0;
- _$jscoverage['router.js'][89] = 0;
+ _$jscoverage['router.js'][88] = 0;
_$jscoverage['router.js'][90] = 0;
_$jscoverage['router.js'][91] = 0;
- _$jscoverage['router.js'][94] = 0;
+ _$jscoverage['router.js'][92] = 0;
_$jscoverage['router.js'][95] = 0;
_$jscoverage['router.js'][96] = 0;
- _$jscoverage['router.js'][98] = 0;
+ _$jscoverage['router.js'][97] = 0;
_$jscoverage['router.js'][99] = 0;
_$jscoverage['router.js'][100] = 0;
- _$jscoverage['router.js'][102] = 0;
- _$jscoverage['router.js'][104] = 0;
- _$jscoverage['router.js'][107] = 0;
+ _$jscoverage['router.js'][101] = 0;
+ _$jscoverage['router.js'][103] = 0;
+ _$jscoverage['router.js'][105] = 0;
_$jscoverage['router.js'][108] = 0;
_$jscoverage['router.js'][109] = 0;
_$jscoverage['router.js'][110] = 0;
@@ -74,70 +74,71 @@ if (! _$jscoverage['router.js']) {
_$jscoverage['router.js'][116] = 0;
_$jscoverage['router.js'][117] = 0;
_$jscoverage['router.js'][118] = 0;
- _$jscoverage['router.js'][120] = 0;
+ _$jscoverage['router.js'][119] = 0;
_$jscoverage['router.js'][121] = 0;
_$jscoverage['router.js'][122] = 0;
_$jscoverage['router.js'][123] = 0;
_$jscoverage['router.js'][124] = 0;
- _$jscoverage['router.js'][130] = 0;
+ _$jscoverage['router.js'][125] = 0;
_$jscoverage['router.js'][131] = 0;
- _$jscoverage['router.js'][135] = 0;
+ _$jscoverage['router.js'][132] = 0;
_$jscoverage['router.js'][136] = 0;
_$jscoverage['router.js'][137] = 0;
- _$jscoverage['router.js'][140] = 0;
+ _$jscoverage['router.js'][138] = 0;
_$jscoverage['router.js'][141] = 0;
- _$jscoverage['router.js'][144] = 0;
+ _$jscoverage['router.js'][142] = 0;
_$jscoverage['router.js'][145] = 0;
_$jscoverage['router.js'][146] = 0;
_$jscoverage['router.js'][147] = 0;
- _$jscoverage['router.js'][154] = 0;
+ _$jscoverage['router.js'][148] = 0;
_$jscoverage['router.js'][155] = 0;
_$jscoverage['router.js'][156] = 0;
_$jscoverage['router.js'][157] = 0;
_$jscoverage['router.js'][158] = 0;
_$jscoverage['router.js'][159] = 0;
- _$jscoverage['router.js'][163] = 0;
+ _$jscoverage['router.js'][160] = 0;
_$jscoverage['router.js'][164] = 0;
_$jscoverage['router.js'][165] = 0;
- _$jscoverage['router.js'][168] = 0;
- _$jscoverage['router.js'][171] = 0;
+ _$jscoverage['router.js'][166] = 0;
+ _$jscoverage['router.js'][169] = 0;
_$jscoverage['router.js'][172] = 0;
_$jscoverage['router.js'][173] = 0;
_$jscoverage['router.js'][174] = 0;
_$jscoverage['router.js'][175] = 0;
_$jscoverage['router.js'][176] = 0;
- _$jscoverage['router.js'][180] = 0;
- _$jscoverage['router.js'][183] = 0;
+ _$jscoverage['router.js'][177] = 0;
+ _$jscoverage['router.js'][181] = 0;
_$jscoverage['router.js'][184] = 0;
_$jscoverage['router.js'][185] = 0;
_$jscoverage['router.js'][186] = 0;
_$jscoverage['router.js'][187] = 0;
- _$jscoverage['router.js'][189] = 0;
- _$jscoverage['router.js'][194] = 0;
+ _$jscoverage['router.js'][188] = 0;
+ _$jscoverage['router.js'][190] = 0;
_$jscoverage['router.js'][195] = 0;
_$jscoverage['router.js'][196] = 0;
_$jscoverage['router.js'][197] = 0;
_$jscoverage['router.js'][198] = 0;
_$jscoverage['router.js'][199] = 0;
- _$jscoverage['router.js'][202] = 0;
+ _$jscoverage['router.js'][200] = 0;
_$jscoverage['router.js'][203] = 0;
_$jscoverage['router.js'][204] = 0;
- _$jscoverage['router.js'][208] = 0;
- _$jscoverage['router.js'][211] = 0;
+ _$jscoverage['router.js'][205] = 0;
+ _$jscoverage['router.js'][209] = 0;
_$jscoverage['router.js'][212] = 0;
_$jscoverage['router.js'][213] = 0;
_$jscoverage['router.js'][214] = 0;
_$jscoverage['router.js'][215] = 0;
_$jscoverage['router.js'][216] = 0;
- _$jscoverage['router.js'][219] = 0;
+ _$jscoverage['router.js'][217] = 0;
_$jscoverage['router.js'][220] = 0;
_$jscoverage['router.js'][221] = 0;
_$jscoverage['router.js'][222] = 0;
- _$jscoverage['router.js'][226] = 0;
- _$jscoverage['router.js'][229] = 0;
+ _$jscoverage['router.js'][223] = 0;
+ _$jscoverage['router.js'][227] = 0;
_$jscoverage['router.js'][230] = 0;
_$jscoverage['router.js'][231] = 0;
_$jscoverage['router.js'][232] = 0;
+ _$jscoverage['router.js'][233] = 0;
}
_$jscoverage['router.js'][1]++;
var util = require("util"), XRegExp = require("xregexp").XRegExp, caseRegExp = /([A-Z]+[a-z]|[A-Z]+)/g, HTTP_METHODS = ["head", "get", "post", "put", "delete", "trace", "options", "connect", "patch"];
@@ -250,229 +251,231 @@ Router.prototype.getDefault = (function (request) {
_$jscoverage['router.js'][83]++;
Router.prototype.getCustom = (function (request) {
_$jscoverage['router.js'][84]++;
- var pathname = request.pathname;
+ var self = this;
_$jscoverage['router.js'][85]++;
- var endpoint, xRegExpResult, routeParams, chain;
+ var pathname = request.pathname;
_$jscoverage['router.js'][86]++;
- this.customRoutes.every((function (route) {
+ var endpoint, xRegExpResult, routeParams, chain;
_$jscoverage['router.js'][87]++;
+ this.customRoutes.every((function (route) {
+ _$jscoverage['router.js'][88]++;
if (pathname.match(route.xRegExp)) {
- _$jscoverage['router.js'][89]++;
- endpoint = route.endpoint[request.method];
_$jscoverage['router.js'][90]++;
+ endpoint = route.endpoint[request.method];
+ _$jscoverage['router.js'][91]++;
if (! endpoint) {
- _$jscoverage['router.js'][91]++;
+ _$jscoverage['router.js'][92]++;
return true;
}
- _$jscoverage['router.js'][94]++;
- xRegExpResult = XRegExp.exec(pathname, route.xRegExp);
_$jscoverage['router.js'][95]++;
- routeParams = getRouteParams(xRegExpResult, route.xRegExp, route.defaults);
+ xRegExpResult = XRegExp.exec(pathname, route.xRegExp);
_$jscoverage['router.js'][96]++;
+ routeParams = getRouteParams(xRegExpResult, route.xRegExp, route.defaults);
+ _$jscoverage['router.js'][97]++;
fillObject(request.querystring, routeParams);
- _$jscoverage['router.js'][98]++;
- chain = getMiddlewareChain(this.middlewareList, pathname);
_$jscoverage['router.js'][99]++;
- chain.push(endpoint);
+ chain = getMiddlewareChain(self.middlewareList, pathname);
_$jscoverage['router.js'][100]++;
+ chain.push(endpoint);
+ _$jscoverage['router.js'][101]++;
return false;
}
- _$jscoverage['router.js'][102]++;
+ _$jscoverage['router.js'][103]++;
return true;
}));
- _$jscoverage['router.js'][104]++;
+ _$jscoverage['router.js'][105]++;
return chain;
});
-_$jscoverage['router.js'][107]++;
+_$jscoverage['router.js'][108]++;
Router.prototype.update = (function (modules, middlewareList) {
- _$jscoverage['router.js'][108]++;
- var self = this;
_$jscoverage['router.js'][109]++;
- var routes = Object.create(null);
+ var self = this;
_$jscoverage['router.js'][110]++;
- var conflictsMap = Object.create(null);
+ var routes = Object.create(null);
_$jscoverage['router.js'][111]++;
- Object.keys(modules).forEach((function (version) {
+ var conflictsMap = Object.create(null);
_$jscoverage['router.js'][112]++;
- Object.keys(modules[version]).forEach((function (moduleName) {
+ Object.keys(modules).forEach((function (version) {
_$jscoverage['router.js'][113]++;
- var module_name = normalizeCase(moduleName);
+ Object.keys(modules[version]).forEach((function (moduleName) {
_$jscoverage['router.js'][114]++;
- var module = modules[version][moduleName];
+ var module_name = normalizeCase(moduleName);
_$jscoverage['router.js'][115]++;
- var methodNames = Object.keys(module.__proto__).concat(Object.keys(module));
+ var module = modules[version][moduleName];
_$jscoverage['router.js'][116]++;
- methodNames.forEach((function (methodName) {
+ var methodNames = Object.keys(module.__proto__).concat(Object.keys(module));
_$jscoverage['router.js'][117]++;
+ methodNames.forEach((function (methodName) {
+ _$jscoverage['router.js'][118]++;
if (methodName[0] === "_") {
- _$jscoverage['router.js'][118]++;
+ _$jscoverage['router.js'][119]++;
return;
}
- _$jscoverage['router.js'][120]++;
- var method = module[methodName];
_$jscoverage['router.js'][121]++;
- var method_name = normalizeCase(methodName);
+ var method = module[methodName];
_$jscoverage['router.js'][122]++;
- var route = self.getDefaultRoute(version, module_name, method_name);
+ var method_name = normalizeCase(methodName);
_$jscoverage['router.js'][123]++;
+ var route = self.getDefaultRoute(version, module_name, method_name);
+ _$jscoverage['router.js'][124]++;
if (routes[route]) {
- _$jscoverage['router.js'][124]++;
+ _$jscoverage['router.js'][125]++;
throw new Error(util.format("Routing conflict on \"%s\": %s.%s.%s is anbiguous with %s.%s.%s", route, conflictsMap[route].version, conflictsMap[route].moduleName, conflictsMap[route].methodName, version, moduleName, methodName));
}
- _$jscoverage['router.js'][130]++;
- conflictsMap[route] = {version: version, moduleName: moduleName, methodName: methodName};
_$jscoverage['router.js'][131]++;
+ conflictsMap[route] = {version: version, moduleName: moduleName, methodName: methodName};
+ _$jscoverage['router.js'][132]++;
routes[route] = createEndpointChain(module, method, getMiddlewareChain(middlewareList, route));
}));
}));
}));
- _$jscoverage['router.js'][135]++;
- this.modules = modules;
_$jscoverage['router.js'][136]++;
- this.middlewareList = middlewareList;
+ this.modules = modules;
_$jscoverage['router.js'][137]++;
+ this.middlewareList = middlewareList;
+ _$jscoverage['router.js'][138]++;
this.defaultRoutes = routes;
});
-_$jscoverage['router.js'][140]++;
+_$jscoverage['router.js'][141]++;
function normalizeCase(str) {
- _$jscoverage['router.js'][141]++;
+ _$jscoverage['router.js'][142]++;
return str.replace(/([A-Z][^A-Z]*)/g, "_$1").replace(/^(_)/, "").toLowerCase();
}
-_$jscoverage['router.js'][144]++;
+_$jscoverage['router.js'][145]++;
function getEndpointDescription(endpoint) {
- _$jscoverage['router.js'][145]++;
- var xRegExp = XRegExp("(?<version>[^/]+)/(?<moduleName>[^#]+)#(?<methodName>.+)");
_$jscoverage['router.js'][146]++;
- var result = XRegExp.exec(endpoint, xRegExp);
+ var xRegExp = XRegExp("(?<version>[^/]+)/(?<moduleName>[^#]+)#(?<methodName>.+)");
_$jscoverage['router.js'][147]++;
+ var result = XRegExp.exec(endpoint, xRegExp);
+ _$jscoverage['router.js'][148]++;
return ({version: result.version, moduleName: result.moduleName, methodName: result.methodName});
}
-_$jscoverage['router.js'][154]++;
+_$jscoverage['router.js'][155]++;
function getRouteParams(xRegExpResult, xRegExp, defaults) {
- _$jscoverage['router.js'][155]++;
- var params = Object.create(null);
_$jscoverage['router.js'][156]++;
+ var params = Object.create(null);
+ _$jscoverage['router.js'][157]++;
if (xRegExp.xregexp.captureNames) {
- _$jscoverage['router.js'][157]++;
+ _$jscoverage['router.js'][158]++;
xRegExp.xregexp.captureNames.forEach((function (paramName) {
- _$jscoverage['router.js'][158]++;
+ _$jscoverage['router.js'][159]++;
if (xRegExpResult[paramName] !== undefined) {
- _$jscoverage['router.js'][159]++;
+ _$jscoverage['router.js'][160]++;
params[paramName] = xRegExpResult[paramName];
}
}));
}
- _$jscoverage['router.js'][163]++;
- Object.keys(defaults).forEach((function (paramName) {
_$jscoverage['router.js'][164]++;
+ Object.keys(defaults).forEach((function (paramName) {
+ _$jscoverage['router.js'][165]++;
if (params[paramName] === undefined) {
- _$jscoverage['router.js'][165]++;
+ _$jscoverage['router.js'][166]++;
params[paramName] = defaults[paramName];
}
}));
- _$jscoverage['router.js'][168]++;
+ _$jscoverage['router.js'][169]++;
return params;
}
-_$jscoverage['router.js'][171]++;
+_$jscoverage['router.js'][172]++;
function getMiddlewareChain(middlewareList, route) {
- _$jscoverage['router.js'][172]++;
- var chain = [];
_$jscoverage['router.js'][173]++;
+ var chain = [];
+ _$jscoverage['router.js'][174]++;
if (middlewareList) {
- _$jscoverage['router.js'][174]++;
+ _$jscoverage['router.js'][175]++;
middlewareList.forEach((function (middleware) {
- _$jscoverage['router.js'][175]++;
+ _$jscoverage['router.js'][176]++;
if (route.match(middleware.route)) {
- _$jscoverage['router.js'][176]++;
+ _$jscoverage['router.js'][177]++;
chain.push(middleware.handle);
}
}));
}
- _$jscoverage['router.js'][180]++;
+ _$jscoverage['router.js'][181]++;
return chain;
}
-_$jscoverage['router.js'][183]++;
+_$jscoverage['router.js'][184]++;
function getLastChainRing(endpoint) {
- _$jscoverage['router.js'][184]++;
- return (function (request, response, callback) {
_$jscoverage['router.js'][185]++;
+ return (function (request, response, callback) {
+ _$jscoverage['router.js'][186]++;
try {
- _$jscoverage['router.js'][186]++;
- endpoint(request, response);
_$jscoverage['router.js'][187]++;
+ endpoint(request, response);
+ _$jscoverage['router.js'][188]++;
callback();
}
catch (err) {
- _$jscoverage['router.js'][189]++;
+ _$jscoverage['router.js'][190]++;
callback(err);
}
});
}
-_$jscoverage['router.js'][194]++;
+_$jscoverage['router.js'][195]++;
function createEndpoint(scope, method) {
- _$jscoverage['router.js'][195]++;
- var endpoint = Object.create(null);
_$jscoverage['router.js'][196]++;
+ var endpoint = Object.create(null);
+ _$jscoverage['router.js'][197]++;
if (typeof method === "function") {
- _$jscoverage['router.js'][197]++;
- var endpointFn = getLastChainRing(method.bind(scope));
_$jscoverage['router.js'][198]++;
+ var endpointFn = getLastChainRing(method.bind(scope));
+ _$jscoverage['router.js'][199]++;
Router.prototype.HTTP_METHODS.forEach((function (httpMethod) {
- _$jscoverage['router.js'][199]++;
+ _$jscoverage['router.js'][200]++;
endpoint[httpMethod.toUpperCase()] = endpointFn;
}));
}
else {
- _$jscoverage['router.js'][202]++;
+ _$jscoverage['router.js'][203]++;
Router.prototype.HTTP_METHODS.forEach((function (httpMethod) {
- _$jscoverage['router.js'][203]++;
+ _$jscoverage['router.js'][204]++;
if (typeof method[httpMethod] === "function") {
- _$jscoverage['router.js'][204]++;
+ _$jscoverage['router.js'][205]++;
endpoint[httpMethod.toUpperCase()] = getLastChainRing(method[httpMethod].bind(scope));
}
}));
}
- _$jscoverage['router.js'][208]++;
+ _$jscoverage['router.js'][209]++;
return endpoint;
}
-_$jscoverage['router.js'][211]++;
+_$jscoverage['router.js'][212]++;
function createEndpointChain(scope, method, chain) {
- _$jscoverage['router.js'][212]++;
- var endpointChain = Object.create(null);
_$jscoverage['router.js'][213]++;
+ var endpointChain = Object.create(null);
+ _$jscoverage['router.js'][214]++;
if (typeof method === "function") {
- _$jscoverage['router.js'][214]++;
- chain.push(getLastChainRing(method.bind(scope)));
_$jscoverage['router.js'][215]++;
+ chain.push(getLastChainRing(method.bind(scope)));
+ _$jscoverage['router.js'][216]++;
Router.prototype.HTTP_METHODS.forEach((function (httpMethod) {
- _$jscoverage['router.js'][216]++;
+ _$jscoverage['router.js'][217]++;
endpointChain[httpMethod.toUpperCase()] = chain;
}));
}
else {
- _$jscoverage['router.js'][219]++;
+ _$jscoverage['router.js'][220]++;
Router.prototype.HTTP_METHODS.forEach((function (httpMethod) {
- _$jscoverage['router.js'][220]++;
+ _$jscoverage['router.js'][221]++;
if (typeof method[httpMethod] === "function") {
- _$jscoverage['router.js'][221]++;
- endpointChain[httpMethod.toUpperCase()] = chain.slice(0);
_$jscoverage['router.js'][222]++;
+ endpointChain[httpMethod.toUpperCase()] = chain.slice(0);
+ _$jscoverage['router.js'][223]++;
endpointChain[httpMethod.toUpperCase()].push(getLastChainRing(method[httpMethod].bind(scope)));
}
}));
}
- _$jscoverage['router.js'][226]++;
+ _$jscoverage['router.js'][227]++;
return endpointChain;
}
-_$jscoverage['router.js'][229]++;
+_$jscoverage['router.js'][230]++;
function fillObject(targetObj, fillObj) {
- _$jscoverage['router.js'][230]++;
- Object.keys(fillObj).forEach((function (key) {
_$jscoverage['router.js'][231]++;
+ Object.keys(fillObj).forEach((function (key) {
+ _$jscoverage['router.js'][232]++;
if (! targetObj.hasOwnProperty(key)) {
- _$jscoverage['router.js'][232]++;
+ _$jscoverage['router.js'][233]++;
targetObj[key] = fillObj[key];
}
}));
}
-_$jscoverage['router.js'].source = ["var util = require('util'),"," XRegExp = require('xregexp').XRegExp,"," caseRegExp = /([A-Z]+[a-z]|[A-Z]+)/g,"," HTTP_METHODS = ['head', 'get', 'post', 'put', 'delete', 'trace', 'options', 'connect', 'patch']","","var Router = module.exports = function (options) {"," var self = this"," options = options || {}"," Object.keys(options).forEach(function (key) {"," if (!self.__proto__.hasOwnProperty(key)) {"," self[key] = options[key]"," }"," })"," this.modules = null"," this.middlewareList = null"," this.customRoutes = []"," this.defaultRoute = this.defaultRoute || '/:version/:module/:method'"," this.defaultRoutes = Object.create(null)"," this.disabledDefaultRoutes = Object.create(null)","}","","Router.prototype.HTTP_METHODS = ['head', 'get', 'post', 'put', 'delete', 'trace', 'options', 'connect', 'patch']","","Router.prototype.addRoute = function addRoute(route, endpointPath, defaults, keepDefault) {"," var xRegExp"," var endpointDescription = getEndpointDescription(endpointPath)"," try {"," var module = this.modules[endpointDescription.version][endpointDescription.moduleName]"," var method = module[endpointDescription.methodName]"," var endopoint = createEndpoint(module, method)"," } catch (e) {"," throw new Error(util.format('Can not resolve endopoint %s (%s)', endpointPath, JSON.stringify(endpointDescription)))"," }"," if (route.constructor === String) {"," xRegExp = XRegExp('^' + route.replace(/\\/:([\\w]+)/g, '(?:/(?&lt;$1&gt;[^/]+)?)?') + '/*$')"," } else if (route.xregexp !== undefined) {"," xRegExp = route"," } else {"," throw new Error(util.format('Route must be a String or a XRegExp: %s', route))"," }"," this.customRoutes.push({"," xRegExp: xRegExp,"," endpoint: endopoint,"," defaults: defaults || Object.create(null)"," })"," if (!keepDefault) {"," this.disabledDefaultRoutes["," this.getDefaultRoute("," endpointDescription.version,"," normalizeCase(endpointDescription.moduleName),"," normalizeCase(endpointDescription.methodName)"," )"," ] = true"," }"," return this","}","","Router.prototype.addRoutes = function (routes) {"," var self = this"," routes.forEach(function (route) {"," self.addRoute.apply(self, route)"," })","}","","Router.prototype.getDefaultRoute = function (version, moduleName, methodName) {"," return this.defaultRoute.replace(':version', version)"," .replace(':module', moduleName)"," .replace(':method', methodName)","}","","Router.prototype.get = function (request) {"," return this.getDefault(request) || this.getCustom(request)","}","","Router.prototype.getDefault = function (request) {"," var pathname = request.pathname"," if (!this.disabledDefaultRoutes[pathname]) {"," var endpoint = this.defaultRoutes[pathname]"," return endpoint &amp;&amp; endpoint[request.method]"," }","}","","Router.prototype.getCustom = function (request) {"," var pathname = request.pathname"," var endpoint, xRegExpResult, routeParams, chain"," this.customRoutes.every(function (route) {"," if (pathname.match(route.xRegExp)) {"," // check endpoint availability for the current http method"," endpoint = route.endpoint[request.method]"," if (!endpoint) {"," return true"," }"," // parse route parameters and merge into the request.querystring object"," xRegExpResult = XRegExp.exec(pathname, route.xRegExp)"," routeParams = getRouteParams(xRegExpResult, route.xRegExp, route.defaults)"," fillObject(request.querystring, routeParams)"," // get the chain"," chain = getMiddlewareChain(this.middlewareList, pathname)"," chain.push(endpoint)"," return false"," }"," return true"," })"," return chain","}","","Router.prototype.update = function (modules, middlewareList) {"," var self = this"," var routes = Object.create(null)"," var conflictsMap = Object.create(null)"," Object.keys(modules).forEach(function (version) {"," Object.keys(modules[version]).forEach(function (moduleName) {"," var module_name = normalizeCase(moduleName)"," var module = modules[version][moduleName]"," var methodNames = Object.keys(module.__proto__).concat(Object.keys(module))"," methodNames.forEach(function (methodName) {"," if (methodName[0] === '_') {"," return"," }"," var method = module[methodName]"," var method_name = normalizeCase(methodName)"," var route = self.getDefaultRoute(version, module_name, method_name)"," if (routes[route]) {"," throw new Error(util.format('Routing conflict on \"%s\": %s.%s.%s is anbiguous with %s.%s.%s',"," route,"," conflictsMap[route].version, conflictsMap[route].moduleName, conflictsMap[route].methodName,"," version, moduleName, methodName"," ))"," }"," conflictsMap[route] = { version: version, moduleName: moduleName, methodName: methodName }"," routes[route] = createEndpointChain(module, method, getMiddlewareChain(middlewareList, route))"," })"," })"," })"," this.modules = modules"," this.middlewareList = middlewareList"," this.defaultRoutes = routes","}","","function normalizeCase(str) {"," return str.replace(/([A-Z][^A-Z]*)/g, '_$1').replace(/^(_)/, '').toLowerCase()","}","","function getEndpointDescription(endpoint) {"," var xRegExp = XRegExp('(?&lt;version&gt;[^/]+)/(?&lt;moduleName&gt;[^#]+)#(?&lt;methodName&gt;.+)')"," var result = XRegExp.exec(endpoint, xRegExp)"," return {"," version: result.version,"," moduleName: result.moduleName,"," methodName: result.methodName"," }","}","","function getRouteParams(xRegExpResult, xRegExp, defaults) {"," var params = Object.create(null)"," if (xRegExp.xregexp.captureNames) {"," xRegExp.xregexp.captureNames.forEach(function (paramName) {"," if (xRegExpResult[paramName] !== undefined) {"," params[paramName] = xRegExpResult[paramName]"," }"," })"," }"," Object.keys(defaults).forEach(function (paramName) {"," if (params[paramName] === undefined) {"," params[paramName] = defaults[paramName]"," }"," })"," return params","}","","function getMiddlewareChain(middlewareList, route) {"," var chain = []"," if (middlewareList) {"," middlewareList.forEach(function (middleware) {"," if (route.match(middleware.route)) {"," chain.push(middleware.handle)"," }"," })"," }"," return chain","}","","function getLastChainRing(endpoint) {"," return function (request, response, callback) {"," try {"," endpoint(request, response)"," callback()"," } catch (err) {"," callback(err)"," }"," }","}","","function createEndpoint(scope, method) {"," var endpoint = Object.create(null)"," if (typeof method === 'function') {"," var endpointFn = getLastChainRing(method.bind(scope))"," Router.prototype.HTTP_METHODS.forEach(function (httpMethod) {"," endpoint[httpMethod.toUpperCase()] = endpointFn"," })"," } else {"," Router.prototype.HTTP_METHODS.forEach(function (httpMethod) {"," if (typeof method[httpMethod] === 'function') {"," endpoint[httpMethod.toUpperCase()] = getLastChainRing(method[httpMethod].bind(scope))"," }"," })"," }"," return endpoint","}","","function createEndpointChain(scope, method, chain) {"," var endpointChain = Object.create(null)"," if (typeof method === 'function') {"," chain.push(getLastChainRing(method.bind(scope)))"," Router.prototype.HTTP_METHODS.forEach(function (httpMethod) {"," endpointChain[httpMethod.toUpperCase()] = chain"," })"," } else {"," Router.prototype.HTTP_METHODS.forEach(function (httpMethod) {"," if (typeof method[httpMethod] === 'function') {"," endpointChain[httpMethod.toUpperCase()] = chain.slice(0)"," endpointChain[httpMethod.toUpperCase()].push(getLastChainRing(method[httpMethod].bind(scope)))"," }"," })"," }"," return endpointChain","}","","function fillObject (targetObj, fillObj) {"," Object.keys(fillObj).forEach(function (key) {"," if (!targetObj.hasOwnProperty(key)) {"," targetObj[key] = fillObj[key]"," }"," })","}"];
+_$jscoverage['router.js'].source = ["var util = require('util'),"," XRegExp = require('xregexp').XRegExp,"," caseRegExp = /([A-Z]+[a-z]|[A-Z]+)/g,"," HTTP_METHODS = ['head', 'get', 'post', 'put', 'delete', 'trace', 'options', 'connect', 'patch']","","var Router = module.exports = function (options) {"," var self = this"," options = options || {}"," Object.keys(options).forEach(function (key) {"," if (!self.__proto__.hasOwnProperty(key)) {"," self[key] = options[key]"," }"," })"," this.modules = null"," this.middlewareList = null"," this.customRoutes = []"," this.defaultRoute = this.defaultRoute || '/:version/:module/:method'"," this.defaultRoutes = Object.create(null)"," this.disabledDefaultRoutes = Object.create(null)","}","","Router.prototype.HTTP_METHODS = ['head', 'get', 'post', 'put', 'delete', 'trace', 'options', 'connect', 'patch']","","Router.prototype.addRoute = function addRoute(route, endpointPath, defaults, keepDefault) {"," var xRegExp"," var endpointDescription = getEndpointDescription(endpointPath)"," try {"," var module = this.modules[endpointDescription.version][endpointDescription.moduleName]"," var method = module[endpointDescription.methodName]"," var endopoint = createEndpoint(module, method)"," } catch (e) {"," throw new Error(util.format('Can not resolve endopoint %s (%s)', endpointPath, JSON.stringify(endpointDescription)))"," }"," if (route.constructor === String) {"," xRegExp = XRegExp('^' + route.replace(/\\/:([\\w]+)/g, '(?:/(?&lt;$1&gt;[^/]+)?)?') + '/*$')"," } else if (route.xregexp !== undefined) {"," xRegExp = route"," } else {"," throw new Error(util.format('Route must be a String or a XRegExp: %s', route))"," }"," this.customRoutes.push({"," xRegExp: xRegExp,"," endpoint: endopoint,"," defaults: defaults || Object.create(null)"," })"," if (!keepDefault) {"," this.disabledDefaultRoutes["," this.getDefaultRoute("," endpointDescription.version,"," normalizeCase(endpointDescription.moduleName),"," normalizeCase(endpointDescription.methodName)"," )"," ] = true"," }"," return this","}","","Router.prototype.addRoutes = function (routes) {"," var self = this"," routes.forEach(function (route) {"," self.addRoute.apply(self, route)"," })","}","","Router.prototype.getDefaultRoute = function (version, moduleName, methodName) {"," return this.defaultRoute.replace(':version', version)"," .replace(':module', moduleName)"," .replace(':method', methodName)","}","","Router.prototype.get = function (request) {"," return this.getDefault(request) || this.getCustom(request)","}","","Router.prototype.getDefault = function (request) {"," var pathname = request.pathname"," if (!this.disabledDefaultRoutes[pathname]) {"," var endpoint = this.defaultRoutes[pathname]"," return endpoint &amp;&amp; endpoint[request.method]"," }","}","","Router.prototype.getCustom = function (request) {"," var self = this"," var pathname = request.pathname"," var endpoint, xRegExpResult, routeParams, chain"," this.customRoutes.every(function (route) {"," if (pathname.match(route.xRegExp)) {"," // check endpoint availability for the current http method"," endpoint = route.endpoint[request.method]"," if (!endpoint) {"," return true"," }"," // parse route parameters and merge into the request.querystring object"," xRegExpResult = XRegExp.exec(pathname, route.xRegExp)"," routeParams = getRouteParams(xRegExpResult, route.xRegExp, route.defaults)"," fillObject(request.querystring, routeParams)"," // get the chain"," chain = getMiddlewareChain(self.middlewareList, pathname)"," chain.push(endpoint)"," return false"," }"," return true"," })"," return chain","}","","Router.prototype.update = function (modules, middlewareList) {"," var self = this"," var routes = Object.create(null)"," var conflictsMap = Object.create(null)"," Object.keys(modules).forEach(function (version) {"," Object.keys(modules[version]).forEach(function (moduleName) {"," var module_name = normalizeCase(moduleName)"," var module = modules[version][moduleName]"," var methodNames = Object.keys(module.__proto__).concat(Object.keys(module))"," methodNames.forEach(function (methodName) {"," if (methodName[0] === '_') {"," return"," }"," var method = module[methodName]"," var method_name = normalizeCase(methodName)"," var route = self.getDefaultRoute(version, module_name, method_name)"," if (routes[route]) {"," throw new Error(util.format('Routing conflict on \"%s\": %s.%s.%s is anbiguous with %s.%s.%s',"," route,"," conflictsMap[route].version, conflictsMap[route].moduleName, conflictsMap[route].methodName,"," version, moduleName, methodName"," ))"," }"," conflictsMap[route] = { version: version, moduleName: moduleName, methodName: methodName }"," routes[route] = createEndpointChain(module, method, getMiddlewareChain(middlewareList, route))"," })"," })"," })"," this.modules = modules"," this.middlewareList = middlewareList"," this.defaultRoutes = routes","}","","function normalizeCase(str) {"," return str.replace(/([A-Z][^A-Z]*)/g, '_$1').replace(/^(_)/, '').toLowerCase()","}","","function getEndpointDescription(endpoint) {"," var xRegExp = XRegExp('(?&lt;version&gt;[^/]+)/(?&lt;moduleName&gt;[^#]+)#(?&lt;methodName&gt;.+)')"," var result = XRegExp.exec(endpoint, xRegExp)"," return {"," version: result.version,"," moduleName: result.moduleName,"," methodName: result.methodName"," }","}","","function getRouteParams(xRegExpResult, xRegExp, defaults) {"," var params = Object.create(null)"," if (xRegExp.xregexp.captureNames) {"," xRegExp.xregexp.captureNames.forEach(function (paramName) {"," if (xRegExpResult[paramName] !== undefined) {"," params[paramName] = xRegExpResult[paramName]"," }"," })"," }"," Object.keys(defaults).forEach(function (paramName) {"," if (params[paramName] === undefined) {"," params[paramName] = defaults[paramName]"," }"," })"," return params","}","","function getMiddlewareChain(middlewareList, route) {"," var chain = []"," if (middlewareList) {"," middlewareList.forEach(function (middleware) {"," if (route.match(middleware.route)) {"," chain.push(middleware.handle)"," }"," })"," }"," return chain","}","","function getLastChainRing(endpoint) {"," return function (request, response, callback) {"," try {"," endpoint(request, response)"," callback()"," } catch (err) {"," callback(err)"," }"," }","}","","function createEndpoint(scope, method) {"," var endpoint = Object.create(null)"," if (typeof method === 'function') {"," var endpointFn = getLastChainRing(method.bind(scope))"," Router.prototype.HTTP_METHODS.forEach(function (httpMethod) {"," endpoint[httpMethod.toUpperCase()] = endpointFn"," })"," } else {"," Router.prototype.HTTP_METHODS.forEach(function (httpMethod) {"," if (typeof method[httpMethod] === 'function') {"," endpoint[httpMethod.toUpperCase()] = getLastChainRing(method[httpMethod].bind(scope))"," }"," })"," }"," return endpoint","}","","function createEndpointChain(scope, method, chain) {"," var endpointChain = Object.create(null)"," if (typeof method === 'function') {"," chain.push(getLastChainRing(method.bind(scope)))"," Router.prototype.HTTP_METHODS.forEach(function (httpMethod) {"," endpointChain[httpMethod.toUpperCase()] = chain"," })"," } else {"," Router.prototype.HTTP_METHODS.forEach(function (httpMethod) {"," if (typeof method[httpMethod] === 'function') {"," endpointChain[httpMethod.toUpperCase()] = chain.slice(0)"," endpointChain[httpMethod.toUpperCase()].push(getLastChainRing(method[httpMethod].bind(scope)))"," }"," })"," }"," return endpointChain","}","","function fillObject (targetObj, fillObj) {"," Object.keys(fillObj).forEach(function (key) {"," if (!targetObj.hasOwnProperty(key)) {"," targetObj[key] = fillObj[key]"," }"," })","}"];
View
3 lib/router.js
@@ -81,6 +81,7 @@ Router.prototype.getDefault = function (request) {
}
Router.prototype.getCustom = function (request) {
+ var self = this
var pathname = request.pathname
var endpoint, xRegExpResult, routeParams, chain
this.customRoutes.every(function (route) {
@@ -95,7 +96,7 @@ Router.prototype.getCustom = function (request) {
routeParams = getRouteParams(xRegExpResult, route.xRegExp, route.defaults)
fillObject(request.querystring, routeParams)
// get the chain
- chain = getMiddlewareChain(this.middlewareList, pathname)
+ chain = getMiddlewareChain(self.middlewareList, pathname)
chain.push(endpoint)
return false
}
View
4 test/fixtures/router-module-class.js
@@ -16,8 +16,8 @@ module.exports.prototype = {
wrong_case_api: getFakeApi('wrong_case_api'),
AA: getFakeApi('AA'),
photo: {
- get: getFakeApi('photo', 'get'),
- post: getFakeApi('photo', 'post'),
+ get: getFakeApi('photo', 'GET'),
+ post: getFakeApi('photo', 'POST'),
delete: 'not_a_function'
}
}
View
4 test/fixtures/router-module.js
@@ -13,8 +13,8 @@ module.exports = {
wrong_case_api: getFakeApi('wrong_case_api'),
AA: getFakeApi('AA'),
photo: {
- get: getFakeApi('photo', 'get'),
- post: getFakeApi('photo', 'post'),
+ get: getFakeApi('photo', 'GET'),
+ post: getFakeApi('photo', 'POST'),
delete: 'not_a_function'
}
}
View
13 test/router-test.js
@@ -152,7 +152,7 @@ describe('Router', function () {
checkChain(router, '/1/test/foo_api', [fakeMiddlewares[0], fakeMiddlewares[2], fakeMiddlewares[3], fakeMiddlewares[5]], 'fooApi')
checkChain(router, '/1/test/post', [fakeMiddlewares[0], fakeMiddlewares[4], fakeMiddlewares[5]], 'post')
checkChain(router, '/1/test/wrong_case_api', [fakeMiddlewares[0], fakeMiddlewares[2], fakeMiddlewares[5]], 'wrong_case_api')
- checkChain(router, '/1/test/photo', [fakeMiddlewares[0], fakeMiddlewares[5]], 'photos', ['get', 'post'])
+ checkChain(router, '/1/test/photo', [fakeMiddlewares[0], fakeMiddlewares[5]], 'photo', ['GET', 'POST'])
})
it('should return the functions chain / custom defaultRoute', function () {
router = new Router({ defaultRoute: '/:module/api/:version/:method' })
@@ -168,21 +168,21 @@ describe('Router', function () {
checkChain(router, '/test/api/1/foo_api', [fakeMiddlewares[0], fakeMiddlewares[2], fakeMiddlewares[3], fakeMiddlewares[5]], 'fooApi')
checkChain(router, '/test/api/1/post', [fakeMiddlewares[0], fakeMiddlewares[4], fakeMiddlewares[5]], 'post')
checkChain(router, '/test/api/1/wrong_case_api', [fakeMiddlewares[0], fakeMiddlewares[2], fakeMiddlewares[5]], 'wrong_case_api')
- checkChain(router, '/test/api/1/photo', [fakeMiddlewares[0], fakeMiddlewares[5]], 'photos', ['get', 'post'])
+ checkChain(router, '/test/api/1/photo', [fakeMiddlewares[0], fakeMiddlewares[5]], 'photo', ['get', 'post'])
})
})
describe('#getCustom()', function () {
it('should return the functions chain', function () {
router = new Router()
router.update({ 1: { test: objectModule } }, [
{ route: /.+/, handle: fakeMiddlewares[0] },
- { route: /$\/post/, handle: fakeMiddlewares[1] },
+ { route: /^\/post/, handle: fakeMiddlewares[1] },
{ route: /st/, handle: fakeMiddlewares[2] },
{ route: /^\/p/, handle: fakeMiddlewares[3] }
])
router.addRoute('/post/:param1/:param2', '1/test#post')
router.addRoute('/test_module/:param1/:param2', '1/test#post')
- router.addRoute(XRegExp('^/p/(?<message>[\\w!]+)$'), '1/test#post')
+ router.addRoute(XRegExp('^/p$'), '1/test#post')
checkChain(router, '/post', [fakeMiddlewares[0], fakeMiddlewares[1], fakeMiddlewares[2], fakeMiddlewares[3]], 'post')
checkChain(router, '/test_module', [fakeMiddlewares[0], fakeMiddlewares[2]], 'post')
checkChain(router, '/p', [fakeMiddlewares[0], fakeMiddlewares[3]], 'post')
@@ -317,6 +317,7 @@ function checkChain(router, route, expetedChain, expetedMethodName, httpMethods)
generateFakeRequests(route, null, httpMethods).forEach(function (request) {
var chain = router.get(request)
should.exist(chain, route)
+ chain = chain.slice(0)
checkLastChainLink(chain.pop(), request.method, expetedMethodName)
chain.should.eql(expetedChain, route)
})
@@ -328,9 +329,9 @@ function checkLastChainLink(fn, expectedHttpMethod, expectedMethodName) {
ok = true
module.database.should.be.equal('fake_database')
methodName.should.be.equal(expectedMethodName)
- httpMethod.should.be.equal(expectedHttpMethod)
+ httpMethod.should.be.equal(expectedHttpMethod, 331)
}, expectedHttpMethod, function (err) {
- throw err
+ should.not.exist(err)
})
ok.should.be.ok
}

0 comments on commit 35da557

Please sign in to comment.
Something went wrong with that request. Please try again.