Skip to content
Myr

tests: expand res.sendFile options tests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent e8594c3 · Browse files · 2 files+596 added−130 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+596 added−130 removed

  • tests:+596 added−130 removed

2 files+596 added−130 removed

modified:test/support/utils.js+14 added
modified:test/res.sendFile.js+582 added-130 removed
26 unmodified lines
27 .get('/')28 .expect(500, /TypeError: path must be a string to res.sendFile/, done)29 })3031 it('should error for non-absolute path', function (done) {32 var app = createApp('name.txt')3334 request(app)35 .get('/')36 .expect(500, /TypeError: path must be absolute/, done)37 })3839 it('should transfer a file', function (done) {40 var app = createApp(path.resolve(fixtures, 'name.txt'));4142 request(app)
54 unmodified lines
97 .get('/')98 .expect(404, done);99 });100101 it('should send cache-control by default', function (done) {102 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))103104 request(app)105 .get('/')106 .expect('Cache-Control', 'public, max-age=0')107 .expect(200, done)108 })109110 it('should not serve dotfiles by default', function (done) {111 var app = createApp(path.resolve(__dirname, 'fixtures/.name'))112113 request(app)114 .get('/')115 .expect(404, done)116 })117118 it('should not override manual content-types', function (done) {119 var app = express();120121 app.use(function (req, res) {
33 unmodified lines
155 assert.ok(err)156 server.close(cb)157 })158 })134135 describe('with "cacheControl" option', function () {136 it('should enable cacheControl by default', function (done) {137 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))138139 request(app)140 .get('/')141 .expect('Cache-Control', 'public, max-age=0')142 .expect(200, done)143 })144145 it('should accept cacheControl option', function (done) {146 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { cacheControl: false })147148 request(app)149 .get('/')150 .expect(utils.shouldNotHaveHeader('Cache-Control'))151 .expect(200, done)152 })153 })154155 describe('with "dotfiles" option', function () {156 it('should not serve dotfiles by default', function (done) {157 var app = createApp(path.resolve(__dirname, 'fixtures/.name'));158159 request(app)160 .get('/')161 .expect(404, done);162 });163164 it('should accept dotfiles option', function(done){165 var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });166167 request(app)168 .get('/')169 .expect(200)170 .expect(utils.shouldHaveBody(Buffer.from('tobi')))171 .end(done)172 });173 });174175 describe('with "headers" option', function () {176 it('should accept headers option', function (done) {177 var headers = {178 'x-success': 'sent',179 'x-other': 'done'180 };181 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { headers: headers });182183 request(app)184 .get('/')185 .expect('x-success', 'sent')186 .expect('x-other', 'done')187 .expect(200, done);188 });189190 it('should ignore headers option on 404', function (done) {191 var headers = { 'x-success': 'sent' };192 var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });193194 request(app)195 .get('/')196 .expect(utils.shouldNotHaveHeader('X-Success'))197 .expect(404, done);198 });199 });200201 describe('with "immutable" option', function () {202 it('should add immutable cache-control directive', function (done) {203 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {204 immutable: true,205 maxAge: '4h'206 })207208 request(app)209 .get('/')210 .expect('Cache-Control', 'public, max-age=14400, immutable')211 .expect(200, done)212 })213 })214215 describe('with "maxAge" option', function () {216 it('should set cache-control max-age from number', function (done) {217 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {218 maxAge: 14400000219 })220221 request(app)222 .get('/')223 .expect('Cache-Control', 'public, max-age=14400')224 .expect(200, done)225 })226227 it('should set cache-control max-age from string', function (done) {228 var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), {229 maxAge: '4h'230 })231232 request(app)233 .get('/')234 .expect('Cache-Control', 'public, max-age=14400')235 .expect(200, done)236 })237 })238239 describe('with "root" option', function () {240 it('should not transfer relative with without', function (done) {241 var app = createApp('test/fixtures/name.txt');242243 request(app)244 .get('/')245 .expect(500, /must be absolute/, done);246 })247248 it('should serve relative to "root"', function (done) {249 var app = createApp('name.txt', {root: fixtures});250251 request(app)252 .get('/')253 .expect(200, 'tobi', done);254 })255256 it('should disallow requesting out of "root"', function (done) {257 var app = createApp('foo/../../user.html', {root: fixtures});258259 request(app)260 .get('/')261 .expect(403, done);262 })263 })159 })160161 describe('.sendFile(path, fn)', function () {162 it('should invoke the callback when complete', function (done) {
105 unmodified lines
268 request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))269 .get('/')270 .expect(200, 'to', done)271 })272273 describe('with "acceptRanges" option', function () {274 describe('when true', function () {275 it('should advertise byte range accepted', function (done) {276 var app = express()277278 app.use(function (req, res) {279 res.sendFile(path.resolve(fixtures, 'nums.txt'), {280 acceptRanges: true281 })282 })283284 request(app)285 .get('/')286 .expect(200)287 .expect('Accept-Ranges', 'bytes')288 .expect('123456789')289 .end(done)290 })291292 it('should respond to range request', function (done) {293 var app = express()294295 app.use(function (req, res) {296 res.sendFile(path.resolve(fixtures, 'nums.txt'), {297 acceptRanges: true298 })299 })300301 request(app)302 .get('/')303 .set('Range', 'bytes=0-4')304 .expect(206, '12345', done)305 })306 })307308 describe('when false', function () {309 it('should not advertise accept-ranges', function (done) {310 var app = express()311312 app.use(function (req, res) {313 res.sendFile(path.resolve(fixtures, 'nums.txt'), {314 acceptRanges: false315 })316 })317318 request(app)319 .get('/')320 .expect(200)321 .expect(utils.shouldNotHaveHeader('Accept-Ranges'))322 .end(done)323 })324325 it('should not honor range requests', function (done) {326 var app = express()327328 app.use(function (req, res) {329 res.sendFile(path.resolve(fixtures, 'nums.txt'), {330 acceptRanges: false331 })332 })333334 request(app)335 .get('/')336 .set('Range', 'bytes=0-4')337 .expect(200, '123456789', done)338 })339 })340 })341342 describe('with "cacheControl" option', function () {343 describe('when true', function () {344 it('should send cache-control header', function (done) {345 var app = express()346347 app.use(function (req, res) {348 res.sendFile(path.resolve(fixtures, 'user.html'), {349 cacheControl: true350 })351 })352353 request(app)354 .get('/')355 .expect(200)356 .expect('Cache-Control', 'public, max-age=0')357 .end(done)358 })359 })360361 describe('when false', function () {362 it('should not send cache-control header', function (done) {363 var app = express()364365 app.use(function (req, res) {366 res.sendFile(path.resolve(fixtures, 'user.html'), {367 cacheControl: false368 })369 })370371 request(app)372 .get('/')373 .expect(200)374 .expect(utils.shouldNotHaveHeader('Cache-Control'))375 .end(done)376 })377 })378 })379380 describe('with "dotfiles" option', function () {381 describe('when "allow"', function () {382 it('should allow dotfiles', function (done) {383 var app = express()384385 app.use(function (req, res) {386 res.sendFile(path.resolve(fixtures, '.name'), {387 dotfiles: 'allow'388 })389 })390391 request(app)392 .get('/')393 .expect(200)394 .expect(utils.shouldHaveBody(Buffer.from('tobi')))395 .end(done)396 })397 })398399 describe('when "deny"', function () {400 it('should deny dotfiles', function (done) {401 var app = express()402403 app.use(function (req, res) {404 res.sendFile(path.resolve(fixtures, '.name'), {405 dotfiles: 'deny'406 })407 })408409 request(app)410 .get('/')411 .expect(403)412 .expect(/Forbidden/)413 .end(done)414 })415 })416417 describe('when "ignore"', function () {418 it('should ignore dotfiles', function (done) {419 var app = express()420421 app.use(function (req, res) {422 res.sendFile(path.resolve(fixtures, '.name'), {423 dotfiles: 'ignore'424 })425 })426427 request(app)428 .get('/')429 .expect(404)430 .expect(/Not Found/)431 .end(done)432 })433 })434 })435436 describe('with "headers" option', function () {437 it('should set headers on response', function (done) {438 var app = express()439440 app.use(function (req, res) {441 res.sendFile(path.resolve(fixtures, 'user.html'), {442 headers: {443 'X-Foo': 'Bar',444 'X-Bar': 'Foo'445 }446 })447 })448449 request(app)450 .get('/')451 .expect(200)452 .expect('X-Foo', 'Bar')453 .expect('X-Bar', 'Foo')454 .end(done)455 })456457 it('should use last header when duplicated', function (done) {458 var app = express()459460 app.use(function (req, res) {461 res.sendFile(path.resolve(fixtures, 'user.html'), {462 headers: {463 'X-Foo': 'Bar',464 'x-foo': 'bar'465 }466 })467 })468469 request(app)470 .get('/')471 .expect(200)472 .expect('X-Foo', 'bar')473 .end(done)474 })475476 it('should override Content-Type', function (done) {477 var app = express()478479 app.use(function (req, res) {480 res.sendFile(path.resolve(fixtures, 'user.html'), {481 headers: {482 'Content-Type': 'text/x-custom'483 }484 })485 })486487 request(app)488 .get('/')489 .expect(200)490 .expect('Content-Type', 'text/x-custom')491 .end(done)492 })493494 it('should not set headers on 404', function (done) {495 var app = express()496497 app.use(function (req, res) {498 res.sendFile(path.resolve(fixtures, 'does-not-exist'), {499 headers: {500 'X-Foo': 'Bar'501 }502 })503 })504505 request(app)506 .get('/')507 .expect(404)508 .expect(utils.shouldNotHaveHeader('X-Foo'))509 .end(done)510 })511 })512513 describe('with "immutable" option', function () {514 describe('when true', function () {515 it('should send cache-control header with immutable', function (done) {516 var app = express()517518 app.use(function (req, res) {519 res.sendFile(path.resolve(fixtures, 'user.html'), {520 immutable: true521 })522 })523524 request(app)525 .get('/')526 .expect(200)527 .expect('Cache-Control', 'public, max-age=0, immutable')528 .end(done)529 })530 })531532 describe('when false', function () {533 it('should not send cache-control header with immutable', function (done) {534 var app = express()535536 app.use(function (req, res) {537 res.sendFile(path.resolve(fixtures, 'user.html'), {538 immutable: false539 })540 })541542 request(app)543 .get('/')544 .expect(200)545 .expect('Cache-Control', 'public, max-age=0')546 .end(done)547 })548 })549 })550551 describe('with "lastModified" option', function () {552 describe('when true', function () {553 it('should send last-modified header', function (done) {554 var app = express()555556 app.use(function (req, res) {557 res.sendFile(path.resolve(fixtures, 'user.html'), {558 lastModified: true559 })560 })561562 request(app)563 .get('/')564 .expect(200)565 .expect(utils.shouldHaveHeader('Last-Modified'))566 .end(done)567 })568569 it('should conditionally respond with if-modified-since', function (done) {570 var app = express()571572 app.use(function (req, res) {573 res.sendFile(path.resolve(fixtures, 'user.html'), {574 lastModified: true575 })576 })577578 request(app)579 .get('/')580 .set('If-Modified-Since', (new Date(Date.now() + 99999).toUTCString()))581 .expect(304, done)582 })583 })584585 describe('when false', function () {586 it('should not have last-modified header', function (done) {587 var app = express()588589 app.use(function (req, res) {590 res.sendFile(path.resolve(fixtures, 'user.html'), {591 lastModified: false592 })593 })594595 request(app)596 .get('/')597 .expect(200)598 .expect(utils.shouldNotHaveHeader('Last-Modified'))599 .end(done)600 })601602 it('should not honor if-modified-since', function (done) {603 var app = express()604605 app.use(function (req, res) {606 res.sendFile(path.resolve(fixtures, 'user.html'), {607 lastModified: false608 })609 })610611 request(app)612 .get('/')613 .set('If-Modified-Since', (new Date(Date.now() + 99999).toUTCString()))614 .expect(200)615 .expect(utils.shouldNotHaveHeader('Last-Modified'))616 .end(done)617 })618 })619 })620621 describe('with "maxAge" option', function () {622 it('should set cache-control max-age to milliseconds', function (done) {623 var app = express()624625 app.use(function (req, res) {626 res.sendFile(path.resolve(fixtures, 'user.html'), {627 maxAge: 20000628 })629 })630631 request(app)632 .get('/')633 .expect(200)634 .expect('Cache-Control', 'public, max-age=20')635 .end(done)636 })637638 it('should cap cache-control max-age to 1 year', function (done) {639 var app = express()640641 app.use(function (req, res) {642 res.sendFile(path.resolve(fixtures, 'user.html'), {643 maxAge: 99999999999644 })645 })646647 request(app)648 .get('/')649 .expect(200)650 .expect('Cache-Control', 'public, max-age=31536000')651 .end(done)652 })653654 it('should min cache-control max-age to 0', function (done) {655 var app = express()656657 app.use(function (req, res) {658 res.sendFile(path.resolve(fixtures, 'user.html'), {659 maxAge: -20000660 })661 })662663 request(app)664 .get('/')665 .expect(200)666 .expect('Cache-Control', 'public, max-age=0')667 .end(done)668 })669670 it('should floor cache-control max-age', function (done) {671 var app = express()672673 app.use(function (req, res) {674 res.sendFile(path.resolve(fixtures, 'user.html'), {675 maxAge: 21911.23676 })677 })678679 request(app)680 .get('/')681 .expect(200)682 .expect('Cache-Control', 'public, max-age=21')683 .end(done)684 })685686 describe('when cacheControl: false', function () {687 it('shold not send cache-control', function (done) {688 var app = express()689690 app.use(function (req, res) {691 res.sendFile(path.resolve(fixtures, 'user.html'), {692 cacheControl: false,693 maxAge: 20000694 })695 })696697 request(app)698 .get('/')699 .expect(200)700 .expect(utils.shouldNotHaveHeader('Cache-Control'))701 .end(done)702 })703 })704705 describe('when string', function () {706 it('should accept plain number as milliseconds', function (done) {707 var app = express()708709 app.use(function (req, res) {710 res.sendFile(path.resolve(fixtures, 'user.html'), {711 maxAge: '20000'712 })713 })714715 request(app)716 .get('/')717 .expect(200)718 .expect('Cache-Control', 'public, max-age=20')719 .end(done)720 })721722 it('should accept suffix "s" for seconds', function (done) {723 var app = express()724725 app.use(function (req, res) {726 res.sendFile(path.resolve(fixtures, 'user.html'), {727 maxAge: '20s'728 })729 })730731 request(app)732 .get('/')733 .expect(200)734 .expect('Cache-Control', 'public, max-age=20')735 .end(done)736 })737738 it('should accept suffix "m" for minutes', function (done) {739 var app = express()740741 app.use(function (req, res) {742 res.sendFile(path.resolve(fixtures, 'user.html'), {743 maxAge: '20m'744 })745 })746747 request(app)748 .get('/')749 .expect(200)750 .expect('Cache-Control', 'public, max-age=1200')751 .end(done)752 })753754 it('should accept suffix "d" for days', function (done) {755 var app = express()756757 app.use(function (req, res) {758 res.sendFile(path.resolve(fixtures, 'user.html'), {759 maxAge: '20d'760 })761 })762763 request(app)764 .get('/')765 .expect(200)766 .expect('Cache-Control', 'public, max-age=1728000')767 .end(done)768 })769 })770 })771772 describe('with "root" option', function () {773 it('should allow relative path', function (done) {774 var app = express()775776 app.use(function (req, res) {777 res.sendFile('name.txt', {778 root: fixtures779 })780 })781782 request(app)783 .get('/')784 .expect(200, 'tobi', done)785 })786787 it('should allow up within root', function (done) {788 var app = express()789790 app.use(function (req, res) {791 res.sendFile('fake/../name.txt', {792 root: fixtures793 })794 })795796 request(app)797 .get('/')798 .expect(200, 'tobi', done)799 })800801 it('should reject up outside root', function (done) {802 var app = express()803804 app.use(function (req, res) {805 res.sendFile('..' + path.sep + path.relative(path.dirname(fixtures), path.join(fixtures, 'name.txt')), {806 root: fixtures807 })808 })809810 request(app)811 .get('/')812 .expect(403, done)813 })814815 it('should reject reading outside root', function (done) {816 var app = express()817818 app.use(function (req, res) {819 res.sendFile('../name.txt', {820 root: fixtures821 })822 })823824 request(app)825 .get('/')826 .expect(403, done)827 })828 })829 })830831 describe('.sendfile(path, fn)', function(){832 it('should invoke the callback when complete', function(done){
450 unmodified lines