Skip to content
Myr

res.sendFile.js

This file

Path

test/res.sendFile.js

899 lines · 22.9 KiB · JavaScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 2a0c35a point at it.

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 81–101why, one step back
81 it('should 404 for directory', function (done) {
82 var app = createApp(path.resolve(fixtures, 'blog'));8384 request(app)85 .get('/')86 .expect(404, done);87 });8889 it('should 404 when not found', function (done) {90 var app = createApp(path.resolve(fixtures, 'does-no-exist'));9192 app.use(function (req, res) {93 res.statusCode = 200;94 res.send('no!');95 });9697 request(app)98 .get('/')99 .expect(404, done);100 });101

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 102–118why, one step back
102 it('should send cache-control by default', function (done) {
103 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))104105 request(app)106 .get('/')107 .expect('Cache-Control', 'public, max-age=0')108 .expect(200, done)109 })110111 it('should not serve dotfiles by default', function (done) {112 var app = createApp(path.resolve(__dirname, 'fixtures/.name'))113114 request(app)115 .get('/')116 .expect(404, done)117 })118

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 119–132why, one step back
119 it('should not override manual content-types', function (done) {
120 var app = express();121122 app.use(function (req, res) {123 res.contentType('application/x-bogus');124 res.sendFile(path.resolve(fixtures, 'name.txt'));125 });126127 request(app)128 .get('/')129 .expect('Content-Type', 'application/x-bogus')130 .end(done);131 })132

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 137–140why, one step back
137
138 app.use(function (req, res) {139 setImmediate(function () {140 res.sendFile(path.resolve(fixtures, 'name.txt'));

Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615

lines 141–144why, one step back
141 setTimeout(function () {
142 cb(error)143 }, 10)144 })

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 146–148why, one step back
146 });
147148 app.use(function (err, req, res, next) {

Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615

lines 149–150why, one step back
149 error = err
150 next(err)

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 151–152why, one step back
151 });
152

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 155–158why, one step back
155 test.end(function (err) {
156 assert.ok(err)157 server.close(cb)158 })

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 159why, one step back
159 })

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 160–172why, one step back
160 })
161162 describe('.sendFile(path, fn)', function () {163 it('should invoke the callback when complete', function (done) {164 var cb = after(2, done);165 var app = createApp(path.resolve(fixtures, 'name.txt'), cb);166167 request(app)168 .get('/')169 .expect(200, cb);170 })171172 it('should invoke the callback when client aborts', function (done) {

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 174–178why, one step back
174 var app = express();
175176 app.use(function (req, res) {177 setImmediate(function () {178 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 179–181why, one step back
179 assert.ok(err)
180 assert.strictEqual(err.code, 'ECONNABORTED')181 cb()

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 182–183why, one step back
182 });
183 });

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 185–186why, one step back
185 });
186

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 189–192why, one step back
189 test.end(function (err) {
190 assert.ok(err)191 server.close(cb)192 })

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 193–195why, one step back
193 })
194195 it('should invoke the callback when client already aborted', function (done) {

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 197–201why, one step back
197 var app = express();
198199 app.use(function (req, res) {200 onFinished(res, function () {201 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 202–204why, one step back
202 assert.ok(err)
203 assert.strictEqual(err.code, 'ECONNABORTED')204 cb()

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 205–206why, one step back
205 });
206 });

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 208–209why, one step back
208 });
209

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 212–215why, one step back
212 test.end(function (err) {
213 assert.ok(err)214 server.close(cb)215 })

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 216why, one step back
216 })

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 217–250why, one step back
217
218 it('should invoke the callback without error when HEAD', function (done) {219 var app = express();220 var cb = after(2, done);221222 app.use(function (req, res) {223 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);224 });225226 request(app)227 .head('/')228 .expect(200, cb);229 });230231 it('should invoke the callback without error when 304', function (done) {232 var app = express();233 var cb = after(3, done);234235 app.use(function (req, res) {236 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);237 });238239 request(app)240 .get('/')241 .expect('ETag', /^(?:W\/)?"[^"]+"$/)242 .expect(200, 'tobi', function (err, res) {243 if (err) return cb(err);244 var etag = res.headers.etag;245 request(app)246 .get('/')247 .set('If-None-Match', etag)248 .expect(304, cb);249 });250 });

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 251–256why, one step back
251
252 it('should invoke the callback on 404', function(done){253 var app = express();254255 app.use(function (req, res) {256 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 258–261why, one step back
258 });
259 });260261 request(app)

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 264why, one step back
264 })

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 265why, one step back
265

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

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 267–272why, one step back
267 it('should presist store', function (done) {
268 var app = express()269 var cb = after(2, done)270 var store = { foo: 'bar' }271272 app.use(function (req, res, next) {

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

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 274–298why, one step back
274 req.asyncLocalStorage.run(store, next)
275 })276277 app.use(function (req, res) {278 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {279 if (err) return cb(err)280281 var local = req.asyncLocalStorage.getStore()282283 assert.strictEqual(local.foo, 'bar')284 cb()285 })286 })287288 request(app)289 .get('/')290 .expect('Content-Type', 'text/plain; charset=utf-8')291 .expect(200, 'tobi', cb)292 })293294 it('should presist store on error', function (done) {295 var app = express()296 var store = { foo: 'bar' }297298 app.use(function (req, res, next) {

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

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 300–322why, one step back
300 req.asyncLocalStorage.run(store, next)
301 })302303 app.use(function (req, res) {304 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {305 var local = req.asyncLocalStorage.getStore()306307 if (local) {308 res.setHeader('x-store-foo', String(local.foo))309 }310311 res.send(err ? 'got ' + err.status + ' error' : 'no error')312 })313 })314315 request(app)316 .get('/')317 .expect(200)318 .expect('x-store-foo', 'bar')319 .expect('got 404 error')320 .end(done)321 })322 })

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 323–329why, one step back
323 })
324325 describe('.sendFile(path, options)', function () {326 it('should pass options to send module', function (done) {327 request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))328 .get('/')329 .expect(200, 'to', done)

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 330why, one step back
330 })

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 331–887why, one step back
331
332 describe('with "acceptRanges" option', function () {333 describe('when true', function () {334 it('should advertise byte range accepted', function (done) {335 var app = express()336337 app.use(function (req, res) {338 res.sendFile(path.resolve(fixtures, 'nums.txt'), {339 acceptRanges: true340 })341 })342343 request(app)344 .get('/')345 .expect(200)346 .expect('Accept-Ranges', 'bytes')347 .expect('123456789')348 .end(done)349 })350351 it('should respond to range request', function (done) {352 var app = express()353354 app.use(function (req, res) {355 res.sendFile(path.resolve(fixtures, 'nums.txt'), {356 acceptRanges: true357 })358 })359360 request(app)361 .get('/')362 .set('Range', 'bytes=0-4')363 .expect(206, '12345', done)364 })365 })366367 describe('when false', function () {368 it('should not advertise accept-ranges', function (done) {369 var app = express()370371 app.use(function (req, res) {372 res.sendFile(path.resolve(fixtures, 'nums.txt'), {373 acceptRanges: false374 })375 })376377 request(app)378 .get('/')379 .expect(200)380 .expect(utils.shouldNotHaveHeader('Accept-Ranges'))381 .end(done)382 })383384 it('should not honor range requests', function (done) {385 var app = express()386387 app.use(function (req, res) {388 res.sendFile(path.resolve(fixtures, 'nums.txt'), {389 acceptRanges: false390 })391 })392393 request(app)394 .get('/')395 .set('Range', 'bytes=0-4')396 .expect(200, '123456789', done)397 })398 })399 })400401 describe('with "cacheControl" option', function () {402 describe('when true', function () {403 it('should send cache-control header', function (done) {404 var app = express()405406 app.use(function (req, res) {407 res.sendFile(path.resolve(fixtures, 'user.html'), {408 cacheControl: true409 })410 })411412 request(app)413 .get('/')414 .expect(200)415 .expect('Cache-Control', 'public, max-age=0')416 .end(done)417 })418 })419420 describe('when false', function () {421 it('should not send cache-control header', function (done) {422 var app = express()423424 app.use(function (req, res) {425 res.sendFile(path.resolve(fixtures, 'user.html'), {426 cacheControl: false427 })428 })429430 request(app)431 .get('/')432 .expect(200)433 .expect(utils.shouldNotHaveHeader('Cache-Control'))434 .end(done)435 })436 })437 })438439 describe('with "dotfiles" option', function () {440 describe('when "allow"', function () {441 it('should allow dotfiles', function (done) {442 var app = express()443444 app.use(function (req, res) {445 res.sendFile(path.resolve(fixtures, '.name'), {446 dotfiles: 'allow'447 })448 })449450 request(app)451 .get('/')452 .expect(200)453 .expect(utils.shouldHaveBody(Buffer.from('tobi')))454 .end(done)455 })456 })457458 describe('when "deny"', function () {459 it('should deny dotfiles', function (done) {460 var app = express()461462 app.use(function (req, res) {463 res.sendFile(path.resolve(fixtures, '.name'), {464 dotfiles: 'deny'465 })466 })467468 request(app)469 .get('/')470 .expect(403)471 .expect(/Forbidden/)472 .end(done)473 })474 })475476 describe('when "ignore"', function () {477 it('should ignore dotfiles', function (done) {478 var app = express()479480 app.use(function (req, res) {481 res.sendFile(path.resolve(fixtures, '.name'), {482 dotfiles: 'ignore'483 })484 })485486 request(app)487 .get('/')488 .expect(404)489 .expect(/Not Found/)490 .end(done)491 })492 })493 })494495 describe('with "headers" option', function () {496 it('should set headers on response', function (done) {497 var app = express()498499 app.use(function (req, res) {500 res.sendFile(path.resolve(fixtures, 'user.html'), {501 headers: {502 'X-Foo': 'Bar',503 'X-Bar': 'Foo'504 }505 })506 })507508 request(app)509 .get('/')510 .expect(200)511 .expect('X-Foo', 'Bar')512 .expect('X-Bar', 'Foo')513 .end(done)514 })515516 it('should use last header when duplicated', function (done) {517 var app = express()518519 app.use(function (req, res) {520 res.sendFile(path.resolve(fixtures, 'user.html'), {521 headers: {522 'X-Foo': 'Bar',523 'x-foo': 'bar'524 }525 })526 })527528 request(app)529 .get('/')530 .expect(200)531 .expect('X-Foo', 'bar')532 .end(done)533 })534535 it('should override Content-Type', function (done) {536 var app = express()537538 app.use(function (req, res) {539 res.sendFile(path.resolve(fixtures, 'user.html'), {540 headers: {541 'Content-Type': 'text/x-custom'542 }543 })544 })545546 request(app)547 .get('/')548 .expect(200)549 .expect('Content-Type', 'text/x-custom')550 .end(done)551 })552553 it('should not set headers on 404', function (done) {554 var app = express()555556 app.use(function (req, res) {557 res.sendFile(path.resolve(fixtures, 'does-not-exist'), {558 headers: {559 'X-Foo': 'Bar'560 }561 })562 })563564 request(app)565 .get('/')566 .expect(404)567 .expect(utils.shouldNotHaveHeader('X-Foo'))568 .end(done)569 })570 })571572 describe('with "immutable" option', function () {573 describe('when true', function () {574 it('should send cache-control header with immutable', function (done) {575 var app = express()576577 app.use(function (req, res) {578 res.sendFile(path.resolve(fixtures, 'user.html'), {579 immutable: true580 })581 })582583 request(app)584 .get('/')585 .expect(200)586 .expect('Cache-Control', 'public, max-age=0, immutable')587 .end(done)588 })589 })590591 describe('when false', function () {592 it('should not send cache-control header with immutable', function (done) {593 var app = express()594595 app.use(function (req, res) {596 res.sendFile(path.resolve(fixtures, 'user.html'), {597 immutable: false598 })599 })600601 request(app)602 .get('/')603 .expect(200)604 .expect('Cache-Control', 'public, max-age=0')605 .end(done)606 })607 })608 })609610 describe('with "lastModified" option', function () {611 describe('when true', function () {612 it('should send last-modified header', function (done) {613 var app = express()614615 app.use(function (req, res) {616 res.sendFile(path.resolve(fixtures, 'user.html'), {617 lastModified: true618 })619 })620621 request(app)622 .get('/')623 .expect(200)624 .expect(utils.shouldHaveHeader('Last-Modified'))625 .end(done)626 })627628 it('should conditionally respond with if-modified-since', function (done) {629 var app = express()630631 app.use(function (req, res) {632 res.sendFile(path.resolve(fixtures, 'user.html'), {633 lastModified: true634 })635 })636637 request(app)638 .get('/')639 .set('If-Modified-Since', (new Date(Date.now() + 99999).toUTCString()))640 .expect(304, done)641 })642 })643644 describe('when false', function () {645 it('should not have last-modified header', function (done) {646 var app = express()647648 app.use(function (req, res) {649 res.sendFile(path.resolve(fixtures, 'user.html'), {650 lastModified: false651 })652 })653654 request(app)655 .get('/')656 .expect(200)657 .expect(utils.shouldNotHaveHeader('Last-Modified'))658 .end(done)659 })660661 it('should not honor if-modified-since', function (done) {662 var app = express()663664 app.use(function (req, res) {665 res.sendFile(path.resolve(fixtures, 'user.html'), {666 lastModified: false667 })668 })669670 request(app)671 .get('/')672 .set('If-Modified-Since', (new Date(Date.now() + 99999).toUTCString()))673 .expect(200)674 .expect(utils.shouldNotHaveHeader('Last-Modified'))675 .end(done)676 })677 })678 })679680 describe('with "maxAge" option', function () {681 it('should set cache-control max-age to milliseconds', function (done) {682 var app = express()683684 app.use(function (req, res) {685 res.sendFile(path.resolve(fixtures, 'user.html'), {686 maxAge: 20000687 })688 })689690 request(app)691 .get('/')692 .expect(200)693 .expect('Cache-Control', 'public, max-age=20')694 .end(done)695 })696697 it('should cap cache-control max-age to 1 year', function (done) {698 var app = express()699700 app.use(function (req, res) {701 res.sendFile(path.resolve(fixtures, 'user.html'), {702 maxAge: 99999999999703 })704 })705706 request(app)707 .get('/')708 .expect(200)709 .expect('Cache-Control', 'public, max-age=31536000')710 .end(done)711 })712713 it('should min cache-control max-age to 0', function (done) {714 var app = express()715716 app.use(function (req, res) {717 res.sendFile(path.resolve(fixtures, 'user.html'), {718 maxAge: -20000719 })720 })721722 request(app)723 .get('/')724 .expect(200)725 .expect('Cache-Control', 'public, max-age=0')726 .end(done)727 })728729 it('should floor cache-control max-age', function (done) {730 var app = express()731732 app.use(function (req, res) {733 res.sendFile(path.resolve(fixtures, 'user.html'), {734 maxAge: 21911.23735 })736 })737738 request(app)739 .get('/')740 .expect(200)741 .expect('Cache-Control', 'public, max-age=21')742 .end(done)743 })744745 describe('when cacheControl: false', function () {746 it('should not send cache-control', function (done) {747 var app = express()748749 app.use(function (req, res) {750 res.sendFile(path.resolve(fixtures, 'user.html'), {751 cacheControl: false,752 maxAge: 20000753 })754 })755756 request(app)757 .get('/')758 .expect(200)759 .expect(utils.shouldNotHaveHeader('Cache-Control'))760 .end(done)761 })762 })763764 describe('when string', function () {765 it('should accept plain number as milliseconds', function (done) {766 var app = express()767768 app.use(function (req, res) {769 res.sendFile(path.resolve(fixtures, 'user.html'), {770 maxAge: '20000'771 })772 })773774 request(app)775 .get('/')776 .expect(200)777 .expect('Cache-Control', 'public, max-age=20')778 .end(done)779 })780781 it('should accept suffix "s" for seconds', function (done) {782 var app = express()783784 app.use(function (req, res) {785 res.sendFile(path.resolve(fixtures, 'user.html'), {786 maxAge: '20s'787 })788 })789790 request(app)791 .get('/')792 .expect(200)793 .expect('Cache-Control', 'public, max-age=20')794 .end(done)795 })796797 it('should accept suffix "m" for minutes', function (done) {798 var app = express()799800 app.use(function (req, res) {801 res.sendFile(path.resolve(fixtures, 'user.html'), {802 maxAge: '20m'803 })804 })805806 request(app)807 .get('/')808 .expect(200)809 .expect('Cache-Control', 'public, max-age=1200')810 .end(done)811 })812813 it('should accept suffix "d" for days', function (done) {814 var app = express()815816 app.use(function (req, res) {817 res.sendFile(path.resolve(fixtures, 'user.html'), {818 maxAge: '20d'819 })820 })821822 request(app)823 .get('/')824 .expect(200)825 .expect('Cache-Control', 'public, max-age=1728000')826 .end(done)827 })828 })829 })830831 describe('with "root" option', function () {832 it('should allow relative path', function (done) {833 var app = express()834835 app.use(function (req, res) {836 res.sendFile('name.txt', {837 root: fixtures838 })839 })840841 request(app)842 .get('/')843 .expect(200, 'tobi', done)844 })845846 it('should allow up within root', function (done) {847 var app = express()848849 app.use(function (req, res) {850 res.sendFile('fake/../name.txt', {851 root: fixtures852 })853 })854855 request(app)856 .get('/')857 .expect(200, 'tobi', done)858 })859860 it('should reject up outside root', function (done) {861 var app = express()862863 app.use(function (req, res) {864 res.sendFile('..' + path.sep + path.relative(path.dirname(fixtures), path.join(fixtures, 'name.txt')), {865 root: fixtures866 })867 })868869 request(app)870 .get('/')871 .expect(403, done)872 })873874 it('should reject reading outside root', function (done) {875 var app = express()876877 app.use(function (req, res) {878 res.sendFile('../name.txt', {879 root: fixtures880 })881 })882883 request(app)884 .get('/')885 .expect(403, done)886 })887 })

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 888–899why, one step back
888 })
889})890891function createApp(path, options, fn) {892 var app = express();893894 app.use(function (req, res) {895 res.sendFile(path, options, fn);896 });897898 return app;899}