2012-05-10 17:11:43 -07:00
|
|
|
|
2015-02-04 00:03:02 -05:00
|
|
|
var after = require('after');
|
|
|
|
|
var assert = require('assert');
|
2018-09-24 23:40:21 -04:00
|
|
|
var Buffer = require('safe-buffer').Buffer
|
2015-02-04 00:03:02 -05:00
|
|
|
var express = require('..');
|
|
|
|
|
var request = require('supertest');
|
2012-05-10 17:11:43 -07:00
|
|
|
|
|
|
|
|
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('/')
|
2014-05-29 22:53:59 -04:00
|
|
|
.expect('Content-Type', 'text/html; charset=UTF-8')
|
|
|
|
|
.expect('Content-Disposition', 'attachment; filename="user.html"')
|
|
|
|
|
.expect(200, '<p>{{user.name}}</p>', done)
|
2012-05-10 17:11:43 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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('/')
|
2014-05-29 22:53:59 -04:00
|
|
|
.expect('Content-Type', 'text/html; charset=UTF-8')
|
|
|
|
|
.expect('Content-Disposition', 'attachment; filename="document"')
|
|
|
|
|
.expect(200, done)
|
2012-05-10 17:11:43 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('.download(path, fn)', function(){
|
|
|
|
|
it('should invoke the callback', function(done){
|
2015-02-04 00:03:02 -05:00
|
|
|
var app = express();
|
|
|
|
|
var cb = after(2, done);
|
2012-05-10 17:11:43 -07:00
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
2015-02-04 00:03:02 -05:00
|
|
|
res.download('test/fixtures/user.html', cb);
|
2012-05-10 17:11:43 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
2014-05-29 22:53:59 -04:00
|
|
|
.expect('Content-Type', 'text/html; charset=UTF-8')
|
|
|
|
|
.expect('Content-Disposition', 'attachment; filename="user.html"')
|
2015-02-04 00:03:02 -05:00
|
|
|
.expect(200, cb);
|
2012-05-10 17:11:43 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('.download(path, filename, fn)', function(){
|
|
|
|
|
it('should invoke the callback', function(done){
|
2015-02-04 00:03:02 -05:00
|
|
|
var app = express();
|
|
|
|
|
var cb = after(2, done);
|
2012-05-10 17:11:43 -07:00
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
|
|
|
|
res.download('test/fixtures/user.html', 'document', done);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
2014-05-29 22:53:59 -04:00
|
|
|
.expect('Content-Type', 'text/html; charset=UTF-8')
|
|
|
|
|
.expect('Content-Disposition', 'attachment; filename="document"')
|
2015-02-04 00:03:02 -05:00
|
|
|
.expect(200, cb);
|
2012-05-10 17:11:43 -07:00
|
|
|
})
|
|
|
|
|
})
|
2012-07-13 09:24:09 -07:00
|
|
|
|
2017-07-20 21:24:04 +10:00
|
|
|
describe('.download(path, filename, options, fn)', function () {
|
|
|
|
|
it('should invoke the callback', function (done) {
|
|
|
|
|
var app = express()
|
|
|
|
|
var cb = after(2, done)
|
|
|
|
|
var options = {}
|
|
|
|
|
|
|
|
|
|
app.use(function (req, res) {
|
|
|
|
|
res.download('test/fixtures/user.html', 'document', options, done)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.expect(200)
|
|
|
|
|
.expect('Content-Type', 'text/html; charset=UTF-8')
|
|
|
|
|
.expect('Content-Disposition', 'attachment; filename="document"')
|
|
|
|
|
.end(cb)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should allow options to res.sendFile()', function (done) {
|
|
|
|
|
var app = express()
|
|
|
|
|
|
|
|
|
|
app.use(function (req, res) {
|
|
|
|
|
res.download('test/fixtures/.name', 'document', {
|
|
|
|
|
dotfiles: 'allow',
|
|
|
|
|
maxAge: '4h'
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.expect(200)
|
|
|
|
|
.expect('Content-Disposition', 'attachment; filename="document"')
|
|
|
|
|
.expect('Cache-Control', 'public, max-age=14400')
|
2018-09-24 23:40:21 -04:00
|
|
|
.expect(shouldHaveBody(Buffer.from('tobi')))
|
2017-07-20 21:24:04 +10:00
|
|
|
.end(done)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('when options.headers contains Content-Disposition', function () {
|
2019-02-01 09:56:14 +09:00
|
|
|
it('should be ignored', function (done) {
|
2017-07-20 21:24:04 +10:00
|
|
|
var app = express()
|
|
|
|
|
|
|
|
|
|
app.use(function (req, res) {
|
|
|
|
|
res.download('test/fixtures/user.html', 'document', {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'text/x-custom',
|
|
|
|
|
'Content-Disposition': 'inline'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.expect(200)
|
|
|
|
|
.expect('Content-Type', 'text/x-custom')
|
|
|
|
|
.expect('Content-Disposition', 'attachment; filename="document"')
|
|
|
|
|
.end(done)
|
|
|
|
|
})
|
|
|
|
|
|
2019-02-01 09:56:14 +09:00
|
|
|
it('should be ignored case-insensitively', function (done) {
|
2017-07-20 21:24:04 +10:00
|
|
|
var app = express()
|
|
|
|
|
|
|
|
|
|
app.use(function (req, res) {
|
|
|
|
|
res.download('test/fixtures/user.html', 'document', {
|
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'text/x-custom',
|
|
|
|
|
'content-disposition': 'inline'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.expect(200)
|
|
|
|
|
.expect('Content-Type', 'text/x-custom')
|
|
|
|
|
.expect('Content-Disposition', 'attachment; filename="document"')
|
|
|
|
|
.end(done)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2012-07-13 09:24:09 -07:00
|
|
|
describe('on failure', function(){
|
|
|
|
|
it('should invoke the callback', function(done){
|
2015-02-04 00:03:02 -05:00
|
|
|
var app = express();
|
2012-07-13 09:24:09 -07:00
|
|
|
|
2015-02-04 00:03:02 -05:00
|
|
|
app.use(function (req, res, next) {
|
2012-07-13 09:24:09 -07:00
|
|
|
res.download('test/fixtures/foobar.html', function(err){
|
2015-02-04 00:03:02 -05:00
|
|
|
if (!err) return next(new Error('expected error'));
|
|
|
|
|
res.send('got ' + err.status + ' ' + err.code);
|
2012-07-13 09:24:09 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
2015-02-04 00:03:02 -05:00
|
|
|
.expect(200, 'got 404 ENOENT', done);
|
2012-07-13 09:24:09 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should remove Content-Disposition', function(done){
|
|
|
|
|
var app = express()
|
|
|
|
|
|
2015-02-04 00:03:02 -05:00
|
|
|
app.use(function (req, res, next) {
|
2012-07-13 09:24:09 -07:00
|
|
|
res.download('test/fixtures/foobar.html', function(err){
|
2015-02-04 00:03:02 -05:00
|
|
|
if (!err) return next(new Error('expected error'));
|
2012-07-13 09:24:09 -07:00
|
|
|
res.end('failed');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
2015-02-04 00:03:02 -05:00
|
|
|
.expect(shouldNotHaveHeader('Content-Disposition'))
|
|
|
|
|
.expect(200, 'failed', done);
|
2012-07-13 09:24:09 -07:00
|
|
|
})
|
|
|
|
|
})
|
2012-05-10 17:11:43 -07:00
|
|
|
})
|
2015-02-04 00:03:02 -05:00
|
|
|
|
2018-09-24 23:40:21 -04:00
|
|
|
function shouldHaveBody (buf) {
|
|
|
|
|
return function (res) {
|
|
|
|
|
var body = !Buffer.isBuffer(res.body)
|
|
|
|
|
? Buffer.from(res.text)
|
|
|
|
|
: res.body
|
|
|
|
|
assert.ok(body, 'response has body')
|
|
|
|
|
assert.strictEqual(body.toString('hex'), buf.toString('hex'))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 00:03:02 -05:00
|
|
|
function shouldNotHaveHeader(header) {
|
|
|
|
|
return function (res) {
|
|
|
|
|
assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header);
|
|
|
|
|
};
|
|
|
|
|
}
|