res.download.js
This file
Path
test/res.download.js
487 lines · 12.8 KiB · JavaScript
Last changed by
Upgrade `content-disposition` (#7233)Blake Embreybefore adoption59e205a
History of this fileTake it
1'use strict'23var after = require('after');4var assert = require('node:assert')5var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage6const { Buffer } = require('node:buffer');78var express = require('..');9var path = require('node:path')10var request = require('supertest');11var utils = require('./support/utils')1213var FIXTURES_PATH = path.join(__dirname, 'fixtures')1415describe('res', function(){16 describe('.download(path)', function(){17 it('should transfer as an attachment', function(done){18 var app = express();1920 app.use(function(req, res){21 res.download('test/fixtures/user.html');22 });2324 request(app)25 .get('/')26 .expect('Content-Type', 'text/html; charset=utf-8')27 .expect('Content-Disposition', 'attachment; filename=user.html')28 .expect(200, '<p>{{user.name}}</p>', done)29 })3031 it('should accept range requests', function (done) {32 var app = express()3334 app.get('/', function (req, res) {35 res.download('test/fixtures/user.html')36 })3738 request(app)39 .get('/')40 .expect('Accept-Ranges', 'bytes')41 .expect(200, '<p>{{user.name}}</p>', done)42 })4344 it('should respond with requested byte range', function (done) {45 var app = express()4647 app.get('/', function (req, res) {48 res.download('test/fixtures/user.html')49 })5051 request(app)52 .get('/')53 .set('Range', 'bytes=0-2')54 .expect('Content-Range', 'bytes 0-2/20')55 .expect(206, '<p>', done)56 })57 })5859 describe('.download(path, filename)', function(){60 it('should provide an alternate filename', function(done){61 var app = express();6263 app.use(function(req, res){64 res.download('test/fixtures/user.html', 'document');65 });6667 request(app)68 .get('/')69 .expect('Content-Type', 'text/html; charset=utf-8')70 .expect('Content-Disposition', 'attachment; filename=document')71 .expect(200, done)72 })73 })7475 describe('.download(path, fn)', function(){76 it('should invoke the callback', function(done){77 var app = express();78 var cb = after(2, done);7980 app.use(function(req, res){81 res.download('test/fixtures/user.html', cb);82 });8384 request(app)85 .get('/')86 .expect('Content-Type', 'text/html; charset=utf-8')87 .expect('Content-Disposition', 'attachment; filename=user.html')88 .expect(200, cb);89 })9091 describe('async local storage', function () {92 it('should persist store', function (done) {93 var app = express()94 var cb = after(2, done)95 var store = { foo: 'bar' }9697 app.use(function (req, res, next) {98 req.asyncLocalStorage = new AsyncLocalStorage()99 req.asyncLocalStorage.run(store, next)100 })101102 app.use(function (req, res) {103 res.download('test/fixtures/name.txt', function (err) {104 if (err) return cb(err)105106 var local = req.asyncLocalStorage.getStore()107108 assert.strictEqual(local.foo, 'bar')109 cb()110 })111 })112113 request(app)114 .get('/')115 .expect('Content-Type', 'text/plain; charset=utf-8')116 .expect('Content-Disposition', 'attachment; filename=name.txt')117 .expect(200, 'tobi', cb)118 })119120 it('should persist store on error', function (done) {121 var app = express()122 var store = { foo: 'bar' }123124 app.use(function (req, res, next) {125 req.asyncLocalStorage = new AsyncLocalStorage()126 req.asyncLocalStorage.run(store, next)127 })128129 app.use(function (req, res) {130 res.download('test/fixtures/does-not-exist', function (err) {131 var local = req.asyncLocalStorage.getStore()132133 if (local) {134 res.setHeader('x-store-foo', String(local.foo))135 }136137 res.send(err ? 'got ' + err.status + ' error' : 'no error')138 })139 })140141 request(app)142 .get('/')143 .expect(200)144 .expect('x-store-foo', 'bar')145 .expect('got 404 error')146 .end(done)147 })148 })149 })150151 describe('.download(path, options)', function () {152 it('should allow options to res.sendFile()', function (done) {153 var app = express()154155 app.use(function (req, res) {156 res.download('test/fixtures/.name', {157 dotfiles: 'allow',158 maxAge: '4h'159 })160 })161162 request(app)163 .get('/')164 .expect(200)165 .expect('Content-Disposition', 'attachment; filename=.name')166 .expect('Cache-Control', 'public, max-age=14400')167 .expect(utils.shouldHaveBody(Buffer.from('tobi')))168 .end(done)169 })170171 describe('with "headers" option', function () {172 it('should set headers on response', function (done) {173 var app = express()174175 app.use(function (req, res) {176 res.download('test/fixtures/user.html', {177 headers: {178 'X-Foo': 'Bar',179 'X-Bar': 'Foo'180 }181 })182 })183184 request(app)185 .get('/')186 .expect(200)187 .expect('X-Foo', 'Bar')188 .expect('X-Bar', 'Foo')189 .end(done)190 })191192 it('should use last header when duplicated', function (done) {193 var app = express()194195 app.use(function (req, res) {196 res.download('test/fixtures/user.html', {197 headers: {198 'X-Foo': 'Bar',199 'x-foo': 'bar'200 }201 })202 })203204 request(app)205 .get('/')206 .expect(200)207 .expect('X-Foo', 'bar')208 .end(done)209 })210211 it('should override Content-Type', function (done) {212 var app = express()213214 app.use(function (req, res) {215 res.download('test/fixtures/user.html', {216 headers: {217 'Content-Type': 'text/x-custom'218 }219 })220 })221222 request(app)223 .get('/')224 .expect(200)225 .expect('Content-Type', 'text/x-custom')226 .end(done)227 })228229 it('should not set headers on 404', function (done) {230 var app = express()231232 app.use(function (req, res) {233 res.download('test/fixtures/does-not-exist', {234 headers: {235 'X-Foo': 'Bar'236 }237 })238 })239240 request(app)241 .get('/')242 .expect(404)243 .expect(utils.shouldNotHaveHeader('X-Foo'))244 .end(done)245 })246247 describe('when headers contains Content-Disposition', function () {248 it('should be ignored', function (done) {249 var app = express()250251 app.use(function (req, res) {252 res.download('test/fixtures/user.html', {253 headers: {254 'Content-Disposition': 'inline'255 }256 })257 })258259 request(app)260 .get('/')261 .expect(200)262 .expect('Content-Disposition', 'attachment; filename=user.html')263 .end(done)264 })265266 it('should be ignored case-insensitively', function (done) {267 var app = express()268269 app.use(function (req, res) {270 res.download('test/fixtures/user.html', {271 headers: {272 'content-disposition': 'inline'273 }274 })275 })276277 request(app)278 .get('/')279 .expect(200)280 .expect('Content-Disposition', 'attachment; filename=user.html')281 .end(done)282 })283 })284 })285286 describe('with "root" option', function () {287 it('should allow relative path', function (done) {288 var app = express()289290 app.use(function (req, res) {291 res.download('name.txt', {292 root: FIXTURES_PATH293 })294 })295296 request(app)297 .get('/')298 .expect(200)299 .expect('Content-Disposition', 'attachment; filename=name.txt')300 .expect(utils.shouldHaveBody(Buffer.from('tobi')))301 .end(done)302 })303304 it('should allow up within root', function (done) {305 var app = express()306307 app.use(function (req, res) {308 res.download('fake/../name.txt', {309 root: FIXTURES_PATH310 })311 })312313 request(app)314 .get('/')315 .expect(200)316 .expect('Content-Disposition', 'attachment; filename=name.txt')317 .expect(utils.shouldHaveBody(Buffer.from('tobi')))318 .end(done)319 })320321 it('should reject up outside root', function (done) {322 var app = express()323324 app.use(function (req, res) {325 var p = '..' + path.sep +326 path.relative(path.dirname(FIXTURES_PATH), path.join(FIXTURES_PATH, 'name.txt'))327328 res.download(p, {329 root: FIXTURES_PATH330 })331 })332333 request(app)334 .get('/')335 .expect(403)336 .expect(utils.shouldNotHaveHeader('Content-Disposition'))337 .end(done)338 })339340 it('should reject reading outside root', function (done) {341 var app = express()342343 app.use(function (req, res) {344 res.download('../name.txt', {345 root: FIXTURES_PATH346 })347 })348349 request(app)350 .get('/')351 .expect(403)352 .expect(utils.shouldNotHaveHeader('Content-Disposition'))353 .end(done)354 })355 })356 })357358 describe('.download(path, filename, fn)', function(){359 it('should invoke the callback', function(done){360 var app = express();361 var cb = after(2, done);362363 app.use(function(req, res){364 res.download('test/fixtures/user.html', 'document', cb)365 });366367 request(app)368 .get('/')369 .expect('Content-Type', 'text/html; charset=utf-8')370 .expect('Content-Disposition', 'attachment; filename=document')371 .expect(200, cb);372 })373 })374375 describe('.download(path, filename, options, fn)', function () {376 it('should invoke the callback', function (done) {377 var app = express()378 var cb = after(2, done)379 var options = {}380381 app.use(function (req, res) {382 res.download('test/fixtures/user.html', 'document', options, cb)383 })384385 request(app)386 .get('/')387 .expect(200)388 .expect('Content-Type', 'text/html; charset=utf-8')389 .expect('Content-Disposition', 'attachment; filename=document')390 .end(cb)391 })392393 it('should allow options to res.sendFile()', function (done) {394 var app = express()395396 app.use(function (req, res) {397 res.download('test/fixtures/.name', 'document', {398 dotfiles: 'allow',399 maxAge: '4h'400 })401 })402403 request(app)404 .get('/')405 .expect(200)406 .expect('Content-Disposition', 'attachment; filename=document')407 .expect('Cache-Control', 'public, max-age=14400')408 .expect(utils.shouldHaveBody(Buffer.from('tobi')))409 .end(done)410 })411412 describe('when options.headers contains Content-Disposition', function () {413 it('should be ignored', function (done) {414 var app = express()415416 app.use(function (req, res) {417 res.download('test/fixtures/user.html', 'document', {418 headers: {419 'Content-Type': 'text/x-custom',420 'Content-Disposition': 'inline'421 }422 })423 })424425 request(app)426 .get('/')427 .expect(200)428 .expect('Content-Type', 'text/x-custom')429 .expect('Content-Disposition', 'attachment; filename=document')430 .end(done)431 })432433 it('should be ignored case-insensitively', function (done) {434 var app = express()435436 app.use(function (req, res) {437 res.download('test/fixtures/user.html', 'document', {438 headers: {439 'content-type': 'text/x-custom',440 'content-disposition': 'inline'441 }442 })443 })444445 request(app)446 .get('/')447 .expect(200)448 .expect('Content-Type', 'text/x-custom')449 .expect('Content-Disposition', 'attachment; filename=document')450 .end(done)451 })452 })453 })454455 describe('on failure', function(){456 it('should invoke the callback', function(done){457 var app = express();458459 app.use(function (req, res, next) {460 res.download('test/fixtures/foobar.html', function(err){461 if (!err) return next(new Error('expected error'));462 res.send('got ' + err.status + ' ' + err.code);463 });464 });465466 request(app)467 .get('/')468 .expect(200, 'got 404 ENOENT', done);469 })470471 it('should remove Content-Disposition', function(done){472 var app = express()473474 app.use(function (req, res, next) {475 res.download('test/fixtures/foobar.html', function(err){476 if (!err) return next(new Error('expected error'));477 res.end('failed');478 });479 });480481 request(app)482 .get('/')483 .expect(utils.shouldNotHaveHeader('Content-Disposition'))484 .expect(200, 'failed', done)485 })486 })487})