res.sendFile.js
This file
Path
test/res.sendFile.js
639 lines · 15.8 KiB · JavaScript
Last changed by
Fix res.sendFile logging standard write errorsDouglas Christopher Wilsonbefore adoption4b26bbd
History of this fileTake it
Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 1–5why, one step back12var after = require('after');3var express = require('../')4 , request = require('supertest')5 , assert = require('assert');Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 6why, one step back6var onFinished = require('on-finished');Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 7–12why, one step back7var path = require('path');8var should = require('should');9var fixtures = path.join(__dirname, 'fixtures');1011describe('res', function(){12 describe('.sendFile(path)', function () {tests: add additional res.sendFile testDouglas Christopher Wilsonbefore adoptionfbceae2
lines 13–20why, one step back13 it('should error missing path', function (done) {14 var app = createApp();1516 request(app)17 .get('/')18 .expect(500, /path.*required/, done);19 });20Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 21–36why, one step back21 it('should transfer a file', function (done) {22 var app = createApp(path.resolve(fixtures, 'name.txt'));2324 request(app)25 .get('/')26 .expect(200, 'tobi', done);27 });2829 it('should transfer a file with special characters in string', function (done) {30 var app = createApp(path.resolve(fixtures, '% of dogs.txt'));3132 request(app)33 .get('/')34 .expect(200, '20%', done);35 });36tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c
lines 37–44why, one step back37 it('should 404 for directory', function (done) {38 var app = createApp(path.resolve(fixtures, 'blog'));3940 request(app)41 .get('/')42 .expect(404, done);43 });44Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 45–71why, one step back45 it('should 404 when not found', function (done) {46 var app = createApp(path.resolve(fixtures, 'does-no-exist'));4748 app.use(function (req, res) {49 res.statusCode = 200;50 res.send('no!');51 });5253 request(app)54 .get('/')55 .expect(404, done);56 });5758 it('should not override manual content-types', function (done) {59 var app = express();6061 app.use(function (req, res) {62 res.contentType('application/x-bogus');63 res.sendFile(path.resolve(fixtures, 'name.txt'));64 });6566 request(app)67 .get('/')68 .expect('Content-Type', 'application/x-bogus')69 .end(done);70 })71Fix res.sendFile logging standard write errorsDouglas Christopher Wilsonbefore adoptionb326ae8
lines 72–92why, one step back72 it('should not error if the client aborts', function (done) {73 var cb = after(1, done);74 var app = express();7576 app.use(function (req, res) {77 setImmediate(function () {78 res.sendFile(path.resolve(fixtures, 'name.txt'));79 cb();80 });81 test.abort();82 });8384 app.use(function (err, req, res, next) {85 err.code.should.be.empty;86 cb();87 });8889 var test = request(app).get('/');90 test.expect(200, cb);91 })92Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 93–176why, one step back93 describe('with "dotfiles" option', function () {94 it('should not serve dotfiles by default', function (done) {95 var app = createApp(path.resolve(__dirname, 'fixtures/.name'));9697 request(app)98 .get('/')99 .expect(404, done);100 });101102 it('should accept dotfiles option', function(done){103 var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });104105 request(app)106 .get('/')107 .expect(200, 'tobi', done);108 });109 });110111 describe('with "headers" option', function () {112 it('should accept headers option', function (done) {113 var headers = {114 'x-success': 'sent',115 'x-other': 'done'116 };117 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });118119 request(app)120 .get('/')121 .expect('x-success', 'sent')122 .expect('x-other', 'done')123 .expect(200, done);124 });125126 it('should ignore headers option on 404', function (done) {127 var headers = { 'x-success': 'sent' };128 var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });129130 request(app)131 .get('/')132 .expect(404, function (err, res) {133 if (err) return done(err);134 res.headers.should.not.have.property('x-success');135 done();136 });137 });138 });139140 describe('with "root" option', function () {141 it('should not transfer relative with without', function (done) {142 var app = createApp('test/fixtures/name.txt');143144 request(app)145 .get('/')146 .expect(500, /must be absolute/, done);147 })148149 it('should serve relative to "root"', function (done) {150 var app = createApp('name.txt', {root: fixtures});151152 request(app)153 .get('/')154 .expect(200, 'tobi', done);155 })156157 it('should disallow requesting out of "root"', function (done) {158 var app = createApp('foo/../../user.html', {root: fixtures});159160 request(app)161 .get('/')162 .expect(403, done);163 })164 })165 })166167 describe('.sendFile(path, fn)', function () {168 it('should invoke the callback when complete', function (done) {169 var cb = after(2, done);170 var app = createApp(path.resolve(fixtures, 'name.txt'), cb);171172 request(app)173 .get('/')174 .expect(200, cb);175 })176Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 177–214why, one step back177 it('should invoke the callback when client aborts', function (done) {178 var cb = after(1, done);179 var app = express();180181 app.use(function (req, res) {182 setImmediate(function () {183 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {184 should(err).be.ok;185 err.code.should.equal('ECONNABORT');186 cb();187 });188 });189 test.abort();190 });191192 var test = request(app).get('/');193 test.expect(200, cb);194 })195196 it('should invoke the callback when client already aborted', function (done) {197 var cb = after(1, done);198 var app = express();199200 app.use(function (req, res) {201 onFinished(res, function () {202 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {203 should(err).be.ok;204 err.code.should.equal('ECONNABORT');205 cb();206 });207 });208 test.abort();209 });210211 var test = request(app).get('/');212 test.expect(200, cb);213 })214Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 215–234why, one step back215 it('should invoke the callback on 404', function(done){216 var app = express();217218 app.use(function (req, res) {219 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {220 should(err).be.ok;221 err.status.should.equal(404);222 res.send('got it');223 });224 });225226 request(app)227 .get('/')228 .expect(200, 'got it', done);229 })230 })231232 describe('.sendfile(path, fn)', function(){233 it('should invoke the callback when complete', function(done){234 var app = express();tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c
line 235why, one step back235 var cb = after(2, done);Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 236–237why, one step back236237 app.use(function(req, res){tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c
line 238why, one step back238 res.sendfile('test/fixtures/user.html', cb)Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 239–242why, one step back239 });240241 request(app)242 .get('/')tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c
line 243why, one step back243 .expect(200, cb);Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 244–256why, one step back244 })245246 it('should utilize the same options as express.static()', function(done){247 var app = express();248249 app.use(function(req, res){250 res.sendfile('test/fixtures/user.html', { maxAge: 60000 });251 });252253 request(app)254 .get('/')255 .expect('Cache-Control', 'public, max-age=60')256 .end(done);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 257–294why, one step back257 })258259 it('should invoke the callback when client aborts', function (done) {260 var cb = after(1, done);261 var app = express();262263 app.use(function (req, res) {264 setImmediate(function () {265 res.sendfile('test/fixtures/name.txt', function (err) {266 should(err).be.ok;267 err.code.should.equal('ECONNABORT');268 cb();269 });270 });271 test.abort();272 });273274 var test = request(app).get('/');275 test.expect(200, cb);276 })277278 it('should invoke the callback when client already aborted', function (done) {279 var cb = after(1, done);280 var app = express();281282 app.use(function (req, res) {283 onFinished(res, function () {284 res.sendfile('test/fixtures/name.txt', function (err) {285 should(err).be.ok;286 err.code.should.equal('ECONNABORT');287 cb();288 });289 });290 test.abort();291 });292293 var test = request(app).get('/');294 test.expect(200, cb);Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 295–452why, one step back295 })296297 it('should invoke the callback on 404', function(done){298 var app = express()299 , calls = 0;300301 app.use(function(req, res){302 res.sendfile('test/fixtures/nope.html', function(err){303 ++calls;304 assert(!res.headersSent);305 res.send(err.message);306 });307 });308309 request(app)310 .get('/')311 .end(function(err, res){312 assert(1 == calls, 'called too many times');313 res.text.should.startWith("ENOENT, stat");314 res.statusCode.should.equal(200);315 done();316 });317 })318319 it('should not override manual content-types', function(done){320 var app = express();321322 app.use(function(req, res){323 res.contentType('txt');324 res.sendfile('test/fixtures/user.html');325 });326327 request(app)328 .get('/')329 .expect('Content-Type', 'text/plain; charset=utf-8')330 .end(done);331 })332333 it('should invoke the callback on 403', function(done){334 var app = express()335 , calls = 0;336337 app.use(function(req, res){338 res.sendfile('test/fixtures/foo/../user.html', function(err){339 assert(!res.headersSent);340 ++calls;341 res.send(err.message);342 });343 });344345 request(app)346 .get('/')347 .expect('Forbidden')348 .expect(200, done);349 })350351 it('should invoke the callback on socket error', function(done){352 var app = express()353 , calls = 0;354355 app.use(function(req, res){356 res.sendfile('test/fixtures/user.html', function(err){357 assert(!res.headersSent);358 req.socket.listeners('error').should.have.length(1); // node's original handler359 done();360 });361362 req.socket.emit('error', new Error('broken!'));363 });364365 request(app)366 .get('/')367 .end(function(){});368 })369 })370371 describe('.sendfile(path)', function(){372 it('should not serve dotfiles', function(done){373 var app = express();374375 app.use(function(req, res){376 res.sendfile('test/fixtures/.name');377 });378379 request(app)380 .get('/')381 .expect(404, done);382 })383384 it('should accept dotfiles option', function(done){385 var app = express();386387 app.use(function(req, res){388 res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });389 });390391 request(app)392 .get('/')393 .expect(200, 'tobi', done);394 })395396 it('should accept headers option', function(done){397 var app = express();398 var headers = {399 'x-success': 'sent',400 'x-other': 'done'401 };402403 app.use(function(req, res){404 res.sendfile('test/fixtures/user.html', { headers: headers });405 });406407 request(app)408 .get('/')409 .expect('x-success', 'sent')410 .expect('x-other', 'done')411 .expect(200, done);412 })413414 it('should ignore headers option on 404', function(done){415 var app = express();416 var headers = { 'x-success': 'sent' };417418 app.use(function(req, res){419 res.sendfile('test/fixtures/user.nothing', { headers: headers });420 });421422 request(app)423 .get('/')424 .expect(404, function (err, res) {425 if (err) return done(err);426 res.headers.should.not.have.property('x-success');427 done();428 });429 })430431 it('should transfer a file', function (done) {432 var app = express();433434 app.use(function (req, res) {435 res.sendfile('test/fixtures/name.txt');436 });437438 request(app)439 .get('/')440 .expect(200, 'tobi', done);441 });442443 it('should transfer a directory index file', function (done) {444 var app = express();445446 app.use(function (req, res) {447 res.sendfile('test/fixtures/blog/');448 });449450 request(app)451 .get('/')452 .expect(200, '<b>index</b>', done);tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c
lines 453–464why, one step back453 });454455 it('should 404 for directory without trailing slash', function (done) {456 var app = express();457458 app.use(function (req, res) {459 res.sendfile('test/fixtures/blog');460 });461462 request(app)463 .get('/')464 .expect(404, done);Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 465–478why, one step back465 });466467 it('should transfer a file with urlencoded name', function (done) {468 var app = express();469470 app.use(function (req, res) {471 res.sendfile('test/fixtures/%25%20of%20dogs.txt');472 });473474 request(app)475 .get('/')476 .expect(200, '20%', done);477 });478Fix res.sendFile logging standard write errorsDouglas Christopher Wilsonbefore adoption4b26bbd
lines 479–499why, one step back479 it('should not error if the client aborts', function (done) {480 var cb = after(1, done);481 var app = express();482483 app.use(function (req, res) {484 setImmediate(function () {485 res.sendfile(path.resolve(fixtures, 'name.txt'));486 cb();487 });488 test.abort();489 });490491 app.use(function (err, req, res, next) {492 err.code.should.be.empty;493 cb();494 });495496 var test = request(app).get('/');497 test.expect(200, cb);498 })499Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 500–639why, one step back500 describe('with an absolute path', function(){501 it('should transfer the file', function(done){502 var app = express();503504 app.use(function(req, res){505 res.sendfile(__dirname + '/fixtures/user.html');506 });507508 request(app)509 .get('/')510 .end(function(err, res){511 res.text.should.equal('<p>{{user.name}}</p>');512 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');513 done();514 });515 })516 })517518 describe('with a relative path', function(){519 it('should transfer the file', function(done){520 var app = express();521522 app.use(function(req, res){523 res.sendfile('test/fixtures/user.html');524 });525526 request(app)527 .get('/')528 .end(function(err, res){529 res.text.should.equal('<p>{{user.name}}</p>');530 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');531 done();532 });533 })534535 it('should serve relative to "root"', function(done){536 var app = express();537538 app.use(function(req, res){539 res.sendfile('user.html', { root: 'test/fixtures/' });540 });541542 request(app)543 .get('/')544 .end(function(err, res){545 res.text.should.equal('<p>{{user.name}}</p>');546 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');547 done();548 });549 })550551 it('should consider ../ malicious when "root" is not set', function(done){552 var app = express();553554 app.use(function(req, res){555 res.sendfile('test/fixtures/foo/../user.html');556 });557558 request(app)559 .get('/')560 .expect(403, done);561 })562563 it('should allow ../ when "root" is set', function(done){564 var app = express();565566 app.use(function(req, res){567 res.sendfile('foo/../user.html', { root: 'test/fixtures' });568 });569570 request(app)571 .get('/')572 .expect(200, done);573 })574575 it('should disallow requesting out of "root"', function(done){576 var app = express();577578 app.use(function(req, res){579 res.sendfile('foo/../../user.html', { root: 'test/fixtures' });580 });581582 request(app)583 .get('/')584 .expect(403, done);585 })586587 it('should next(404) when not found', function(done){588 var app = express()589 , calls = 0;590591 app.use(function(req, res){592 res.sendfile('user.html');593 });594595 app.use(function(req, res){596 assert(0, 'this should not be called');597 });598599 app.use(function(err, req, res, next){600 ++calls;601 next(err);602 });603604 request(app)605 .get('/')606 .end(function(err, res){607 res.statusCode.should.equal(404);608 calls.should.equal(1);609 done();610 });611 })612613 describe('with non-GET', function(){614 it('should still serve', function(done){615 var app = express()616 , calls = 0;617618 app.use(function(req, res){619 res.sendfile(__dirname + '/fixtures/name.txt');620 });621622 request(app)623 .get('/')624 .expect('tobi', done);625 })626 })627 })628 })629})630631function createApp(path, options, fn) {632 var app = express();633634 app.use(function (req, res) {635 res.sendFile(path, options, fn);636 });637638 return app;639}