Skip to content
Myr

res.sendFile.js

This file

Path

test/res.sendFile.js

727 lines · 18.0 KiB · JavaScript

Last changed by

build: support io.js 1.xDouglas Christopher Wilsonbefore adoptionc894c84

History of this file

Take it

The walk stopped after 200 commits: lines older than ad3ca25 point at it.

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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');

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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);

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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)

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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);

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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);

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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 });

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 389–390why, one step back
389
390 it('should invoke the callback on 404', function(done){

build: support io.js 1.xDouglas Christopher Wilsonbefore adoptionc894c84

lines 391–392why, one step back
391 var app = express();
392 var calls = 0;

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 393–395why, one step back
393
394 app.use(function(req, res){395 res.sendfile('test/fixtures/nope.html', function(err){

build: support io.js 1.xDouglas Christopher Wilsonbefore adoptionc894c84

line 396why, one step back
396 assert.equal(calls++, 0);

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 397–403why, one step back
397 assert(!res.headersSent);
398 res.send(err.message);399 });400 });401402 request(app)403 .get('/')

build: support io.js 1.xDouglas Christopher Wilsonbefore adoptionc894c84

line 404why, one step back
404 .expect(200, /^ENOENT.*?, stat/, done);

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 405–540why, one step back
405 })
406407 it('should not override manual content-types', function(done){408 var app = express();409410 app.use(function(req, res){411 res.contentType('txt');412 res.sendfile('test/fixtures/user.html');413 });414415 request(app)416 .get('/')417 .expect('Content-Type', 'text/plain; charset=utf-8')418 .end(done);419 })420421 it('should invoke the callback on 403', function(done){422 var app = express()423 , calls = 0;424425 app.use(function(req, res){426 res.sendfile('test/fixtures/foo/../user.html', function(err){427 assert(!res.headersSent);428 ++calls;429 res.send(err.message);430 });431 });432433 request(app)434 .get('/')435 .expect('Forbidden')436 .expect(200, done);437 })438439 it('should invoke the callback on socket error', function(done){440 var app = express()441 , calls = 0;442443 app.use(function(req, res){444 res.sendfile('test/fixtures/user.html', function(err){445 assert(!res.headersSent);446 req.socket.listeners('error').should.have.length(1); // node's original handler447 done();448 });449450 req.socket.emit('error', new Error('broken!'));451 });452453 request(app)454 .get('/')455 .end(function(){});456 })457 })458459 describe('.sendfile(path)', function(){460 it('should not serve dotfiles', function(done){461 var app = express();462463 app.use(function(req, res){464 res.sendfile('test/fixtures/.name');465 });466467 request(app)468 .get('/')469 .expect(404, done);470 })471472 it('should accept dotfiles option', function(done){473 var app = express();474475 app.use(function(req, res){476 res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });477 });478479 request(app)480 .get('/')481 .expect(200, 'tobi', done);482 })483484 it('should accept headers option', function(done){485 var app = express();486 var headers = {487 'x-success': 'sent',488 'x-other': 'done'489 };490491 app.use(function(req, res){492 res.sendfile('test/fixtures/user.html', { headers: headers });493 });494495 request(app)496 .get('/')497 .expect('x-success', 'sent')498 .expect('x-other', 'done')499 .expect(200, done);500 })501502 it('should ignore headers option on 404', function(done){503 var app = express();504 var headers = { 'x-success': 'sent' };505506 app.use(function(req, res){507 res.sendfile('test/fixtures/user.nothing', { headers: headers });508 });509510 request(app)511 .get('/')512 .expect(404, function (err, res) {513 if (err) return done(err);514 res.headers.should.not.have.property('x-success');515 done();516 });517 })518519 it('should transfer a file', function (done) {520 var app = express();521522 app.use(function (req, res) {523 res.sendfile('test/fixtures/name.txt');524 });525526 request(app)527 .get('/')528 .expect(200, 'tobi', done);529 });530531 it('should transfer a directory index file', function (done) {532 var app = express();533534 app.use(function (req, res) {535 res.sendfile('test/fixtures/blog/');536 });537538 request(app)539 .get('/')540 .expect(200, '<b>index</b>', done);

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

lines 541–552why, one step back
541 });
542543 it('should 404 for directory without trailing slash', function (done) {544 var app = express();545546 app.use(function (req, res) {547 res.sendfile('test/fixtures/blog');548 });549550 request(app)551 .get('/')552 .expect(404, done);

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 553–566why, one step back
553 });
554555 it('should transfer a file with urlencoded name', function (done) {556 var app = express();557558 app.use(function (req, res) {559 res.sendfile('test/fixtures/%25%20of%20dogs.txt');560 });561562 request(app)563 .get('/')564 .expect(200, '20%', done);565 });566

Fix res.sendFile logging standard write errorsDouglas Christopher Wilsonbefore adoption4b26bbd

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 588–727why, one step back
588 describe('with an absolute path', function(){
589 it('should transfer the file', function(done){590 var app = express();591592 app.use(function(req, res){593 res.sendfile(__dirname + '/fixtures/user.html');594 });595596 request(app)597 .get('/')598 .end(function(err, res){599 res.text.should.equal('<p>{{user.name}}</p>');600 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');601 done();602 });603 })604 })605606 describe('with a relative path', function(){607 it('should transfer the file', function(done){608 var app = express();609610 app.use(function(req, res){611 res.sendfile('test/fixtures/user.html');612 });613614 request(app)615 .get('/')616 .end(function(err, res){617 res.text.should.equal('<p>{{user.name}}</p>');618 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');619 done();620 });621 })622623 it('should serve relative to "root"', function(done){624 var app = express();625626 app.use(function(req, res){627 res.sendfile('user.html', { root: 'test/fixtures/' });628 });629630 request(app)631 .get('/')632 .end(function(err, res){633 res.text.should.equal('<p>{{user.name}}</p>');634 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');635 done();636 });637 })638639 it('should consider ../ malicious when "root" is not set', function(done){640 var app = express();641642 app.use(function(req, res){643 res.sendfile('test/fixtures/foo/../user.html');644 });645646 request(app)647 .get('/')648 .expect(403, done);649 })650651 it('should allow ../ when "root" is set', function(done){652 var app = express();653654 app.use(function(req, res){655 res.sendfile('foo/../user.html', { root: 'test/fixtures' });656 });657658 request(app)659 .get('/')660 .expect(200, done);661 })662663 it('should disallow requesting out of "root"', function(done){664 var app = express();665666 app.use(function(req, res){667 res.sendfile('foo/../../user.html', { root: 'test/fixtures' });668 });669670 request(app)671 .get('/')672 .expect(403, done);673 })674675 it('should next(404) when not found', function(done){676 var app = express()677 , calls = 0;678679 app.use(function(req, res){680 res.sendfile('user.html');681 });682683 app.use(function(req, res){684 assert(0, 'this should not be called');685 });686687 app.use(function(err, req, res, next){688 ++calls;689 next(err);690 });691692 request(app)693 .get('/')694 .end(function(err, res){695 res.statusCode.should.equal(404);696 calls.should.equal(1);697 done();698 });699 })700701 describe('with non-GET', function(){702 it('should still serve', function(done){703 var app = express()704 , calls = 0;705706 app.use(function(req, res){707 res.sendfile(__dirname + '/fixtures/name.txt');708 });709710 request(app)711 .get('/')712 .expect('tobi', done);713 })714 })715 })716 })717})718719function createApp(path, options, fn) {720 var app = express();721722 app.use(function (req, res) {723 res.sendFile(path, options, fn);724 });725726 return app;727}