/
res.download.js
113 lines (93 loc) · 3.02 KB
/
res.download.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var after = require('after');
var assert = require('assert');
var express = require('..');
var request = require('supertest');
describe('res', function(){
describe('.download(path)', function(){
it('should transfer as an attachment', function(done){
var app = express();
app.use(function(req, res){
res.download('test/fixtures/user.html');
});
request(app)
.get('/')
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect('Content-Disposition', 'attachment; filename="user.html"')
.expect(200, '<p>{{user.name}}</p>', done)
})
})
describe('.download(path, filename)', function(){
it('should provide an alternate filename', function(done){
var app = express();
app.use(function(req, res){
res.download('test/fixtures/user.html', 'document');
});
request(app)
.get('/')
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect('Content-Disposition', 'attachment; filename="document"')
.expect(200, done)
})
})
describe('.download(path, fn)', function(){
it('should invoke the callback', function(done){
var app = express();
var cb = after(2, done);
app.use(function(req, res){
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, cb);
})
})
describe('.download(path, filename, fn)', function(){
it('should invoke the callback', function(done){
var app = express();
var cb = after(2, done);
app.use(function(req, res){
res.download('test/fixtures/user.html', 'document', done);
});
request(app)
.get('/')
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect('Content-Disposition', 'attachment; filename="document"')
.expect(200, cb);
})
})
describe('on failure', function(){
it('should invoke the callback', function(done){
var app = express();
app.use(function (req, res, next) {
res.download('test/fixtures/foobar.html', function(err){
if (!err) return next(new Error('expected error'));
res.send('got ' + err.status + ' ' + err.code);
});
});
request(app)
.get('/')
.expect(200, 'got 404 ENOENT', done);
})
it('should remove Content-Disposition', function(done){
var app = express()
, calls = 0;
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(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);
};
}