Permalink
Browse files

Now invoking 'response.end' will ensure any remaining handlers are ca…

…nceled
  • Loading branch information...
1 parent 9dae412 commit f93bbe888fa0f72ab9509fb9df447d432326ab22 @tarruda committed Aug 29, 2012
Showing with 21 additions and 2 deletions.
  1. +7 −1 src/router.coffee
  2. +14 −1 test/router.coffee
View
@@ -233,12 +233,18 @@ class Router
for rule in ruleArray
if extracted = rule.extractor.extract(p)
req.params = extracted
+ end = res.end
+ status =
+ done = false
+ res.end = ->
+ status.done = true
+ end.call(res)
handlerChain = rule.handlers
handle = (i) ->
if i == handlerChain.length - 1
n = next
else
- n = -> process.nextTick(-> handle(i + 1))
+ n = -> process.nextTick(-> handle(i + 1)) if ! status.done
current = handlerChain[i]
current(req, res, n)
handle(0)
View
@@ -1,7 +1,7 @@
connect = require('connect')
createRouter = require('../src/router')
-describe 'Static rule matching', ->
+describe 'Rules', ->
router = createRouter()
app = connect()
app.use(router.route)
@@ -21,6 +21,12 @@ describe 'Static rule matching', ->
router.get '/^pattern/that/uses/many/handlers',
(req, res) -> res.write('part3'); res.end()
+ router.get '/cancel',
+ (req, res, next) -> res.write('p1'); next(),
+ (req, res, next) -> res.write('p2'); res.end(),
+ (req, res, next) -> res.write('p3'); next(),
+ (req, res, next) -> res.write('p4'); res.end()
+
it 'should match simple patterns', (done) ->
app.request()
.get('/$imple/.get/pattern$')
@@ -43,6 +49,13 @@ describe 'Static rule matching', ->
res.body.should.eql('part1part2part3')
done()
+ it 'should cancel pipeline when handler ends the request', (done) ->
+ app.request()
+ .get('/cancel')
+ .end (res) ->
+ res.body.should.eql('p1p2')
+ done()
+
describe 'Builtin string parser', ->
router = createRouter()

0 comments on commit f93bbe8

Please sign in to comment.