res.sendfile.js
This file
Path
test/res.sendfile.js
238 lines · 5.8 KiB · JavaScript
Last changed by
update send to 0.4.0Douglas Christopher Wilsonbefore adoptionb49453c
History of this fileTake it
The walk stopped after 200 commits: lines older than a10f695 point at it.
pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 1–2why, one step back12var express = require('../')remove unnecessary test/support/httpJonathan Ongbefore adoption328c6d3
line 3why, one step back3 , request = require('supertest')pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
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){update send to 0.4.0Douglas Christopher Wilsonbefore adoptionb49453c
line 12why, one step back12 res.sendfile('test/fixtures/user.html', done)pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
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;update connect to 2.16.1Douglas Christopher Wilsonbefore adoption65f13c3
line 41why, one step back41 assert(!res.headersSent);pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 42–49why, 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');be less picky with ENOENT errors in testsJonathan Ongbefore adoption2e197e2
line 50why, one step back50 res.text.should.startWith("ENOENT, stat");pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 51–75why, one step back51 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('/')66 .expect('Content-Type', 'text/plain')67 .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){update connect to 2.16.1Douglas Christopher Wilsonbefore adoption65f13c3
line 76why, one step back76 assert(!res.headersSent);pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
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){update connect to 2.16.1Douglas Christopher Wilsonbefore adoption65f13c3
line 94why, one step back94 assert(!res.headersSent);pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 95–238why, 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(){109 describe('with an absolute path', function(){110 it('should transfer the file', function(done){111 var app = express();112113 app.use(function(req, res){114 res.sendfile(__dirname + '/fixtures/user.html');115 });116117 request(app)118 .get('/')119 .end(function(err, res){120 res.text.should.equal('<p>{{user.name}}</p>');121 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');122 done();123 });124 })125 })126127 describe('with a relative path', function(){128 it('should transfer the file', function(done){129 var app = express();130131 app.use(function(req, res){132 res.sendfile('test/fixtures/user.html');133 });134135 request(app)136 .get('/')137 .end(function(err, res){138 res.text.should.equal('<p>{{user.name}}</p>');139 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');140 done();141 });142 })143144 it('should serve relative to "root"', function(done){145 var app = express();146147 app.use(function(req, res){148 res.sendfile('user.html', { root: 'test/fixtures/' });149 });150151 request(app)152 .get('/')153 .end(function(err, res){154 res.text.should.equal('<p>{{user.name}}</p>');155 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');156 done();157 });158 })159160 it('should consider ../ malicious when "root" is not set', function(done){161 var app = express();162163 app.use(function(req, res){164 res.sendfile('test/fixtures/foo/../user.html');165 });166167 request(app)168 .get('/')169 .expect(403, done);170 })171172 it('should allow ../ when "root" is set', function(done){173 var app = express();174175 app.use(function(req, res){176 res.sendfile('foo/../user.html', { root: 'test/fixtures' });177 });178179 request(app)180 .get('/')181 .expect(200, done);182 })183184 it('should disallow requesting out of "root"', function(done){185 var app = express();186187 app.use(function(req, res){188 res.sendfile('foo/../../user.html', { root: 'test/fixtures' });189 });190191 request(app)192 .get('/')193 .expect(403, done);194 })195196 it('should next(404) when not found', function(done){197 var app = express()198 , calls = 0;199200 app.use(function(req, res){201 res.sendfile('user.html');202 });203204 app.use(function(req, res){205 assert(0, 'this should not be called');206 });207208 app.use(function(err, req, res, next){209 ++calls;210 next(err);211 });212213 request(app)214 .get('/')215 .end(function(err, res){216 res.statusCode.should.equal(404);217 calls.should.equal(1);218 done();219 });220 })221222 describe('with non-GET', function(){223 it('should still serve', function(done){224 var app = express()225 , calls = 0;226227 app.use(function(req, res){228 res.sendfile(__dirname + '/fixtures/name.txt');229 });230231 request(app)232 .get('/')233 .expect('tobi', done);234 })235 })236 })237 })238})