tests: improve res.download tests

This commit is contained in:
Douglas Christopher Wilson
2015-02-04 00:03:02 -05:00
parent 63ab25579b
commit ca480d7043

View File

@@ -1,7 +1,8 @@
var express = require('../') var after = require('after');
, request = require('supertest') var assert = require('assert');
, assert = require('assert'); var express = require('..');
var request = require('supertest');
describe('res', function(){ describe('res', function(){
describe('.download(path)', function(){ describe('.download(path)', function(){
@@ -38,27 +39,25 @@ describe('res', function(){
describe('.download(path, fn)', function(){ describe('.download(path, fn)', function(){
it('should invoke the callback', function(done){ it('should invoke the callback', function(done){
var app = express() var app = express();
, calls = 0; var cb = after(2, done);
app.use(function(req, res){ app.use(function(req, res){
res.download('test/fixtures/user.html', done); res.download('test/fixtures/user.html', cb);
}); });
request(app) request(app)
.get('/') .get('/')
.expect('Content-Type', 'text/html; charset=UTF-8') .expect('Content-Type', 'text/html; charset=UTF-8')
.expect('Content-Disposition', 'attachment; filename="user.html"') .expect('Content-Disposition', 'attachment; filename="user.html"')
.expect(200, function(err){ .expect(200, cb);
assert.ifError(err)
})
}) })
}) })
describe('.download(path, filename, fn)', function(){ describe('.download(path, filename, fn)', function(){
it('should invoke the callback', function(done){ it('should invoke the callback', function(done){
var app = express() var app = express();
, calls = 0; var cb = after(2, done);
app.use(function(req, res){ app.use(function(req, res){
res.download('test/fixtures/user.html', 'document', done); res.download('test/fixtures/user.html', 'document', done);
@@ -68,48 +67,47 @@ describe('res', function(){
.get('/') .get('/')
.expect('Content-Type', 'text/html; charset=UTF-8') .expect('Content-Type', 'text/html; charset=UTF-8')
.expect('Content-Disposition', 'attachment; filename="document"') .expect('Content-Disposition', 'attachment; filename="document"')
.expect(200, function(err){ .expect(200, cb);
assert.ifError(err)
})
}) })
}) })
describe('on failure', function(){ describe('on failure', function(){
it('should invoke the callback', function(done){ it('should invoke the callback', function(done){
var app = express() var app = express();
, calls = 0;
app.use(function(req, res){ app.use(function (req, res, next) {
res.download('test/fixtures/foobar.html', function(err){ res.download('test/fixtures/foobar.html', function(err){
assert(404 == err.status); if (!err) return next(new Error('expected error'));
assert('ENOENT' == err.code); res.send('got ' + err.status + ' ' + err.code);
done();
}); });
}); });
request(app) request(app)
.get('/') .get('/')
.end(function(){}); .expect(200, 'got 404 ENOENT', done);
}) })
it('should remove Content-Disposition', function(done){ it('should remove Content-Disposition', function(done){
var app = express() var app = express()
, calls = 0; , calls = 0;
app.use(function(req, res){ app.use(function (req, res, next) {
res.download('test/fixtures/foobar.html', function(err){ res.download('test/fixtures/foobar.html', function(err){
if (!err) return next(new Error('expected error'));
res.end('failed'); res.end('failed');
}); });
}); });
request(app) request(app)
.get('/') .get('/')
.expect('failed') .expect(shouldNotHaveHeader('Content-Disposition'))
.end(function(err, res){ .expect(200, 'failed', done);
if (err) return done(err);
res.header.should.not.have.property('content-disposition');
done();
});
}) })
}) })
}) })
function shouldNotHaveHeader(header) {
return function (res) {
assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header);
};
}