Skip to content
Myr

res.sendFile.js

This file

Path

test/res.sendFile.js

912 lines · 23.3 KiB · JavaScript

Last changed by

feat: add support for ETag option in res.sendFile (#6073)Juan Josébefore adoption327af12

History of this file

Take it

The walk stopped after 200 commits: lines older than 6aa4a45 point at it.

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 1why, one step back
1'use strict'

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 2–3why, one step back
2
3var after = require('after');

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

lines 4–5why, one step back
4var assert = require('node:assert')
5var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage

cleanup: remove AsyncLocalStorage check from tests (#6147)Phillip Bartabefore adoption4e92ac9

line 6why, one step back
6

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 7–9why, one step back
7var express = require('../')
8 , request = require('supertest')9var onFinished = require('on-finished');

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

line 10why, one step back
10var path = require('node:path');

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 11why, one step back
11var fixtures = path.join(__dirname, 'fixtures');

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 12why, one step back
12var utils = require('./support/utils');

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 13–23why, one step back
13
14describe('res', function(){15 describe('.sendFile(path)', function () {16 it('should error missing path', function (done) {17 var app = createApp();1819 request(app)20 .get('/')21 .expect(500, /path.*required/, done);22 });23

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 24–31why, one step back
24 it('should error for non-string path', function (done) {
25 var app = createApp(42)2627 request(app)28 .get('/')29 .expect(500, /TypeError: path must be a string to res.sendFile/, done)30 })31

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 32–39why, one step back
32 it('should error for non-absolute path', function (done) {
33 var app = createApp('name.txt')3435 request(app)36 .get('/')37 .expect(500, /TypeError: path must be absolute/, done)38 })39

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 40–55why, one step back
40 it('should transfer a file', function (done) {
41 var app = createApp(path.resolve(fixtures, 'name.txt'));4243 request(app)44 .get('/')45 .expect(200, 'tobi', done);46 });4748 it('should transfer a file with special characters in string', function (done) {49 var app = createApp(path.resolve(fixtures, '% of dogs.txt'));5051 request(app)52 .get('/')53 .expect(200, '20%', done);54 });55

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 56–80why, one step back
56 it('should include ETag', function (done) {
57 var app = createApp(path.resolve(fixtures, 'name.txt'));5859 request(app)60 .get('/')61 .expect('ETag', /^(?:W\/)?"[^"]+"$/)62 .expect(200, 'tobi', done);63 });6465 it('should 304 when ETag matches', function (done) {66 var app = createApp(path.resolve(fixtures, 'name.txt'));6768 request(app)69 .get('/')70 .expect('ETag', /^(?:W\/)?"[^"]+"$/)71 .expect(200, 'tobi', function (err, res) {72 if (err) return done(err);73 var etag = res.headers.etag;74 request(app)75 .get('/')76 .set('If-None-Match', etag)77 .expect(304, done);78 });79 });80

feat: add support for ETag option in res.sendFile (#6073)Juan Josébefore adoption327af12

lines 81–93why, one step back
81 it('should disable the ETag function if requested', function (done) {
82 var app = createApp(path.resolve(fixtures, 'name.txt')).disable('etag');8384 request(app)85 .get('/')86 .expect(handleHeaders)87 .expect(200, done);8889 function handleHeaders (res) {90 assert(res.headers.etag === undefined);91 }92 });93

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 94–114why, one step back
94 it('should 404 for directory', function (done) {
95 var app = createApp(path.resolve(fixtures, 'blog'));9697 request(app)98 .get('/')99 .expect(404, done);100 });101102 it('should 404 when not found', function (done) {103 var app = createApp(path.resolve(fixtures, 'does-no-exist'));104105 app.use(function (req, res) {106 res.statusCode = 200;107 res.send('no!');108 });109110 request(app)111 .get('/')112 .expect(404, done);113 });114

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 115–131why, one step back
115 it('should send cache-control by default', function (done) {
116 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))117118 request(app)119 .get('/')120 .expect('Cache-Control', 'public, max-age=0')121 .expect(200, done)122 })123124 it('should not serve dotfiles by default', function (done) {125 var app = createApp(path.resolve(__dirname, 'fixtures/.name'))126127 request(app)128 .get('/')129 .expect(404, done)130 })131

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 132–145why, one step back
132 it('should not override manual content-types', function (done) {
133 var app = express();134135 app.use(function (req, res) {136 res.contentType('application/x-bogus');137 res.sendFile(path.resolve(fixtures, 'name.txt'));138 });139140 request(app)141 .get('/')142 .expect('Content-Type', 'application/x-bogus')143 .end(done);144 })145

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 146–147why, one step back
146 it('should not error if the client aborts', function (done) {
147 var app = express();

Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615

lines 148–149why, one step back
148 var cb = after(2, done)
149 var error = null

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 150–153why, one step back
150
151 app.use(function (req, res) {152 setImmediate(function () {153 res.sendFile(path.resolve(fixtures, 'name.txt'));

Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615

lines 154–157why, one step back
154 setTimeout(function () {
155 cb(error)156 }, 10)157 })

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 158why, one step back
158 test.req.abort()

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 159–161why, one step back
159 });
160161 app.use(function (err, req, res, next) {

Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615

lines 162–163why, one step back
162 error = err
163 next(err)

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 164–165why, one step back
164 });
165

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 166–167why, one step back
166 var server = app.listen()
167 var test = request(server).get('/')

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 168–171why, one step back
168 test.end(function (err) {
169 assert.ok(err)170 server.close(cb)171 })

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 172why, one step back
172 })

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 173–185why, one step back
173 })
174175 describe('.sendFile(path, fn)', function () {176 it('should invoke the callback when complete', function (done) {177 var cb = after(2, done);178 var app = createApp(path.resolve(fixtures, 'name.txt'), cb);179180 request(app)181 .get('/')182 .expect(200, cb);183 })184185 it('should invoke the callback when client aborts', function (done) {

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 186why, one step back
186 var cb = after(2, done)

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 187–191why, one step back
187 var app = express();
188189 app.use(function (req, res) {190 setImmediate(function () {191 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 192–194why, one step back
192 assert.ok(err)
193 assert.strictEqual(err.code, 'ECONNABORTED')194 cb()

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 195–196why, one step back
195 });
196 });

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 197why, one step back
197 test.req.abort()

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 198–199why, one step back
198 });
199

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 200–201why, one step back
200 var server = app.listen()
201 var test = request(server).get('/')

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 202–205why, one step back
202 test.end(function (err) {
203 assert.ok(err)204 server.close(cb)205 })

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 206–208why, one step back
206 })
207208 it('should invoke the callback when client already aborted', function (done) {

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 209why, one step back
209 var cb = after(2, done)

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 210–214why, one step back
210 var app = express();
211212 app.use(function (req, res) {213 onFinished(res, function () {214 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 215–217why, one step back
215 assert.ok(err)
216 assert.strictEqual(err.code, 'ECONNABORTED')217 cb()

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 218–219why, one step back
218 });
219 });

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 220why, one step back
220 test.req.abort()

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 221–222why, one step back
221 });
222

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 223–224why, one step back
223 var server = app.listen()
224 var test = request(server).get('/')

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 225–228why, one step back
225 test.end(function (err) {
226 assert.ok(err)227 server.close(cb)228 })

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 229why, one step back
229 })

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 230–263why, one step back
230
231 it('should invoke the callback without error when HEAD', function (done) {232 var app = express();233 var cb = after(2, done);234235 app.use(function (req, res) {236 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);237 });238239 request(app)240 .head('/')241 .expect(200, cb);242 });243244 it('should invoke the callback without error when 304', function (done) {245 var app = express();246 var cb = after(3, done);247248 app.use(function (req, res) {249 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);250 });251252 request(app)253 .get('/')254 .expect('ETag', /^(?:W\/)?"[^"]+"$/)255 .expect(200, 'tobi', function (err, res) {256 if (err) return cb(err);257 var etag = res.headers.etag;258 request(app)259 .get('/')260 .set('If-None-Match', etag)261 .expect(304, cb);262 });263 });

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 264–269why, one step back
264
265 it('should invoke the callback on 404', function(done){266 var app = express();267268 app.use(function (req, res) {269 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 270why, one step back
270 res.send(err ? 'got ' + err.status + ' error' : 'no error')

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 271–274why, one step back
271 });
272 });273274 request(app)

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 275–276why, one step back
275 .get('/')
276 .expect(200, 'got 404 error', done)

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 277why, one step back
277 })

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 278why, one step back
278

