Skip to content

Commit

Permalink
tests: add more middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 20, 2014
1 parent 1bb6fc0 commit 4f0f358
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 4 deletions.
19 changes: 17 additions & 2 deletions test/bodyParser.js
Expand Up @@ -261,9 +261,8 @@ describe('connect.bodyParser()', function(){
})
})

// I'm too lazy to test this in both `.json()` and `.urlencoded()`
describe('verify', function () {
it('should throw 403 if verification fails', function (done) {
it('should throw 403 if verification fails in json', function (done) {
var app = connect();

app.use(connect.bodyParser({
Expand All @@ -279,6 +278,22 @@ describe('connect.bodyParser()', function(){
.expect(403, done);
})

it('should throw 403 if verification fails in urlencoded', function (done) {
var app = connect();

app.use(connect.bodyParser({
verify: function () {
throw new Error();
}
}))

app.request()
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.write('user=tobi')
.expect(403, done);
})

it('should not throw if verification does not throw', function (done) {
var app = connect();

Expand Down
13 changes: 13 additions & 0 deletions test/cookieSession.js
Expand Up @@ -20,6 +20,19 @@ describe('connect.cookieSession()', function(){
app.use(connect.cookieSession({ secret: 'some secret' }));
})

it('should 500 without secret', function(done){
var app = connect()
.use(connect.cookieParser())
.use(connect.cookieSession())
.use(function(req, res, next){
res.end();
});

app.request()
.get('/')
.expect(500, done);
})

it('should default to a browser-session length cookie', function(done){
var app = connect()
.use(connect.cookieParser())
Expand Down
46 changes: 45 additions & 1 deletion test/limit.js
Expand Up @@ -10,6 +10,32 @@ app.use(function(req, res){
});

describe('connect.limit()', function(){
it('should require limit', function(){
connect.limit.bind().should.throw()
})

it('should no break data events', function(done){
var app = connect();

app.use(connect.limit('1kb'));

app.use(function(req, res){
var buf = '';
req.on('data', function(chunk){
buf += chunk;
});
req.on('end', function(){
res.end(buf);
});
});

app.request()
.post('/')
.set('Content-Type', 'text/plain')
.write('hi!')
.expect('hi!', done);
})

describe('when Content-Length is below', function(){
it('should bypass limit()', function(done){
app.request()
Expand All @@ -27,4 +53,22 @@ describe('connect.limit()', function(){
.expect(413, done);
})
})
})

describe('when multiple limits', function(){
it('should only honor first', function(done){
var app = connect();

app.use(connect.limit('15kb'));
app.use(connect.limit('5kb'));

app.use(function(req, res){
res.end('stuff');
});

app.request()
.post('/')
.set('Content-Length', 10 * 1024)
.expect(200, done);
})
})
})
23 changes: 23 additions & 0 deletions test/multipart.js
Expand Up @@ -126,6 +126,29 @@ describe('connect.multipart()', function(){
});
})

it('should work with multiple fields of same name', function(done){
app.request()
.post('/')
.set('Content-Type', 'multipart/form-data; boundary=foo')
.write('--foo\r\n')
.write('Content-Disposition: form-data; name="user"\r\n')
.write('\r\n')
.write('Tobi')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="user"\r\n')
.write('\r\n')
.write('Bob')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="user"\r\n')
.write('\r\n')
.write('Sam')
.write('\r\n--foo--')
.end(function(res){
res.body.should.equal('{"user":["Tobi","Bob","Sam"]}');
done();
});
})

it('should support nesting', function(done){
app.request()
.post('/')
Expand Down
20 changes: 19 additions & 1 deletion test/urlencoded.js
Expand Up @@ -36,4 +36,22 @@ describe('connect.urlencoded()', function(){
done();
});
})
})

it('should support legacy verify function', function(done){
var app = connect();

app.use(connect.urlencoded({verify: function (req, res, str) {
if (str.indexOf('user') === 0) throw new Error('ack!')
}}));

app.use(function(req, res){
res.end(JSON.stringify(req.body));
});

app.request()
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.write('user=tobi')
.expect(403, done);
})
})

0 comments on commit 4f0f358

Please sign in to comment.