Skip to content

Commit

Permalink
Now invoking 'response.end' will ensure any remaining handlers are ca…
Browse files Browse the repository at this point in the history
…nceled
  • Loading branch information
tarruda committed Aug 29, 2012
1 parent 9dae412 commit f93bbe8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/router.coffee
Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion test/router.coffee
@@ -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)
Expand All @@ -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$')
Expand All @@ -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()
Expand Down

0 comments on commit f93bbe8

Please sign in to comment.