cleanup: remove AsyncLocalStorage check from tests (#6147)Phillip Bartabefore adoption4e92ac9

line 279why, one step back
279 describe('async local storage', function () {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 280–285why, one step back
280 it('should presist store', function (done) {
281 var app = express()282 var cb = after(2, done)283 var store = { foo: 'bar' }284285 app.use(function (req, res, next) {

cleanup: remove AsyncLocalStorage check from tests (#6147)Phillip Bartabefore adoption4e92ac9

line 286why, one step back
286 req.asyncLocalStorage = new AsyncLocalStorage()

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 287–311why, one step back
287 req.asyncLocalStorage.run(store, next)
288 })289290 app.use(function (req, res) {291 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {292 if (err) return cb(err)293294 var local = req.asyncLocalStorage.getStore()295296 assert.strictEqual(local.foo, 'bar')297 cb()298 })299 })300301 request(app)302 .get('/')303 .expect('Content-Type', 'text/plain; charset=utf-8')304 .expect(200, 'tobi', cb)305 })306307 it('should presist store on error', function (done) {308 var app = express()309 var store = { foo: 'bar' }310311 app.use(function (req, res, next) {

cleanup: remove AsyncLocalStorage check from tests (#6147)Phillip Bartabefore adoption4e92ac9

line 312why, one step back
312 req.asyncLocalStorage = new AsyncLocalStorage()

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 313–335why, one step back
313 req.asyncLocalStorage.run(store, next)
314 })315316 app.use(function (req, res) {317 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {318 var local = req.asyncLocalStorage.getStore()319320 if (local) {321 res.setHeader('x-store-foo', String(local.foo))322 }323324 res.send(err ? 'got ' + err.status + ' error' : 'no error')325 })326 })327328 request(app)329 .get('/')330 .expect(200)331 .expect('x-store-foo', 'bar')332 .expect('got 404 error')333 .end(done)334 })335 })

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 336–342why, one step back
336 })
337338 describe('.sendFile(path, options)', function () {339 it('should pass options to send module', function (done) {340 request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))341 .get('/')342 .expect(200, 'to', done)

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

line 343why, one step back
343 })

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 344–900why, one step back
344
345 describe('with "acceptRanges" option', function () {346 describe('when true', function () {347 it('should advertise byte range accepted', function (done) {348 var app = express()349350 app.use(function (req, res) {351 res.sendFile(path.resolve(fixtures, 'nums.txt'), {352 acceptRanges: true353 })354 })355356 request(app)357 .get('/')358 .expect(200)359 .expect('Accept-Ranges', 'bytes')360 .expect('123456789')361 .end(done)362 })363364 it('should respond to range request', function (done) {365 var app = express()366367 app.use(function (req, res) {368 res.sendFile(path.resolve(fixtures, 'nums.txt'), {369 acceptRanges: true370 })371 })372373 request(app)374 .get('/')375 .set('Range', 'bytes=0-4')376 .expect(206, '12345', done)377 })378 })379380 describe('when false', function () {381 it('should not advertise accept-ranges', function (done) {382 var app = express()383384 app.use(function (req, res) {385 res.sendFile(path.resolve(fixtures, 'nums.txt'), {386 acceptRanges: false387 })388 })389390 request(app)391 .get('/')392 .expect(200)393 .expect(utils.shouldNotHaveHeader('Accept-Ranges'))394 .end(done)395 })396397 it('should not honor range requests', function (done) {398 var app = express()399400 app.use(function (req, res) {401 res.sendFile(path.resolve(fixtures, 'nums.txt'), {402 acceptRanges: false403 })404 })405406 request(app)407 .get('/')408 .set('Range', 'bytes=0-4')409 .expect(200, '123456789', done)410 })411 })412 })413414 describe('with "cacheControl" option', function () {415 describe('when true', function () {416 it('should send cache-control header', function (done) {417 var app = express()418419 app.use(function (req, res) {420 res.sendFile(path.resolve(fixtures, 'user.html'), {421 cacheControl: true422 })423 })424425 request(app)426 .get('/')427 .expect(200)428 .expect('Cache-Control', 'public, max-age=0')429 .end(done)430 })431 })432433 describe('when false', function () {434 it('should not send cache-control header', function (done) {435 var app = express()436437 app.use(function (req, res) {438 res.sendFile(path.resolve(fixtures, 'user.html'), {439 cacheControl: false440 })441 })442443 request(app)444 .get('/')445 .expect(200)446 .expect(utils.shouldNotHaveHeader('Cache-Control'))447 .end(done)448 })449 })450 })451452 describe('with "dotfiles" option', function () {453 describe('when "allow"', function () {454 it('should allow dotfiles', function (done) {455 var app = express()456457 app.use(function (req, res) {458 res.sendFile(path.resolve(fixtures, '.name'), {459 dotfiles: 'allow'460 })461 })462463 request(app)464 .get('/')465 .expect(200)466 .expect(utils.shouldHaveBody(Buffer.from('tobi')))467 .end(done)468 })469 })470471 describe('when "deny"', function () {472 it('should deny dotfiles', function (done) {473 var app = express()474475 app.use(function (req, res) {476 res.sendFile(path.resolve(fixtures, '.name'), {477 dotfiles: 'deny'478 })479 })480481 request(app)482 .get('/')483 .expect(403)484 .expect(/Forbidden/)485 .end(done)486 })487 })488489 describe('when "ignore"', function () {490 it('should ignore dotfiles', function (done) {491 var app = express()492493 app.use(function (req, res) {494 res.sendFile(path.resolve(fixtures, '.name'), {495 dotfiles: 'ignore'496 })497 })498499 request(app)500 .get('/')501 .expect(404)502 .expect(/Not Found/)503 .end(done)504 })505 })506 })507508 describe('with "headers" option', function () {509 it('should set headers on response', function (done) {510 var app = express()511512 app.use(function (req, res) {513 res.sendFile(path.resolve(fixtures, 'user.html'), {514 headers: {515 'X-Foo': 'Bar',516 'X-Bar': 'Foo'517 }518 })519 })520521 request(app)522 .get('/')523 .expect(200)524 .expect('X-Foo', 'Bar')525 .expect('X-Bar', 'Foo')526 .end(done)527 })528529 it('should use last header when duplicated', function (done) {530 var app = express()531532 app.use(function (req, res) {533 res.sendFile(path.resolve(fixtures, 'user.html'), {534 headers: {535 'X-Foo': 'Bar',536 'x-foo': 'bar'537 }538 })539 })540541 request(app)542 .get('/')543 .expect(200)544 .expect('X-Foo', 'bar')545 .end(done)546 })547548 it('should override Content-Type', function (done) {549 var app = express()550551 app.use(function (req, res) {552 res.sendFile(path.resolve(fixtures, 'user.html'), {553 headers: {554 'Content-Type': 'text/x-custom'555 }556 })557 })558559 request(app)560 .get('/')561 .expect(200)562 .expect('Content-Type', 'text/x-custom')563 .end(done)564 })565566 it('should not set headers on 404', function (done) {567 var app = express()568569 app.use(function (req, res) {570 res.sendFile(path.resolve(fixtures, 'does-not-exist'), {571 headers: {572 'X-Foo': 'Bar'573 }574 })575 })576577 request(app)578 .get('/')579 .expect(404)580 .expect(utils.shouldNotHaveHeader('X-Foo'))581 .end(done)582 })583 })584585 describe('with "immutable" option', function () {586 describe('when true', function () {587 it('should send cache-control header with immutable', function (done) {588 var app = express()589590 app.use(function (req, res) {591 res.sendFile(path.resolve(fixtures, 'user.html'), {592 immutable: true593 })594 })595596 request(app)597 .get('/')598 .expect(200)599 .expect('Cache-Control', 'public, max-age=0, immutable')600 .end(done)601 })602 })603604 describe('when false', function () {605 it('should not send cache-control header with immutable', function (done) {606 var app = express()607608 app.use(function (req, res) {609 res.sendFile(path.resolve(fixtures, 'user.html'), {610 immutable: false611 })612 })613614 request(app)615 .get('/')616 .expect(200)617 .expect('Cache-Control', 'public, max-age=0')618 .end(done)619 })620 })621 })622623 describe('with "lastModified" option', function () {624 describe('when true', function () {625 it('should send last-modified header', function (done) {626 var app = express()627628 app.use(function (req, res) {629 res.sendFile(path.resolve(fixtures, 'user.html'), {630 lastModified: true631 })632 })633634 request(app)635 .get('/')636 .expect(200)637 .expect(utils.shouldHaveHeader('Last-Modified'))638 .end(done)639 })640641 it('should conditionally respond with if-modified-since', function (done) {642 var app = express()643644 app.use(function (req, res) {645 res.sendFile(path.resolve(fixtures, 'user.html'), {646 lastModified: true647 })648 })649650 request(app)651 .get('/')652 .set('If-Modified-Since', (new Date(Date.now() + 99999).toUTCString()))653 .expect(304, done)654 })655 })656657 describe('when false', function () {658 it('should not have last-modified header', function (done) {659 var app = express()660661 app.use(function (req, res) {662 res.sendFile(path.resolve(fixtures, 'user.html'), {663 lastModified: false664 })665 })666667 request(app)668 .get('/')669 .expect(200)670 .expect(utils.shouldNotHaveHeader('Last-Modified'))671 .end(done)672 })673674 it('should not honor if-modified-since', function (done) {675 var app = express()676677 app.use(function (req, res) {678 res.sendFile(path.resolve(fixtures, 'user.html'), {679 lastModified: false680 })681 })682683 request(app)684 .get('/')685 .set('If-Modified-Since', (new Date(Date.now() + 99999).toUTCString()))686 .expect(200)687 .expect(utils.shouldNotHaveHeader('Last-Modified'))688 .end(done)689 })690 })691 })692693 describe('with "maxAge" option', function () {694 it('should set cache-control max-age to milliseconds', function (done) {695 var app = express()696697 app.use(function (req, res) {698 res.sendFile(path.resolve(fixtures, 'user.html'), {699 maxAge: 20000700 })701 })702703 request(app)704 .get('/')705 .expect(200)706 .expect('Cache-Control', 'public, max-age=20')707 .end(done)708 })709710 it('should cap cache-control max-age to 1 year', function (done) {711 var app = express()712713 app.use(function (req, res) {714 res.sendFile(path.resolve(fixtures, 'user.html'), {715 maxAge: 99999999999716 })717 })718719 request(app)720 .get('/')721 .expect(200)722 .expect('Cache-Control', 'public, max-age=31536000')723 .end(done)724 })725726 it('should min cache-control max-age to 0', function (done) {727 var app = express()728729 app.use(function (req, res) {730 res.sendFile(path.resolve(fixtures, 'user.html'), {731 maxAge: -20000732 })733 })734735 request(app)736 .get('/')737 .expect(200)738 .expect('Cache-Control', 'public, max-age=0')739 .end(done)740 })741742 it('should floor cache-control max-age', function (done) {743 var app = express()744745 app.use(function (req, res) {746 res.sendFile(path.resolve(fixtures, 'user.html'), {747 maxAge: 21911.23748 })749 })750751 request(app)752 .get('/')753 .expect(200)754 .expect('Cache-Control', 'public, max-age=21')755 .end(done)756 })757758 describe('when cacheControl: false', function () {759 it('should not send cache-control', function (done) {760 var app = express()761762 app.use(function (req, res) {763 res.sendFile(path.resolve(fixtures, 'user.html'), {764 cacheControl: false,765 maxAge: 20000766 })767 })768769 request(app)770 .get('/')771 .expect(200)772 .expect(utils.shouldNotHaveHeader('Cache-Control'))773 .end(done)774 })775 })776777 describe('when string', function () {778 it('should accept plain number as milliseconds', function (done) {779 var app = express()780781 app.use(function (req, res) {782 res.sendFile(path.resolve(fixtures, 'user.html'), {783 maxAge: '20000'784 })785 })786787 request(app)788 .get('/')789 .expect(200)790 .expect('Cache-Control', 'public, max-age=20')791 .end(done)792 })793794 it('should accept suffix "s" for seconds', function (done) {795 var app = express()796797 app.use(function (req, res) {798 res.sendFile(path.resolve(fixtures, 'user.html'), {799 maxAge: '20s'800 })801 })802803 request(app)804 .get('/')805 .expect(200)806 .expect('Cache-Control', 'public, max-age=20')807 .end(done)808 })809810 it('should accept suffix "m" for minutes', function (done) {811 var app = express()812813 app.use(function (req, res) {814 res.sendFile(path.resolve(fixtures, 'user.html'), {815 maxAge: '20m'816 })817 })818819 request(app)820 .get('/')821 .expect(200)822 .expect('Cache-Control', 'public, max-age=1200')823 .end(done)824 })825826 it('should accept suffix "d" for days', function (done) {827 var app = express()828829 app.use(function (req, res) {830 res.sendFile(path.resolve(fixtures, 'user.html'), {831 maxAge: '20d'832 })833 })834835 request(app)836 .get('/')837 .expect(200)838 .expect('Cache-Control', 'public, max-age=1728000')839 .end(done)840 })841 })842 })843844 describe('with "root" option', function () {845 it('should allow relative path', function (done) {846 var app = express()847848 app.use(function (req, res) {849 res.sendFile('name.txt', {850 root: fixtures851 })852 })853854 request(app)855 .get('/')856 .expect(200, 'tobi', done)857 })858859 it('should allow up within root', function (done) {860 var app = express()861862 app.use(function (req, res) {863 res.sendFile('fake/../name.txt', {864 root: fixtures865 })866 })867868 request(app)869 .get('/')870 .expect(200, 'tobi', done)871 })872873 it('should reject up outside root', function (done) {874 var app = express()875876 app.use(function (req, res) {877 res.sendFile('..' + path.sep + path.relative(path.dirname(fixtures), path.join(fixtures, 'name.txt')), {878 root: fixtures879 })880 })881882 request(app)883 .get('/')884 .expect(403, done)885 })886887 it('should reject reading outside root', function (done) {888 var app = express()889890 app.use(function (req, res) {891 res.sendFile('../name.txt', {892 root: fixtures893 })894 })895896 request(app)897 .get('/')898 .expect(403, done)899 })900 })

Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45

lines 901–912why, one step back
901 })
902})903904function createApp(path, options, fn) {905 var app = express();906907 app.use(function (req, res) {908 res.sendFile(path, options, fn);909 });910911 return app;912}