Skip to content
Myr

res.sendFile.js

This file

Path

test/res.sendFile.js

755 lines · 18.8 KiB · JavaScript

Last changed by

tests: use path.join instead of concatenationchainhelenbefore adoptioneece385

History of this file

Take 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 "cacheControl" option', function () {120      it('should enable cacheControl by default', function (done) {121        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))122123        request(app)124        .get('/')125        .expect('Cache-Control', 'public, max-age=0')126        .expect(200, done)127      })128129      it('should accept cacheControl option', function (done) {130        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { cacheControl: false })131132        request(app)133        .get('/')134        .expect(utils.shouldNotHaveHeader('Cache-Control'))135        .expect(200, done)136      })137    })138139    describe('with "dotfiles" option', function () {140      it('should not serve dotfiles by default', function (done) {141        var app = createApp(path.resolve(__dirname, 'fixtures/.name'));142143        request(app)144        .get('/')145        .expect(404, done);146      });147148      it('should accept dotfiles option', function(done){149        var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });150151        request(app)152        .get('/')153        .expect(200, 'tobi', done);154      });155    });156157    describe('with "headers" option', function () {158      it('should accept headers option', function (done) {159        var headers = {160          'x-success': 'sent',161          'x-other': 'done'162        };163        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });164165        request(app)166        .get('/')167        .expect('x-success', 'sent')168        .expect('x-other', 'done')169        .expect(200, done);170      });171172      it('should ignore headers option on 404', function (done) {173        var headers = { 'x-success': 'sent' };174        var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });175176        request(app)177        .get('/')178        .expect(utils.shouldNotHaveHeader('X-Success'))179        .expect(404, done);180      });181    });182183    describe('with "root" option', function () {184      it('should not transfer relative with without', function (done) {185        var app = createApp('test/fixtures/name.txt');186187        request(app)188        .get('/')189        .expect(500, /must be absolute/, done);190      })191192      it('should serve relative to "root"', function (done) {193        var app = createApp('name.txt', {root: fixtures});194195        request(app)196        .get('/')197        .expect(200, 'tobi', done);198      })199200      it('should disallow requesting out of "root"', function (done) {201        var app = createApp('foo/../../user.html', {root: fixtures});202203        request(app)204        .get('/')205        .expect(403, done);206      })207    })208  })209210  describe('.sendFile(path, fn)', function () {211    it('should invoke the callback when complete', function (done) {212      var cb = after(2, done);213      var app = createApp(path.resolve(fixtures, 'name.txt'), cb);214215      request(app)216      .get('/')217      .expect(200, cb);218    })219220    it('should invoke the callback when client aborts', function (done) {221      var cb = after(1, done);222      var app = express();223224      app.use(function (req, res) {225        setImmediate(function () {226          res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {227            should(err).be.ok;228            err.code.should.equal('ECONNABORTED');229            cb();230          });231        });232        test.abort();233      });234235      var test = request(app).get('/');236      test.expect(200, cb);237    })238239    it('should invoke the callback when client already aborted', function (done) {240      var cb = after(1, done);241      var app = express();242243      app.use(function (req, res) {244        onFinished(res, function () {245          res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {246            should(err).be.ok;247            err.code.should.equal('ECONNABORTED');248            cb();249          });250        });251        test.abort();252      });253254      var test = request(app).get('/');255      test.expect(200, cb);256    })257258    it('should invoke the callback without error when HEAD', function (done) {259      var app = express();260      var cb = after(2, done);261262      app.use(function (req, res) {263        res.sendFile(path.resolve(fixtures, 'name.txt'), cb);264      });265266      request(app)267      .head('/')268      .expect(200, cb);269    });270271    it('should invoke the callback without error when 304', function (done) {272      var app = express();273      var cb = after(3, done);274275      app.use(function (req, res) {276        res.sendFile(path.resolve(fixtures, 'name.txt'), cb);277      });278279      request(app)280      .get('/')281      .expect('ETag', /^(?:W\/)?"[^"]+"$/)282      .expect(200, 'tobi', function (err, res) {283        if (err) return cb(err);284        var etag = res.headers.etag;285        request(app)286        .get('/')287        .set('If-None-Match', etag)288        .expect(304, cb);289      });290    });291292    it('should invoke the callback on 404', function(done){293      var app = express();294295      app.use(function (req, res) {296        res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {297          should(err).be.ok;298          err.status.should.equal(404);299          res.send('got it');300        });301      });302303      request(app)304      .get('/')305      .expect(200, 'got it', done);306    })307  })308309  describe('.sendFile(path, options)', function () {310    it('should pass options to send module', function (done) {311      request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))312      .get('/')313      .expect(200, 'to', done)314    })315  })316317  describe('.sendfile(path, fn)', function(){318    it('should invoke the callback when complete', function(done){319      var app = express();320      var cb = after(2, done);321322      app.use(function(req, res){323        res.sendfile('test/fixtures/user.html', cb)324      });325326      request(app)327      .get('/')328      .expect(200, cb);329    })330331    it('should utilize the same options as express.static()', function(done){332      var app = express();333334      app.use(function(req, res){335        res.sendfile('test/fixtures/user.html', { maxAge: 60000 });336      });337338      request(app)339      .get('/')340      .expect('Cache-Control', 'public, max-age=60')341      .end(done);342    })343344    it('should invoke the callback when client aborts', function (done) {345      var cb = after(1, done);346      var app = express();347348      app.use(function (req, res) {349        setImmediate(function () {350          res.sendfile('test/fixtures/name.txt', function (err) {351            should(err).be.ok;352            err.code.should.equal('ECONNABORTED');353            cb();354          });355        });356        test.abort();357      });358359      var test = request(app).get('/');360      test.expect(200, cb);361    })362363    it('should invoke the callback when client already aborted', function (done) {364      var cb = after(1, done);365      var app = express();366367      app.use(function (req, res) {368        onFinished(res, function () {369          res.sendfile('test/fixtures/name.txt', function (err) {370            should(err).be.ok;371            err.code.should.equal('ECONNABORTED');372            cb();373          });374        });375        test.abort();376      });377378      var test = request(app).get('/');379      test.expect(200, cb);380    })381382    it('should invoke the callback without error when HEAD', function (done) {383      var app = express();384      var cb = after(2, done);385386      app.use(function (req, res) {387        res.sendfile('test/fixtures/name.txt', cb);388      });389390      request(app)391      .head('/')392      .expect(200, cb);393    });394395    it('should invoke the callback without error when 304', function (done) {396      var app = express();397      var cb = after(3, done);398399      app.use(function (req, res) {400        res.sendfile('test/fixtures/name.txt', cb);401      });402403      request(app)404      .get('/')405      .expect('ETag', /^(?:W\/)?"[^"]+"$/)406      .expect(200, 'tobi', function (err, res) {407        if (err) return cb(err);408        var etag = res.headers.etag;409        request(app)410        .get('/')411        .set('If-None-Match', etag)412        .expect(304, cb);413      });414    });415416    it('should invoke the callback on 404', function(done){417      var app = express();418      var calls = 0;419420      app.use(function(req, res){421        res.sendfile('test/fixtures/nope.html', function(err){422          assert.equal(calls++, 0);423          assert(!res.headersSent);424          res.send(err.message);425        });426      });427428      request(app)429      .get('/')430      .expect(200, /^ENOENT.*?, stat/, done);431    })432433    it('should not override manual content-types', function(done){434      var app = express();435436      app.use(function(req, res){437        res.contentType('txt');438        res.sendfile('test/fixtures/user.html');439      });440441      request(app)442      .get('/')443      .expect('Content-Type', 'text/plain; charset=utf-8')444      .end(done);445    })446447    it('should invoke the callback on 403', function(done){448      var app = express()449        , calls = 0;450451      app.use(function(req, res){452        res.sendfile('test/fixtures/foo/../user.html', function(err){453          assert(!res.headersSent);454          ++calls;455          res.send(err.message);456        });457      });458459      request(app)460      .get('/')461      .expect('Forbidden')462      .expect(200, done);463    })464465    it('should invoke the callback on socket error', function(done){466      var app = express()467        , calls = 0;468469      app.use(function(req, res){470        res.sendfile('test/fixtures/user.html', function(err){471          assert(!res.headersSent);472          req.socket.listeners('error').should.have.length(1); // node's original handler473          done();474        });475476        req.socket.emit('error', new Error('broken!'));477      });478479      request(app)480      .get('/')481      .end(function(){});482    })483  })484485  describe('.sendfile(path)', function(){486    it('should not serve dotfiles', function(done){487      var app = express();488489      app.use(function(req, res){490        res.sendfile('test/fixtures/.name');491      });492493      request(app)494      .get('/')495      .expect(404, done);496    })497498    it('should accept dotfiles option', function(done){499      var app = express();500501      app.use(function(req, res){502        res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });503      });504505      request(app)506      .get('/')507      .expect(200, 'tobi', done);508    })509510    it('should accept headers option', function(done){511      var app = express();512      var headers = {513        'x-success': 'sent',514        'x-other': 'done'515      };516517      app.use(function(req, res){518        res.sendfile('test/fixtures/user.html', { headers: headers });519      });520521      request(app)522      .get('/')523      .expect('x-success', 'sent')524      .expect('x-other', 'done')525      .expect(200, done);526    })527528    it('should ignore headers option on 404', function(done){529      var app = express();530      var headers = { 'x-success': 'sent' };531532      app.use(function(req, res){533        res.sendfile('test/fixtures/user.nothing', { headers: headers });534      });535536      request(app)537      .get('/')538        .expect(utils.shouldNotHaveHeader('X-Success'))539        .expect(404, done);540    })541542    it('should transfer a file', function (done) {543      var app = express();544545      app.use(function (req, res) {546        res.sendfile('test/fixtures/name.txt');547      });548549      request(app)550      .get('/')551      .expect(200, 'tobi', done);552    });553554    it('should transfer a directory index file', function (done) {555      var app = express();556557      app.use(function (req, res) {558        res.sendfile('test/fixtures/blog/');559      });560561      request(app)562      .get('/')563      .expect(200, '<b>index</b>', done);564    });565566    it('should 404 for directory without trailing slash', function (done) {567      var app = express();568569      app.use(function (req, res) {570        res.sendfile('test/fixtures/blog');571      });572573      request(app)574      .get('/')575      .expect(404, done);576    });577578    it('should transfer a file with urlencoded name', function (done) {579      var app = express();580581      app.use(function (req, res) {582        res.sendfile('test/fixtures/%25%20of%20dogs.txt');583      });584585      request(app)586      .get('/')587      .expect(200, '20%', done);588    });589590    it('should not error if the client aborts', function (done) {591      var cb = after(1, done);592      var app = express();593594      app.use(function (req, res) {595        setImmediate(function () {596          res.sendfile(path.resolve(fixtures, 'name.txt'));597          cb();598        });599        test.abort();600      });601602      app.use(function (err, req, res, next) {603        err.code.should.be.empty;604        cb();605      });606607      var test = request(app).get('/');608      test.expect(200, cb);609    })610611    describe('with an absolute path', function(){612      it('should transfer the file', function(done){613        var app = express();614615        app.use(function(req, res){616          res.sendfile(path.join(__dirname, '/fixtures/user.html'))617        });618619        request(app)620        .get('/')621        .expect('Content-Type', 'text/html; charset=UTF-8')622        .expect(200, '<p>{{user.name}}</p>', done);623      })624    })625626    describe('with a relative path', function(){627      it('should transfer the file', function(done){628        var app = express();629630        app.use(function(req, res){631          res.sendfile('test/fixtures/user.html');632        });633634        request(app)635        .get('/')636        .expect('Content-Type', 'text/html; charset=UTF-8')637        .expect(200, '<p>{{user.name}}</p>', done);638      })639640      it('should serve relative to "root"', function(done){641        var app = express();642643        app.use(function(req, res){644          res.sendfile('user.html', { root: 'test/fixtures/' });645        });646647        request(app)648        .get('/')649        .expect('Content-Type', 'text/html; charset=UTF-8')650        .expect(200, '<p>{{user.name}}</p>', done);651      })652653      it('should consider ../ malicious when "root" is not set', function(done){654        var app = express();655656        app.use(function(req, res){657          res.sendfile('test/fixtures/foo/../user.html');658        });659660        request(app)661        .get('/')662        .expect(403, done);663      })664665      it('should allow ../ when "root" is set', function(done){666        var app = express();667668        app.use(function(req, res){669          res.sendfile('foo/../user.html', { root: 'test/fixtures' });670        });671672        request(app)673        .get('/')674        .expect(200, done);675      })676677      it('should disallow requesting out of "root"', function(done){678        var app = express();679680        app.use(function(req, res){681          res.sendfile('foo/../../user.html', { root: 'test/fixtures' });682        });683684        request(app)685        .get('/')686        .expect(403, done);687      })688689      it('should next(404) when not found', function(done){690        var app = express()691          , calls = 0;692693        app.use(function(req, res){694          res.sendfile('user.html');695        });696697        app.use(function(req, res){698          assert(0, 'this should not be called');699        });700701        app.use(function(err, req, res, next){702          ++calls;703          next(err);704        });705706        request(app)707        .get('/')708        .end(function(err, res){709          res.statusCode.should.equal(404);710          calls.should.equal(1);711          done();712        });713      })714715      describe('with non-GET', function(){716        it('should still serve', function(done){717          var app = express()718            , calls = 0;719720          app.use(function(req, res){721            res.sendfile(path.join(__dirname, '/fixtures/name.txt'))722          });723724          request(app)725          .get('/')726          .expect('tobi', done);727        })728      })729    })730  })731})732733describe('.sendfile(path, options)', function () {734  it('should pass options to send module', function (done) {735    var app = express()736737    app.use(function (req, res) {738      res.sendfile(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 })739    })740741    request(app)742      .get('/')743      .expect(200, 'to', done)744  })745})746747function createApp(path, options, fn) {748  var app = express();749750  app.use(function (req, res) {751    res.sendFile(path, options, fn);752  });753754  return app;755}