res.sendFile.js
This file
Path
test/res.sendFile.js
911 lines · 23.2 KiB · JavaScript
Last changed by
Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
History of this fileTake it
The walk stopped after 200 commits: lines older than e05a520 point at it.
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 1why, one step back1'use strict'Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 2–3why, one step back23var after = require('after');Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
line 4why, one step back4var assert = require('assert')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 5why, one step back5var asyncHooks = tryRequire('async_hooks')Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
line 6why, one step back6var Buffer = require('safe-buffer').BufferMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 7–8why, one step back7var express = require('../')8 , request = require('supertest')Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 9why, one step back9var onFinished = require('on-finished');Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 10–11why, one step back10var path = require('path');11var fixtures = path.join(__dirname, 'fixtures');Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 12why, one step back12var utils = require('./support/utils');Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 13why, one step back13Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 14–17why, one step back14var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'15 ? describe16 : describe.skip17Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 18–19why, one step back18describe('res', function(){19 describe('.sendFile(path)', function () {tests: add additional res.sendFile testDouglas Christopher Wilsonbefore adoptionfbceae2
lines 20–27why, one step back20 it('should error missing path', function (done) {21 var app = createApp();2223 request(app)24 .get('/')25 .expect(500, /path.*required/, done);26 });27Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 28–35why, one step back28 it('should error for non-string path', function (done) {29 var app = createApp(42)3031 request(app)32 .get('/')33 .expect(500, /TypeError: path must be a string to res.sendFile/, done)34 })35Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 36–43why, one step back36 it('should error for non-absolute path', function (done) {37 var app = createApp('name.txt')3839 request(app)40 .get('/')41 .expect(500, /TypeError: path must be absolute/, done)42 })43Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 44–59why, one step back44 it('should transfer a file', function (done) {45 var app = createApp(path.resolve(fixtures, 'name.txt'));4647 request(app)48 .get('/')49 .expect(200, 'tobi', done);50 });5152 it('should transfer a file with special characters in string', function (done) {53 var app = createApp(path.resolve(fixtures, '% of dogs.txt'));5455 request(app)56 .get('/')57 .expect(200, '20%', done);58 });59Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 60–84why, one step back60 it('should include ETag', function (done) {61 var app = createApp(path.resolve(fixtures, 'name.txt'));6263 request(app)64 .get('/')65 .expect('ETag', /^(?:W\/)?"[^"]+"$/)66 .expect(200, 'tobi', done);67 });6869 it('should 304 when ETag matches', function (done) {70 var app = createApp(path.resolve(fixtures, 'name.txt'));7172 request(app)73 .get('/')74 .expect('ETag', /^(?:W\/)?"[^"]+"$/)75 .expect(200, 'tobi', function (err, res) {76 if (err) return done(err);77 var etag = res.headers.etag;78 request(app)79 .get('/')80 .set('If-None-Match', etag)81 .expect(304, done);82 });83 });84tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c
lines 85–92why, one step back85 it('should 404 for directory', function (done) {86 var app = createApp(path.resolve(fixtures, 'blog'));8788 request(app)89 .get('/')90 .expect(404, done);91 });92Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 93–105why, one step back93 it('should 404 when not found', function (done) {94 var app = createApp(path.resolve(fixtures, 'does-no-exist'));9596 app.use(function (req, res) {97 res.statusCode = 200;98 res.send('no!');99 });100101 request(app)102 .get('/')103 .expect(404, done);104 });105Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 106–122why, one step back106 it('should send cache-control by default', function (done) {107 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))108109 request(app)110 .get('/')111 .expect('Cache-Control', 'public, max-age=0')112 .expect(200, done)113 })114115 it('should not serve dotfiles by default', function (done) {116 var app = createApp(path.resolve(__dirname, 'fixtures/.name'))117118 request(app)119 .get('/')120 .expect(404, done)121 })122Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 123–136why, one step back123 it('should not override manual content-types', function (done) {124 var app = express();125126 app.use(function (req, res) {127 res.contentType('application/x-bogus');128 res.sendFile(path.resolve(fixtures, 'name.txt'));129 });130131 request(app)132 .get('/')133 .expect('Content-Type', 'application/x-bogus')134 .end(done);135 })136Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 137–138why, one step back137 it('should not error if the client aborts', function (done) {138 var app = express();Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 139–140why, one step back139 var cb = after(2, done)140 var error = nullMerge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 141–144why, one step back141142 app.use(function (req, res) {143 setImmediate(function () {144 res.sendFile(path.resolve(fixtures, 'name.txt'));Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 145–148why, one step back145 setTimeout(function () {146 cb(error)147 }, 10)148 })Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 149why, one step back149 test.req.abort()Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 150–152why, one step back150 });151152 app.use(function (err, req, res, next) {Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 153–154why, one step back153 error = err154 next(err)Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 155–156why, one step back155 });156Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 157–158why, one step back157 var server = app.listen()158 var test = request(server).get('/')Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 159–162why, one step back159 test.end(function (err) {160 assert.ok(err)161 server.close(cb)162 })Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 163why, one step back163 })Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 164–175why, one step back164 })165166 describe('.sendFile(path, fn)', function () {167 it('should invoke the callback when complete', function (done) {168 var cb = after(2, done);169 var app = createApp(path.resolve(fixtures, 'name.txt'), cb);170171 request(app)172 .get('/')173 .expect(200, cb);174 })175Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 176why, one step back176 it('should invoke the callback when client aborts', function (done) {Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 177why, one step back177 var cb = after(2, done)Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 178–182why, one step back178 var app = express();179180 app.use(function (req, res) {181 setImmediate(function () {182 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 183–185why, one step back183 assert.ok(err)184 assert.strictEqual(err.code, 'ECONNABORTED')185 cb()Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 186–187why, one step back186 });187 });Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 188why, one step back188 test.req.abort()Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 189–190why, one step back189 });190Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 191–192why, one step back191 var server = app.listen()192 var test = request(server).get('/')Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 193–196why, one step back193 test.end(function (err) {194 assert.ok(err)195 server.close(cb)196 })Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 197–199why, one step back197 })198199 it('should invoke the callback when client already aborted', function (done) {Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 200why, one step back200 var cb = after(2, done)Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 201–205why, one step back201 var app = express();202203 app.use(function (req, res) {204 onFinished(res, function () {205 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 206–208why, one step back206 assert.ok(err)207 assert.strictEqual(err.code, 'ECONNABORTED')208 cb()Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 209–210why, one step back209 });210 });Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 211why, one step back211 test.req.abort()Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 212–213why, one step back212 });213Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 214–215why, one step back214 var server = app.listen()215 var test = request(server).get('/')Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 216–219why, one step back216 test.end(function (err) {217 assert.ok(err)218 server.close(cb)219 })Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 220why, one step back220 })Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 221–254why, one step back221222 it('should invoke the callback without error when HEAD', function (done) {223 var app = express();224 var cb = after(2, done);225226 app.use(function (req, res) {227 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);228 });229230 request(app)231 .head('/')232 .expect(200, cb);233 });234235 it('should invoke the callback without error when 304', function (done) {236 var app = express();237 var cb = after(3, done);238239 app.use(function (req, res) {240 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);241 });242243 request(app)244 .get('/')245 .expect('ETag', /^(?:W\/)?"[^"]+"$/)246 .expect(200, 'tobi', function (err, res) {247 if (err) return cb(err);248 var etag = res.headers.etag;249 request(app)250 .get('/')251 .set('If-None-Match', etag)252 .expect(304, cb);253 });254 });Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 255why, one step back255Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 256–260why, one step back256 it('should invoke the callback on 404', function(done){257 var app = express();258259 app.use(function (req, res) {260 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 261why, one step back261 res.send(err ? 'got ' + err.status + ' error' : 'no error')Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 262–265why, one step back262 });263 });264265 request(app)Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 266–267why, one step back266 .get('/')267 .expect(200, 'got 404 error', done)Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 268why, one step back268 })Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 269–326why, one step back269270 describeAsyncHooks('async local storage', function () {271 it('should presist store', function (done) {272 var app = express()273 var cb = after(2, done)274 var store = { foo: 'bar' }275276 app.use(function (req, res, next) {277 req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()278 req.asyncLocalStorage.run(store, next)279 })280281 app.use(function (req, res) {282 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {283 if (err) return cb(err)284285 var local = req.asyncLocalStorage.getStore()286287 assert.strictEqual(local.foo, 'bar')288 cb()289 })290 })291292 request(app)293 .get('/')294 .expect('Content-Type', 'text/plain; charset=utf-8')295 .expect(200, 'tobi', cb)296 })297298 it('should presist store on error', function (done) {299 var app = express()300 var store = { foo: 'bar' }301302 app.use(function (req, res, next) {303 req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()304 req.asyncLocalStorage.run(store, next)305 })306307 app.use(function (req, res) {308 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {309 var local = req.asyncLocalStorage.getStore()310311 if (local) {312 res.setHeader('x-store-foo', String(local.foo))313 }314315 res.send(err ? 'got ' + err.status + ' error' : 'no error')316 })317 })318319 request(app)320 .get('/')321 .expect(200)322 .expect('x-store-foo', 'bar')323 .expect('got 404 error')324 .end(done)325 })326 })Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 327–333why, one step back327 })328329 describe('.sendFile(path, options)', function () {330 it('should pass options to send module', function (done) {331 request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))332 .get('/')333 .expect(200, 'to', done)Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 334why, one step back334 })Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 335–891why, one step back335336 describe('with "acceptRanges" option', function () {337 describe('when true', function () {338 it('should advertise byte range accepted', function (done) {339 var app = express()340341 app.use(function (req, res) {342 res.sendFile(path.resolve(fixtures, 'nums.txt'), {343 acceptRanges: true344 })345 })346347 request(app)348 .get('/')349 .expect(200)350 .expect('Accept-Ranges', 'bytes')351 .expect('123456789')352 .end(done)353 })354355 it('should respond to range request', function (done) {356 var app = express()357358 app.use(function (req, res) {359 res.sendFile(path.resolve(fixtures, 'nums.txt'), {360 acceptRanges: true361 })362 })363364 request(app)365 .get('/')366 .set('Range', 'bytes=0-4')367 .expect(206, '12345', done)368 })369 })370371 describe('when false', function () {372 it('should not advertise accept-ranges', function (done) {373 var app = express()374375 app.use(function (req, res) {376 res.sendFile(path.resolve(fixtures, 'nums.txt'), {377 acceptRanges: false378 })379 })380381 request(app)382 .get('/')383 .expect(200)384 .expect(utils.shouldNotHaveHeader('Accept-Ranges'))385 .end(done)386 })387388 it('should not honor range requests', function (done) {389 var app = express()390391 app.use(function (req, res) {392 res.sendFile(path.resolve(fixtures, 'nums.txt'), {393 acceptRanges: false394 })395 })396397 request(app)398 .get('/')399 .set('Range', 'bytes=0-4')400 .expect(200, '123456789', done)401 })402 })403 })404405 describe('with "cacheControl" option', function () {406 describe('when true', function () {407 it('should send cache-control header', function (done) {408 var app = express()409410 app.use(function (req, res) {411 res.sendFile(path.resolve(fixtures, 'user.html'), {412 cacheControl: true413 })414 })415416 request(app)417 .get('/')418 .expect(200)419 .expect('Cache-Control', 'public, max-age=0')420 .end(done)421 })422 })423424 describe('when false', function () {425 it('should not send cache-control header', function (done) {426 var app = express()427428 app.use(function (req, res) {429 res.sendFile(path.resolve(fixtures, 'user.html'), {430 cacheControl: false431 })432 })433434 request(app)435 .get('/')436 .expect(200)437 .expect(utils.shouldNotHaveHeader('Cache-Control'))438 .end(done)439 })440 })441 })442443 describe('with "dotfiles" option', function () {444 describe('when "allow"', function () {445 it('should allow dotfiles', function (done) {446 var app = express()447448 app.use(function (req, res) {449 res.sendFile(path.resolve(fixtures, '.name'), {450 dotfiles: 'allow'451 })452 })453454 request(app)455 .get('/')456 .expect(200)457 .expect(utils.shouldHaveBody(Buffer.from('tobi')))458 .end(done)459 })460 })461462 describe('when "deny"', function () {463 it('should deny dotfiles', function (done) {464 var app = express()465466 app.use(function (req, res) {467 res.sendFile(path.resolve(fixtures, '.name'), {468 dotfiles: 'deny'469 })470 })471472 request(app)473 .get('/')474 .expect(403)475 .expect(/Forbidden/)476 .end(done)477 })478 })479480 describe('when "ignore"', function () {481 it('should ignore dotfiles', function (done) {482 var app = express()483484 app.use(function (req, res) {485 res.sendFile(path.resolve(fixtures, '.name'), {486 dotfiles: 'ignore'487 })488 })489490 request(app)491 .get('/')492 .expect(404)493 .expect(/Not Found/)494 .end(done)495 })496 })497 })498499 describe('with "headers" option', function () {500 it('should set headers on response', function (done) {501 var app = express()502503 app.use(function (req, res) {504 res.sendFile(path.resolve(fixtures, 'user.html'), {505 headers: {506 'X-Foo': 'Bar',507 'X-Bar': 'Foo'508 }509 })510 })511512 request(app)513 .get('/')514 .expect(200)515 .expect('X-Foo', 'Bar')516 .expect('X-Bar', 'Foo')517 .end(done)518 })519520 it('should use last header when duplicated', function (done) {521 var app = express()522523 app.use(function (req, res) {524 res.sendFile(path.resolve(fixtures, 'user.html'), {525 headers: {526 'X-Foo': 'Bar',527 'x-foo': 'bar'528 }529 })530 })531532 request(app)533 .get('/')534 .expect(200)535 .expect('X-Foo', 'bar')536 .end(done)537 })538539 it('should override Content-Type', function (done) {540 var app = express()541542 app.use(function (req, res) {543 res.sendFile(path.resolve(fixtures, 'user.html'), {544 headers: {545 'Content-Type': 'text/x-custom'546 }547 })548 })549550 request(app)551 .get('/')552 .expect(200)553 .expect('Content-Type', 'text/x-custom')554 .end(done)555 })556557 it('should not set headers on 404', function (done) {558 var app = express()559560 app.use(function (req, res) {561 res.sendFile(path.resolve(fixtures, 'does-not-exist'), {562 headers: {563 'X-Foo': 'Bar'564 }565 })566 })567568 request(app)569 .get('/')570 .expect(404)571 .expect(utils.shouldNotHaveHeader('X-Foo'))572 .end(done)573 })574 })575576 describe('with "immutable" option', function () {577 describe('when true', function () {578 it('should send cache-control header with immutable', function (done) {579 var app = express()580581 app.use(function (req, res) {582 res.sendFile(path.resolve(fixtures, 'user.html'), {583 immutable: true584 })585 })586587 request(app)588 .get('/')589 .expect(200)590 .expect('Cache-Control', 'public, max-age=0, immutable')591 .end(done)592 })593 })594595 describe('when false', function () {596 it('should not send cache-control header with immutable', function (done) {597 var app = express()598599 app.use(function (req, res) {600 res.sendFile(path.resolve(fixtures, 'user.html'), {601 immutable: false602 })603 })604605 request(app)606 .get('/')607 .expect(200)608 .expect('Cache-Control', 'public, max-age=0')609 .end(done)610 })611 })612 })613614 describe('with "lastModified" option', function () {615 describe('when true', function () {616 it('should send last-modified header', function (done) {617 var app = express()618619 app.use(function (req, res) {620 res.sendFile(path.resolve(fixtures, 'user.html'), {621 lastModified: true622 })623 })624625 request(app)626 .get('/')627 .expect(200)628 .expect(utils.shouldHaveHeader('Last-Modified'))629 .end(done)630 })631632 it('should conditionally respond with if-modified-since', function (done) {633 var app = express()634635 app.use(function (req, res) {636 res.sendFile(path.resolve(fixtures, 'user.html'), {637 lastModified: true638 })639 })640641 request(app)642 .get('/')643 .set('If-Modified-Since', (new Date(Date.now() + 99999).toUTCString()))644 .expect(304, done)645 })646 })647648 describe('when false', function () {649 it('should not have last-modified header', function (done) {650 var app = express()651652 app.use(function (req, res) {653 res.sendFile(path.resolve(fixtures, 'user.html'), {654 lastModified: false655 })656 })657658 request(app)659 .get('/')660 .expect(200)661 .expect(utils.shouldNotHaveHeader('Last-Modified'))662 .end(done)663 })664665 it('should not honor if-modified-since', function (done) {666 var app = express()667668 app.use(function (req, res) {669 res.sendFile(path.resolve(fixtures, 'user.html'), {670 lastModified: false671 })672 })673674 request(app)675 .get('/')676 .set('If-Modified-Since', (new Date(Date.now() + 99999).toUTCString()))677 .expect(200)678 .expect(utils.shouldNotHaveHeader('Last-Modified'))679 .end(done)680 })681 })682 })683684 describe('with "maxAge" option', function () {685 it('should set cache-control max-age to milliseconds', function (done) {686 var app = express()687688 app.use(function (req, res) {689 res.sendFile(path.resolve(fixtures, 'user.html'), {690 maxAge: 20000691 })692 })693694 request(app)695 .get('/')696 .expect(200)697 .expect('Cache-Control', 'public, max-age=20')698 .end(done)699 })700701 it('should cap cache-control max-age to 1 year', function (done) {702 var app = express()703704 app.use(function (req, res) {705 res.sendFile(path.resolve(fixtures, 'user.html'), {706 maxAge: 99999999999707 })708 })709710 request(app)711 .get('/')712 .expect(200)713 .expect('Cache-Control', 'public, max-age=31536000')714 .end(done)715 })716717 it('should min cache-control max-age to 0', function (done) {718 var app = express()719720 app.use(function (req, res) {721 res.sendFile(path.resolve(fixtures, 'user.html'), {722 maxAge: -20000723 })724 })725726 request(app)727 .get('/')728 .expect(200)729 .expect('Cache-Control', 'public, max-age=0')730 .end(done)731 })732733 it('should floor cache-control max-age', function (done) {734 var app = express()735736 app.use(function (req, res) {737 res.sendFile(path.resolve(fixtures, 'user.html'), {738 maxAge: 21911.23739 })740 })741742 request(app)743 .get('/')744 .expect(200)745 .expect('Cache-Control', 'public, max-age=21')746 .end(done)747 })748749 describe('when cacheControl: false', function () {750 it('should not send cache-control', function (done) {751 var app = express()752753 app.use(function (req, res) {754 res.sendFile(path.resolve(fixtures, 'user.html'), {755 cacheControl: false,756 maxAge: 20000757 })758 })759760 request(app)761 .get('/')762 .expect(200)763 .expect(utils.shouldNotHaveHeader('Cache-Control'))764 .end(done)765 })766 })767768 describe('when string', function () {769 it('should accept plain number as milliseconds', function (done) {770 var app = express()771772 app.use(function (req, res) {773 res.sendFile(path.resolve(fixtures, 'user.html'), {774 maxAge: '20000'775 })776 })777778 request(app)779 .get('/')780 .expect(200)781 .expect('Cache-Control', 'public, max-age=20')782 .end(done)783 })784785 it('should accept suffix "s" for seconds', function (done) {786 var app = express()787788 app.use(function (req, res) {789 res.sendFile(path.resolve(fixtures, 'user.html'), {790 maxAge: '20s'791 })792 })793794 request(app)795 .get('/')796 .expect(200)797 .expect('Cache-Control', 'public, max-age=20')798 .end(done)799 })800801 it('should accept suffix "m" for minutes', function (done) {802 var app = express()803804 app.use(function (req, res) {805 res.sendFile(path.resolve(fixtures, 'user.html'), {806 maxAge: '20m'807 })808 })809810 request(app)811 .get('/')812 .expect(200)813 .expect('Cache-Control', 'public, max-age=1200')814 .end(done)815 })816817 it('should accept suffix "d" for days', function (done) {818 var app = express()819820 app.use(function (req, res) {821 res.sendFile(path.resolve(fixtures, 'user.html'), {822 maxAge: '20d'823 })824 })825826 request(app)827 .get('/')828 .expect(200)829 .expect('Cache-Control', 'public, max-age=1728000')830 .end(done)831 })832 })833 })834835 describe('with "root" option', function () {836 it('should allow relative path', function (done) {837 var app = express()838839 app.use(function (req, res) {840 res.sendFile('name.txt', {841 root: fixtures842 })843 })844845 request(app)846 .get('/')847 .expect(200, 'tobi', done)848 })849850 it('should allow up within root', function (done) {851 var app = express()852853 app.use(function (req, res) {854 res.sendFile('fake/../name.txt', {855 root: fixtures856 })857 })858859 request(app)860 .get('/')861 .expect(200, 'tobi', done)862 })863864 it('should reject up outside root', function (done) {865 var app = express()866867 app.use(function (req, res) {868 res.sendFile('..' + path.sep + path.relative(path.dirname(fixtures), path.join(fixtures, 'name.txt')), {869 root: fixtures870 })871 })872873 request(app)874 .get('/')875 .expect(403, done)876 })877878 it('should reject reading outside root', function (done) {879 var app = express()880881 app.use(function (req, res) {882 res.sendFile('../name.txt', {883 root: fixtures884 })885 })886887 request(app)888 .get('/')889 .expect(403, done)890 })891 })Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 892–902why, one step back892 })893})894895function createApp(path, options, fn) {896 var app = express();897898 app.use(function (req, res) {899 res.sendFile(path, options, fn);900 });901902 return app;Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 903–904why, one step back903}904Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 905–909why, one step back905function tryRequire (name) {906 try {907 return require(name)908 } catch (e) {909 return {}Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
line 910why, one step back910 }Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 911why, one step back911}