Skip to content
Myr

res.sendFile.js

This file

Path

test/res.sendFile.js

795 lines · 20.1 KiB · JavaScript

Last changed by

tests: run mocha with --no-exit to detect hangsDouglas Christopher Wilsonbefore adoptionde129c2

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          server.close(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 server = app.listen()116      var test = request(server).get('/')117      test.expect(200, cb);118    })119120    describe('with "cacheControl" option', function () {121      it('should enable cacheControl by default', function (done) {122        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))123124        request(app)125        .get('/')126        .expect('Cache-Control', 'public, max-age=0')127        .expect(200, done)128      })129130      it('should accept cacheControl option', function (done) {131        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { cacheControl: false })132133        request(app)134        .get('/')135        .expect(utils.shouldNotHaveHeader('Cache-Control'))136        .expect(200, done)137      })138    })139140    describe('with "dotfiles" option', function () {141      it('should not serve dotfiles by default', function (done) {142        var app = createApp(path.resolve(__dirname, 'fixtures/.name'));143144        request(app)145        .get('/')146        .expect(404, done);147      });148149      it('should accept dotfiles option', function(done){150        var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });151152        request(app)153        .get('/')154        .expect(200, 'tobi', done);155      });156    });157158    describe('with "headers" option', function () {159      it('should accept headers option', function (done) {160        var headers = {161          'x-success': 'sent',162          'x-other': 'done'163        };164        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });165166        request(app)167        .get('/')168        .expect('x-success', 'sent')169        .expect('x-other', 'done')170        .expect(200, done);171      });172173      it('should ignore headers option on 404', function (done) {174        var headers = { 'x-success': 'sent' };175        var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });176177        request(app)178        .get('/')179        .expect(utils.shouldNotHaveHeader('X-Success'))180        .expect(404, done);181      });182    });183184    describe('with "immutable" option', function () {185      it('should add immutable cache-control directive', function (done) {186        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {187          immutable: true,188          maxAge: '4h'189        })190191        request(app)192        .get('/')193        .expect('Cache-Control', 'public, max-age=14400, immutable')194        .expect(200, done)195      })196    })197198    describe('with "maxAge" option', function () {199      it('should set cache-control max-age from number', function (done) {200        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {201          maxAge: 14400000202        })203204        request(app)205        .get('/')206        .expect('Cache-Control', 'public, max-age=14400')207        .expect(200, done)208      })209210      it('should set cache-control max-age from string', function (done) {211        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {212          maxAge: '4h'213        })214215        request(app)216        .get('/')217        .expect('Cache-Control', 'public, max-age=14400')218        .expect(200, done)219      })220    })221222    describe('with "root" option', function () {223      it('should not transfer relative with without', function (done) {224        var app = createApp('test/fixtures/name.txt');225226        request(app)227        .get('/')228        .expect(500, /must be absolute/, done);229      })230231      it('should serve relative to "root"', function (done) {232        var app = createApp('name.txt', {root: fixtures});233234        request(app)235        .get('/')236        .expect(200, 'tobi', done);237      })238239      it('should disallow requesting out of "root"', function (done) {240        var app = createApp('foo/../../user.html', {root: fixtures});241242        request(app)243        .get('/')244        .expect(403, done);245      })246    })247  })248249  describe('.sendFile(path, fn)', function () {250    it('should invoke the callback when complete', function (done) {251      var cb = after(2, done);252      var app = createApp(path.resolve(fixtures, 'name.txt'), cb);253254      request(app)255      .get('/')256      .expect(200, cb);257    })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(path.resolve(fixtures, 'name.txt'), function (err) {266            should(err).be.ok()267            err.code.should.equal('ECONNABORTED');268            server.close(cb)269          });270        });271        test.abort();272      });273274      var server = app.listen()275      var test = request(server).get('/')276      test.expect(200, cb);277    })278279    it('should invoke the callback when client already aborted', function (done) {280      var cb = after(1, done);281      var app = express();282283      app.use(function (req, res) {284        onFinished(res, function () {285          res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {286            should(err).be.ok()287            err.code.should.equal('ECONNABORTED');288            server.close(cb)289          });290        });291        test.abort();292      });293294      var server = app.listen()295      var test = request(server).get('/')296      test.expect(200, cb);297    })298299    it('should invoke the callback without error when HEAD', function (done) {300      var app = express();301      var cb = after(2, done);302303      app.use(function (req, res) {304        res.sendFile(path.resolve(fixtures, 'name.txt'), cb);305      });306307      request(app)308      .head('/')309      .expect(200, cb);310    });311312    it('should invoke the callback without error when 304', function (done) {313      var app = express();314      var cb = after(3, done);315316      app.use(function (req, res) {317        res.sendFile(path.resolve(fixtures, 'name.txt'), cb);318      });319320      request(app)321      .get('/')322      .expect('ETag', /^(?:W\/)?"[^"]+"$/)323      .expect(200, 'tobi', function (err, res) {324        if (err) return cb(err);325        var etag = res.headers.etag;326        request(app)327        .get('/')328        .set('If-None-Match', etag)329        .expect(304, cb);330      });331    });332333    it('should invoke the callback on 404', function(done){334      var app = express();335336      app.use(function (req, res) {337        res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {338          should(err).be.ok()339          err.status.should.equal(404);340          res.send('got it');341        });342      });343344      request(app)345      .get('/')346      .expect(200, 'got it', done);347    })348  })349350  describe('.sendFile(path, options)', function () {351    it('should pass options to send module', function (done) {352      request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))353      .get('/')354      .expect(200, 'to', done)355    })356  })357358  describe('.sendfile(path, fn)', function(){359    it('should invoke the callback when complete', function(done){360      var app = express();361      var cb = after(2, done);362363      app.use(function(req, res){364        res.sendfile('test/fixtures/user.html', cb)365      });366367      request(app)368      .get('/')369      .expect(200, cb);370    })371372    it('should utilize the same options as express.static()', function(done){373      var app = express();374375      app.use(function(req, res){376        res.sendfile('test/fixtures/user.html', { maxAge: 60000 });377      });378379      request(app)380      .get('/')381      .expect('Cache-Control', 'public, max-age=60')382      .end(done);383    })384385    it('should invoke the callback when client aborts', function (done) {386      var cb = after(1, done);387      var app = express();388389      app.use(function (req, res) {390        setImmediate(function () {391          res.sendfile('test/fixtures/name.txt', function (err) {392            should(err).be.ok()393            err.code.should.equal('ECONNABORTED');394            server.close(cb)395          });396        });397        test.abort();398      });399400      var server = app.listen()401      var test = request(server).get('/')402      test.expect(200, cb);403    })404405    it('should invoke the callback when client already aborted', function (done) {406      var cb = after(1, done);407      var app = express();408409      app.use(function (req, res) {410        onFinished(res, function () {411          res.sendfile('test/fixtures/name.txt', function (err) {412            should(err).be.ok()413            err.code.should.equal('ECONNABORTED');414            server.close(cb)415          });416        });417        test.abort();418      });419420      var server = app.listen()421      var test = request(server).get('/')422      test.expect(200, cb);423    })424425    it('should invoke the callback without error when HEAD', function (done) {426      var app = express();427      var cb = after(2, done);428429      app.use(function (req, res) {430        res.sendfile('test/fixtures/name.txt', cb);431      });432433      request(app)434      .head('/')435      .expect(200, cb);436    });437438    it('should invoke the callback without error when 304', function (done) {439      var app = express();440      var cb = after(3, done);441442      app.use(function (req, res) {443        res.sendfile('test/fixtures/name.txt', cb);444      });445446      request(app)447      .get('/')448      .expect('ETag', /^(?:W\/)?"[^"]+"$/)449      .expect(200, 'tobi', function (err, res) {450        if (err) return cb(err);451        var etag = res.headers.etag;452        request(app)453        .get('/')454        .set('If-None-Match', etag)455        .expect(304, cb);456      });457    });458459    it('should invoke the callback on 404', function(done){460      var app = express();461      var calls = 0;462463      app.use(function(req, res){464        res.sendfile('test/fixtures/nope.html', function(err){465          assert.equal(calls++, 0);466          assert(!res.headersSent);467          res.send(err.message);468        });469      });470471      request(app)472      .get('/')473      .expect(200, /^ENOENT.*?, stat/, done);474    })475476    it('should not override manual content-types', function(done){477      var app = express();478479      app.use(function(req, res){480        res.contentType('txt');481        res.sendfile('test/fixtures/user.html');482      });483484      request(app)485      .get('/')486      .expect('Content-Type', 'text/plain; charset=utf-8')487      .end(done);488    })489490    it('should invoke the callback on 403', function(done){491      var app = express()492493      app.use(function(req, res){494        res.sendfile('test/fixtures/foo/../user.html', function(err){495          assert(!res.headersSent);496          res.send(err.message);497        });498      });499500      request(app)501      .get('/')502      .expect('Forbidden')503      .expect(200, done);504    })505506    it('should invoke the callback on socket error', function(done){507      var app = express()508509      app.use(function(req, res){510        res.sendfile('test/fixtures/user.html', function(err){511          assert(!res.headersSent);512          req.socket.listeners('error').should.have.length(1); // node's original handler513          done();514        });515516        req.socket.emit('error', new Error('broken!'));517      });518519      request(app)520      .get('/')521      .end(function(){});522    })523  })524525  describe('.sendfile(path)', function(){526    it('should not serve dotfiles', function(done){527      var app = express();528529      app.use(function(req, res){530        res.sendfile('test/fixtures/.name');531      });532533      request(app)534      .get('/')535      .expect(404, done);536    })537538    it('should accept dotfiles option', function(done){539      var app = express();540541      app.use(function(req, res){542        res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });543      });544545      request(app)546      .get('/')547      .expect(200, 'tobi', done);548    })549550    it('should accept headers option', function(done){551      var app = express();552      var headers = {553        'x-success': 'sent',554        'x-other': 'done'555      };556557      app.use(function(req, res){558        res.sendfile('test/fixtures/user.html', { headers: headers });559      });560561      request(app)562      .get('/')563      .expect('x-success', 'sent')564      .expect('x-other', 'done')565      .expect(200, done);566    })567568    it('should ignore headers option on 404', function(done){569      var app = express();570      var headers = { 'x-success': 'sent' };571572      app.use(function(req, res){573        res.sendfile('test/fixtures/user.nothing', { headers: headers });574      });575576      request(app)577      .get('/')578        .expect(utils.shouldNotHaveHeader('X-Success'))579        .expect(404, done);580    })581582    it('should transfer a file', function (done) {583      var app = express();584585      app.use(function (req, res) {586        res.sendfile('test/fixtures/name.txt');587      });588589      request(app)590      .get('/')591      .expect(200, 'tobi', done);592    });593594    it('should transfer a directory index file', function (done) {595      var app = express();596597      app.use(function (req, res) {598        res.sendfile('test/fixtures/blog/');599      });600601      request(app)602      .get('/')603      .expect(200, '<b>index</b>', done);604    });605606    it('should 404 for directory without trailing slash', function (done) {607      var app = express();608609      app.use(function (req, res) {610        res.sendfile('test/fixtures/blog');611      });612613      request(app)614      .get('/')615      .expect(404, done);616    });617618    it('should transfer a file with urlencoded name', function (done) {619      var app = express();620621      app.use(function (req, res) {622        res.sendfile('test/fixtures/%25%20of%20dogs.txt');623      });624625      request(app)626      .get('/')627      .expect(200, '20%', done);628    });629630    it('should not error if the client aborts', function (done) {631      var cb = after(1, done);632      var app = express();633634      app.use(function (req, res) {635        setImmediate(function () {636          res.sendfile(path.resolve(fixtures, 'name.txt'));637          server.close(cb)638        });639        test.abort();640      });641642      app.use(function (err, req, res, next) {643        err.code.should.be.empty()644        cb();645      });646647      var server = app.listen()648      var test = request(server).get('/')649      test.expect(200, cb);650    })651652    describe('with an absolute path', function(){653      it('should transfer the file', function(done){654        var app = express();655656        app.use(function(req, res){657          res.sendfile(path.join(__dirname, '/fixtures/user.html'))658        });659660        request(app)661        .get('/')662        .expect('Content-Type', 'text/html; charset=UTF-8')663        .expect(200, '<p>{{user.name}}</p>', done);664      })665    })666667    describe('with a relative path', function(){668      it('should transfer the file', function(done){669        var app = express();670671        app.use(function(req, res){672          res.sendfile('test/fixtures/user.html');673        });674675        request(app)676        .get('/')677        .expect('Content-Type', 'text/html; charset=UTF-8')678        .expect(200, '<p>{{user.name}}</p>', done);679      })680681      it('should serve relative to "root"', function(done){682        var app = express();683684        app.use(function(req, res){685          res.sendfile('user.html', { root: 'test/fixtures/' });686        });687688        request(app)689        .get('/')690        .expect('Content-Type', 'text/html; charset=UTF-8')691        .expect(200, '<p>{{user.name}}</p>', done);692      })693694      it('should consider ../ malicious when "root" is not set', function(done){695        var app = express();696697        app.use(function(req, res){698          res.sendfile('test/fixtures/foo/../user.html');699        });700701        request(app)702        .get('/')703        .expect(403, done);704      })705706      it('should allow ../ when "root" is set', function(done){707        var app = express();708709        app.use(function(req, res){710          res.sendfile('foo/../user.html', { root: 'test/fixtures' });711        });712713        request(app)714        .get('/')715        .expect(200, done);716      })717718      it('should disallow requesting out of "root"', function(done){719        var app = express();720721        app.use(function(req, res){722          res.sendfile('foo/../../user.html', { root: 'test/fixtures' });723        });724725        request(app)726        .get('/')727        .expect(403, done);728      })729730      it('should next(404) when not found', function(done){731        var app = express()732          , calls = 0;733734        app.use(function(req, res){735          res.sendfile('user.html');736        });737738        app.use(function(req, res){739          assert(0, 'this should not be called');740        });741742        app.use(function(err, req, res, next){743          ++calls;744          next(err);745        });746747        request(app)748        .get('/')749        .end(function(err, res){750          res.statusCode.should.equal(404);751          calls.should.equal(1);752          done();753        });754      })755756      describe('with non-GET', function(){757        it('should still serve', function(done){758          var app = express()759760          app.use(function(req, res){761            res.sendfile(path.join(__dirname, '/fixtures/name.txt'))762          });763764          request(app)765          .get('/')766          .expect('tobi', done);767        })768      })769    })770  })771})772773describe('.sendfile(path, options)', function () {774  it('should pass options to send module', function (done) {775    var app = express()776777    app.use(function (req, res) {778      res.sendfile(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 })779    })780781    request(app)782      .get('/')783      .expect(200, 'to', done)784  })785})786787function createApp(path, options, fn) {788  var app = express();789790  app.use(function (req, res) {791    res.sendFile(path, options, fn);792  });793794  return app;795}