Skip to content
Myr

res.sendFile.js

This file

Path

test/res.sendFile.js

300 lines · 7.7 KiB · JavaScript

Last changed by

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

History of this file

Take it

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 1–5why, one step back
1
2var after = require('after');3var express = require('../')4 , request = require('supertest')5 , assert = require('assert');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 6why, one step back
6var onFinished = require('on-finished');

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 7–12why, one step back
7var path = require('path');
8var should = require('should');9var fixtures = path.join(__dirname, 'fixtures');1011describe('res', function(){12 describe('.sendFile(path)', function () {

tests: add additional res.sendFile testDouglas Christopher Wilsonbefore adoptionfbceae2

lines 13–20why, one step back
13 it('should error missing path', function (done) {
14 var app = createApp();1516 request(app)17 .get('/')18 .expect(500, /path.*required/, done);19 });20

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 21–36why, one step back
21 it('should transfer a file', function (done) {
22 var app = createApp(path.resolve(fixtures, 'name.txt'));2324 request(app)25 .get('/')26 .expect(200, 'tobi', done);27 });2829 it('should transfer a file with special characters in string', function (done) {30 var app = createApp(path.resolve(fixtures, '% of dogs.txt'));3132 request(app)33 .get('/')34 .expect(200, '20%', done);35 });36

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 37–61why, one step back
37 it('should include ETag', function (done) {
38 var app = createApp(path.resolve(fixtures, 'name.txt'));3940 request(app)41 .get('/')42 .expect('ETag', /^(?:W\/)?"[^"]+"$/)43 .expect(200, 'tobi', done);44 });4546 it('should 304 when ETag matches', function (done) {47 var app = createApp(path.resolve(fixtures, 'name.txt'));4849 request(app)50 .get('/')51 .expect('ETag', /^(?:W\/)?"[^"]+"$/)52 .expect(200, 'tobi', function (err, res) {53 if (err) return done(err);54 var etag = res.headers.etag;55 request(app)56 .get('/')57 .set('If-None-Match', etag)58 .expect(304, done);59 });60 });61

tests: add some res.sendfile directory testsDouglas Christopher Wilsonbefore adoption4986b1c

lines 62–69why, one step back
62 it('should 404 for directory', function (done) {
63 var app = createApp(path.resolve(fixtures, 'blog'));6465 request(app)66 .get('/')67 .expect(404, done);68 });69

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 70–96why, one step back
70 it('should 404 when not found', function (done) {
71 var app = createApp(path.resolve(fixtures, 'does-no-exist'));7273 app.use(function (req, res) {74 res.statusCode = 200;75 res.send('no!');76 });7778 request(app)79 .get('/')80 .expect(404, done);81 });8283 it('should not override manual content-types', function (done) {84 var app = express();8586 app.use(function (req, res) {87 res.contentType('application/x-bogus');88 res.sendFile(path.resolve(fixtures, 'name.txt'));89 });9091 request(app)92 .get('/')93 .expect('Content-Type', 'application/x-bogus')94 .end(done);95 })96

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 97–117why, one step back
97 it('should not error if the client aborts', function (done) {
98 var cb = after(1, done);99 var app = express();100101 app.use(function (req, res) {102 setImmediate(function () {103 res.sendFile(path.resolve(fixtures, 'name.txt'));104 cb();105 });106 test.abort();107 });108109 app.use(function (err, req, res, next) {110 err.code.should.be.empty;111 cb();112 });113114 var test = request(app).get('/');115 test.expect(200, cb);116 })117

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 118–201why, one step back
118 describe('with "dotfiles" option', function () {
119 it('should not serve dotfiles by default', function (done) {120 var app = createApp(path.resolve(__dirname, 'fixtures/.name'));121122 request(app)123 .get('/')124 .expect(404, done);125 });126127 it('should accept dotfiles option', function(done){128 var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });129130 request(app)131 .get('/')132 .expect(200, 'tobi', done);133 });134 });135136 describe('with "headers" option', function () {137 it('should accept headers option', function (done) {138 var headers = {139 'x-success': 'sent',140 'x-other': 'done'141 };142 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });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 headers = { 'x-success': 'sent' };153 var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });154155 request(app)156 .get('/')157 .expect(404, function (err, res) {158 if (err) return done(err);159 res.headers.should.not.have.property('x-success');160 done();161 });162 });163 });164165 describe('with "root" option', function () {166 it('should not transfer relative with without', function (done) {167 var app = createApp('test/fixtures/name.txt');168169 request(app)170 .get('/')171 .expect(500, /must be absolute/, done);172 })173174 it('should serve relative to "root"', function (done) {175 var app = createApp('name.txt', {root: fixtures});176177 request(app)178 .get('/')179 .expect(200, 'tobi', done);180 })181182 it('should disallow requesting out of "root"', function (done) {183 var app = createApp('foo/../../user.html', {root: fixtures});184185 request(app)186 .get('/')187 .expect(403, done);188 })189 })190 })191192 describe('.sendFile(path, fn)', function () {193 it('should invoke the callback when complete', function (done) {194 var cb = after(2, done);195 var app = createApp(path.resolve(fixtures, 'name.txt'), cb);196197 request(app)198 .get('/')199 .expect(200, cb);200 })201

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 202–209why, one step back
202 it('should invoke the callback when client aborts', function (done) {
203 var cb = after(1, done);204 var app = express();205206 app.use(function (req, res) {207 setImmediate(function () {208 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {209 should(err).be.ok;

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

line 210why, one step back
210 err.code.should.equal('ECONNABORTED');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 211–228why, one step back
211 cb();
212 });213 });214 test.abort();215 });216217 var test = request(app).get('/');218 test.expect(200, cb);219 })220221 it('should invoke the callback when client already aborted', function (done) {222 var cb = after(1, done);223 var app = express();224225 app.use(function (req, res) {226 onFinished(res, function () {227 res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) {228 should(err).be.ok;

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

line 229why, one step back
229 err.code.should.equal('ECONNABORTED');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 230–238why, one step back
230 cb();
231 });232 });233 test.abort();234 });235236 var test = request(app).get('/');237 test.expect(200, cb);238 })

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 239–272why, one step back
239
240 it('should invoke the callback without error when HEAD', function (done) {241 var app = express();242 var cb = after(2, done);243244 app.use(function (req, res) {245 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);246 });247248 request(app)249 .head('/')250 .expect(200, cb);251 });252253 it('should invoke the callback without error when 304', function (done) {254 var app = express();255 var cb = after(3, done);256257 app.use(function (req, res) {258 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);259 });260261 request(app)262 .get('/')263 .expect('ETag', /^(?:W\/)?"[^"]+"$/)264 .expect(200, 'tobi', function (err, res) {265 if (err) return cb(err);266 var etag = res.headers.etag;267 request(app)268 .get('/')269 .set('If-None-Match', etag)270 .expect(304, cb);271 });272 });

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 273why, one step back
273

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 274–300why, one step back
274 it('should invoke the callback on 404', function(done){
275 var app = express();276277 app.use(function (req, res) {278 res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) {279 should(err).be.ok;280 err.status.should.equal(404);281 res.send('got it');282 });283 });284285 request(app)286 .get('/')287 .expect(200, 'got it', done);288 })289 })290})291292function createApp(path, options, fn) {293 var app = express();294295 app.use(function (req, res) {296 res.sendFile(path, options, fn);297 });298299 return app;300}