Permalink
Browse files

Added tests describing 'conditional middlewares' behavior

  • Loading branch information...
tarruda committed Aug 30, 2012
1 parent 2d7a03a commit 036b5d3c06e5269e05994952d26d14ceb7221c71
Showing with 72 additions and 16 deletions.
  1. +72 −16 test/router.coffee
View
@@ -6,27 +6,26 @@ describe 'Rules', ->
app = connect()
app.use(router.route)
- before ->
- router.get '/$imple/.get/pattern$', (req, res) ->
- res.write('body1')
- res.end()
+ router.get '/$imple/.get/pattern$', (req, res) ->
+ res.write('body1')
+ res.end()
- router.post('/not-a-get/pattern*', -> res.end())
+ router.post('/not-a-get/pattern*', -> res.end())
- router.del('/not-a-get/pattern*', -> res.end())
+ router.del('/not-a-get/pattern*', -> res.end())
- router.get '/^pattern/that/uses/many/handlers',
- (req, res, next) -> res.write('part1'); next(),
- (req, res, next) -> res.write('part2'); next()
+ router.get '/^pattern/that/uses/many/handlers',
+ (req, res, next) -> res.write('part1'); next(),
+ (req, res, next) -> res.write('part2'); next()
- router.get '/^pattern/that/uses/many/handlers',
- (req, res) -> res.write('part3'); res.end()
+ 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()
+ 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()
@@ -357,3 +356,60 @@ describe 'Custom parser', ->
limit: '20'
done()
+
+describe 'Conditional middlewares', ->
+ router = createRouter()
+ app = connect()
+ app.use(router.route)
+
+ router.get '/public/<path:file>', (req, res) ->
+ res.write(req.params.file)
+ res.end()
+
+ router.all '/private/<path:path>', (req, res, next) ->
+ if req.headers['x-user']
+ req.loggedIn = true
+ return next('route')
+ res.writeHead(401)
+ res.end()
+
+ router.post '/private/addpost/<title>', (req, res) ->
+ req.loggedIn.should.eql(true)
+ res.write(JSON.stringify(req.params))
+ res.end()
+
+ router.get '/private/posts', (req, res) ->
+ req.loggedIn.should.eql(true)
+ res.write('post list')
+ res.end()
+
+ it 'should allow anyone to access public urls', (done) ->
+ app.request()
+ .get('/public/some/javascript.js')
+ .end (res) ->
+ res.body.should.eql('some/javascript.js')
+ done()
+
+ it 'should allow logged user to access private urls', (done) ->
+ app.request()
+ .set('X-User', 'user')
+ .post('/private/addpost/abc')
+ .end (res) ->
+ res.body.should.eql('["abc"]') # erases parameters set by middleware routes
+ app.request()
+ .set('X-User', 'user')
+ .get('/private/posts')
+ .end (res) ->
+ res.body.should.eql('post list')
+ done()
+
+ it 'should not allow anonymous user to access private urls', (done) ->
+ app.request()
+ .post('/private/addpost/abc')
+ .end (res) ->
+ res.statusCode.should.eql(401)
+ app.request()
+ .get('/private/posts')
+ .end (res) ->
+ res.statusCode.should.eql(401)
+ done()

0 comments on commit 036b5d3

Please sign in to comment.