2 var after = require('after');
3 var assert = require('assert');
4 var express = require('..');
5 var request = require('supertest');
7 describe('res', function(){
8 describe('.download(path)', function(){
9 it('should transfer as an attachment', function(done){
12 app.use(function(req, res){
13 res.download('test/fixtures/user.html');
18 .expect('Content-Type', 'text/html; charset=UTF-8')
19 .expect('Content-Disposition', 'attachment; filename="user.html"')
20 .expect(200, '<p>{{user.name}}</p>', done)
24 describe('.download(path, filename)', function(){
25 it('should provide an alternate filename', function(done){
28 app.use(function(req, res){
29 res.download('test/fixtures/user.html', 'document');
34 .expect('Content-Type', 'text/html; charset=UTF-8')
35 .expect('Content-Disposition', 'attachment; filename="document"')
40 describe('.download(path, fn)', function(){
41 it('should invoke the callback', function(done){
43 var cb = after(2, done);
45 app.use(function(req, res){
46 res.download('test/fixtures/user.html', cb);
51 .expect('Content-Type', 'text/html; charset=UTF-8')
52 .expect('Content-Disposition', 'attachment; filename="user.html"')
57 describe('.download(path, filename, fn)', function(){
58 it('should invoke the callback', function(done){
60 var cb = after(2, done);
62 app.use(function(req, res){
63 res.download('test/fixtures/user.html', 'document', done);
68 .expect('Content-Type', 'text/html; charset=UTF-8')
69 .expect('Content-Disposition', 'attachment; filename="document"')
74 describe('.download(path, filename, options, fn)', function () {
75 it('should invoke the callback', function (done) {
77 var cb = after(2, done)
80 app.use(function (req, res) {
81 res.download('test/fixtures/user.html', 'document', options, done)
87 .expect('Content-Type', 'text/html; charset=UTF-8')
88 .expect('Content-Disposition', 'attachment; filename="document"')
92 it('should allow options to res.sendFile()', function (done) {
95 app.use(function (req, res) {
96 res.download('test/fixtures/.name', 'document', {
105 .expect('Content-Disposition', 'attachment; filename="document"')
106 .expect('Cache-Control', 'public, max-age=14400')
111 describe('when options.headers contains Content-Disposition', function () {
112 it('should should be ignored', function (done) {
115 app.use(function (req, res) {
116 res.download('test/fixtures/user.html', 'document', {
118 'Content-Type': 'text/x-custom',
119 'Content-Disposition': 'inline'
127 .expect('Content-Type', 'text/x-custom')
128 .expect('Content-Disposition', 'attachment; filename="document"')
132 it('should should be ignored case-insensitively', function (done) {
135 app.use(function (req, res) {
136 res.download('test/fixtures/user.html', 'document', {
138 'content-type': 'text/x-custom',
139 'content-disposition': 'inline'
147 .expect('Content-Type', 'text/x-custom')
148 .expect('Content-Disposition', 'attachment; filename="document"')
154 describe('on failure', function(){
155 it('should invoke the callback', function(done){
158 app.use(function (req, res, next) {
159 res.download('test/fixtures/foobar.html', function(err){
160 if (!err) return next(new Error('expected error'));
161 res.send('got ' + err.status + ' ' + err.code);
167 .expect(200, 'got 404 ENOENT', done);
170 it('should remove Content-Disposition', function(done){
173 app.use(function (req, res, next) {
174 res.download('test/fixtures/foobar.html', function(err){
175 if (!err) return next(new Error('expected error'));
182 .expect(shouldNotHaveHeader('Content-Disposition'))
183 .expect(200, 'failed', done);
188 function shouldNotHaveHeader(header) {
189 return function (res) {
190 assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header);