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 fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
var after = require('after');var express = require('../') , request = require('supertest') , assert = require('assert');var onFinished = require('on-finished');var path = require('path');var should = require('should');var fixtures = path.join(__dirname, 'fixtures');
describe('res', function(){ describe('.sendFile(path)', function () { it('should error missing path', function (done) { var app = createApp();
request(app) .get('/') .expect(500, /path.*required/, done); });
it('should transfer a file', function (done) { var app = createApp(path.resolve(fixtures, 'name.txt'));
request(app) .get('/') .expect(200, 'tobi', done); });
it('should transfer a file with special characters in string', function (done) { var app = createApp(path.resolve(fixtures, '% of dogs.txt'));
request(app) .get('/') .expect(200, '20%', done); });
it('should include ETag', function (done) { var app = createApp(path.resolve(fixtures, 'name.txt'));
request(app) .get('/') .expect('ETag', /^(?:W\/)?"[^"]+"$/) .expect(200, 'tobi', done); });
it('should 304 when ETag matches', function (done) { var app = createApp(path.resolve(fixtures, 'name.txt'));
request(app) .get('/') .expect('ETag', /^(?:W\/)?"[^"]+"$/) .expect(200, 'tobi', function (err, res) { if (err) return done(err); var etag = res.headers.etag; request(app) .get('/') .set('If-None-Match', etag) .expect(304, done); }); });
it('should 404 for directory', function (done) { var app = createApp(path.resolve(fixtures, 'blog'));
request(app) .get('/') .expect(404, done); });
it('should 404 when not found', function (done) { var app = createApp(path.resolve(fixtures, 'does-no-exist'));
app.use(function (req, res) { res.statusCode = 200; res.send('no!'); });
request(app) .get('/') .expect(404, done); });
it('should not override manual content-types', function (done) { var app = express();
app.use(function (req, res) { res.contentType('application/x-bogus'); res.sendFile(path.resolve(fixtures, 'name.txt')); });
request(app) .get('/') .expect('Content-Type', 'application/x-bogus') .end(done); })
it('should not error if the client aborts', function (done) { var cb = after(1, done); var app = express();
app.use(function (req, res) { setImmediate(function () { res.sendFile(path.resolve(fixtures, 'name.txt')); cb(); }); test.abort(); });
app.use(function (err, req, res, next) { err.code.should.be.empty; cb(); });
var test = request(app).get('/'); test.expect(200, cb); })
describe('with "dotfiles" option', function () { it('should not serve dotfiles by default', function (done) { var app = createApp(path.resolve(__dirname, 'fixtures/.name'));
request(app) .get('/') .expect(404, done); });
it('should accept dotfiles option', function(done){ var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });
request(app) .get('/') .expect(200, 'tobi', done); }); });
describe('with "headers" option', function () { it('should accept headers option', function (done) { var headers = { 'x-success': 'sent', 'x-other': 'done' }; var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });
request(app) .get('/') .expect('x-success', 'sent') .expect('x-other', 'done') .expect(200, done); });
it('should ignore headers option on 404', function (done) { var headers = { 'x-success': 'sent' }; var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });
request(app) .get('/') .expect(404, function (err, res) { if (err) return done(err); res.headers.should.not.have.property('x-success'); done(); }); }); });
describe('with "root" option', function () { it('should not transfer relative with without', function (done) { var app = createApp('test/fixtures/name.txt');
request(app) .get('/') .expect(500, /must be absolute/, done); })
it('should serve relative to "root"', function (done) { var app = createApp('name.txt', {root: fixtures});
request(app) .get('/') .expect(200, 'tobi', done); })
it('should disallow requesting out of "root"', function (done) { var app = createApp('foo/../../user.html', {root: fixtures});
request(app) .get('/') .expect(403, done); }) }) })
describe('.sendFile(path, fn)', function () { it('should invoke the callback when complete', function (done) { var cb = after(2, done); var app = createApp(path.resolve(fixtures, 'name.txt'), cb);
request(app) .get('/') .expect(200, cb); })
it('should invoke the callback when client aborts', function (done) { var cb = after(1, done); var app = express();
app.use(function (req, res) { setImmediate(function () { res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) { should(err).be.ok; err.code.should.equal('ECONNABORTED'); cb(); }); }); test.abort(); });
var test = request(app).get('/'); test.expect(200, cb); })
it('should invoke the callback when client already aborted', function (done) { var cb = after(1, done); var app = express();
app.use(function (req, res) { onFinished(res, function () { res.sendFile(path.resolve(fixtures, 'name.txt'), function (err) { should(err).be.ok; err.code.should.equal('ECONNABORTED'); cb(); }); }); test.abort(); });
var test = request(app).get('/'); test.expect(200, cb); })
it('should invoke the callback without error when HEAD', function (done) { var app = express(); var cb = after(2, done);
app.use(function (req, res) { res.sendFile(path.resolve(fixtures, 'name.txt'), cb); });
request(app) .head('/') .expect(200, cb); });
it('should invoke the callback without error when 304', function (done) { var app = express(); var cb = after(3, done);
app.use(function (req, res) { res.sendFile(path.resolve(fixtures, 'name.txt'), cb); });
request(app) .get('/') .expect('ETag', /^(?:W\/)?"[^"]+"$/) .expect(200, 'tobi', function (err, res) { if (err) return cb(err); var etag = res.headers.etag; request(app) .get('/') .set('If-None-Match', etag) .expect(304, cb); }); });
it('should invoke the callback on 404', function(done){ var app = express();
app.use(function (req, res) { res.sendFile(path.resolve(fixtures, 'does-not-exist'), function (err) { should(err).be.ok; err.status.should.equal(404); res.send('got it'); }); });
request(app) .get('/') .expect(200, 'got it', done); }) })})
function createApp(path, options, fn) { var app = express();
app.use(function (req, res) { res.sendFile(path, options, fn); });
return app;}