Skip to content

Commit ca480d7

Browse files
committed
tests: improve res.download tests
1 parent 63ab255 commit ca480d7

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

test/res.download.js

+26-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

2-
var express = require('../')
3-
, request = require('supertest')
4-
, assert = require('assert');
2+
var after = require('after');
3+
var assert = require('assert');
4+
var express = require('..');
5+
var request = require('supertest');
56

67
describe('res', function(){
78
describe('.download(path)', function(){
@@ -38,27 +39,25 @@ describe('res', function(){
3839

3940
describe('.download(path, fn)', function(){
4041
it('should invoke the callback', function(done){
41-
var app = express()
42-
, calls = 0;
42+
var app = express();
43+
var cb = after(2, done);
4344

4445
app.use(function(req, res){
45-
res.download('test/fixtures/user.html', done);
46+
res.download('test/fixtures/user.html', cb);
4647
});
4748

4849
request(app)
4950
.get('/')
5051
.expect('Content-Type', 'text/html; charset=UTF-8')
5152
.expect('Content-Disposition', 'attachment; filename="user.html"')
52-
.expect(200, function(err){
53-
assert.ifError(err)
54-
})
53+
.expect(200, cb);
5554
})
5655
})
5756

5857
describe('.download(path, filename, fn)', function(){
5958
it('should invoke the callback', function(done){
60-
var app = express()
61-
, calls = 0;
59+
var app = express();
60+
var cb = after(2, done);
6261

6362
app.use(function(req, res){
6463
res.download('test/fixtures/user.html', 'document', done);
@@ -68,48 +67,47 @@ describe('res', function(){
6867
.get('/')
6968
.expect('Content-Type', 'text/html; charset=UTF-8')
7069
.expect('Content-Disposition', 'attachment; filename="document"')
71-
.expect(200, function(err){
72-
assert.ifError(err)
73-
})
70+
.expect(200, cb);
7471
})
7572
})
7673

7774
describe('on failure', function(){
7875
it('should invoke the callback', function(done){
79-
var app = express()
80-
, calls = 0;
76+
var app = express();
8177

82-
app.use(function(req, res){
78+
app.use(function (req, res, next) {
8379
res.download('test/fixtures/foobar.html', function(err){
84-
assert(404 == err.status);
85-
assert('ENOENT' == err.code);
86-
done();
80+
if (!err) return next(new Error('expected error'));
81+
res.send('got ' + err.status + ' ' + err.code);
8782
});
8883
});
8984

9085
request(app)
9186
.get('/')
92-
.end(function(){});
87+
.expect(200, 'got 404 ENOENT', done);
9388
})
9489

9590
it('should remove Content-Disposition', function(done){
9691
var app = express()
9792
, calls = 0;
9893

99-
app.use(function(req, res){
94+
app.use(function (req, res, next) {
10095
res.download('test/fixtures/foobar.html', function(err){
96+
if (!err) return next(new Error('expected error'));
10197
res.end('failed');
10298
});
10399
});
104100

105101
request(app)
106102
.get('/')
107-
.expect('failed')
108-
.end(function(err, res){
109-
if (err) return done(err);
110-
res.header.should.not.have.property('content-disposition');
111-
done();
112-
});
103+
.expect(shouldNotHaveHeader('Content-Disposition'))
104+
.expect(200, 'failed', done);
113105
})
114106
})
115107
})
108+
109+
function shouldNotHaveHeader(header) {
110+
return function (res) {
111+
assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header);
112+
};
113+
}

0 commit comments

Comments
 (0)