Skip to content

Commit

Permalink
Fixed handler registration behavior
Browse files Browse the repository at this point in the history
Now two handlers registered to the same pattern will be treated as different
routes registrations(instead of being appended to the same handler array) if
the method used to register them was different.

On most cases this won't change
the current behavior, only when using 'all' mixed with some other registration
method.
  • Loading branch information
tarruda committed Jan 18, 2013
1 parent d040dca commit 066771f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class Router
checkRule(0)

# Register one of more handler functions to a single route.
register: (methodName, pattern, handlers...) ->
register: (prefix, methodName, pattern, handlers...) ->
ruleArray = @rules[methodName]
# Only allow rules to be registered before compilation
if @compiled
Expand All @@ -299,7 +299,27 @@ class Router
throw new Error('Pattern must be rule string or regex')
# Id used to search for existing rules. That way multiple registrations
# to the same rule will append to the same handler array.
id = pattern.toString()
#
# The prefix is basically the router method used to register the handler,
# so possible prefixes are: 'get', 'post', 'put', 'del' and 'all'.
#
# This means that while two handlers may be registered to the same
# pattern, they will be appended to different rules if the method
# used to register them is different.
#
# For example, consider a GET request to /starting/path:
#
# router.all '/starting/path', (req, res, next) ->
# # check some condition then
# next('route')
#
# router.get '/starting/path', (req, res) ->
# # handle request
#
# In this above example, next('route') on the first handler
# will invoke the other handler even though they use the same
# url pattern.
id = "#{prefix}##{pattern.toString()}"
handlerArray = null
# Check if the rule is already registered in this array.
for rule in ruleArray
Expand Down Expand Up @@ -333,12 +353,16 @@ module.exports = (parsers) ->
return {
route: (req, res, next) -> r.route(req, res, next)
registerParser: (name, parser) -> compiler.parsers[name] = parser
get: (pattern, handlers...) -> r.register('GET', pattern, handlers...)
post: (pattern, handlers...) -> r.register('POST', pattern, handlers...)
put: (pattern, handlers...) -> r.register('PUT', pattern, handlers...)
del: (pattern, handlers...) -> r.register('DELETE', pattern, handlers...)
get: (pattern, handlers...) ->
r.register('get', 'GET', pattern, handlers...)
post: (pattern, handlers...) ->
r.register('post', 'POST', pattern, handlers...)
put: (pattern, handlers...) ->
r.register('put', 'PUT', pattern, handlers...)
del: (pattern, handlers...) ->
r.register('del', 'DELETE', pattern, handlers...)
all: (pattern, handlers...) ->
for method in ['GET', 'POST', 'PUT', 'DELETE']
r.register(method, pattern, handlers...)
r.register('all', method, pattern, handlers...)
return
}
20 changes: 20 additions & 0 deletions test/router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,26 @@ describe 'Custom parser', ->
done()


describe 'Handlers registered on same pattern but by different methods', ->
router = createRouter()
app = connect()
app.use(router.route)

router.all '/starting/path', (req, res, next) ->
next('route')

router.get '/starting/path', (req, res) ->
res.write('matched!')
res.end()

it 'should be treated as separate routes', (done) ->
app.request()
.get('/starting/path')
.end (res) ->
res.body.should.eql('matched!')
done()


describe 'Conditional middlewares', ->
router = createRouter()
app = connect()
Expand Down

0 comments on commit 066771f

Please sign in to comment.