-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent next from being called multiple times #26
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
bf0b4e9
Add reflection property for layer
calebmer f819bec
Revert "Add reflection property for layer"
calebmer 113cf3f
Once-ify next method when passed to middleware
calebmer ba30899
Change error message
calebmer 544d0cd
Extract async error catching
calebmer c53d740
Add another test
calebmer e507bde
Rename error
calebmer 1e530cb
Multi-next errors are cathable
calebmer 4d8344d
Multi-next errors are catchable
calebmer cb28b7f
Merge branch 'many-next' of https://github.com/calebmer/router into m…
calebmer ab009c2
Test refactoring
calebmer 0fb9b31
next() can be called twice with an error
calebmer 127940d
Fix node v0.8.x bug
calebmer 657450b
Assertion message describes wanted outcome
calebmer 823a873
Refactor once method
calebmer ba289a9
Use after module in tests
calebmer 7a3b12f
Make headers sent check a configuration option
calebmer 9037468
More descriptive JSDoc
calebmer 28ad1ae
New lines for block
calebmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
|
||
var after = require('after') | ||
var Router = require('..') | ||
var utils = require('./support/utils') | ||
|
||
var assert = utils.assert | ||
var createHitHandle = utils.createHitHandle | ||
var createErrorHitHandle = utils.createErrorHitHandle | ||
var shouldHitHandle = utils.shouldHitHandle | ||
var shouldNotHitHandle = utils.shouldNotHitHandle | ||
var manyAsyncCalls = utils.manyAsyncCalls | ||
var createServer = utils.createServer | ||
var request = utils.request | ||
|
||
describe('middleware', function () { | ||
it('cannot call the next function twice', function (done) { | ||
var router = Router() | ||
var server = createServer(router) | ||
done = after(2, done) | ||
|
||
router.use(function (req, res, next) { | ||
next() | ||
next() | ||
}) | ||
|
||
router.use(function (req, res, next) { res.end() }) | ||
router.use(createHitHandle(1, { checkHeadersSent: true })) | ||
router.use(createErrorHitHandle(2, { checkHeadersSent: true })) | ||
|
||
router.use(function (error, req, res, next) { | ||
assert.equal(error.message, 'next() cannot be called twice') | ||
done() | ||
}) | ||
|
||
request(server) | ||
.get('/') | ||
.expect(shouldNotHitHandle(1)) | ||
.expect(shouldNotHitHandle(2)) | ||
.expect(200, done) | ||
}) | ||
|
||
it('cannot call the next function twice in an error handler', function (done) { | ||
var router = Router() | ||
var server = createServer(router) | ||
done = after(2, done) | ||
|
||
router.use(function (req, res, next) { | ||
next(new Error('Happy error')) | ||
}) | ||
|
||
router.use(function (error, req, res, next) { | ||
next() | ||
next() | ||
}) | ||
|
||
router.use(function (req, res, next) { res.end() }) | ||
router.use(createHitHandle(1, { checkHeadersSent: true })) | ||
router.use(createErrorHitHandle(2, { checkHeadersSent: true })) | ||
|
||
router.use(function (error, req, res, next) { | ||
assert.equal(error.message, 'next() cannot be called twice') | ||
done() | ||
}) | ||
|
||
request(server) | ||
.get('/') | ||
.expect(shouldNotHitHandle(1)) | ||
.expect(shouldNotHitHandle(2)) | ||
.expect(200, done) | ||
}) | ||
|
||
it('can call next multiple times with an error', function (done) { | ||
var router = Router() | ||
var server = createServer(router) | ||
done = after(3, done) | ||
|
||
router.use(function (req, res, next) { | ||
next(new Error('1')) | ||
next(new Error('2')) | ||
res.end() | ||
}) | ||
|
||
router.use(createHitHandle(1, { checkHeadersSent: true })) | ||
|
||
router.use(function (error, req, res, next) { | ||
assert.equal(error.message, '1') | ||
done() | ||
}) | ||
|
||
router.use(function (error, req, res, next) { | ||
assert.equal(error.message, '2') | ||
done() | ||
}) | ||
|
||
request(server) | ||
.get('/') | ||
.expect(shouldNotHitHandle(1)) | ||
.expect(200, done) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function needs jsdoc comment.