Skip to content
Myr

res.sendFile.js

This file

Path

test/res.sendFile.js

732 lines · 18.1 KiB · JavaScript

Last changed by

Fix wrong code on aborted connections from res.sendFileDouglas Christopher Wilsonbefore adoptiondbc61fc

History of this file

Take it

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 1–5why, one step back
1
2var 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 back
6var onFinished = require('on-finished');

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 7–12why, one step back
7var 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 back
13 it('should error missing path', function (done) {
14 var app = createApp();1516 request(app)17 .get('/')18 .expect(500, /path.*required/, done);19 });20

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 21–36why, one step back
21 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 });36

Fix incorrect "Request aborted" for res.sendFile when HEAD or 304Douglas Christopher Wilsonbefore adoption3387916

lines 37–61why, one step back
37 it('should include ETag', function (done) {
38 var app = createApp(path.resolve(fixtures, 'name.txt'));3940 request(app)41 .get('/')42 .expect('ETag', /^(?:W\/)?"[^"]+"$/)43 .expect(200, 'tobi', done);44 });4546 it('should 304 when ETag matches', function (done) {47 var app = createApp(path.resolve(fixtures, 'name.txt'));4849 request(app)50 .get('/')51 .expect('ETag', /^(?:W\/)?"[^"]+"$/)52 .expect(200, 'tobi', function (err, res) {53 if (err) return done(err);54 var etag = res.headers.etag;55 request(app)56 .get('/')57 .set('If-None-Match', etag)58 .expect(304, done);59 });60 });61

tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c

lines 62–69why, one step back
62 it('should 404 for directory', function (done) {
63 var app = createApp(path.resolve(fixtures, 'blog'));6465 request(app)66 .get('/')67 .expect(404, done);68 });69

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 70–96why, one step back
70 it('should 404 when not found', function (done) {
71 var app = createApp(path.resolve(fixtures, 'does-no-exist'));7273 app.use(function (req, res) {74 res.statusCode = 200;75 res.send('no!');76 });7778 request(app)79 .get('/')80 .expect(404, done);81 });8283 it('should not override manual content-types', function (done) {84 var app = express();8586 app.use(function (req, res) {87 res.contentType('application/x-bogus');88 res.sendFile(path.resolve(fixtures, 'name.txt'));89 });9091 request(app)92 .get('/')93 .expect('Content-Type', 'application/x-bogus')94 .end(done);95 })96

Fix res.sendFile logging standard write errorsDouglas Christopher Wilsonbefore adoptionb326ae8

lines 97–117why, one step back
97 it('should not error if the client aborts', function (done) {
98 var cb = after(1, done);99 var app = express();100101 app.use(function (req, res) {102 setImmediate(function () {103 res.sendFile(path.resolve(fixtures, 'name.txt'));104 cb();105 });106 test.abort();107 });108109 app.use(function (err, req, res, next) {110 err.code.should.be.empty;111 cb();112 });113114 var test = request(app).get('/');115 test.expect(200, cb);116 })117

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 118–201why, one step back
118 describe('with "dotfiles" option', function () {
119 it('should not serve dotfiles by default', function (done) {120 var app = createApp(path.resolve(__dirname, 'fixtures/.name'));121122 request(app)123 .get('/')124 .expect(404, done);125 });126127 it('should accept dotfiles option', function(done){128 var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });129130 request(app)131 .get('/')132 .expect(200, 'tobi', done);133 });134 });135136 describe('with "headers" option', function () {137 it('should accept headers option', function (done) {138 var headers = {139 'x-success': 'sent',140 'x-other': 'done'141 };142 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });143144 request(app)145 .get('/')146 .expect('x-success', 'sent')147 .expect('x-other', 'done')148 .expect(200, done);149 });150151 it('should ignore headers option on 404', function (done) {152 var headers = { 'x-success': 'sent' };153 var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });154155 request(app)156 .get('/')157 .expect(404, function (err, res) {158 if (err) return done(err);159 res.headers.should.not.have.property('x-success');160 done();161 });162 });163 });164165 describe('with "root" option', function () {166 it('should not transfer relative with without', function (done) {167 var app = createApp('test/fixtures/name.txt');168169 request(app)170 .get('/')171 .expect(500, /must be absolute/, done);172 })173174 it('should serve relative to "root"', function (done) {175 var app = createApp('name.txt', {root: fixtures});176177 request(app)178 .get('/')179 .expect(200, 'tobi', done);180 })181182 it('should disallow requesting out of "root"', function (done) {183 var app = createApp('foo/../../user.html', {root: fixtures});184185 request(app)186 .get('/')187 .expect(403, done);188 })189 })190 })191192 describe('.sendFile(path, fn)', function () {193 it('should invoke the callback when complete', function (done) {194 var cb = after(2, done);195 var app = createApp(path.resolve(fixtures, 'name.txt'), cb);196197 request(app)198 .get('/')199 .expect(200, cb);200 })201

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 202–209why, one step back
202 it('should invoke the callback when client aborts', function (done) {
203 var cb = after(1, done);204 var app = express();205206 app.use(function (req, res) {207 setImmediate(function () {208 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {209 should(err).be.ok;

Fix wrong code on aborted connections from res.sendFileDouglas Christopher Wilsonbefore adoptiondbc61fc

line 210why, one step back
210 err.code.should.equal('ECONNABORTED');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 211–228why, one step back
211 cb();
212 });213 });214 test.abort();215 });216217 var test = request(app).get('/');218 test.expect(200, cb);219 })220221 it('should invoke the callback when client already aborted', function (done) {222 var cb = after(1, done);223 var app = express();224225 app.use(function (req, res) {226 onFinished(res, function () {227 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {228 should(err).be.ok;

Fix wrong code on aborted connections from res.sendFileDouglas Christopher Wilsonbefore adoptiondbc61fc

line 229why, one step back
229 err.code.should.equal('ECONNABORTED');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 230–239why, one step back
230 cb();
231 });232 });233 test.abort();234 });235236 var test = request(app).get('/');237 test.expect(200, cb);238 })239

