Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add more test cases

  • Loading branch information...
commit 8807538b6c7e1e74e189b4935f0b1ec548e94393 1 parent 41f2c4e
@fengmk2 fengmk2 authored
View
4 README.md
@@ -107,7 +107,7 @@ function loadUser(req, res, next) {
}
}
-var router = urlrouter(function (app) {
+var routerMiddleware = urlrouter(function (app) {
app.get('/', function (req, res) {
res.end('GET home page' + req.url + ' , headers: ' + JSON.stringify(req.headers));
});
@@ -149,7 +149,7 @@ var router = urlrouter(function (app) {
});
}, options);
-http.createServer(router).listen(3000);
+http.createServer(routerMiddleware).listen(3000);
```
## Contributors
View
6 lib/urlrouter.js
@@ -86,14 +86,14 @@ function router(fn, options) {
for (var i = 0, l = localRoutes.length; i < l; i++) {
var route = localRoutes[i];
var urlroute = route[0];
- var fn = route[1];
+ var handle = route[1];
var middleware = route[2];
var match = urlroute.match(pathname);
if (match) {
req[options.paramsName] = match;
// if middleware not exists or empty, return directly
if (!middleware || !middleware.length) {
- return fn(req, res, next);
+ return handle(req, res, next);
}
// route middleware
var k = 0;
@@ -105,7 +105,7 @@ function router(fn, options) {
} else if (mw) {
return mw(req, res, routeMiddleware);
} else {
- return fn(req, res, next);
+ return handle(req, res, next);
}
};
return routeMiddleware();
View
2  package.json
@@ -21,6 +21,8 @@
"mocha": "*",
"should": "*",
"rewire": "*",
+ "supertest": "*",
+ "pedding": "*",
"connect": ">=1.8.0"
},
"optionalDependencies": {},
View
46 test/urlrouter.js
@@ -9,6 +9,7 @@
*/
var rewire = require('rewire');
+var pedding = require('pedding');
var fs = require('fs');
var http = require('http');
var connect = require('connect');
@@ -50,6 +51,19 @@ var router = urlrouter(function (app) {
app.get('/mwReturn', middleware, function (req, res) {
res.end(req.method + ' ' + req.url);
});
+ app.get('/searchlist', function (req, res) {
+ res.end(JSON.stringify({
+ url: req.url,
+ params: req.params
+ }));
+ });
+ app.get('/search.:format?', function (req, res) {
+ res.end(JSON.stringify({
+ url: req.url,
+ params: req.params
+ }));
+ });
+
app.head('/status', function (req, res) {
res.end();
});
@@ -209,6 +223,38 @@ var router = urlrouter(function (app) {
done();
});
});
+
+ it('should /search.:format?', function (done) {
+ done = pedding(2, done);
+ app.request().get('/search').end(function (res) {
+ res.should.status(200);
+ var result = JSON.parse(res.body);
+ result.should.have.keys('url', 'params');
+ result.should.have.property('url', '/search');
+ result.params.should.eql({});
+ done();
+ });
+ app.request().get('/search.json').end(function (res) {
+ res.should.status(200);
+ var result = JSON.parse(res.body);
+ result.should.have.keys('url', 'params');
+ result.should.have.property('url', '/search.json');
+ result.params.should.eql({format: 'json'});
+ done();
+ });
+ });
+
+ it('should /searchlist', function (done) {
+ app.request().get('/searchlist').end(function (res) {
+ res.should.status(200);
+ var result = JSON.parse(res.body);
+ result.should.have.keys('url', 'params');
+ result.should.have.property('url', '/searchlist');
+ result.params.should.eql({});
+ done();
+ });
+ });
+
});
describe('post()', function () {
View
2  test/utils.js
@@ -82,7 +82,7 @@ describe('utils.js', function () {
]
],
["/products.:format?",
- ["/products.json", "/products.xml", "/products"]
+ ["/products.json", "/products.xml", "/products"],
],
["/user/:id.:format?", ["/user/12", "/user/12.json"]],
["/users", ["/users", "/users/"]],

0 comments on commit 8807538

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