Skip to content

Commit

Permalink
tests: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 27, 2014
1 parent 4439aef commit a96b32a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,33 @@ describe('bodyParser.json()', function(){
.send('["tobi"]')
.expect(403, 'no arrays', done)
})

it('should allow custom codes', function(done){
var server = createServer({verify: function(req, res, buf){
if (buf[0] !== 0x5b) return
var err = new Error('no arrays')
err.status = 400
throw err
}})

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send('["tobi"]')
.expect(400, 'no arrays', done)
})

it('should allow pass-through', function(done){
var server = createServer({verify: function(req, res, buf){
if (buf[0] === 0x5b) throw new Error('no arrays')
}})

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"user":"tobi"}', done)
})
})

it('should support utf-8', function(done){
Expand Down
38 changes: 38 additions & 0 deletions test/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ describe('bodyParser.urlencoded()', function(){
test.expect(400, /content length/, done)
})

it('should handle empty message-body', function(done){
var server = createServer({ limit: '1kb' })

request(server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.set('Transfer-Encoding', 'chunked')
.send('')
.expect(200, '{}', done)
})

describe('with limit option', function(){
it('should 413 when over limit with Content-Length', function(done){
var buf = new Buffer(1024)
Expand Down Expand Up @@ -160,6 +171,33 @@ describe('bodyParser.urlencoded()', function(){
.send(' user=tobi')
.expect(403, 'no leading space', done)
})

it('should allow custom codes', function(done){
var server = createServer({verify: function(req, res, buf){
if (buf[0] !== 0x20) return
var err = new Error('no leading space')
err.status = 400
throw err
}})

request(server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send(' user=tobi')
.expect(400, 'no leading space', done)
})

it('should allow pass-through', function(done){
var server = createServer({verify: function(req, res, buf){
if (buf[0] === 0x5b) throw new Error('no arrays')
}})

request(server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('user=tobi')
.expect(200, '{"user":"tobi"}', done)
})
})
})

Expand Down

0 comments on commit a96b32a

Please sign in to comment.