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