Permalink
Browse files

Supports enforceable query string parameters. Added jasmine specs

  • Loading branch information...
1 parent d520191 commit 2b7202c0d168b0a4183ed78e1b300c50dd14ec45 @knowlecules committed May 9, 2012
Showing with 94 additions and 11 deletions.
  1. +1 −1 examples/server.js
  2. +31 −10 lib/rapid-rest.js
  3. +11 −0 package.json
  4. +51 −0 spec/routes-spec.js
View
@@ -1,4 +1,4 @@
-var routes = require("../lib/rapid-rest")();
+var routes = require("../lib/rapid-rest2")();
var port = 8004;
/* Accepts a POST at URL matching /here/?A?/?B?
View
@@ -1,4 +1,5 @@
var http = require('http');
+var url = require('url');
module.exports = function() {
var rest = function(route) {
@@ -67,37 +68,57 @@ module.exports = function() {
var data = body;// JSON.parse(body);
fn(req, res, segments, data);
});
- }
+ }
}
return route.method;
};
return route.method;
};
- rest.router = (function() {
+ rest.router = (function () {
var routes = {};
return {
- add : function(url) {
+ add : function (urlPath) {
var location = routes;
- var urlMatch = url.replace(/[\-[\]\{\}\(\)\*\+\?\.,\/\\\^\$\|#\s]/g, "\\$&").replace(/(:[^(\\/)]+)/g,"([^/]*)");
+ var urlDef = url.parse(urlPath, true);
+ var urlMatch = urlDef.pathname.replace(/[\-[\]\{\}\(\)\*\+\?\.,\/\\\^\$\|#\s]/g, "\\$&").replace(/(:[^(\\/)]+)/g,"([^/]*)");
var patternRE = new RegExp(urlMatch);
- var paramKeys = (url.indexOf("/:")) ? url.replace(/(\/[^:][^\/]+)/g, '').substr(2).split("/:") :[];
+ var paramKeys = (urlDef.pathname.indexOf("/:")) ? urlDef.pathname.replace(/(\/[^:][^\/]+)/g, '').substr(2).split("/:") :[];
+ var queryKeys = urlDef.query;
+
location[urlMatch] = rest.handlers({});
- rest.matchDefs[urlMatch] = {re:patternRE, paramKeys:paramKeys};
+ rest.matchDefs[urlMatch] = {re:patternRE, paramKeys:paramKeys, queryKeys:queryKeys};
return location[urlMatch];
},
handle : function(req, res) {
// Handle options
-
var method = req.method.toLowerCase();
var location = routes;
var paramKeys;
var params={};
+
+ var urlDef;
+ var urlPath = req.url;
+ if(req.url.indexOf("?")){
+ urlDef = url.parse(req.url,true);
+ urlPath = urlDef.pathname;
+ }
+
for (var pattern in rest.matchDefs) {
- var urlParams = req.url.match(rest.matchDefs[pattern].re);
- if ( urlParams != null){
+ var matchDef = rest.matchDefs[pattern];
+ var urlParams = urlPath.match(matchDef.re);
+
+ if ( urlParams !== null){
+ // Validate required querystring parameters
+ if(urlDef && matchDef.queryKeys){
+ for(var kvp in urlDef.query) {
+ if(!matchDef.query[kvp]){
+ continue;
+ }
+ }
+ }
// Align variables in the URL with the REST definition
var paramKeys = rest.matchDefs[pattern].paramKeys;
for(var ikey=0; ikey < paramKeys.length; ikey++) {
@@ -117,7 +138,7 @@ module.exports = function() {
},
respond : function(code, headers, data) {
- rest.respond(res,code, headers, data);
+ rest.respond(res,code, headers, data);
}
}
})()
View
@@ -0,0 +1,11 @@
+{
+ "name": "rapid-rest",
+ "description": "Minimal rest server",
+ "author": "Jason Rudland <knowlecules@gmail.com>",
+ "dependencies": {
+ },
+ "devDependencies": {
+ "jasmine": "x.x.x"
+ },
+ "engine": "node >= 0.6.1"
+}
View
@@ -0,0 +1,51 @@
+var routes = require("../lib/rapid-rest")();
+
+describe('rapid-rest-routes', function(){
+
+ it('path with url params', function(){
+ routes('/here/:here_name/:user')
+ ('post', function(req, res, params, jsonData){
+ here(req, res, params, jsonData);
+ });
+ var rePattern = "\\/here\\/([^/]*)\\/([^/]*)";
+
+ var matchDef = routes.matchDefs[rePattern] ;
+ expect(matchDef).not.toEqual(null);
+
+ expect(matchDef.re).toEqual(/\/here\/([^/]*)\/([^/]*)/);
+ expect(matchDef.paramKeys[0]).toEqual("here_name");
+ expect(matchDef.paramKeys[1]).toEqual("user");
+ });
+
+ it('enforced querystring kvps', function(){
+ routes('/here?here_name={here_name}&user={user}')
+ ('get', function(req, res, params, jsonData){
+ here(req, res, params, jsonData);
+ });
+ var rePattern = "\\/here";
+
+ var matchDef = routes.matchDefs[rePattern] ;
+ expect(matchDef).not.toEqual(null);
+
+ expect(matchDef.re).toEqual(/\/here\/([^/]*)\/([^/]*)/);
+ expect(matchDef.queryKeys["here_name"]).not.toEqual("here_name");
+ expect(matchDef.queryKeys["user"]).not.toEqual("user");
+ });
+
+ it('enforced querystring kvps with url params', function(){
+ routes('/there/:where?here_name={here_name}&user={user}')
+ ('get', function(req, res, params, jsonData){
+ here(req, res, params, jsonData);
+ });
+ var rePattern = "\\/there\\/([^/]*)";
+
+ var matchDef = routes.matchDefs[rePattern] ;
+ expect(matchDef).not.toEqual(null);
+
+ expect(matchDef.re).toEqual(/\/there\/([^/]*)/);
+ expect(matchDef.paramKeys[0]).toEqual("where");
+ expect(matchDef.queryKeys["here_name"]).toBeDefined();
+ expect(matchDef.queryKeys["user"]).toBeDefined();
+ });
+
+});

0 comments on commit 2b7202c

Please sign in to comment.