Fix incorrect "Request aborted" for res.sendFile when HEAD or 304Douglas Christopher Wilsonbefore adoption3387916

lines 240–273why, one step back
240 it('should invoke the callback without error when HEAD', function (done) {
241 var app = express();242 var cb = after(2, done);243244 app.use(function (req, res) {245 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);246 });247248 request(app)249 .head('/')250 .expect(200, cb);251 });252253 it('should invoke the callback without error when 304', function (done) {254 var app = express();255 var cb = after(3, done);256257 app.use(function (req, res) {258 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);259 });260261 request(app)262 .get('/')263 .expect('ETag', /^(?:W\/)?"[^"]+"$/)264 .expect(200, 'tobi', function (err, res) {265 if (err) return cb(err);266 var etag = res.headers.etag;267 request(app)268 .get('/')269 .set('If-None-Match', etag)270 .expect(304, cb);271 });272 });273

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 274–293why, one step back
274 it('should invoke the callback on 404', function(done){
275 var app = express();276277 app.use(function (req, res) {278 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {279 should(err).be.ok;280 err.status.should.equal(404);281 res.send('got it');282 });283 });284285 request(app)286 .get('/')287 .expect(200, 'got it', done);288 })289 })290291 describe('.sendfile(path, fn)', function(){292 it('should invoke the callback when complete', function(done){293 var app = express();

tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 295–296why, one step back
295
296 app.use(function(req, res){

tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c

line 297why, one step back
297 res.sendfile('test/fixtures/user.html', cb)

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 298–301why, one step back
298 });
299300 request(app)301 .get('/')

tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c

line 302why, one step back
302 .expect(200, cb);

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 303–315why, one step back
303 })
304305 it('should utilize the same options as express.static()', function(done){306 var app = express();307308 app.use(function(req, res){309 res.sendfile('test/fixtures/user.html', { maxAge: 60000 });310 });311312 request(app)313 .get('/')314 .expect('Cache-Control', 'public, max-age=60')315 .end(done);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 316–325why, one step back
316 })
317318 it('should invoke the callback when client aborts', function (done) {319 var cb = after(1, done);320 var app = express();321322 app.use(function (req, res) {323 setImmediate(function () {324 res.sendfile('test/fixtures/name.txt', function (err) {325 should(err).be.ok;

Fix wrong code on aborted connections from res.sendFileDouglas Christopher Wilsonbefore adoptiondbc61fc

line 326why, one step back
326 err.code.should.equal('ECONNABORTED');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 327–344why, one step back
327 cb();
328 });329 });330 test.abort();331 });332333 var test = request(app).get('/');334 test.expect(200, cb);335 })336337 it('should invoke the callback when client already aborted', function (done) {338 var cb = after(1, done);339 var app = express();340341 app.use(function (req, res) {342 onFinished(res, function () {343 res.sendfile('test/fixtures/name.txt', function (err) {344 should(err).be.ok;

Fix wrong code on aborted connections from res.sendFileDouglas Christopher Wilsonbefore adoptiondbc61fc

line 345why, one step back
345 err.code.should.equal('ECONNABORTED');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 346–353why, one step back
346 cb();
347 });348 });349 test.abort();350 });351352 var test = request(app).get('/');353 test.expect(200, cb);

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 354why, one step back
354 })

Fix incorrect "Request aborted" for res.sendFile when HEAD or 304Douglas Christopher Wilsonbefore adoption3387916

lines 355–388why, one step back
355
356 it('should invoke the callback without error when HEAD', function (done) {357 var app = express();358 var cb = after(2, done);359360 app.use(function (req, res) {361 res.sendfile('test/fixtures/name.txt', cb);362 });363364 request(app)365 .head('/')366 .expect(200, cb);367 });368369 it('should invoke the callback without error when 304', function (done) {370 var app = express();371 var cb = after(3, done);372373 app.use(function (req, res) {374 res.sendfile('test/fixtures/name.txt', cb);375 });376377 request(app)378 .get('/')379 .expect('ETag', /^(?:W\/)?"[^"]+"$/)380 .expect(200, 'tobi', function (err, res) {381 if (err) return cb(err);382 var etag = res.headers.etag;383 request(app)384 .get('/')385 .set('If-None-Match', etag)386 .expect(304, cb);387 });388 });

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 389–545why, one step back
389
390 it('should invoke the callback on 404', function(done){391 var app = express()392 , calls = 0;393394 app.use(function(req, res){395 res.sendfile('test/fixtures/nope.html', function(err){396 ++calls;397 assert(!res.headersSent);398 res.send(err.message);399 });400 });401402 request(app)403 .get('/')404 .end(function(err, res){405 assert(1 == calls, 'called too many times');406 res.text.should.startWith("ENOENT, stat");407 res.statusCode.should.equal(200);408 done();409 });410 })411412 it('should not override manual content-types', function(done){413 var app = express();414415 app.use(function(req, res){416 res.contentType('txt');417 res.sendfile('test/fixtures/user.html');418 });419420 request(app)421 .get('/')422 .expect('Content-Type', 'text/plain; charset=utf-8')423 .end(done);424 })425426 it('should invoke the callback on 403', function(done){427 var app = express()428 , calls = 0;429430 app.use(function(req, res){431 res.sendfile('test/fixtures/foo/../user.html', function(err){432 assert(!res.headersSent);433 ++calls;434 res.send(err.message);435 });436 });437438 request(app)439 .get('/')440 .expect('Forbidden')441 .expect(200, done);442 })443444 it('should invoke the callback on socket error', function(done){445 var app = express()446 , calls = 0;447448 app.use(function(req, res){449 res.sendfile('test/fixtures/user.html', function(err){450 assert(!res.headersSent);451 req.socket.listeners('error').should.have.length(1); // node's original handler452 done();453 });454455 req.socket.emit('error', new Error('broken!'));456 });457458 request(app)459 .get('/')460 .end(function(){});461 })462 })463464 describe('.sendfile(path)', function(){465 it('should not serve dotfiles', function(done){466 var app = express();467468 app.use(function(req, res){469 res.sendfile('test/fixtures/.name');470 });471472 request(app)473 .get('/')474 .expect(404, done);475 })476477 it('should accept dotfiles option', function(done){478 var app = express();479480 app.use(function(req, res){481 res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });482 });483484 request(app)485 .get('/')486 .expect(200, 'tobi', done);487 })488489 it('should accept headers option', function(done){490 var app = express();491 var headers = {492 'x-success': 'sent',493 'x-other': 'done'494 };495496 app.use(function(req, res){497 res.sendfile('test/fixtures/user.html', { headers: headers });498 });499500 request(app)501 .get('/')502 .expect('x-success', 'sent')503 .expect('x-other', 'done')504 .expect(200, done);505 })506507 it('should ignore headers option on 404', function(done){508 var app = express();509 var headers = { 'x-success': 'sent' };510511 app.use(function(req, res){512 res.sendfile('test/fixtures/user.nothing', { headers: headers });513 });514515 request(app)516 .get('/')517 .expect(404, function (err, res) {518 if (err) return done(err);519 res.headers.should.not.have.property('x-success');520 done();521 });522 })523524 it('should transfer a file', function (done) {525 var app = express();526527 app.use(function (req, res) {528 res.sendfile('test/fixtures/name.txt');529 });530531 request(app)532 .get('/')533 .expect(200, 'tobi', done);534 });535536 it('should transfer a directory index file', function (done) {537 var app = express();538539 app.use(function (req, res) {540 res.sendfile('test/fixtures/blog/');541 });542543 request(app)544 .get('/')545 .expect(200, '<b>index</b>', done);

tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c

lines 546–557why, one step back
546 });
547548 it('should 404 for directory without trailing slash', function (done) {549 var app = express();550551 app.use(function (req, res) {552 res.sendfile('test/fixtures/blog');553 });554555 request(app)556 .get('/')557 .expect(404, done);

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 558–571why, one step back
558 });
559560 it('should transfer a file with urlencoded name', function (done) {561 var app = express();562563 app.use(function (req, res) {564 res.sendfile('test/fixtures/%25%20of%20dogs.txt');565 });566567 request(app)568 .get('/')569 .expect(200, '20%', done);570 });571

Fix res.sendFile logging standard write errorsDouglas Christopher Wilsonbefore adoption4b26bbd

lines 572–592why, one step back
572 it('should not error if the client aborts', function (done) {
573 var cb = after(1, done);574 var app = express();575576 app.use(function (req, res) {577 setImmediate(function () {578 res.sendfile(path.resolve(fixtures, 'name.txt'));579 cb();580 });581 test.abort();582 });583584 app.use(function (err, req, res, next) {585 err.code.should.be.empty;586 cb();587 });588589 var test = request(app).get('/');590 test.expect(200, cb);591 })592

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 593–732why, one step back
593 describe('with an absolute path', function(){
594 it('should transfer the file', function(done){595 var app = express();596597 app.use(function(req, res){598 res.sendfile(__dirname + '/fixtures/user.html');599 });600601 request(app)602 .get('/')603 .end(function(err, res){604 res.text.should.equal('<p>{{user.name}}</p>');605 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');606 done();607 });608 })609 })610611 describe('with a relative path', function(){612 it('should transfer the file', function(done){613 var app = express();614615 app.use(function(req, res){616 res.sendfile('test/fixtures/user.html');617 });618619 request(app)620 .get('/')621 .end(function(err, res){622 res.text.should.equal('<p>{{user.name}}</p>');623 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');624 done();625 });626 })627628 it('should serve relative to "root"', function(done){629 var app = express();630631 app.use(function(req, res){632 res.sendfile('user.html', { root: 'test/fixtures/' });633 });634635 request(app)636 .get('/')637 .end(function(err, res){638 res.text.should.equal('<p>{{user.name}}</p>');639 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');640 done();641 });642 })643644 it('should consider ../ malicious when "root" is not set', function(done){645 var app = express();646647 app.use(function(req, res){648 res.sendfile('test/fixtures/foo/../user.html');649 });650651 request(app)652 .get('/')653 .expect(403, done);654 })655656 it('should allow ../ when "root" is set', function(done){657 var app = express();658659 app.use(function(req, res){660 res.sendfile('foo/../user.html', { root: 'test/fixtures' });661 });662663 request(app)664 .get('/')665 .expect(200, done);666 })667668 it('should disallow requesting out of "root"', function(done){669 var app = express();670671 app.use(function(req, res){672 res.sendfile('foo/../../user.html', { root: 'test/fixtures' });673 });674675 request(app)676 .get('/')677 .expect(403, done);678 })679680 it('should next(404) when not found', function(done){681 var app = express()682 , calls = 0;683684 app.use(function(req, res){685 res.sendfile('user.html');686 });687688 app.use(function(req, res){689 assert(0, 'this should not be called');690 });691692 app.use(function(err, req, res, next){693 ++calls;694 next(err);695 });696697 request(app)698 .get('/')699 .end(function(err, res){700 res.statusCode.should.equal(404);701 calls.should.equal(1);702 done();703 });704 })705706 describe('with non-GET', function(){707 it('should still serve', function(done){708 var app = express()709 , calls = 0;710711 app.use(function(req, res){712 res.sendfile(__dirname + '/fixtures/name.txt');713 });714715 request(app)716 .get('/')717 .expect('tobi', done);718 })719 })720 })721 })722})723724function createApp(path, options, fn) {725 var app = express();726727 app.use(function (req, res) {728 res.sendFile(path, options, fn);729 });730731 return app;732}