res.sendfile.js
This file
Path
test/res.sendfile.js
297 lines · 7.2 KiB · JavaScript
Last changed by
Add headers option to res.sendfileDouglas Christopher Wilsonbefore adoptionce3d1fe
History of this fileTake it
The walk stopped after 200 commits: lines older than ac2cbef point at it.
Merge pull request #1899 from visionmedia/remove-configureRoman Shtylmanbefore adoptionac2cbef
lines 1–2why, one step back12var express = require('../')remove unnecessary test/support/httpJonathan Ongbefore adoption643397e
line 3why, one step back3 , request = require('supertest')Merge pull request #1899 from visionmedia/remove-configureRoman Shtylmanbefore adoptionac2cbef
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)Merge pull request #1899 from visionmedia/remove-configureRoman Shtylmanbefore adoptionac2cbef
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);Merge pull request #1899 from visionmedia/remove-configureRoman Shtylmanbefore adoptionac2cbef
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')Merge pull request #1899 from visionmedia/remove-configureRoman Shtylmanbefore adoptionac2cbef
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);Merge pull request #1899 from visionmedia/remove-configureRoman Shtylmanbefore adoptionac2cbef
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);Merge pull request #1899 from visionmedia/remove-configureRoman Shtylmanbefore adoptionac2cbef
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 })132Add headers option to res.sendfileDouglas Christopher Wilsonbefore adoptionce3d1fe
lines 133–167why, one step back133 it('should accept headers option', function(done){134 var app = express();135 var headers = {136 'x-success': 'sent',137 'x-other': 'done'138 };139140 app.use(function(req, res){141 res.sendfile('test/fixtures/user.html', { headers: headers });142 });143144 request(app)145 .get('/')146 .expect('x-success', 'sent')147 .expect('x-other', 'done')148 .expect(200, done);149 })150151 it('should ignore headers option on 404', function(done){152 var app = express();153 var headers = { 'x-success': 'sent' };154155 app.use(function(req, res){156 res.sendfile('test/fixtures/user.nothing', { headers: headers });157 });158159 request(app)160 .get('/')161 .expect(404, function (err, res) {162 if (err) return done(err);163 res.headers.should.not.have.property('x-success');164 done();165 });166 })167Merge pull request #1899 from visionmedia/remove-configureRoman Shtylmanbefore adoptionac2cbef
lines 168–297why, one step back168 describe('with an absolute path', function(){169 it('should transfer the file', function(done){170 var app = express();171172 app.use(function(req, res){173 res.sendfile(__dirname + '/fixtures/user.html');174 });175176 request(app)177 .get('/')178 .end(function(err, res){179 res.text.should.equal('<p>{{user.name}}</p>');180 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');181 done();182 });183 })184 })185186 describe('with a relative path', function(){187 it('should transfer the file', function(done){188 var app = express();189190 app.use(function(req, res){191 res.sendfile('test/fixtures/user.html');192 });193194 request(app)195 .get('/')196 .end(function(err, res){197 res.text.should.equal('<p>{{user.name}}</p>');198 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');199 done();200 });201 })202203 it('should serve relative to "root"', function(done){204 var app = express();205206 app.use(function(req, res){207 res.sendfile('user.html', { root: 'test/fixtures/' });208 });209210 request(app)211 .get('/')212 .end(function(err, res){213 res.text.should.equal('<p>{{user.name}}</p>');214 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');215 done();216 });217 })218219 it('should consider ../ malicious when "root" is not set', function(done){220 var app = express();221222 app.use(function(req, res){223 res.sendfile('test/fixtures/foo/../user.html');224 });225226 request(app)227 .get('/')228 .expect(403, done);229 })230231 it('should allow ../ when "root" is set', function(done){232 var app = express();233234 app.use(function(req, res){235 res.sendfile('foo/../user.html', { root: 'test/fixtures' });236 });237238 request(app)239 .get('/')240 .expect(200, done);241 })242243 it('should disallow requesting out of "root"', function(done){244 var app = express();245246 app.use(function(req, res){247 res.sendfile('foo/../../user.html', { root: 'test/fixtures' });248 });249250 request(app)251 .get('/')252 .expect(403, done);253 })254255 it('should next(404) when not found', function(done){256 var app = express()257 , calls = 0;258259 app.use(function(req, res){260 res.sendfile('user.html');261 });262263 app.use(function(req, res){264 assert(0, 'this should not be called');265 });266267 app.use(function(err, req, res, next){268 ++calls;269 next(err);270 });271272 request(app)273 .get('/')274 .end(function(err, res){275 res.statusCode.should.equal(404);276 calls.should.equal(1);277 done();278 });279 })280281 describe('with non-GET', function(){282 it('should still serve', function(done){283 var app = express()284 , calls = 0;285286 app.use(function(req, res){287 res.sendfile(__dirname + '/fixtures/name.txt');288 });289290 request(app)291 .get('/')292 .expect('tobi', done);293 })294 })295 })296 })297})