Skip to content
Myr

res.sendFile.js

This file

Path

test/res.sendFile.js

735 lines · 18.2 KiB · JavaScript

Last changed by

deps: send@0.14.1Douglas Christopher Wilsonbefore adoptionfc40702

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 "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, options)', function () {290    it('should pass options to send module', function (done) {291      request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))292      .get('/')293      .expect(200, 'to', done)294    })295  })296297  describe('.sendfile(path, fn)', function(){298    it('should invoke the callback when complete', function(done){299      var app = express();300      var cb = after(2, done);301302      app.use(function(req, res){303        res.sendfile('test/fixtures/user.html', cb)304      });305306      request(app)307      .get('/')308      .expect(200, cb);309    })310311    it('should utilize the same options as express.static()', function(done){312      var app = express();313314      app.use(function(req, res){315        res.sendfile('test/fixtures/user.html', { maxAge: 60000 });316      });317318      request(app)319      .get('/')320      .expect('Cache-Control', 'public, max-age=60')321      .end(done);322    })323324    it('should invoke the callback when client aborts', function (done) {325      var cb = after(1, done);326      var app = express();327328      app.use(function (req, res) {329        setImmediate(function () {330          res.sendfile('test/fixtures/name.txt', function (err) {331            should(err).be.ok;332            err.code.should.equal('ECONNABORTED');333            cb();334          });335        });336        test.abort();337      });338339      var test = request(app).get('/');340      test.expect(200, cb);341    })342343    it('should invoke the callback when client already aborted', function (done) {344      var cb = after(1, done);345      var app = express();346347      app.use(function (req, res) {348        onFinished(res, function () {349          res.sendfile('test/fixtures/name.txt', function (err) {350            should(err).be.ok;351            err.code.should.equal('ECONNABORTED');352            cb();353          });354        });355        test.abort();356      });357358      var test = request(app).get('/');359      test.expect(200, cb);360    })361362    it('should invoke the callback without error when HEAD', function (done) {363      var app = express();364      var cb = after(2, done);365366      app.use(function (req, res) {367        res.sendfile('test/fixtures/name.txt', cb);368      });369370      request(app)371      .head('/')372      .expect(200, cb);373    });374375    it('should invoke the callback without error when 304', function (done) {376      var app = express();377      var cb = after(3, done);378379      app.use(function (req, res) {380        res.sendfile('test/fixtures/name.txt', cb);381      });382383      request(app)384      .get('/')385      .expect('ETag', /^(?:W\/)?"[^"]+"$/)386      .expect(200, 'tobi', function (err, res) {387        if (err) return cb(err);388        var etag = res.headers.etag;389        request(app)390        .get('/')391        .set('If-None-Match', etag)392        .expect(304, cb);393      });394    });395396    it('should invoke the callback on 404', function(done){397      var app = express();398      var calls = 0;399400      app.use(function(req, res){401        res.sendfile('test/fixtures/nope.html', function(err){402          assert.equal(calls++, 0);403          assert(!res.headersSent);404          res.send(err.message);405        });406      });407408      request(app)409      .get('/')410      .expect(200, /^ENOENT.*?, stat/, done);411    })412413    it('should not override manual content-types', function(done){414      var app = express();415416      app.use(function(req, res){417        res.contentType('txt');418        res.sendfile('test/fixtures/user.html');419      });420421      request(app)422      .get('/')423      .expect('Content-Type', 'text/plain; charset=utf-8')424      .end(done);425    })426427    it('should invoke the callback on 403', function(done){428      var app = express()429        , calls = 0;430431      app.use(function(req, res){432        res.sendfile('test/fixtures/foo/../user.html', function(err){433          assert(!res.headersSent);434          ++calls;435          res.send(err.message);436        });437      });438439      request(app)440      .get('/')441      .expect('Forbidden')442      .expect(200, done);443    })444445    it('should invoke the callback on socket error', function(done){446      var app = express()447        , calls = 0;448449      app.use(function(req, res){450        res.sendfile('test/fixtures/user.html', function(err){451          assert(!res.headersSent);452          req.socket.listeners('error').should.have.length(1); // node's original handler453          done();454        });455456        req.socket.emit('error', new Error('broken!'));457      });458459      request(app)460      .get('/')461      .end(function(){});462    })463  })464465  describe('.sendfile(path)', function(){466    it('should not serve dotfiles', function(done){467      var app = express();468469      app.use(function(req, res){470        res.sendfile('test/fixtures/.name');471      });472473      request(app)474      .get('/')475      .expect(404, done);476    })477478    it('should accept dotfiles option', function(done){479      var app = express();480481      app.use(function(req, res){482        res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });483      });484485      request(app)486      .get('/')487      .expect(200, 'tobi', done);488    })489490    it('should accept headers option', function(done){491      var app = express();492      var headers = {493         'x-success': 'sent',494         'x-other': 'done'495      };496497      app.use(function(req, res){498        res.sendfile('test/fixtures/user.html', { headers: headers });499      });500501      request(app)502      .get('/')503      .expect('x-success', 'sent')504      .expect('x-other', 'done')505      .expect(200, done);506    })507508    it('should ignore headers option on 404', function(done){509      var app = express();510      var headers = { 'x-success': 'sent' };511512      app.use(function(req, res){513        res.sendfile('test/fixtures/user.nothing', { headers: headers });514      });515516      request(app)517      .get('/')518        .expect(utils.shouldNotHaveHeader('X-Success'))519        .expect(404, done);520    })521522    it('should transfer a file', function (done) {523      var app = express();524525      app.use(function (req, res) {526        res.sendfile('test/fixtures/name.txt');527      });528529      request(app)530      .get('/')531      .expect(200, 'tobi', done);532    });533534    it('should transfer a directory index file', function (done) {535      var app = express();536537      app.use(function (req, res) {538        res.sendfile('test/fixtures/blog/');539      });540541      request(app)542      .get('/')543      .expect(200, '<b>index</b>', done);544    });545546    it('should 404 for directory without trailing slash', function (done) {547      var app = express();548549      app.use(function (req, res) {550        res.sendfile('test/fixtures/blog');551      });552553      request(app)554      .get('/')555      .expect(404, done);556    });557558    it('should transfer a file with urlencoded name', function (done) {559      var app = express();560561      app.use(function (req, res) {562        res.sendfile('test/fixtures/%25%20of%20dogs.txt');563      });564565      request(app)566      .get('/')567      .expect(200, '20%', done);568    });569570    it('should not error if the client aborts', function (done) {571      var cb = after(1, done);572      var app = express();573574      app.use(function (req, res) {575        setImmediate(function () {576          res.sendfile(path.resolve(fixtures, 'name.txt'));577          cb();578        });579        test.abort();580      });581582      app.use(function (err, req, res, next) {583        err.code.should.be.empty;584        cb();585      });586587      var test = request(app).get('/');588      test.expect(200, cb);589    })590591    describe('with an absolute path', function(){592      it('should transfer the file', function(done){593        var app = express();594595        app.use(function(req, res){596          res.sendfile(__dirname + '/fixtures/user.html');597        });598599        request(app)600        .get('/')601        .expect('Content-Type', 'text/html; charset=UTF-8')602        .expect(200, '<p>{{user.name}}</p>', done);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        .expect('Content-Type', 'text/html; charset=UTF-8')617        .expect(200, '<p>{{user.name}}</p>', done);618      })619620      it('should serve relative to "root"', function(done){621        var app = express();622623        app.use(function(req, res){624          res.sendfile('user.html', { root: 'test/fixtures/' });625        });626627        request(app)628        .get('/')629        .expect('Content-Type', 'text/html; charset=UTF-8')630        .expect(200, '<p>{{user.name}}</p>', done);631      })632633      it('should consider ../ malicious when "root" is not set', function(done){634        var app = express();635636        app.use(function(req, res){637          res.sendfile('test/fixtures/foo/../user.html');638        });639640        request(app)641        .get('/')642        .expect(403, done);643      })644645      it('should allow ../ when "root" is set', function(done){646        var app = express();647648        app.use(function(req, res){649          res.sendfile('foo/../user.html', { root: 'test/fixtures' });650        });651652        request(app)653        .get('/')654        .expect(200, done);655      })656657      it('should disallow requesting out of "root"', function(done){658        var app = express();659660        app.use(function(req, res){661          res.sendfile('foo/../../user.html', { root: 'test/fixtures' });662        });663664        request(app)665        .get('/')666        .expect(403, done);667      })668669      it('should next(404) when not found', function(done){670        var app = express()671          , calls = 0;672673        app.use(function(req, res){674          res.sendfile('user.html');675        });676677        app.use(function(req, res){678          assert(0, 'this should not be called');679        });680681        app.use(function(err, req, res, next){682          ++calls;683          next(err);684        });685686        request(app)687        .get('/')688        .end(function(err, res){689          res.statusCode.should.equal(404);690          calls.should.equal(1);691          done();692        });693      })694695      describe('with non-GET', function(){696        it('should still serve', function(done){697          var app = express()698            , calls = 0;699700          app.use(function(req, res){701            res.sendfile(__dirname + '/fixtures/name.txt');702          });703704          request(app)705          .get('/')706          .expect('tobi', done);707        })708      })709    })710  })711})712713  describe('.sendfile(path, options)', function () {714    it('should pass options to send module', function (done) {715      var app = express()716717      app.use(function (req, res) {718        res.sendfile(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 })719      })720721      request(app)722      .get('/')723      .expect(200, 'to', done)724    })725  })726727function createApp(path, options, fn) {728  var app = express();729730  app.use(function (req, res) {731    res.sendFile(path, options, fn);732  });733734  return app;735}