Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
tests: improve res.download tests
  • Loading branch information
dougwilson committed Feb 16, 2015
1 parent 63ab255 commit ca480d7
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions test/res.download.js
@@ -1,7 +1,8 @@

var express = require('../')
, request = require('supertest')
, assert = require('assert');
var after = require('after');
var assert = require('assert');
var express = require('..');
var request = require('supertest');

describe('res', function(){
describe('.download(path)', function(){
Expand Down Expand Up @@ -38,27 +39,25 @@ describe('res', function(){

describe('.download(path, fn)', function(){
it('should invoke the callback', function(done){
var app = express()
, calls = 0;
var app = express();
var cb = after(2, done);

app.use(function(req, res){
res.download('test/fixtures/user.html', done);
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, function(err){
assert.ifError(err)
})
.expect(200, cb);
})
})

describe('.download(path, filename, fn)', function(){
it('should invoke the callback', function(done){
var app = express()
, calls = 0;
var app = express();
var cb = after(2, done);

app.use(function(req, res){
res.download('test/fixtures/user.html', 'document', done);
Expand All @@ -68,48 +67,47 @@ describe('res', function(){
.get('/')
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect('Content-Disposition', 'attachment; filename="document"')
.expect(200, function(err){
assert.ifError(err)
})
.expect(200, cb);
})
})

describe('on failure', function(){
it('should invoke the callback', function(done){
var app = express()
, calls = 0;
var app = express();

app.use(function(req, res){
app.use(function (req, res, next) {
res.download('test/fixtures/foobar.html', function(err){
assert(404 == err.status);
assert('ENOENT' == err.code);
done();
if (!err) return next(new Error('expected error'));
res.send('got ' + err.status + ' ' + err.code);
});
});

request(app)
.get('/')
.end(function(){});
.expect(200, 'got 404 ENOENT', done);
})

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

app.use(function(req, res){
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('failed')
.end(function(err, res){
if (err) return done(err);
res.header.should.not.have.property('content-disposition');
done();
});
.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);
};
}

0 comments on commit ca480d7

Please sign in to comment.