res.sendFile.js
This file
Path
test/res.sendFile.js
1,411 lines · 35.5 KiB · JavaScript
Last changed by
tests: fix typo in descriptionHashenbefore adoption1b2e097
History of this fileTake it
The walk stopped after 200 commits: lines older than b02d3a1 point at it.
tests: use strict modeDouglas Christopher Wilsonbefore adoptiona659137
line 1why, one step back1'use strict'docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 2–3why, one step back23var after = require('after');deps: on-finished@2.4.1Douglas Christopher Wilsonbefore adoption2e2d78c
line 4why, one step back4var asyncHooks = tryRequire('async_hooks')docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 5–13why, one step back5var Buffer = require('safe-buffer').Buffer6var express = require('../')7 , request = require('supertest')8 , assert = require('assert');9var onFinished = require('on-finished');10var path = require('path');11var fixtures = path.join(__dirname, 'fixtures');12var utils = require('./support/utils');13deps: on-finished@2.4.1Douglas Christopher Wilsonbefore adoption2e2d78c
lines 14–17why, one step back14var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'15 ? describe16 : describe.skip17docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 18–35why, one step back18describe('res', function(){19 describe('.sendFile(path)', function () {20 it('should error missing path', function (done) {21 var app = createApp();2223 request(app)24 .get('/')25 .expect(500, /path.*required/, done);26 });2728 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 })35tests: expand res.sendFile options testsDouglas Christopher Wilsonbefore adoption291993d
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 })43docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 44–105why, 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 });5960 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 });8485 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 });9293 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 });105tests: expand res.sendFile options testsDouglas Christopher Wilsonbefore adoption291993d
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 })122docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 123–148why, 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 })136137 it('should not error if the client aborts', function (done) {138 var app = express();139 var cb = after(2, done)140 var error = null141142 app.use(function (req, res) {143 setImmediate(function () {144 res.sendFile(path.resolve(fixtures, 'name.txt'));145 setTimeout(function () {146 cb(error)147 }, 10)148 })build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
line 149why, one step back149 test.req.abort()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 150–158why, one step back150 });151152 app.use(function (err, req, res, next) {153 error = err154 next(err)155 });156157 var server = app.listen()158 var test = request(server).get('/')build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
lines 159–162why, one step back159 test.end(function (err) {160 assert.ok(err)161 server.close(cb)162 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 163–176why, one step back163 })164 })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 })175176 it('should invoke the callback when client aborts', function (done) {build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
line 177why, one step back177 var cb = after(2, done)docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
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) {build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
lines 183–185why, one step back183 assert.ok(err)184 assert.strictEqual(err.code, 'ECONNABORTED')185 cb()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 186–187why, one step back186 });187 });build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
line 188why, one step back188 test.req.abort()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 189–192why, one step back189 });190191 var server = app.listen()192 var test = request(server).get('/')build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
lines 193–196why, one step back193 test.end(function (err) {194 assert.ok(err)195 server.close(cb)196 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 197–199why, one step back197 })198199 it('should invoke the callback when client already aborted', function (done) {build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
line 200why, one step back200 var cb = after(2, done)docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
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) {build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
lines 206–208why, one step back206 assert.ok(err)207 assert.strictEqual(err.code, 'ECONNABORTED')208 cb()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 209–210why, one step back209 });210 });build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
line 211why, one step back211 test.req.abort()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 212–215why, one step back212 });213214 var server = app.listen()215 var test = request(server).get('/')build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
lines 216–219why, one step back216 test.end(function (err) {217 assert.ok(err)218 server.close(cb)219 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 220–260why, one step back220 })221222 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 });255256 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) {tests: remove usage of shouldDouglas Christopher Wilsonbefore adoptionbc5ca05
line 261why, one step back261 res.send(err ? 'got ' + err.status + ' error' : 'no error')docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 262–265why, one step back262 });263 });264265 request(app)tests: remove usage of shouldDouglas Christopher Wilsonbefore adoptionbc5ca05
lines 266–267why, one step back266 .get('/')267 .expect(200, 'got 404 error', done)docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
line 268why, one step back268 })deps: on-finished@2.4.1Douglas Christopher Wilsonbefore adoption2e2d78c
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 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
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)tests: expand res.sendFile options testsDouglas Christopher Wilsonbefore adoption291993d
lines 334–749why, one step back334 })335336 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 () {tests: fix typo in descriptionHashenbefore adoption1b2e097
line 750why, one step back750 it('should not send cache-control', function (done) {tests: expand res.sendFile options testsDouglas Christopher Wilsonbefore adoption291993d
lines 751–890why, one step back751 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 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 891–921why, one step back891 })892 })893894 describe('.sendfile(path, fn)', function(){895 it('should invoke the callback when complete', function(done){896 var app = express();897 var cb = after(2, done);898899 app.use(function(req, res){900 res.sendfile('test/fixtures/user.html', cb)901 });902903 request(app)904 .get('/')905 .expect(200, cb);906 })907908 it('should utilize the same options as express.static()', function(done){909 var app = express();910911 app.use(function(req, res){912 res.sendfile('test/fixtures/user.html', { maxAge: 60000 });913 });914915 request(app)916 .get('/')917 .expect('Cache-Control', 'public, max-age=60')918 .end(done);919 })920921 it('should invoke the callback when client aborts', function (done) {build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
line 922why, one step back922 var cb = after(2, done)docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 923–927why, one step back923 var app = express();924925 app.use(function (req, res) {926 setImmediate(function () {927 res.sendfile('test/fixtures/name.txt', function (err) {build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
lines 928–930why, one step back928 assert.ok(err)929 assert.strictEqual(err.code, 'ECONNABORTED')930 cb()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 931–932why, one step back931 });932 });build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
line 933why, one step back933 test.req.abort()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 934–937why, one step back934 });935936 var server = app.listen()937 var test = request(server).get('/')build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
lines 938–941why, one step back938 test.end(function (err) {939 assert.ok(err)940 server.close(cb)941 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 942–944why, one step back942 })943944 it('should invoke the callback when client already aborted', function (done) {build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
line 945why, one step back945 var cb = after(2, done)docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 946–950why, one step back946 var app = express();947948 app.use(function (req, res) {949 onFinished(res, function () {950 res.sendfile('test/fixtures/name.txt', function (err) {build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
lines 951–953why, one step back951 assert.ok(err)952 assert.strictEqual(err.code, 'ECONNABORTED')953 cb()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 954–955why, one step back954 });955 });build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
line 956why, one step back956 test.req.abort()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 957–960why, one step back957 });958959 var server = app.listen()960 var test = request(server).get('/')build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
lines 961–964why, one step back961 test.end(function (err) {962 assert.ok(err)963 server.close(cb)964 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 965–1053why, one step back965 })966967 it('should invoke the callback without error when HEAD', function (done) {968 var app = express();969 var cb = after(2, done);970971 app.use(function (req, res) {972 res.sendfile('test/fixtures/name.txt', cb);973 });974975 request(app)976 .head('/')977 .expect(200, cb);978 });979980 it('should invoke the callback without error when 304', function (done) {981 var app = express();982 var cb = after(3, done);983984 app.use(function (req, res) {985 res.sendfile('test/fixtures/name.txt', cb);986 });987988 request(app)989 .get('/')990 .expect('ETag', /^(?:W\/)?"[^"]+"$/)991 .expect(200, 'tobi', function (err, res) {992 if (err) return cb(err);993 var etag = res.headers.etag;994 request(app)995 .get('/')996 .set('If-None-Match', etag)997 .expect(304, cb);998 });999 });10001001 it('should invoke the callback on 404', function(done){1002 var app = express();1003 var calls = 0;10041005 app.use(function(req, res){1006 res.sendfile('test/fixtures/nope.html', function(err){1007 assert.equal(calls++, 0);1008 assert(!res.headersSent);1009 res.send(err.message);1010 });1011 });10121013 request(app)1014 .get('/')1015 .expect(200, /^ENOENT.*?, stat/, done);1016 })10171018 it('should not override manual content-types', function(done){1019 var app = express();10201021 app.use(function(req, res){1022 res.contentType('txt');1023 res.sendfile('test/fixtures/user.html');1024 });10251026 request(app)1027 .get('/')1028 .expect('Content-Type', 'text/plain; charset=utf-8')1029 .end(done);1030 })10311032 it('should invoke the callback on 403', function(done){1033 var app = express()10341035 app.use(function(req, res){1036 res.sendfile('test/fixtures/foo/../user.html', function(err){1037 assert(!res.headersSent);1038 res.send(err.message);1039 });1040 });10411042 request(app)1043 .get('/')1044 .expect('Forbidden')1045 .expect(200, done);1046 })10471048 it('should invoke the callback on socket error', function(done){1049 var app = express()10501051 app.use(function(req, res){1052 res.sendfile('test/fixtures/user.html', function(err){1053 assert(!res.headersSent);tests: remove usage of shouldDouglas Christopher Wilsonbefore adoptionbc5ca05
line 1054why, one step back1054 assert.strictEqual(req.socket.listeners('error').length, 1) // node's original handlerdocs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 1055–1064why, one step back1055 done();1056 });10571058 req.socket.emit('error', new Error('broken!'));1059 });10601061 request(app)1062 .get('/')1063 .end(function(){});1064 })deps: on-finished@2.4.1Douglas Christopher Wilsonbefore adoption2e2d78c
lines 1065–1122why, one step back10651066 describeAsyncHooks('async local storage', function () {1067 it('should presist store', function (done) {1068 var app = express()1069 var cb = after(2, done)1070 var store = { foo: 'bar' }10711072 app.use(function (req, res, next) {1073 req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()1074 req.asyncLocalStorage.run(store, next)1075 })10761077 app.use(function (req, res) {1078 res.sendfile('test/fixtures/name.txt', function (err) {1079 if (err) return cb(err)10801081 var local = req.asyncLocalStorage.getStore()10821083 assert.strictEqual(local.foo, 'bar')1084 cb()1085 })1086 })10871088 request(app)1089 .get('/')1090 .expect('Content-Type', 'text/plain; charset=UTF-8')1091 .expect(200, 'tobi', cb)1092 })10931094 it('should presist store on error', function (done) {1095 var app = express()1096 var store = { foo: 'bar' }10971098 app.use(function (req, res, next) {1099 req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()1100 req.asyncLocalStorage.run(store, next)1101 })11021103 app.use(function (req, res) {1104 res.sendfile('test/fixtures/does-not-exist', function (err) {1105 var local = req.asyncLocalStorage.getStore()11061107 if (local) {1108 res.setHeader('x-store-foo', String(local.foo))1109 }11101111 res.send(err ? 'got ' + err.status + ' error' : 'no error')1112 })1113 })11141115 request(app)1116 .get('/')1117 .expect(200)1118 .expect('x-store-foo', 'bar')1119 .expect('got 404 error')1120 .end(done)1121 })1122 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 1123–1145why, one step back1123 })11241125 describe('.sendfile(path)', function(){1126 it('should not serve dotfiles', function(done){1127 var app = express();11281129 app.use(function(req, res){1130 res.sendfile('test/fixtures/.name');1131 });11321133 request(app)1134 .get('/')1135 .expect(404, done);1136 })11371138 it('should accept dotfiles option', function(done){1139 var app = express();11401141 app.use(function(req, res){1142 res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });1143 });11441145 request(app)tests: remove duplicate utilsDouglas Christopher Wilsonbefore adoption18f782b
lines 1146–1149why, one step back1146 .get('/')1147 .expect(200)1148 .expect(utils.shouldHaveBody(Buffer.from('tobi')))1149 .end(done)docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 1150–1243why, one step back1150 })11511152 it('should accept headers option', function(done){1153 var app = express();1154 var headers = {1155 'x-success': 'sent',1156 'x-other': 'done'1157 };11581159 app.use(function(req, res){1160 res.sendfile('test/fixtures/user.html', { headers: headers });1161 });11621163 request(app)1164 .get('/')1165 .expect('x-success', 'sent')1166 .expect('x-other', 'done')1167 .expect(200, done);1168 })11691170 it('should ignore headers option on 404', function(done){1171 var app = express();1172 var headers = { 'x-success': 'sent' };11731174 app.use(function(req, res){1175 res.sendfile('test/fixtures/user.nothing', { headers: headers });1176 });11771178 request(app)1179 .get('/')1180 .expect(utils.shouldNotHaveHeader('X-Success'))1181 .expect(404, done);1182 })11831184 it('should transfer a file', function (done) {1185 var app = express();11861187 app.use(function (req, res) {1188 res.sendfile('test/fixtures/name.txt');1189 });11901191 request(app)1192 .get('/')1193 .expect(200, 'tobi', done);1194 });11951196 it('should transfer a directory index file', function (done) {1197 var app = express();11981199 app.use(function (req, res) {1200 res.sendfile('test/fixtures/blog/');1201 });12021203 request(app)1204 .get('/')1205 .expect(200, '<b>index</b>', done);1206 });12071208 it('should 404 for directory without trailing slash', function (done) {1209 var app = express();12101211 app.use(function (req, res) {1212 res.sendfile('test/fixtures/blog');1213 });12141215 request(app)1216 .get('/')1217 .expect(404, done);1218 });12191220 it('should transfer a file with urlencoded name', function (done) {1221 var app = express();12221223 app.use(function (req, res) {1224 res.sendfile('test/fixtures/%25%20of%20dogs.txt');1225 });12261227 request(app)1228 .get('/')1229 .expect(200, '20%', done);1230 });12311232 it('should not error if the client aborts', function (done) {1233 var app = express();1234 var cb = after(2, done)1235 var error = null12361237 app.use(function (req, res) {1238 setImmediate(function () {1239 res.sendfile(path.resolve(fixtures, 'name.txt'));1240 setTimeout(function () {1241 cb(error)1242 }, 10)1243 });build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
line 1244why, one step back1244 test.req.abort()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 1245–1253why, one step back1245 });12461247 app.use(function (err, req, res, next) {1248 error = err1249 next(err)1250 });12511252 var server = app.listen()1253 var test = request(server).get('/')build: supertest@6.2.2Douglas Christopher Wilsonbefore adoption9d09762
lines 1254–1257why, one step back1254 test.end(function (err) {1255 assert.ok(err)1256 server.close(cb)1257 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 1258–1355why, one step back1258 })12591260 describe('with an absolute path', function(){1261 it('should transfer the file', function(done){1262 var app = express();12631264 app.use(function(req, res){1265 res.sendfile(path.join(__dirname, '/fixtures/user.html'))1266 });12671268 request(app)1269 .get('/')1270 .expect('Content-Type', 'text/html; charset=UTF-8')1271 .expect(200, '<p>{{user.name}}</p>', done);1272 })1273 })12741275 describe('with a relative path', function(){1276 it('should transfer the file', function(done){1277 var app = express();12781279 app.use(function(req, res){1280 res.sendfile('test/fixtures/user.html');1281 });12821283 request(app)1284 .get('/')1285 .expect('Content-Type', 'text/html; charset=UTF-8')1286 .expect(200, '<p>{{user.name}}</p>', done);1287 })12881289 it('should serve relative to "root"', function(done){1290 var app = express();12911292 app.use(function(req, res){1293 res.sendfile('user.html', { root: 'test/fixtures/' });1294 });12951296 request(app)1297 .get('/')1298 .expect('Content-Type', 'text/html; charset=UTF-8')1299 .expect(200, '<p>{{user.name}}</p>', done);1300 })13011302 it('should consider ../ malicious when "root" is not set', function(done){1303 var app = express();13041305 app.use(function(req, res){1306 res.sendfile('test/fixtures/foo/../user.html');1307 });13081309 request(app)1310 .get('/')1311 .expect(403, done);1312 })13131314 it('should allow ../ when "root" is set', function(done){1315 var app = express();13161317 app.use(function(req, res){1318 res.sendfile('foo/../user.html', { root: 'test/fixtures' });1319 });13201321 request(app)1322 .get('/')1323 .expect(200, done);1324 })13251326 it('should disallow requesting out of "root"', function(done){1327 var app = express();13281329 app.use(function(req, res){1330 res.sendfile('foo/../../user.html', { root: 'test/fixtures' });1331 });13321333 request(app)1334 .get('/')1335 .expect(403, done);1336 })13371338 it('should next(404) when not found', function(done){1339 var app = express()1340 , calls = 0;13411342 app.use(function(req, res){1343 res.sendfile('user.html');1344 });13451346 app.use(function(req, res){1347 assert(0, 'this should not be called');1348 });13491350 app.use(function(err, req, res, next){1351 ++calls;1352 next(err);1353 });13541355 request(app)tests: remove usage of shouldDouglas Christopher Wilsonbefore adoptionbc5ca05
lines 1356–1361why, one step back1356 .get('/')1357 .expect(404, function (err) {1358 if (err) return done(err)1359 assert.strictEqual(calls, 1)1360 done()1361 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 1362–1379why, one step back1362 })13631364 describe('with non-GET', function(){1365 it('should still serve', function(done){1366 var app = express()13671368 app.use(function(req, res){1369 res.sendfile(path.join(__dirname, '/fixtures/name.txt'))1370 });13711372 request(app)1373 .get('/')1374 .expect('tobi', done);1375 })1376 })1377 })1378 })1379tests: fix position of res.sendfile(path, options) testDouglas Christopher Wilsonbefore adoptioncf9f662
lines 1380–1382why, one step back1380 describe('.sendfile(path, options)', function () {1381 it('should pass options to send module', function (done) {1382 var app = express()docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
line 1383why, one step back1383tests: fix position of res.sendfile(path, options) testDouglas Christopher Wilsonbefore adoptioncf9f662
lines 1384–1386why, one step back1384 app.use(function (req, res) {1385 res.sendfile(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 })1386 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
line 1387why, one step back1387tests: fix position of res.sendfile(path, options) testDouglas Christopher Wilsonbefore adoptioncf9f662
lines 1388–1391why, one step back1388 request(app)1389 .get('/')1390 .expect(200, 'to', done)1391 })docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 1392–1402why, one step back1392 })1393})13941395function createApp(path, options, fn) {1396 var app = express();13971398 app.use(function (req, res) {1399 res.sendFile(path, options, fn);1400 });14011402 return app;deps: on-finished@2.4.1Douglas Christopher Wilsonbefore adoption2e2d78c
lines 1403–1410why, one step back1403}14041405function tryRequire (name) {1406 try {1407 return require(name)1408 } catch (e) {1409 return {}1410 }docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
line 1411why, one step back1411}