Skip to content
Myr

res.sendFile.js

This file

Path

test/res.sendFile.js

803 lines · 20.2 KiB · JavaScript

Last changed by

Fix issue where "Request aborted" may be logged in res.sendfileDouglas Christopher Wilsonbefore adoption3d10279

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 app = express();100      var cb = after(2, done)101      var error = null102103      app.use(function (req, res) {104        setImmediate(function () {105          res.sendFile(path.resolve(fixtures, 'name.txt'));106          server.close(cb)107          setTimeout(function () {108            cb(error)109          }, 10)110        })111        test.abort();112      });113114      app.use(function (err, req, res, next) {115        error = err116        next(err)117      });118119      var server = app.listen()120      var test = request(server).get('/')121      test.end()122    })123124    describe('with "cacheControl" option', function () {125      it('should enable cacheControl by default', function (done) {126        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))127128        request(app)129        .get('/')130        .expect('Cache-Control', 'public, max-age=0')131        .expect(200, done)132      })133134      it('should accept cacheControl option', function (done) {135        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { cacheControl: false })136137        request(app)138        .get('/')139        .expect(utils.shouldNotHaveHeader('Cache-Control'))140        .expect(200, done)141      })142    })143144    describe('with "dotfiles" option', function () {145      it('should not serve dotfiles by default', function (done) {146        var app = createApp(path.resolve(__dirname, 'fixtures/.name'));147148        request(app)149        .get('/')150        .expect(404, done);151      });152153      it('should accept dotfiles option', function(done){154        var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });155156        request(app)157        .get('/')158        .expect(200, 'tobi', done);159      });160    });161162    describe('with "headers" option', function () {163      it('should accept headers option', function (done) {164        var headers = {165          'x-success': 'sent',166          'x-other': 'done'167        };168        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });169170        request(app)171        .get('/')172        .expect('x-success', 'sent')173        .expect('x-other', 'done')174        .expect(200, done);175      });176177      it('should ignore headers option on 404', function (done) {178        var headers = { 'x-success': 'sent' };179        var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });180181        request(app)182        .get('/')183        .expect(utils.shouldNotHaveHeader('X-Success'))184        .expect(404, done);185      });186    });187188    describe('with "immutable" option', function () {189      it('should add immutable cache-control directive', function (done) {190        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {191          immutable: true,192          maxAge: '4h'193        })194195        request(app)196        .get('/')197        .expect('Cache-Control', 'public, max-age=14400, immutable')198        .expect(200, done)199      })200    })201202    describe('with "maxAge" option', function () {203      it('should set cache-control max-age from number', function (done) {204        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {205          maxAge: 14400000206        })207208        request(app)209        .get('/')210        .expect('Cache-Control', 'public, max-age=14400')211        .expect(200, done)212      })213214      it('should set cache-control max-age from string', function (done) {215        var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {216          maxAge: '4h'217        })218219        request(app)220        .get('/')221        .expect('Cache-Control', 'public, max-age=14400')222        .expect(200, done)223      })224    })225226    describe('with "root" option', function () {227      it('should not transfer relative with without', function (done) {228        var app = createApp('test/fixtures/name.txt');229230        request(app)231        .get('/')232        .expect(500, /must be absolute/, done);233      })234235      it('should serve relative to "root"', function (done) {236        var app = createApp('name.txt', {root: fixtures});237238        request(app)239        .get('/')240        .expect(200, 'tobi', done);241      })242243      it('should disallow requesting out of "root"', function (done) {244        var app = createApp('foo/../../user.html', {root: fixtures});245246        request(app)247        .get('/')248        .expect(403, done);249      })250    })251  })252253  describe('.sendFile(path, fn)', function () {254    it('should invoke the callback when complete', function (done) {255      var cb = after(2, done);256      var app = createApp(path.resolve(fixtures, 'name.txt'), cb);257258      request(app)259      .get('/')260      .expect(200, cb);261    })262263    it('should invoke the callback when client aborts', function (done) {264      var cb = after(1, done);265      var app = express();266267      app.use(function (req, res) {268        setImmediate(function () {269          res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {270            should(err).be.ok()271            err.code.should.equal('ECONNABORTED');272            server.close(cb)273          });274        });275        test.abort();276      });277278      var server = app.listen()279      var test = request(server).get('/')280      test.expect(200, cb);281    })282283    it('should invoke the callback when client already aborted', function (done) {284      var cb = after(1, done);285      var app = express();286287      app.use(function (req, res) {288        onFinished(res, function () {289          res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {290            should(err).be.ok()291            err.code.should.equal('ECONNABORTED');292            server.close(cb)293          });294        });295        test.abort();296      });297298      var server = app.listen()299      var test = request(server).get('/')300      test.expect(200, cb);301    })302303    it('should invoke the callback without error when HEAD', function (done) {304      var app = express();305      var cb = after(2, done);306307      app.use(function (req, res) {308        res.sendFile(path.resolve(fixtures, 'name.txt'), cb);309      });310311      request(app)312      .head('/')313      .expect(200, cb);314    });315316    it('should invoke the callback without error when 304', function (done) {317      var app = express();318      var cb = after(3, done);319320      app.use(function (req, res) {321        res.sendFile(path.resolve(fixtures, 'name.txt'), cb);322      });323324      request(app)325      .get('/')326      .expect('ETag', /^(?:W\/)?"[^"]+"$/)327      .expect(200, 'tobi', function (err, res) {328        if (err) return cb(err);329        var etag = res.headers.etag;330        request(app)331        .get('/')332        .set('If-None-Match', etag)333        .expect(304, cb);334      });335    });336337    it('should invoke the callback on 404', function(done){338      var app = express();339340      app.use(function (req, res) {341        res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {342          should(err).be.ok()343          err.status.should.equal(404);344          res.send('got it');345        });346      });347348      request(app)349      .get('/')350      .expect(200, 'got it', done);351    })352  })353354  describe('.sendFile(path, options)', function () {355    it('should pass options to send module', function (done) {356      request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))357      .get('/')358      .expect(200, 'to', done)359    })360  })361362  describe('.sendfile(path, fn)', function(){363    it('should invoke the callback when complete', function(done){364      var app = express();365      var cb = after(2, done);366367      app.use(function(req, res){368        res.sendfile('test/fixtures/user.html', cb)369      });370371      request(app)372      .get('/')373      .expect(200, cb);374    })375376    it('should utilize the same options as express.static()', function(done){377      var app = express();378379      app.use(function(req, res){380        res.sendfile('test/fixtures/user.html', { maxAge: 60000 });381      });382383      request(app)384      .get('/')385      .expect('Cache-Control', 'public, max-age=60')386      .end(done);387    })388389    it('should invoke the callback when client aborts', function (done) {390      var cb = after(1, done);391      var app = express();392393      app.use(function (req, res) {394        setImmediate(function () {395          res.sendfile('test/fixtures/name.txt', function (err) {396            should(err).be.ok()397            err.code.should.equal('ECONNABORTED');398            server.close(cb)399          });400        });401        test.abort();402      });403404      var server = app.listen()405      var test = request(server).get('/')406      test.expect(200, cb);407    })408409    it('should invoke the callback when client already aborted', function (done) {410      var cb = after(1, done);411      var app = express();412413      app.use(function (req, res) {414        onFinished(res, function () {415          res.sendfile('test/fixtures/name.txt', function (err) {416            should(err).be.ok()417            err.code.should.equal('ECONNABORTED');418            server.close(cb)419          });420        });421        test.abort();422      });423424      var server = app.listen()425      var test = request(server).get('/')426      test.expect(200, cb);427    })428429    it('should invoke the callback without error when HEAD', function (done) {430      var app = express();431      var cb = after(2, done);432433      app.use(function (req, res) {434        res.sendfile('test/fixtures/name.txt', cb);435      });436437      request(app)438      .head('/')439      .expect(200, cb);440    });441442    it('should invoke the callback without error when 304', function (done) {443      var app = express();444      var cb = after(3, done);445446      app.use(function (req, res) {447        res.sendfile('test/fixtures/name.txt', cb);448      });449450      request(app)451      .get('/')452      .expect('ETag', /^(?:W\/)?"[^"]+"$/)453      .expect(200, 'tobi', function (err, res) {454        if (err) return cb(err);455        var etag = res.headers.etag;456        request(app)457        .get('/')458        .set('If-None-Match', etag)459        .expect(304, cb);460      });461    });462463    it('should invoke the callback on 404', function(done){464      var app = express();465      var calls = 0;466467      app.use(function(req, res){468        res.sendfile('test/fixtures/nope.html', function(err){469          assert.equal(calls++, 0);470          assert(!res.headersSent);471          res.send(err.message);472        });473      });474475      request(app)476      .get('/')477      .expect(200, /^ENOENT.*?, stat/, done);478    })479480    it('should not override manual content-types', function(done){481      var app = express();482483      app.use(function(req, res){484        res.contentType('txt');485        res.sendfile('test/fixtures/user.html');486      });487488      request(app)489      .get('/')490      .expect('Content-Type', 'text/plain; charset=utf-8')491      .end(done);492    })493494    it('should invoke the callback on 403', function(done){495      var app = express()496497      app.use(function(req, res){498        res.sendfile('test/fixtures/foo/../user.html', function(err){499          assert(!res.headersSent);500          res.send(err.message);501        });502      });503504      request(app)505      .get('/')506      .expect('Forbidden')507      .expect(200, done);508    })509510    it('should invoke the callback on socket error', function(done){511      var app = express()512513      app.use(function(req, res){514        res.sendfile('test/fixtures/user.html', function(err){515          assert(!res.headersSent);516          req.socket.listeners('error').should.have.length(1); // node's original handler517          done();518        });519520        req.socket.emit('error', new Error('broken!'));521      });522523      request(app)524      .get('/')525      .end(function(){});526    })527  })528529  describe('.sendfile(path)', function(){530    it('should not serve dotfiles', function(done){531      var app = express();532533      app.use(function(req, res){534        res.sendfile('test/fixtures/.name');535      });536537      request(app)538      .get('/')539      .expect(404, done);540    })541542    it('should accept dotfiles option', function(done){543      var app = express();544545      app.use(function(req, res){546        res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });547      });548549      request(app)550      .get('/')551      .expect(200, 'tobi', done);552    })553554    it('should accept headers option', function(done){555      var app = express();556      var headers = {557        'x-success': 'sent',558        'x-other': 'done'559      };560561      app.use(function(req, res){562        res.sendfile('test/fixtures/user.html', { headers: headers });563      });564565      request(app)566      .get('/')567      .expect('x-success', 'sent')568      .expect('x-other', 'done')569      .expect(200, done);570    })571572    it('should ignore headers option on 404', function(done){573      var app = express();574      var headers = { 'x-success': 'sent' };575576      app.use(function(req, res){577        res.sendfile('test/fixtures/user.nothing', { headers: headers });578      });579580      request(app)581      .get('/')582        .expect(utils.shouldNotHaveHeader('X-Success'))583        .expect(404, done);584    })585586    it('should transfer a file', function (done) {587      var app = express();588589      app.use(function (req, res) {590        res.sendfile('test/fixtures/name.txt');591      });592593      request(app)594      .get('/')595      .expect(200, 'tobi', done);596    });597598    it('should transfer a directory index file', function (done) {599      var app = express();600601      app.use(function (req, res) {602        res.sendfile('test/fixtures/blog/');603      });604605      request(app)606      .get('/')607      .expect(200, '<b>index</b>', done);608    });609610    it('should 404 for directory without trailing slash', function (done) {611      var app = express();612613      app.use(function (req, res) {614        res.sendfile('test/fixtures/blog');615      });616617      request(app)618      .get('/')619      .expect(404, done);620    });621622    it('should transfer a file with urlencoded name', function (done) {623      var app = express();624625      app.use(function (req, res) {626        res.sendfile('test/fixtures/%25%20of%20dogs.txt');627      });628629      request(app)630      .get('/')631      .expect(200, '20%', done);632    });633634    it('should not error if the client aborts', function (done) {635      var app = express();636      var cb = after(2, done)637      var error = null638639      app.use(function (req, res) {640        setImmediate(function () {641          res.sendfile(path.resolve(fixtures, 'name.txt'));642          server.close(cb)643          setTimeout(function () {644            cb(error)645          }, 10)646        });647        test.abort();648      });649650      app.use(function (err, req, res, next) {651        error = err652        next(err)653      });654655      var server = app.listen()656      var test = request(server).get('/')657      test.end()658    })659660    describe('with an absolute path', function(){661      it('should transfer the file', function(done){662        var app = express();663664        app.use(function(req, res){665          res.sendfile(path.join(__dirname, '/fixtures/user.html'))666        });667668        request(app)669        .get('/')670        .expect('Content-Type', 'text/html; charset=UTF-8')671        .expect(200, '<p>{{user.name}}</p>', done);672      })673    })674675    describe('with a relative path', function(){676      it('should transfer the file', function(done){677        var app = express();678679        app.use(function(req, res){680          res.sendfile('test/fixtures/user.html');681        });682683        request(app)684        .get('/')685        .expect('Content-Type', 'text/html; charset=UTF-8')686        .expect(200, '<p>{{user.name}}</p>', done);687      })688689      it('should serve relative to "root"', function(done){690        var app = express();691692        app.use(function(req, res){693          res.sendfile('user.html', { root: 'test/fixtures/' });694        });695696        request(app)697        .get('/')698        .expect('Content-Type', 'text/html; charset=UTF-8')699        .expect(200, '<p>{{user.name}}</p>', done);700      })701702      it('should consider ../ malicious when "root" is not set', function(done){703        var app = express();704705        app.use(function(req, res){706          res.sendfile('test/fixtures/foo/../user.html');707        });708709        request(app)710        .get('/')711        .expect(403, done);712      })713714      it('should allow ../ when "root" is set', function(done){715        var app = express();716717        app.use(function(req, res){718          res.sendfile('foo/../user.html', { root: 'test/fixtures' });719        });720721        request(app)722        .get('/')723        .expect(200, done);724      })725726      it('should disallow requesting out of "root"', function(done){727        var app = express();728729        app.use(function(req, res){730          res.sendfile('foo/../../user.html', { root: 'test/fixtures' });731        });732733        request(app)734        .get('/')735        .expect(403, done);736      })737738      it('should next(404) when not found', function(done){739        var app = express()740          , calls = 0;741742        app.use(function(req, res){743          res.sendfile('user.html');744        });745746        app.use(function(req, res){747          assert(0, 'this should not be called');748        });749750        app.use(function(err, req, res, next){751          ++calls;752          next(err);753        });754755        request(app)756        .get('/')757        .end(function(err, res){758          res.statusCode.should.equal(404);759          calls.should.equal(1);760          done();761        });762      })763764      describe('with non-GET', function(){765        it('should still serve', function(done){766          var app = express()767768          app.use(function(req, res){769            res.sendfile(path.join(__dirname, '/fixtures/name.txt'))770          });771772          request(app)773          .get('/')774          .expect('tobi', done);775        })776      })777    })778  })779})780781describe('.sendfile(path, options)', function () {782  it('should pass options to send module', function (done) {783    var app = express()784785    app.use(function (req, res) {786      res.sendfile(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 })787    })788789    request(app)790      .get('/')791      .expect(200, 'to', done)792  })793})794795function createApp(path, options, fn) {796  var app = express();797798  app.use(function (req, res) {799    res.sendFile(path, options, fn);800  });801802  return app;803}