Skip to content

Commit

Permalink
Fixed bug caused by greedy matching done by generated regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
tarruda committed Aug 29, 2012
1 parent 651f276 commit 8f8d3e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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('$')
Expand Down
18 changes: 17 additions & 1 deletion test/router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8f8d3e0

Please sign in to comment.