forked from expressjs/express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres.download.js
118 lines (99 loc) · 3.07 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
114
115
116
117
118
var express = require('../')
, request = require('./support/http')
, assert = require('assert');
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('/')
.end(function(err, res){
res.should.have.header('Content-Type', 'text/html; charset=UTF-8');
res.should.have.header('Content-Disposition', 'attachment; filename="user.html"');
res.text.should.equal('<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('/')
.end(function(err, res){
res.should.have.header('Content-Type', 'text/html; charset=UTF-8');
res.should.have.header('Content-Disposition', 'attachment; filename="document"');
done();
});
})
})
describe('.download(path, fn)', function(){
it('should invoke the callback', function(done){
var app = express()
, calls = 0;
app.use(function(req, res){
res.download('test/fixtures/user.html', done);
});
request(app)
.get('/')
.end(function(err, res){
res.should.have.header('Content-Type', 'text/html; charset=UTF-8');
res.should.have.header('Content-Disposition', 'attachment; filename="user.html"');
});
})
})
describe('.download(path, filename, fn)', function(){
it('should invoke the callback', function(done){
var app = express()
, calls = 0;
app.use(function(req, res){
res.download('test/fixtures/user.html', 'document', done);
});
request(app)
.get('/')
.end(function(err, res){
res.should.have.header('Content-Type', 'text/html; charset=UTF-8');
res.should.have.header('Content-Disposition', 'attachment; filename="document"');
});
})
})
describe('on failure', function(){
it('should invoke the callback', function(done){
var app = express()
, calls = 0;
app.use(function(req, res){
res.download('test/fixtures/foobar.html', function(err){
assert(404 == err.status);
assert('ENOENT' == err.code);
done();
});
});
request(app)
.get('/')
.end(function(){});
})
it('should remove Content-Disposition', function(done){
var app = express()
, calls = 0;
app.use(function(req, res){
res.download('test/fixtures/foobar.html', function(err){
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();
});
})
})
})