res.sendFile.js
This file
Path
test/res.sendFile.js
392 lines · 10.4 KiB · JavaScript
Last changed by
Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
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 });22Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 23–30why, one step back23 it('should error for non-string path', function (done) {24 var app = createApp(42)2526 request(app)27 .get('/')28 .expect(500, /TypeError: path must be a string to res.sendFile/, done)29 })30Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 31–46why, one step back31 it('should transfer a file', function (done) {32 var app = createApp(path.resolve(fixtures, 'name.txt'));3334 request(app)35 .get('/')36 .expect(200, 'tobi', done);37 });3839 it('should transfer a file with special characters in string', function (done) {40 var app = createApp(path.resolve(fixtures, '% of dogs.txt'));4142 request(app)43 .get('/')44 .expect(200, '20%', done);45 });46Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 47–71why, one step back47 it('should include ETag', function (done) {48 var app = createApp(path.resolve(fixtures, 'name.txt'));4950 request(app)51 .get('/')52 .expect('ETag', /^(?:W\/)?"[^"]+"$/)53 .expect(200, 'tobi', done);54 });5556 it('should 304 when ETag matches', function (done) {57 var app = createApp(path.resolve(fixtures, 'name.txt'));5859 request(app)60 .get('/')61 .expect('ETag', /^(?:W\/)?"[^"]+"$/)62 .expect(200, 'tobi', function (err, res) {63 if (err) return done(err);64 var etag = res.headers.etag;65 request(app)66 .get('/')67 .set('If-None-Match', etag)68 .expect(304, done);69 });70 });71tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c
lines 72–79why, one step back72 it('should 404 for directory', function (done) {73 var app = createApp(path.resolve(fixtures, 'blog'));7475 request(app)76 .get('/')77 .expect(404, done);78 });79Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 80–106why, one step back80 it('should 404 when not found', function (done) {81 var app = createApp(path.resolve(fixtures, 'does-no-exist'));8283 app.use(function (req, res) {84 res.statusCode = 200;85 res.send('no!');86 });8788 request(app)89 .get('/')90 .expect(404, done);91 });9293 it('should not override manual content-types', function (done) {94 var app = express();9596 app.use(function (req, res) {97 res.contentType('application/x-bogus');98 res.sendFile(path.resolve(fixtures, 'name.txt'));99 });100101 request(app)102 .get('/')103 .expect('Content-Type', 'application/x-bogus')104 .end(done);105 })106Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 107–108why, one step back107 it('should not error if the client aborts', function (done) {108 var app = express();Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 109–110why, one step back109 var cb = after(2, done)110 var error = nullMerge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 111–114why, one step back111112 app.use(function (req, res) {113 setImmediate(function () {114 res.sendFile(path.resolve(fixtures, 'name.txt'));Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 115why, one step back115 server.close(cb)Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 116–119why, one step back116 setTimeout(function () {117 cb(error)118 }, 10)119 })Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 120–123why, one step back120 test.abort();121 });122123 app.use(function (err, req, res, next) {Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 124–125why, one step back124 error = err125 next(err)Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 126–127why, one step back126 });127Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 128–129why, one step back128 var server = app.listen()129 var test = request(server).get('/')Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
line 130why, one step back130 test.end()Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 131–132why, one step back131 })132Merge tag '4.14.1'Douglas Christopher Wilsonbefore adoptionbcbb9d5
lines 133–152why, one step back133 describe('with "cacheControl" option', function () {134 it('should enable cacheControl by default', function (done) {135 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))136137 request(app)138 .get('/')139 .expect('Cache-Control', 'public, max-age=0')140 .expect(200, done)141 })142143 it('should accept cacheControl option', function (done) {144 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { cacheControl: false })145146 request(app)147 .get('/')148 .expect(utils.shouldNotHaveHeader('Cache-Control'))149 .expect(200, done)150 })151 })152Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 153–166why, one step back153 describe('with "dotfiles" option', function () {154 it('should not serve dotfiles by default', function (done) {155 var app = createApp(path.resolve(__dirname, 'fixtures/.name'));156157 request(app)158 .get('/')159 .expect(404, done);160 });161162 it('should accept dotfiles option', function(done){163 var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });164165 request(app)166 .get('/')Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 167–169why, one step back167 .expect(200)168 .expect(shouldHaveBody(Buffer.from('tobi')))169 .end(done)Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 170–175why, one step back170 });171 });172173 describe('with "headers" option', function () {174 it('should accept headers option', function (done) {175 var headers = {Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 176–177why, one step back176 'x-success': 'sent',177 'x-other': 'done'Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 178–193why, one step back178 };179 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });180181 request(app)182 .get('/')183 .expect('x-success', 'sent')184 .expect('x-other', 'done')185 .expect(200, done);186 });187188 it('should ignore headers option on 404', function (done) {189 var headers = { 'x-success': 'sent' };190 var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });191192 request(app)193 .get('/')Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 194–195why, one step back194 .expect(utils.shouldNotHaveHeader('X-Success'))195 .expect(404, done);Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 196–198why, one step back196 });197 });198Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 199–236why, one step back199 describe('with "immutable" option', function () {200 it('should add immutable cache-control directive', function (done) {201 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {202 immutable: true,203 maxAge: '4h'204 })205206 request(app)207 .get('/')208 .expect('Cache-Control', 'public, max-age=14400, immutable')209 .expect(200, done)210 })211 })212213 describe('with "maxAge" option', function () {214 it('should set cache-control max-age from number', function (done) {215 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {216 maxAge: 14400000217 })218219 request(app)220 .get('/')221 .expect('Cache-Control', 'public, max-age=14400')222 .expect(200, done)223 })224225 it('should set cache-control max-age from string', function (done) {226 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {227 maxAge: '4h'228 })229230 request(app)231 .get('/')232 .expect('Cache-Control', 'public, max-age=14400')233 .expect(200, done)234 })235 })236Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 237–273why, one step back237 describe('with "root" option', function () {238 it('should not transfer relative with without', function (done) {239 var app = createApp('test/fixtures/name.txt');240241 request(app)242 .get('/')243 .expect(500, /must be absolute/, done);244 })245246 it('should serve relative to "root"', function (done) {247 var app = createApp('name.txt', {root: fixtures});248249 request(app)250 .get('/')251 .expect(200, 'tobi', done);252 })253254 it('should disallow requesting out of "root"', function (done) {255 var app = createApp('foo/../../user.html', {root: fixtures});256257 request(app)258 .get('/')259 .expect(403, done);260 })261 })262 })263264 describe('.sendFile(path, fn)', function () {265 it('should invoke the callback when complete', function (done) {266 var cb = after(2, done);267 var app = createApp(path.resolve(fixtures, 'name.txt'), cb);268269 request(app)270 .get('/')271 .expect(200, cb);272 })273Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 274–280why, one step back274 it('should invoke the callback when client aborts', function (done) {275 var cb = after(1, done);276 var app = express();277278 app.use(function (req, res) {279 setImmediate(function () {280 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 281why, one step back281 should(err).be.ok()Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 282why, one step back282 err.code.should.equal('ECONNABORTED');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 283why, one step back283 server.close(cb)Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 284–288why, one step back284 });285 });286 test.abort();287 });288Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 289–290why, one step back289 var server = app.listen()290 var test = request(server).get('/')Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 291–300why, one step back291 test.expect(200, cb);292 })293294 it('should invoke the callback when client already aborted', function (done) {295 var cb = after(1, done);296 var app = express();297298 app.use(function (req, res) {299 onFinished(res, function () {300 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 301why, one step back301 should(err).be.ok()Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 302why, one step back302 err.code.should.equal('ECONNABORTED');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 303why, one step back303 server.close(cb)Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 304–308why, one step back304 });305 });306 test.abort();307 });308Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 309–310why, one step back309 var server = app.listen()310 var test = request(server).get('/')Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 311–312why, one step back311 test.expect(200, cb);312 })Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 313–346why, one step back313314 it('should invoke the callback without error when HEAD', function (done) {315 var app = express();316 var cb = after(2, done);317318 app.use(function (req, res) {319 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);320 });321322 request(app)323 .head('/')324 .expect(200, cb);325 });326327 it('should invoke the callback without error when 304', function (done) {328 var app = express();329 var cb = after(3, done);330331 app.use(function (req, res) {332 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);333 });334335 request(app)336 .get('/')337 .expect('ETag', /^(?:W\/)?"[^"]+"$/)338 .expect(200, 'tobi', function (err, res) {339 if (err) return cb(err);340 var etag = res.headers.etag;341 request(app)342 .get('/')343 .set('If-None-Match', etag)344 .expect(304, cb);345 });346 });Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 347why, one step back347Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 348–352why, one step back348 it('should invoke the callback on 404', function(done){349 var app = express();350351 app.use(function (req, res) {352 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 353why, one step back353 should(err).be.ok()Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 354–361why, one step back354 err.status.should.equal(404);355 res.send('got it');356 });357 });358359 request(app)360 .get('/')361 .expect(200, 'got it', done);Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 362–369why, one step back362 })363 })364365 describe('.sendFile(path, options)', function () {366 it('should pass options to send module', function (done) {367 request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))368 .get('/')369 .expect(200, 'to', done)Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 370–381why, one step back370 })371 })372})373374function createApp(path, options, fn) {375 var app = express();376377 app.use(function (req, res) {378 res.sendFile(path, options, fn);379 });380381 return app;Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 382–391why, one step back382}383384function shouldHaveBody (buf) {385 return function (res) {386 var body = !Buffer.isBuffer(res.body)387 ? Buffer.from(res.text)388 : res.body389 assert.ok(body, 'response has body')390 assert.strictEqual(body.toString('hex'), buf.toString('hex'))391 }Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 392why, one step back392}