Skip to content
Myr

Allow options without filename in res.download

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent f739b16 · Browse files · 3 files+206 added−62 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+206 added−62 removed

  • tests:+198 added−62 removed
  • other:+8 added−0 removed

3 files+206 added−62 removed

modified:lib/response.js+7 added
559 unmodified lines
560 done = options561 opts = null562 }563564 // support optional filename, where options may be in it's place565 if (typeof filename === 'object' &&566 (typeof options === 'function' || options === undefined)) {567 name = null568 opts = filename569 }570571 // set Content-Disposition when file is sent572 var headers = {573 'Content-Disposition': contentDisposition(name || path)574 };
592 unmodified lines
modified:test/res.download.js+198 added-62 removed
84 unmodified lines
85 .expect(200, cb);86 })87 })8889 describe('.download(path, filename, fn)', function(){90 it('should invoke the callback', function(done){91 var app = express();92 var cb = after(2, done);9394 app.use(function(req, res){95 res.download('test/fixtures/user.html', 'document', cb)96 });9798 request(app)99 .get('/')100 .expect('Content-Type', 'text/html; charset=UTF-8')101 .expect('Content-Disposition', 'attachment; filename="document"')102 .expect(200, cb);103 })104 })105106 describe('.download(path, filename, options, fn)', function () {107 it('should invoke the callback', function (done) {108 var app = express()109 var cb = after(2, done)110 var options = {}111112 app.use(function (req, res) {113 res.download('test/fixtures/user.html', 'document', options, cb)114 })115116 request(app)117 .get('/')118 .expect(200)119 .expect('Content-Type', 'text/html; charset=UTF-8')120 .expect('Content-Disposition', 'attachment; filename="document"')121 .end(cb)122 })12389 describe('.download(path, options)', function () {90 it('should allow options to res.sendFile()', function (done) {91 var app = express()9293 app.use(function (req, res) {128 res.download('test/fixtures/.name', 'document', {94 res.download('test/fixtures/.name', {95 dotfiles: 'allow',96 maxAge: '4h'97 })98 })99100 request(app)101 .get('/')102 .expect(200)137 .expect('Content-Disposition', 'attachment; filename="document"')103 .expect('Content-Disposition', 'attachment; filename=".name"')104 .expect('Cache-Control', 'public, max-age=14400')105 .expect(utils.shouldHaveBody(Buffer.from('tobi')))106 .end(done)107 })108143 describe('when options.headers contains Content-Disposition', function () {144 it('should be ignored', function (done) {109 describe('with "headers" option', function () {110 it('should set headers on response', function (done) {111 var app = express()112113 app.use(function (req, res) {148 res.download('test/fixtures/user.html', 'document', {114 res.download('test/fixtures/user.html', {115 headers: {150 'Content-Type': 'text/x-custom',151 'Content-Disposition': 'inline'116 'X-Foo': 'Bar',117 'X-Bar': 'Foo'118 }119 })120 })121122 request(app)157 .get('/')158 .expect(200)159 .expect('Content-Type', 'text/x-custom')160 .expect('Content-Disposition', 'attachment; filename="document"')161 .end(done)123 .get('/')124 .expect(200)125 .expect('X-Foo', 'Bar')126 .expect('X-Bar', 'Foo')127 .end(done)128 })129164 it('should be ignored case-insensitively', function (done) {130 it('should use last header when duplicated', function (done) {131 var app = express()132133 app.use(function (req, res) {168 res.download('test/fixtures/user.html', 'document', {134 res.download('test/fixtures/user.html', {135 headers: {170 'content-type': 'text/x-custom',171 'content-disposition': 'inline'136 'X-Foo': 'Bar',137 'x-foo': 'bar'138 }139 })140 })141142 request(app)177 .get('/')178 .expect(200)179 .expect('Content-Type', 'text/x-custom')180 .expect('Content-Disposition', 'attachment; filename="document"')181 .end(done)143 .get('/')144 .expect(200)145 .expect('X-Foo', 'bar')146 .end(done)147 })148149 it('should override Content-Type', function (done) {150 var app = express()151152 app.use(function (req, res) {153 res.download('test/fixtures/user.html', {154 headers: {155 'Content-Type': 'text/x-custom'156 }157 })158 })159160 request(app)161 .get('/')162 .expect(200)163 .expect('Content-Type', 'text/x-custom')164 .end(done)165 })166167 it('should not set headers on 404', function (done) {168 var app = express()169170 app.use(function (req, res) {171 res.download('test/fixtures/does-not-exist', {172 headers: {173 'X-Foo': 'Bar'174 }175 })176 })177178 request(app)179 .get('/')180 .expect(404)181 .expect(utils.shouldNotHaveHeader('X-Foo'))182 .end(done)183 })184185 describe('when headers contains Content-Disposition', function () {186 it('should be ignored', function (done) {187 var app = express()188189 app.use(function (req, res) {190 res.download('test/fixtures/user.html', {191 headers: {192 'Content-Disposition': 'inline'193 }194 })195 })196197 request(app)198 .get('/')199 .expect(200)200 .expect('Content-Disposition', 'attachment; filename="user.html"')201 .end(done)202 })203204 it('should be ignored case-insensitively', function (done) {205 var app = express()206207 app.use(function (req, res) {208 res.download('test/fixtures/user.html', {209 headers: {210 'content-disposition': 'inline'211 }212 })213 })214215 request(app)216 .get('/')217 .expect(200)218 .expect('Content-Disposition', 'attachment; filename="user.html"')219 .end(done)220 })221 })222 })223224 describe('with "root" option', function () {225 it('should allow relative path', function (done) {226 var app = express()227228 app.use(function (req, res) {190 res.download('name.txt', 'document', {229 res.download('name.txt', {230 root: FIXTURES_PATH231 })232 })233234 request(app)235 .get('/')236 .expect(200)198 .expect('Content-Disposition', 'attachment; filename="document"')237 .expect('Content-Disposition', 'attachment; filename="name.txt"')238 .expect(utils.shouldHaveBody(Buffer.from('tobi')))239 .end(done)240 })241242 it('should allow up within root', function (done) {243 var app = express()244245 app.use(function (req, res) {207 res.download('fake/../name.txt', 'document', {246 res.download('fake/../name.txt', {247 root: FIXTURES_PATH248 })249 })250251 request(app)252 .get('/')253 .expect(200)215 .expect('Content-Disposition', 'attachment; filename="document"')254 .expect('Content-Disposition', 'attachment; filename="name.txt"')255 .expect(utils.shouldHaveBody(Buffer.from('tobi')))256 .end(done)257 })258
3 unmodified lines
262 app.use(function (req, res) {263 var p = '..' + path.sep +264 path.relative(path.dirname(FIXTURES_PATH), path.join(FIXTURES_PATH, 'name.txt'))265227 res.download(p, 'document', {266 res.download(p, {267 root: FIXTURES_PATH268 })269 })270
7 unmodified lines
278 it('should reject reading outside root', function (done) {279 var app = express()280281 app.use(function (req, res) {243 res.download('../name.txt', 'document', {282 res.download('../name.txt', {283 root: FIXTURES_PATH284 })285 })286
5 unmodified lines
292 })293 })294 })295296 describe('.download(path, filename, fn)', function(){297 it('should invoke the callback', function(done){298 var app = express();299 var cb = after(2, done);300301 app.use(function(req, res){302 res.download('test/fixtures/user.html', 'document', cb)303 });304305 request(app)306 .get('/')307 .expect('Content-Type', 'text/html; charset=UTF-8')308 .expect('Content-Disposition', 'attachment; filename="document"')309 .expect(200, cb);310 })311 })312313 describe('.download(path, filename, options, fn)', function () {314 it('should invoke the callback', function (done) {315 var app = express()316 var cb = after(2, done)317 var options = {}318319 app.use(function (req, res) {320 res.download('test/fixtures/user.html', 'document', options, cb)321 })322323 request(app)324 .get('/')325 .expect(200)326 .expect('Content-Type', 'text/html; charset=UTF-8')327 .expect('Content-Disposition', 'attachment; filename="document"')328 .end(cb)329 })330331 it('should allow options to res.sendFile()', function (done) {332 var app = express()333334 app.use(function (req, res) {335 res.download('test/fixtures/.name', 'document', {336 dotfiles: 'allow',337 maxAge: '4h'338 })339 })340341 request(app)342 .get('/')343 .expect(200)344 .expect('Content-Disposition', 'attachment; filename="document"')345 .expect('Cache-Control', 'public, max-age=14400')346 .expect(utils.shouldHaveBody(Buffer.from('tobi')))347 .end(done)348 })349350 describe('when options.headers contains Content-Disposition', function () {351 it('should be ignored', function (done) {352 var app = express()353354 app.use(function (req, res) {355 res.download('test/fixtures/user.html', 'document', {356 headers: {357 'Content-Type': 'text/x-custom',358 'Content-Disposition': 'inline'359 }360 })361 })362363 request(app)364 .get('/')365 .expect(200)366 .expect('Content-Type', 'text/x-custom')367 .expect('Content-Disposition', 'attachment; filename="document"')368 .end(done)369 })370371 it('should be ignored case-insensitively', function (done) {372 var app = express()373374 app.use(function (req, res) {375 res.download('test/fixtures/user.html', 'document', {376 headers: {377 'content-type': 'text/x-custom',378 'content-disposition': 'inline'379 }380 })381 })382383 request(app)384 .get('/')385 .expect(200)386 .expect('Content-Type', 'text/x-custom')387 .expect('Content-Disposition', 'attachment; filename="document"')388 .end(done)389 })390 })391 })392393 describe('on failure', function(){394 it('should invoke the callback', function(done){395 var app = express();396
29 unmodified lines
modified:History.md+1 added
1unreleased2==========34 * Add "root" option to `res.download`5 * Allow `options` without `filename` in `res.download`6 * Deprecate string and non-integer arguments to `res.status`7 * Ignore `Object.prototype` values in settings through `app.set`/`app.get`8 * Support proper 205 responses using `res.send`9 * deps: finalhandler@1.2.0
3539 unmodified lines