res.sendfile.js
This file
Path
test/res.sendfile.js
242 lines · 6.0 KiB · JavaScript
Last changed by
remove ServerResonse.headerSent monkey patchRoman Shtylmanbefore adoption6835289
History of this fileTake it
The walk stopped after 200 commits: lines older than 78d9c98 point at it.
update connectTJ Holowaychukbefore adoption78d9c98
lines 1–44why, one step back12var express = require('../')3 , request = require('./support/http')4 , 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){12 res.sendfile('test/fixtures/user.html', function(err){13 assert(!err);14 req.socket.listeners('error').should.have.length(1); // node's original handler15 done();16 });17 });1819 request(app)20 .get('/')21 .expect(200)22 .end(function(){});23 })2425 it('should utilize the same options as express.static()', function(done){26 var app = express();2728 app.use(function(req, res){29 res.sendfile('test/fixtures/user.html', { maxAge: 60000 });30 });3132 request(app)33 .get('/')34 .expect('Cache-Control', 'public, max-age=60')35 .end(done);36 })3738 it('should invoke the callback on 404', function(done){39 var app = express()40 , calls = 0;4142 app.use(function(req, res){43 res.sendfile('test/fixtures/nope.html', function(err){44 ++calls;remove ServerResonse.headerSent monkey patchRoman Shtylmanbefore adoption6835289
line 45why, one step back45 assert(!res.headersSent);update connectTJ Holowaychukbefore adoption78d9c98
lines 46–53why, one step back46 res.send(err.message);47 });48 });4950 request(app)51 .get('/')52 .end(function(err, res){53 assert(1 == calls, 'called too many times');be less picky with ENOENT errors in testsJonathan Ongbefore adoption2e197e2
line 54why, one step back54 res.text.should.startWith("ENOENT, stat");update connectTJ Holowaychukbefore adoption78d9c98
lines 55–79why, one step back55 res.statusCode.should.equal(200);56 done();57 });58 })5960 it('should not override manual content-types', function(done){61 var app = express();6263 app.use(function(req, res){64 res.contentType('txt');65 res.sendfile('test/fixtures/user.html');66 });6768 request(app)69 .get('/')70 .expect('Content-Type', 'text/plain')71 .end(done);72 })7374 it('should invoke the callback on 403', function(done){75 var app = express()76 , calls = 0;7778 app.use(function(req, res){79 res.sendfile('test/fixtures/foo/../user.html', function(err){remove ServerResonse.headerSent monkey patchRoman Shtylmanbefore adoption6835289
line 80why, one step back80 assert(!res.headersSent);update connectTJ Holowaychukbefore adoption78d9c98
lines 81–97why, one step back81 ++calls;82 res.send(err.message);83 });84 });8586 request(app)87 .get('/')88 .expect('Forbidden')89 .expect(200, done);90 })9192 it('should invoke the callback on socket error', function(done){93 var app = express()94 , calls = 0;9596 app.use(function(req, res){97 res.sendfile('test/fixtures/user.html', function(err){remove ServerResonse.headerSent monkey patchRoman Shtylmanbefore adoption6835289
line 98why, one step back98 assert(!res.headersSent);update connectTJ Holowaychukbefore adoption78d9c98
lines 99–242why, one step back99 req.socket.listeners('error').should.have.length(1); // node's original handler100 done();101 });102103 req.socket.emit('error', new Error('broken!'));104 });105106 request(app)107 .get('/')108 .end(function(){});109 })110 })111112 describe('.sendfile(path)', function(){113 describe('with an absolute path', function(){114 it('should transfer the file', function(done){115 var app = express();116117 app.use(function(req, res){118 res.sendfile(__dirname + '/fixtures/user.html');119 });120121 request(app)122 .get('/')123 .end(function(err, res){124 res.text.should.equal('<p>{{user.name}}</p>');125 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');126 done();127 });128 })129 })130131 describe('with a relative path', function(){132 it('should transfer the file', function(done){133 var app = express();134135 app.use(function(req, res){136 res.sendfile('test/fixtures/user.html');137 });138139 request(app)140 .get('/')141 .end(function(err, res){142 res.text.should.equal('<p>{{user.name}}</p>');143 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');144 done();145 });146 })147148 it('should serve relative to "root"', function(done){149 var app = express();150151 app.use(function(req, res){152 res.sendfile('user.html', { root: 'test/fixtures/' });153 });154155 request(app)156 .get('/')157 .end(function(err, res){158 res.text.should.equal('<p>{{user.name}}</p>');159 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');160 done();161 });162 })163164 it('should consider ../ malicious when "root" is not set', function(done){165 var app = express();166167 app.use(function(req, res){168 res.sendfile('test/fixtures/foo/../user.html');169 });170171 request(app)172 .get('/')173 .expect(403, done);174 })175176 it('should allow ../ when "root" is set', function(done){177 var app = express();178179 app.use(function(req, res){180 res.sendfile('foo/../user.html', { root: 'test/fixtures' });181 });182183 request(app)184 .get('/')185 .expect(200, done);186 })187188 it('should disallow requesting out of "root"', function(done){189 var app = express();190191 app.use(function(req, res){192 res.sendfile('foo/../../user.html', { root: 'test/fixtures' });193 });194195 request(app)196 .get('/')197 .expect(403, done);198 })199200 it('should next(404) when not found', function(done){201 var app = express()202 , calls = 0;203204 app.use(function(req, res){205 res.sendfile('user.html');206 });207208 app.use(function(req, res){209 assert(0, 'this should not be called');210 });211212 app.use(function(err, req, res, next){213 ++calls;214 next(err);215 });216217 request(app)218 .get('/')219 .end(function(err, res){220 res.statusCode.should.equal(404);221 calls.should.equal(1);222 done();223 });224 })225226 describe('with non-GET', function(){227 it('should still serve', function(done){228 var app = express()229 , calls = 0;230231 app.use(function(req, res){232 res.sendfile(__dirname + '/fixtures/name.txt');233 });234235 request(app)236 .get('/')237 .expect('tobi', done);238 })239 })240 })241 })242})