Skip to content
Browse files

Fixed bug caused by greedy matching done by generated regexp

  • Loading branch information...
1 parent 651f276 commit 8f8d3e09e9285b44461fe21b5de7be73a122084c @tarruda committed Aug 29, 2012
Showing with 20 additions and 3 deletions.
  1. +3 −2 src/router.coffee
  2. +17 −1 test/router.coffee
View
5 src/router.coffee
@@ -56,8 +56,9 @@ class RuleExtractor extends RegexExtractor
pushParam: (dynamicPart) ->
@params.push(dynamicPart)
# Actual parsing/validation is done by the parser function,
- # so a simple catch-all capture group is inserted
- @regexParts.push('(.+)')
+ # so a simple non-greedy(since we insert a '$' at the end
+ # before compilation) catch-all capture group is inserted.
+ @regexParts.push('(.+?)')
compile: ->
@regexParts.push('$')
View
18 test/router.coffee
@@ -274,17 +274,33 @@ describe 'Multiple parameters', ->
app = connect()
app.use(router.route)
- router.get '/<id1>/branch/<int:id2>,<float:id3>/list', (req, res) ->
+ handler = (req, res) ->
res.write(JSON.stringify(req.params))
res.end()
+ router.get('/<id1>/branch/<int:id2>,<float:id3>/list', handler)
+ router.get('/<in(js,css,img):dir>/<path:p>', handler)
+
it 'should keep parameter order', (done) ->
app.request()
.get('/abc/branch/56,7.7756/list')
.end (res) ->
JSON.parse(res.body).should.eql(['abc', 56, 7.7756])
done()
+ it 'should capture parameters in a non-greedy way', (done) ->
+ app.request()
+ .get('/js/some/path/to/a/javascript/file.js')
+ .end (res) ->
+ # Before adjusting the internal regexp for ungreedy matching
+ # this route would not be executed, since it would try to match
+ # 'js/some/path/to/a/javascript' as the first parameter.
+ JSON.parse(res.body).should.eql [
+ 'js'
+ 'some/path/to/a/javascript/file.js'
+ ]
+ done()
+
describe 'Custom parser', ->
router = createRouter()

0 comments on commit 8f8d3e0

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