res.sendFile.js
This file
Path
test/res.sendFile.js
713 lines · 17.6 KiB · JavaScript
Last changed by
tests: use supertest to check response header valuesDouglas Christopher Wilsonbefore adoption12bc16e
History of this fileTake it
12var after = require('after');3var express = require('../')4 , request = require('supertest')5 , assert = require('assert');6var onFinished = require('on-finished');7var path = require('path');8var should = require('should');9var fixtures = path.join(__dirname, 'fixtures');10var utils = require('./support/utils');1112describe('res', function(){13 describe('.sendFile(path)', function () {14 it('should error missing path', function (done) {15 var app = createApp();1617 request(app)18 .get('/')19 .expect(500, /path.*required/, done);20 });2122 it('should transfer a file', function (done) {23 var app = createApp(path.resolve(fixtures, 'name.txt'));2425 request(app)26 .get('/')27 .expect(200, 'tobi', done);28 });2930 it('should transfer a file with special characters in string', function (done) {31 var app = createApp(path.resolve(fixtures, '% of dogs.txt'));3233 request(app)34 .get('/')35 .expect(200, '20%', done);36 });3738 it('should include ETag', function (done) {39 var app = createApp(path.resolve(fixtures, 'name.txt'));4041 request(app)42 .get('/')43 .expect('ETag', /^(?:W\/)?"[^"]+"$/)44 .expect(200, 'tobi', done);45 });4647 it('should 304 when ETag matches', function (done) {48 var app = createApp(path.resolve(fixtures, 'name.txt'));4950 request(app)51 .get('/')52 .expect('ETag', /^(?:W\/)?"[^"]+"$/)53 .expect(200, 'tobi', function (err, res) {54 if (err) return done(err);55 var etag = res.headers.etag;56 request(app)57 .get('/')58 .set('If-None-Match', etag)59 .expect(304, done);60 });61 });6263 it('should 404 for directory', function (done) {64 var app = createApp(path.resolve(fixtures, 'blog'));6566 request(app)67 .get('/')68 .expect(404, done);69 });7071 it('should 404 when not found', function (done) {72 var app = createApp(path.resolve(fixtures, 'does-no-exist'));7374 app.use(function (req, res) {75 res.statusCode = 200;76 res.send('no!');77 });7879 request(app)80 .get('/')81 .expect(404, done);82 });8384 it('should not override manual content-types', function (done) {85 var app = express();8687 app.use(function (req, res) {88 res.contentType('application/x-bogus');89 res.sendFile(path.resolve(fixtures, 'name.txt'));90 });9192 request(app)93 .get('/')94 .expect('Content-Type', 'application/x-bogus')95 .end(done);96 })9798 it('should not error if the client aborts', function (done) {99 var cb = after(1, done);100 var app = express();101102 app.use(function (req, res) {103 setImmediate(function () {104 res.sendFile(path.resolve(fixtures, 'name.txt'));105 cb();106 });107 test.abort();108 });109110 app.use(function (err, req, res, next) {111 err.code.should.be.empty;112 cb();113 });114115 var test = request(app).get('/');116 test.expect(200, cb);117 })118119 describe('with "dotfiles" option', function () {120 it('should not serve dotfiles by default', function (done) {121 var app = createApp(path.resolve(__dirname, 'fixtures/.name'));122123 request(app)124 .get('/')125 .expect(404, done);126 });127128 it('should accept dotfiles option', function(done){129 var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });130131 request(app)132 .get('/')133 .expect(200, 'tobi', done);134 });135 });136137 describe('with "headers" option', function () {138 it('should accept headers option', function (done) {139 var headers = {140 'x-success': 'sent',141 'x-other': 'done'142 };143 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });144145 request(app)146 .get('/')147 .expect('x-success', 'sent')148 .expect('x-other', 'done')149 .expect(200, done);150 });151152 it('should ignore headers option on 404', function (done) {153 var headers = { 'x-success': 'sent' };154 var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });155156 request(app)157 .get('/')158 .expect(utils.shouldNotHaveHeader('X-Success'))159 .expect(404, done);160 });161 });162163 describe('with "root" option', function () {164 it('should not transfer relative with without', function (done) {165 var app = createApp('test/fixtures/name.txt');166167 request(app)168 .get('/')169 .expect(500, /must be absolute/, done);170 })171172 it('should serve relative to "root"', function (done) {173 var app = createApp('name.txt', {root: fixtures});174175 request(app)176 .get('/')177 .expect(200, 'tobi', done);178 })179180 it('should disallow requesting out of "root"', function (done) {181 var app = createApp('foo/../../user.html', {root: fixtures});182183 request(app)184 .get('/')185 .expect(403, done);186 })187 })188 })189190 describe('.sendFile(path, fn)', function () {191 it('should invoke the callback when complete', function (done) {192 var cb = after(2, done);193 var app = createApp(path.resolve(fixtures, 'name.txt'), cb);194195 request(app)196 .get('/')197 .expect(200, cb);198 })199200 it('should invoke the callback when client aborts', function (done) {201 var cb = after(1, done);202 var app = express();203204 app.use(function (req, res) {205 setImmediate(function () {206 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {207 should(err).be.ok;208 err.code.should.equal('ECONNABORTED');209 cb();210 });211 });212 test.abort();213 });214215 var test = request(app).get('/');216 test.expect(200, cb);217 })218219 it('should invoke the callback when client already aborted', function (done) {220 var cb = after(1, done);221 var app = express();222223 app.use(function (req, res) {224 onFinished(res, function () {225 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {226 should(err).be.ok;227 err.code.should.equal('ECONNABORTED');228 cb();229 });230 });231 test.abort();232 });233234 var test = request(app).get('/');235 test.expect(200, cb);236 })237238 it('should invoke the callback without error when HEAD', function (done) {239 var app = express();240 var cb = after(2, done);241242 app.use(function (req, res) {243 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);244 });245246 request(app)247 .head('/')248 .expect(200, cb);249 });250251 it('should invoke the callback without error when 304', function (done) {252 var app = express();253 var cb = after(3, done);254255 app.use(function (req, res) {256 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);257 });258259 request(app)260 .get('/')261 .expect('ETag', /^(?:W\/)?"[^"]+"$/)262 .expect(200, 'tobi', function (err, res) {263 if (err) return cb(err);264 var etag = res.headers.etag;265 request(app)266 .get('/')267 .set('If-None-Match', etag)268 .expect(304, cb);269 });270 });271272 it('should invoke the callback on 404', function(done){273 var app = express();274275 app.use(function (req, res) {276 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {277 should(err).be.ok;278 err.status.should.equal(404);279 res.send('got it');280 });281 });282283 request(app)284 .get('/')285 .expect(200, 'got it', done);286 })287 })288289 describe('.sendfile(path, fn)', function(){290 it('should invoke the callback when complete', function(done){291 var app = express();292 var cb = after(2, done);293294 app.use(function(req, res){295 res.sendfile('test/fixtures/user.html', cb)296 });297298 request(app)299 .get('/')300 .expect(200, cb);301 })302303 it('should utilize the same options as express.static()', function(done){304 var app = express();305306 app.use(function(req, res){307 res.sendfile('test/fixtures/user.html', { maxAge: 60000 });308 });309310 request(app)311 .get('/')312 .expect('Cache-Control', 'public, max-age=60')313 .end(done);314 })315316 it('should invoke the callback when client aborts', function (done) {317 var cb = after(1, done);318 var app = express();319320 app.use(function (req, res) {321 setImmediate(function () {322 res.sendfile('test/fixtures/name.txt', function (err) {323 should(err).be.ok;324 err.code.should.equal('ECONNABORTED');325 cb();326 });327 });328 test.abort();329 });330331 var test = request(app).get('/');332 test.expect(200, cb);333 })334335 it('should invoke the callback when client already aborted', function (done) {336 var cb = after(1, done);337 var app = express();338339 app.use(function (req, res) {340 onFinished(res, function () {341 res.sendfile('test/fixtures/name.txt', function (err) {342 should(err).be.ok;343 err.code.should.equal('ECONNABORTED');344 cb();345 });346 });347 test.abort();348 });349350 var test = request(app).get('/');351 test.expect(200, cb);352 })353354 it('should invoke the callback without error when HEAD', function (done) {355 var app = express();356 var cb = after(2, done);357358 app.use(function (req, res) {359 res.sendfile('test/fixtures/name.txt', cb);360 });361362 request(app)363 .head('/')364 .expect(200, cb);365 });366367 it('should invoke the callback without error when 304', function (done) {368 var app = express();369 var cb = after(3, done);370371 app.use(function (req, res) {372 res.sendfile('test/fixtures/name.txt', cb);373 });374375 request(app)376 .get('/')377 .expect('ETag', /^(?:W\/)?"[^"]+"$/)378 .expect(200, 'tobi', function (err, res) {379 if (err) return cb(err);380 var etag = res.headers.etag;381 request(app)382 .get('/')383 .set('If-None-Match', etag)384 .expect(304, cb);385 });386 });387388 it('should invoke the callback on 404', function(done){389 var app = express();390 var calls = 0;391392 app.use(function(req, res){393 res.sendfile('test/fixtures/nope.html', function(err){394 assert.equal(calls++, 0);395 assert(!res.headersSent);396 res.send(err.message);397 });398 });399400 request(app)401 .get('/')402 .expect(200, /^ENOENT.*?, stat/, done);403 })404405 it('should not override manual content-types', function(done){406 var app = express();407408 app.use(function(req, res){409 res.contentType('txt');410 res.sendfile('test/fixtures/user.html');411 });412413 request(app)414 .get('/')415 .expect('Content-Type', 'text/plain; charset=utf-8')416 .end(done);417 })418419 it('should invoke the callback on 403', function(done){420 var app = express()421 , calls = 0;422423 app.use(function(req, res){424 res.sendfile('test/fixtures/foo/../user.html', function(err){425 assert(!res.headersSent);426 ++calls;427 res.send(err.message);428 });429 });430431 request(app)432 .get('/')433 .expect('Forbidden')434 .expect(200, done);435 })436437 it('should invoke the callback on socket error', function(done){438 var app = express()439 , calls = 0;440441 app.use(function(req, res){442 res.sendfile('test/fixtures/user.html', function(err){443 assert(!res.headersSent);444 req.socket.listeners('error').should.have.length(1); // node's original handler445 done();446 });447448 req.socket.emit('error', new Error('broken!'));449 });450451 request(app)452 .get('/')453 .end(function(){});454 })455 })456457 describe('.sendfile(path)', function(){458 it('should not serve dotfiles', function(done){459 var app = express();460461 app.use(function(req, res){462 res.sendfile('test/fixtures/.name');463 });464465 request(app)466 .get('/')467 .expect(404, done);468 })469470 it('should accept dotfiles option', function(done){471 var app = express();472473 app.use(function(req, res){474 res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });475 });476477 request(app)478 .get('/')479 .expect(200, 'tobi', done);480 })481482 it('should accept headers option', function(done){483 var app = express();484 var headers = {485 'x-success': 'sent',486 'x-other': 'done'487 };488489 app.use(function(req, res){490 res.sendfile('test/fixtures/user.html', { headers: headers });491 });492493 request(app)494 .get('/')495 .expect('x-success', 'sent')496 .expect('x-other', 'done')497 .expect(200, done);498 })499500 it('should ignore headers option on 404', function(done){501 var app = express();502 var headers = { 'x-success': 'sent' };503504 app.use(function(req, res){505 res.sendfile('test/fixtures/user.nothing', { headers: headers });506 });507508 request(app)509 .get('/')510 .expect(utils.shouldNotHaveHeader('X-Success'))511 .expect(404, done);512 })513514 it('should transfer a file', function (done) {515 var app = express();516517 app.use(function (req, res) {518 res.sendfile('test/fixtures/name.txt');519 });520521 request(app)522 .get('/')523 .expect(200, 'tobi', done);524 });525526 it('should transfer a directory index file', function (done) {527 var app = express();528529 app.use(function (req, res) {530 res.sendfile('test/fixtures/blog/');531 });532533 request(app)534 .get('/')535 .expect(200, '<b>index</b>', done);536 });537538 it('should 404 for directory without trailing slash', function (done) {539 var app = express();540541 app.use(function (req, res) {542 res.sendfile('test/fixtures/blog');543 });544545 request(app)546 .get('/')547 .expect(404, done);548 });549550 it('should transfer a file with urlencoded name', function (done) {551 var app = express();552553 app.use(function (req, res) {554 res.sendfile('test/fixtures/%25%20of%20dogs.txt');555 });556557 request(app)558 .get('/')559 .expect(200, '20%', done);560 });561562 it('should not error if the client aborts', function (done) {563 var cb = after(1, done);564 var app = express();565566 app.use(function (req, res) {567 setImmediate(function () {568 res.sendfile(path.resolve(fixtures, 'name.txt'));569 cb();570 });571 test.abort();572 });573574 app.use(function (err, req, res, next) {575 err.code.should.be.empty;576 cb();577 });578579 var test = request(app).get('/');580 test.expect(200, cb);581 })582583 describe('with an absolute path', function(){584 it('should transfer the file', function(done){585 var app = express();586587 app.use(function(req, res){588 res.sendfile(__dirname + '/fixtures/user.html');589 });590591 request(app)592 .get('/')593 .expect('Content-Type', 'text/html; charset=UTF-8')594 .expect(200, '<p>{{user.name}}</p>', done);595 })596 })597598 describe('with a relative path', function(){599 it('should transfer the file', function(done){600 var app = express();601602 app.use(function(req, res){603 res.sendfile('test/fixtures/user.html');604 });605606 request(app)607 .get('/')608 .expect('Content-Type', 'text/html; charset=UTF-8')609 .expect(200, '<p>{{user.name}}</p>', done);610 })611612 it('should serve relative to "root"', function(done){613 var app = express();614615 app.use(function(req, res){616 res.sendfile('user.html', { root: 'test/fixtures/' });617 });618619 request(app)620 .get('/')621 .expect('Content-Type', 'text/html; charset=UTF-8')622 .expect(200, '<p>{{user.name}}</p>', done);623 })624625 it('should consider ../ malicious when "root" is not set', function(done){626 var app = express();627628 app.use(function(req, res){629 res.sendfile('test/fixtures/foo/../user.html');630 });631632 request(app)633 .get('/')634 .expect(403, done);635 })636637 it('should allow ../ when "root" is set', function(done){638 var app = express();639640 app.use(function(req, res){641 res.sendfile('foo/../user.html', { root: 'test/fixtures' });642 });643644 request(app)645 .get('/')646 .expect(200, done);647 })648649 it('should disallow requesting out of "root"', function(done){650 var app = express();651652 app.use(function(req, res){653 res.sendfile('foo/../../user.html', { root: 'test/fixtures' });654 });655656 request(app)657 .get('/')658 .expect(403, done);659 })660661 it('should next(404) when not found', function(done){662 var app = express()663 , calls = 0;664665 app.use(function(req, res){666 res.sendfile('user.html');667 });668669 app.use(function(req, res){670 assert(0, 'this should not be called');671 });672673 app.use(function(err, req, res, next){674 ++calls;675 next(err);676 });677678 request(app)679 .get('/')680 .end(function(err, res){681 res.statusCode.should.equal(404);682 calls.should.equal(1);683 done();684 });685 })686687 describe('with non-GET', function(){688 it('should still serve', function(done){689 var app = express()690 , calls = 0;691692 app.use(function(req, res){693 res.sendfile(__dirname + '/fixtures/name.txt');694 });695696 request(app)697 .get('/')698 .expect('tobi', done);699 })700 })701 })702 })703})704705function createApp(path, options, fn) {706 var app = express();707708 app.use(function (req, res) {709 res.sendFile(path, options, fn);710 });711712 return app;713}