Permalink
Browse files

Added test showing how to use custom parsers

  • Loading branch information...
1 parent f93bbe8 commit 13f7c02bbdc523ff803740346df8b56863156ef3 @tarruda committed Aug 29, 2012
Showing with 32 additions and 2 deletions.
  1. +2 −2 src/router.coffee
  2. +30 −0 test/router.coffee
View
@@ -130,7 +130,7 @@ class Compiler
return null
if parsers
- for own k, v in parsers
+ for own k, v of parsers
@parsers[k] = v
# Regexes used to parse rules. Based on the regexes found at:
@@ -309,7 +309,7 @@ class Router
module.exports = (parsers) ->
- if not compiler then compiler = new Compiler()
+ if not compiler then compiler = new Compiler(parsers)
r = new Router(compiler)
return {
View
@@ -253,3 +253,33 @@ describe 'Accessing branch urls without trailing slash', ->
l = 'http://www.google.com/some/branch/url/?var1=val1&var2=val2'
res.headers['location'].should.eql(l)
done()
+
+
+describe 'Custom parser', ->
+ router = createRouter
+ options: (str) ->
+ console.log str
+ rv = {}
+ options = str.split('/')
+ for option in options
+ [key, value] = option.split('=')
+ rv[key] = value
+ return rv
+
+ app = connect()
+ app.use(router.route)
+
+ router.get '/transactions/<options:query>', (req, res) ->
+ res.write(JSON.stringify(req.params.query))
+ res.end()
+
+ it 'create object containing parsed options', (done) ->
+ app.request()
+ .get('/transactions/gt=5/lt=10/limit=20')
+ .end (res) ->
+ JSON.parse(res.body).should.eql
+ gt: '5'
+ lt: '10'
+ limit: '20'
+ done()
+

0 comments on commit 13f7c02

Please sign in to comment.