res.sendfile.js
This file
Path
test/res.sendfile.js
262 lines · 6.3 KiB · JavaScript
Last changed by
Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
History of this fileTake it
The walk stopped after 200 commits: lines older than 34c83d7 point at it.
3.4.7Jonathan Ongbefore adoption34c83d7
lines 1–2why, one step back12var express = require('../')remove unnecessary test/support/httpJonathan Ongbefore adoption643397e
line 3why, one step back3 , request = require('supertest')3.4.7Jonathan Ongbefore adoption34c83d7
lines 4–11why, one step back4 , assert = require('assert');56describe('res', function(){7 describe('.sendfile(path, fn)', function(){8 it('should invoke the callback when complete', function(done){9 var app = express();1011 app.use(function(req, res){Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 12why, one step back12 res.sendfile('test/fixtures/user.html', done)3.4.7Jonathan Ongbefore adoption34c83d7
lines 13–40why, one step back13 });1415 request(app)16 .get('/')17 .expect(200)18 .end(function(){});19 })2021 it('should utilize the same options as express.static()', function(done){22 var app = express();2324 app.use(function(req, res){25 res.sendfile('test/fixtures/user.html', { maxAge: 60000 });26 });2728 request(app)29 .get('/')30 .expect('Cache-Control', 'public, max-age=60')31 .end(done);32 })3334 it('should invoke the callback on 404', function(done){35 var app = express()36 , calls = 0;3738 app.use(function(req, res){39 res.sendfile('test/fixtures/nope.html', function(err){40 ++calls;remove ServerResonse.headerSent monkey patchRoman Shtylmanbefore adoption6835289
line 41why, one step back41 assert(!res.headersSent);3.4.7Jonathan Ongbefore adoption34c83d7
lines 42–65why, one step back42 res.send(err.message);43 });44 });4546 request(app)47 .get('/')48 .end(function(err, res){49 assert(1 == calls, 'called too many times');50 res.text.should.startWith("ENOENT, stat");51 res.statusCode.should.equal(200);52 done();53 });54 })5556 it('should not override manual content-types', function(done){57 var app = express();5859 app.use(function(req, res){60 res.contentType('txt');61 res.sendfile('test/fixtures/user.html');62 });6364 request(app)65 .get('/')move setHeader charset patch to .setJonathan Ongbefore adoptionbad55f7
line 66why, one step back66 .expect('Content-Type', 'text/plain; charset=utf-8')3.4.7Jonathan Ongbefore adoption34c83d7
lines 67–75why, one step back67 .end(done);68 })6970 it('should invoke the callback on 403', function(done){71 var app = express()72 , calls = 0;7374 app.use(function(req, res){75 res.sendfile('test/fixtures/foo/../user.html', function(err){remove ServerResonse.headerSent monkey patchRoman Shtylmanbefore adoption6835289
line 76why, one step back76 assert(!res.headersSent);3.4.7Jonathan Ongbefore adoption34c83d7
lines 77–93why, one step back77 ++calls;78 res.send(err.message);79 });80 });8182 request(app)83 .get('/')84 .expect('Forbidden')85 .expect(200, done);86 })8788 it('should invoke the callback on socket error', function(done){89 var app = express()90 , calls = 0;9192 app.use(function(req, res){93 res.sendfile('test/fixtures/user.html', function(err){remove ServerResonse.headerSent monkey patchRoman Shtylmanbefore adoption6835289
line 94why, one step back94 assert(!res.headersSent);3.4.7Jonathan Ongbefore adoption34c83d7
lines 95–108why, one step back95 req.socket.listeners('error').should.have.length(1); // node's original handler96 done();97 });9899 req.socket.emit('error', new Error('broken!'));100 });101102 request(app)103 .get('/')104 .end(function(){});105 })106 })107108 describe('.sendfile(path)', function(){Pass options from res.sendfile to sendDouglas Christopher Wilsonbefore adoption2fd3e72
lines 109–132why, one step back109 it('should not serve hidden files', function(done){110 var app = express();111112 app.use(function(req, res){113 res.sendfile('test/fixtures/.name');114 });115116 request(app)117 .get('/')118 .expect(404, done);119 })120121 it('should accept hidden option', function(done){122 var app = express();123124 app.use(function(req, res){125 res.sendfile('test/fixtures/.name', { hidden: true });126 });127128 request(app)129 .get('/')130 .expect(200, 'tobi', done);131 })1323.4.7Jonathan Ongbefore adoption34c83d7
lines 133–262why, one step back133 describe('with an absolute path', function(){134 it('should transfer the file', function(done){135 var app = express();136137 app.use(function(req, res){138 res.sendfile(__dirname + '/fixtures/user.html');139 });140141 request(app)142 .get('/')143 .end(function(err, res){144 res.text.should.equal('<p>{{user.name}}</p>');145 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');146 done();147 });148 })149 })150151 describe('with a relative path', function(){152 it('should transfer the file', function(done){153 var app = express();154155 app.use(function(req, res){156 res.sendfile('test/fixtures/user.html');157 });158159 request(app)160 .get('/')161 .end(function(err, res){162 res.text.should.equal('<p>{{user.name}}</p>');163 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');164 done();165 });166 })167168 it('should serve relative to "root"', function(done){169 var app = express();170171 app.use(function(req, res){172 res.sendfile('user.html', { root: 'test/fixtures/' });173 });174175 request(app)176 .get('/')177 .end(function(err, res){178 res.text.should.equal('<p>{{user.name}}</p>');179 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');180 done();181 });182 })183184 it('should consider ../ malicious when "root" is not set', function(done){185 var app = express();186187 app.use(function(req, res){188 res.sendfile('test/fixtures/foo/../user.html');189 });190191 request(app)192 .get('/')193 .expect(403, done);194 })195196 it('should allow ../ when "root" is set', function(done){197 var app = express();198199 app.use(function(req, res){200 res.sendfile('foo/../user.html', { root: 'test/fixtures' });201 });202203 request(app)204 .get('/')205 .expect(200, done);206 })207208 it('should disallow requesting out of "root"', function(done){209 var app = express();210211 app.use(function(req, res){212 res.sendfile('foo/../../user.html', { root: 'test/fixtures' });213 });214215 request(app)216 .get('/')217 .expect(403, done);218 })219220 it('should next(404) when not found', function(done){221 var app = express()222 , calls = 0;223224 app.use(function(req, res){225 res.sendfile('user.html');226 });227228 app.use(function(req, res){229 assert(0, 'this should not be called');230 });231232 app.use(function(err, req, res, next){233 ++calls;234 next(err);235 });236237 request(app)238 .get('/')239 .end(function(err, res){240 res.statusCode.should.equal(404);241 calls.should.equal(1);242 done();243 });244 })245246 describe('with non-GET', function(){247 it('should still serve', function(done){248 var app = express()249 , calls = 0;250251 app.use(function(req, res){252 res.sendfile(__dirname + '/fixtures/name.txt');253 });254255 request(app)256 .get('/')257 .expect('tobi', done);258 })259 })260 })261 })262})