res.sendFile.js
This file
Path
test/res.sendFile.js
384 lines · 10.2 KiB · JavaScript
Last changed by
Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
History of this fileTake it
Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 1–2why, one step back12var after = require('after');Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 3–4why, one step back3var assert = require('assert')4var Buffer = require('safe-buffer').BufferAdd res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 5–6why, one step back5var express = require('../')6 , request = require('supertest')Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 7why, one step back7var onFinished = require('on-finished');Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 8–10why, one step back8var path = require('path');9var should = require('should');10var fixtures = path.join(__dirname, 'fixtures');Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 11why, one step back11var utils = require('./support/utils');Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 12–14why, one step back1213describe('res', function(){14 describe('.sendFile(path)', function () {tests: add additional res.sendFile testDouglas Christopher Wilsonbefore adoptionfbceae2
lines 15–22why, one step back15 it('should error missing path', function (done) {16 var app = createApp();1718 request(app)19 .get('/')20 .expect(500, /path.*required/, done);21 });22Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 23–38why, one step back23 it('should transfer a file', function (done) {24 var app = createApp(path.resolve(fixtures, 'name.txt'));2526 request(app)27 .get('/')28 .expect(200, 'tobi', done);29 });3031 it('should transfer a file with special characters in string', function (done) {32 var app = createApp(path.resolve(fixtures, '% of dogs.txt'));3334 request(app)35 .get('/')36 .expect(200, '20%', done);37 });38Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 39–63why, one step back39 it('should include ETag', function (done) {40 var app = createApp(path.resolve(fixtures, 'name.txt'));4142 request(app)43 .get('/')44 .expect('ETag', /^(?:W\/)?"[^"]+"$/)45 .expect(200, 'tobi', done);46 });4748 it('should 304 when ETag matches', function (done) {49 var app = createApp(path.resolve(fixtures, 'name.txt'));5051 request(app)52 .get('/')53 .expect('ETag', /^(?:W\/)?"[^"]+"$/)54 .expect(200, 'tobi', function (err, res) {55 if (err) return done(err);56 var etag = res.headers.etag;57 request(app)58 .get('/')59 .set('If-None-Match', etag)60 .expect(304, done);61 });62 });63tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c
lines 64–71why, one step back64 it('should 404 for directory', function (done) {65 var app = createApp(path.resolve(fixtures, 'blog'));6667 request(app)68 .get('/')69 .expect(404, done);70 });71Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 72–98why, one step back72 it('should 404 when not found', function (done) {73 var app = createApp(path.resolve(fixtures, 'does-no-exist'));7475 app.use(function (req, res) {76 res.statusCode = 200;77 res.send('no!');78 });7980 request(app)81 .get('/')82 .expect(404, done);83 });8485 it('should not override manual content-types', function (done) {86 var app = express();8788 app.use(function (req, res) {89 res.contentType('application/x-bogus');90 res.sendFile(path.resolve(fixtures, 'name.txt'));91 });9293 request(app)94 .get('/')95 .expect('Content-Type', 'application/x-bogus')96 .end(done);97 })98Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 99–100why, one step back99 it('should not error if the client aborts', function (done) {100 var app = express();Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 101–102why, one step back101 var cb = after(2, done)102 var error = nullMerge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 103–106why, one step back103104 app.use(function (req, res) {105 setImmediate(function () {106 res.sendFile(path.resolve(fixtures, 'name.txt'));Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 107why, one step back107 server.close(cb)Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 108–111why, one step back108 setTimeout(function () {109 cb(error)110 }, 10)111 })Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 112–115why, one step back112 test.abort();113 });114115 app.use(function (err, req, res, next) {Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 116–117why, one step back116 error = err117 next(err)Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 118–119why, one step back118 });119Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 120–121why, one step back120 var server = app.listen()121 var test = request(server).get('/')Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
line 122why, one step back122 test.end()Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 123–124why, one step back123 })124Merge tag '4.14.1'Douglas Christopher Wilsonbefore adoptionbcbb9d5
lines 125–144why, one step back125 describe('with "cacheControl" option', function () {126 it('should enable cacheControl by default', function (done) {127 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))128129 request(app)130 .get('/')131 .expect('Cache-Control', 'public, max-age=0')132 .expect(200, done)133 })134135 it('should accept cacheControl option', function (done) {136 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { cacheControl: false })137138 request(app)139 .get('/')140 .expect(utils.shouldNotHaveHeader('Cache-Control'))141 .expect(200, done)142 })143 })144Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 145–158why, one step back145 describe('with "dotfiles" option', function () {146 it('should not serve dotfiles by default', function (done) {147 var app = createApp(path.resolve(__dirname, 'fixtures/.name'));148149 request(app)150 .get('/')151 .expect(404, done);152 });153154 it('should accept dotfiles option', function(done){155 var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });156157 request(app)158 .get('/')Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 159–161why, one step back159 .expect(200)160 .expect(shouldHaveBody(Buffer.from('tobi')))161 .end(done)Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 162–167why, one step back162 });163 });164165 describe('with "headers" option', function () {166 it('should accept headers option', function (done) {167 var headers = {Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 168–169why, one step back168 'x-success': 'sent',169 'x-other': 'done'Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 170–185why, one step back170 };171 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });172173 request(app)174 .get('/')175 .expect('x-success', 'sent')176 .expect('x-other', 'done')177 .expect(200, done);178 });179180 it('should ignore headers option on 404', function (done) {181 var headers = { 'x-success': 'sent' };182 var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });183184 request(app)185 .get('/')Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 186–187why, one step back186 .expect(utils.shouldNotHaveHeader('X-Success'))187 .expect(404, done);Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 188–190why, one step back188 });189 });190Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 191–228why, one step back191 describe('with "immutable" option', function () {192 it('should add immutable cache-control directive', function (done) {193 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {194 immutable: true,195 maxAge: '4h'196 })197198 request(app)199 .get('/')200 .expect('Cache-Control', 'public, max-age=14400, immutable')201 .expect(200, done)202 })203 })204205 describe('with "maxAge" option', function () {206 it('should set cache-control max-age from number', function (done) {207 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {208 maxAge: 14400000209 })210211 request(app)212 .get('/')213 .expect('Cache-Control', 'public, max-age=14400')214 .expect(200, done)215 })216217 it('should set cache-control max-age from string', function (done) {218 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {219 maxAge: '4h'220 })221222 request(app)223 .get('/')224 .expect('Cache-Control', 'public, max-age=14400')225 .expect(200, done)226 })227 })228Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 229–265why, one step back229 describe('with "root" option', function () {230 it('should not transfer relative with without', function (done) {231 var app = createApp('test/fixtures/name.txt');232233 request(app)234 .get('/')235 .expect(500, /must be absolute/, done);236 })237238 it('should serve relative to "root"', function (done) {239 var app = createApp('name.txt', {root: fixtures});240241 request(app)242 .get('/')243 .expect(200, 'tobi', done);244 })245246 it('should disallow requesting out of "root"', function (done) {247 var app = createApp('foo/../../user.html', {root: fixtures});248249 request(app)250 .get('/')251 .expect(403, done);252 })253 })254 })255256 describe('.sendFile(path, fn)', function () {257 it('should invoke the callback when complete', function (done) {258 var cb = after(2, done);259 var app = createApp(path.resolve(fixtures, 'name.txt'), cb);260261 request(app)262 .get('/')263 .expect(200, cb);264 })265Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 266–272why, one step back266 it('should invoke the callback when client aborts', function (done) {267 var cb = after(1, done);268 var app = express();269270 app.use(function (req, res) {271 setImmediate(function () {272 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 273why, one step back273 should(err).be.ok()Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 274why, one step back274 err.code.should.equal('ECONNABORTED');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 275why, one step back275 server.close(cb)Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 276–280why, one step back276 });277 });278 test.abort();279 });280Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 281–282why, one step back281 var server = app.listen()282 var test = request(server).get('/')Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 283–292why, one step back283 test.expect(200, cb);284 })285286 it('should invoke the callback when client already aborted', function (done) {287 var cb = after(1, done);288 var app = express();289290 app.use(function (req, res) {291 onFinished(res, function () {292 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 293why, one step back293 should(err).be.ok()Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 294why, one step back294 err.code.should.equal('ECONNABORTED');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 295why, one step back295 server.close(cb)Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 296–300why, one step back296 });297 });298 test.abort();299 });300Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 301–302why, one step back301 var server = app.listen()302 var test = request(server).get('/')Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 303–304why, one step back303 test.expect(200, cb);304 })Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 305–338why, one step back305306 it('should invoke the callback without error when HEAD', function (done) {307 var app = express();308 var cb = after(2, done);309310 app.use(function (req, res) {311 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);312 });313314 request(app)315 .head('/')316 .expect(200, cb);317 });318319 it('should invoke the callback without error when 304', function (done) {320 var app = express();321 var cb = after(3, done);322323 app.use(function (req, res) {324 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);325 });326327 request(app)328 .get('/')329 .expect('ETag', /^(?:W\/)?"[^"]+"$/)330 .expect(200, 'tobi', function (err, res) {331 if (err) return cb(err);332 var etag = res.headers.etag;333 request(app)334 .get('/')335 .set('If-None-Match', etag)336 .expect(304, cb);337 });338 });Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 339why, one step back339Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 340–344why, one step back340 it('should invoke the callback on 404', function(done){341 var app = express();342343 app.use(function (req, res) {344 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 345why, one step back345 should(err).be.ok()Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 346–353why, one step back346 err.status.should.equal(404);347 res.send('got it');348 });349 });350351 request(app)352 .get('/')353 .expect(200, 'got it', done);Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 354–361why, one step back354 })355 })356357 describe('.sendFile(path, options)', function () {358 it('should pass options to send module', function (done) {359 request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))360 .get('/')361 .expect(200, 'to', done)Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 362–373why, one step back362 })363 })364})365366function createApp(path, options, fn) {367 var app = express();368369 app.use(function (req, res) {370 res.sendFile(path, options, fn);371 });372373 return app;Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 374–383why, one step back374}375376function shouldHaveBody (buf) {377 return function (res) {378 var body = !Buffer.isBuffer(res.body)379 ? Buffer.from(res.text)380 : res.body381 assert.ok(body, 'response has body')382 assert.strictEqual(body.toString('hex'), buf.toString('hex'))383 }Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 384why, one step back384}