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
Provenance
- Commit
291993d- Tree
fe02d34- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
e8594c3
2 files changed+596 added−130 removed
modified:test/support/utils.js+14 added
11 unmodified lines12131415161718192013 unmodified lines34353637383940414243444546474849505152535418 unmodified lines11 unmodified lines * @public */
exports.shouldHaveBody = shouldHaveBodyexports.shouldHaveHeader = shouldHaveHeaderexports.shouldNotHaveBody = shouldNotHaveBodyexports.shouldNotHaveHeader = shouldNotHaveHeader;
/**13 unmodified lines }}
/** * Assert that a supertest response does have a header. * * @param {string} header Header name to check * @returns {function} */
function shouldHaveHeader (header) { return function (res) { assert.ok((header.toLowerCase() in res.headers), 'should have header ' + header) }}
/** * Assert that a supertest response does not have a body. * * @returns {function} */18 unmodified linesmodified:test/res.sendFile.js+582 added-130 removed
26 unmodified lines27 .get('/')28 .expect(500, /TypeError: path must be a string to res.sendFile/, done)29 })30 31+ it('should error for non-absolute path', function (done) {32+ var app = createApp('name.txt')33+34+ request(app)35+ .get('/')36+ .expect(500, /TypeError: path must be absolute/, done)37+ })38+39 it('should transfer a file', function (done) {40 var app = createApp(path.resolve(fixtures, 'name.txt'));41 42 request(app)54 unmodified lines97 .get('/')98 .expect(404, done);99 });100 101+ it('should send cache-control by default', function (done) {102+ var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))103+104+ request(app)105+ .get('/')106+ .expect('Cache-Control', 'public, max-age=0')107+ .expect(200, done)108+ })109+110+ it('should not serve dotfiles by default', function (done) {111+ var app = createApp(path.resolve(__dirname, 'fixtures/.name'))112+113+ request(app)114+ .get('/')115+ .expect(404, done)116+ })117+118 it('should not override manual content-types', function (done) {119 var app = express();120 121 app.use(function (req, res) {33 unmodified lines155 assert.ok(err)156 server.close(cb)157 })158 })134-135- describe('with "cacheControl" option', function () {136- it('should enable cacheControl by default', function (done) {137- var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'))138-139- request(app)140- .get('/')141- .expect('Cache-Control', 'public, max-age=0')142- .expect(200, done)143- })144-145- it('should accept cacheControl option', function (done) {146- var app = createApp(path.resolve(__dirname, 'fixtures/name.txt'), { cacheControl: false })147-148- request(app)149- .get('/')150- .expect(utils.shouldNotHaveHeader('Cache-Control'))151- .expect(200, done)152- })153- })154-155- describe('with "dotfiles" option', function () {156- it('should not serve dotfiles by default', function (done) {157- var app = createApp(path.resolve(__dirname, 'fixtures/.name'));158-159- request(app)160- .get('/')161- .expect(404, done);162- });163-164- it('should accept dotfiles option', function(done){165- var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' });166-167- request(app)168- .get('/')169- .expect(200)170- .expect(utils.shouldHaveBody(Buffer.from('tobi')))171- .end(done)172- });173- });174-175- 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 });182-183- request(app)184- .get('/')185- .expect('x-success', 'sent')186- .expect('x-other', 'done')187- .expect(200, done);188- });189-190- 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 });193-194- request(app)195- .get('/')196- .expect(utils.shouldNotHaveHeader('X-Success'))197- .expect(404, done);198- });199- });200-201- 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- })207-208- request(app)209- .get('/')210- .expect('Cache-Control', 'public, max-age=14400, immutable')211- .expect(200, done)212- })213- })214-215- 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- })220-221- request(app)222- .get('/')223- .expect('Cache-Control', 'public, max-age=14400')224- .expect(200, done)225- })226-227- 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- })231-232- request(app)233- .get('/')234- .expect('Cache-Control', 'public, max-age=14400')235- .expect(200, done)236- })237- })238-239- describe('with "root" option', function () {240- it('should not transfer relative with without', function (done) {241- var app = createApp('test/fixtures/name.txt');242-243- request(app)244- .get('/')245- .expect(500, /must be absolute/, done);246- })247-248- it('should serve relative to "root"', function (done) {249- var app = createApp('name.txt', {root: fixtures});250-251- request(app)252- .get('/')253- .expect(200, 'tobi', done);254- })255-256- it('should disallow requesting out of "root"', function (done) {257- var app = createApp('foo/../../user.html', {root: fixtures});258-259- request(app)260- .get('/')261- .expect(403, done);262- })263- })159 })160 161 describe('.sendFile(path, fn)', function () {162 it('should invoke the callback when complete', function (done) {105 unmodified lines268 request(createApp(path.resolve(fixtures, 'name.txt'), { start: 0, end: 1 }))269 .get('/')270 .expect(200, 'to', done)271 })272+273+ describe('with "acceptRanges" option', function () {274+ describe('when true', function () {275+ it('should advertise byte range accepted', function (done) {276+ var app = express()277+278+ app.use(function (req, res) {279+ res.sendFile(path.resolve(fixtures, 'nums.txt'), {280+ acceptRanges: true281+ })282+ })283+284+ request(app)285+ .get('/')286+ .expect(200)287+ .expect('Accept-Ranges', 'bytes')288+ .expect('123456789')289+ .end(done)290+ })291+292+ it('should respond to range request', function (done) {293+ var app = express()294+295+ app.use(function (req, res) {296+ res.sendFile(path.resolve(fixtures, 'nums.txt'), {297+ acceptRanges: true298+ })299+ })300+301+ request(app)302+ .get('/')303+ .set('Range', 'bytes=0-4')304+ .expect(206, '12345', done)305+ })306+ })307+308+ describe('when false', function () {309+ it('should not advertise accept-ranges', function (done) {310+ var app = express()311+312+ app.use(function (req, res) {313+ res.sendFile(path.resolve(fixtures, 'nums.txt'), {314+ acceptRanges: false315+ })316+ })317+318+ request(app)319+ .get('/')320+ .expect(200)321+ .expect(utils.shouldNotHaveHeader('Accept-Ranges'))322+ .end(done)323+ })324+325+ it('should not honor range requests', function (done) {326+ var app = express()327+328+ app.use(function (req, res) {329+ res.sendFile(path.resolve(fixtures, 'nums.txt'), {330+ acceptRanges: false331+ })332+ })333+334+ request(app)335+ .get('/')336+ .set('Range', 'bytes=0-4')337+ .expect(200, '123456789', done)338+ })339+ })340+ })341+342+ describe('with "cacheControl" option', function () {343+ describe('when true', function () {344+ it('should send cache-control header', function (done) {345+ var app = express()346+347+ app.use(function (req, res) {348+ res.sendFile(path.resolve(fixtures, 'user.html'), {349+ cacheControl: true350+ })351+ })352+353+ request(app)354+ .get('/')355+ .expect(200)356+ .expect('Cache-Control', 'public, max-age=0')357+ .end(done)358+ })359+ })360+361+ describe('when false', function () {362+ it('should not send cache-control header', function (done) {363+ var app = express()364+365+ app.use(function (req, res) {366+ res.sendFile(path.resolve(fixtures, 'user.html'), {367+ cacheControl: false368+ })369+ })370+371+ request(app)372+ .get('/')373+ .expect(200)374+ .expect(utils.shouldNotHaveHeader('Cache-Control'))375+ .end(done)376+ })377+ })378+ })379+380+ describe('with "dotfiles" option', function () {381+ describe('when "allow"', function () {382+ it('should allow dotfiles', function (done) {383+ var app = express()384+385+ app.use(function (req, res) {386+ res.sendFile(path.resolve(fixtures, '.name'), {387+ dotfiles: 'allow'388+ })389+ })390+391+ request(app)392+ .get('/')393+ .expect(200)394+ .expect(utils.shouldHaveBody(Buffer.from('tobi')))395+ .end(done)396+ })397+ })398+399+ describe('when "deny"', function () {400+ it('should deny dotfiles', function (done) {401+ var app = express()402+403+ app.use(function (req, res) {404+ res.sendFile(path.resolve(fixtures, '.name'), {405+ dotfiles: 'deny'406+ })407+ })408+409+ request(app)410+ .get('/')411+ .expect(403)412+ .expect(/Forbidden/)413+ .end(done)414+ })415+ })416+417+ describe('when "ignore"', function () {418+ it('should ignore dotfiles', function (done) {419+ var app = express()420+421+ app.use(function (req, res) {422+ res.sendFile(path.resolve(fixtures, '.name'), {423+ dotfiles: 'ignore'424+ })425+ })426+427+ request(app)428+ .get('/')429+ .expect(404)430+ .expect(/Not Found/)431+ .end(done)432+ })433+ })434+ })435+436+ describe('with "headers" option', function () {437+ it('should set headers on response', function (done) {438+ var app = express()439+440+ 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+ })448+449+ request(app)450+ .get('/')451+ .expect(200)452+ .expect('X-Foo', 'Bar')453+ .expect('X-Bar', 'Foo')454+ .end(done)455+ })456+457+ it('should use last header when duplicated', function (done) {458+ var app = express()459+460+ 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+ })468+469+ request(app)470+ .get('/')471+ .expect(200)472+ .expect('X-Foo', 'bar')473+ .end(done)474+ })475+476+ it('should override Content-Type', function (done) {477+ var app = express()478+479+ app.use(function (req, res) {480+ res.sendFile(path.resolve(fixtures, 'user.html'), {481+ headers: {482+ 'Content-Type': 'text/x-custom'483+ }484+ })485+ })486+487+ request(app)488+ .get('/')489+ .expect(200)490+ .expect('Content-Type', 'text/x-custom')491+ .end(done)492+ })493+494+ it('should not set headers on 404', function (done) {495+ var app = express()496+497+ app.use(function (req, res) {498+ res.sendFile(path.resolve(fixtures, 'does-not-exist'), {499+ headers: {500+ 'X-Foo': 'Bar'501+ }502+ })503+ })504+505+ request(app)506+ .get('/')507+ .expect(404)508+ .expect(utils.shouldNotHaveHeader('X-Foo'))509+ .end(done)510+ })511+ })512+513+ 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()517+518+ app.use(function (req, res) {519+ res.sendFile(path.resolve(fixtures, 'user.html'), {520+ immutable: true521+ })522+ })523+524+ request(app)525+ .get('/')526+ .expect(200)527+ .expect('Cache-Control', 'public, max-age=0, immutable')528+ .end(done)529+ })530+ })531+532+ describe('when false', function () {533+ it('should not send cache-control header with immutable', function (done) {534+ var app = express()535+536+ app.use(function (req, res) {537+ res.sendFile(path.resolve(fixtures, 'user.html'), {538+ immutable: false539+ })540+ })541+542+ request(app)543+ .get('/')544+ .expect(200)545+ .expect('Cache-Control', 'public, max-age=0')546+ .end(done)547+ })548+ })549+ })550+551+ describe('with "lastModified" option', function () {552+ describe('when true', function () {553+ it('should send last-modified header', function (done) {554+ var app = express()555+556+ app.use(function (req, res) {557+ res.sendFile(path.resolve(fixtures, 'user.html'), {558+ lastModified: true559+ })560+ })561+562+ request(app)563+ .get('/')564+ .expect(200)565+ .expect(utils.shouldHaveHeader('Last-Modified'))566+ .end(done)567+ })568+569+ it('should conditionally respond with if-modified-since', function (done) {570+ var app = express()571+572+ app.use(function (req, res) {573+ res.sendFile(path.resolve(fixtures, 'user.html'), {574+ lastModified: true575+ })576+ })577+578+ request(app)579+ .get('/')580+ .set('If-Modified-Since', (new Date(Date.now() + 99999).toUTCString()))581+ .expect(304, done)582+ })583+ })584+585+ describe('when false', function () {586+ it('should not have last-modified header', function (done) {587+ var app = express()588+589+ app.use(function (req, res) {590+ res.sendFile(path.resolve(fixtures, 'user.html'), {591+ lastModified: false592+ })593+ })594+595+ request(app)596+ .get('/')597+ .expect(200)598+ .expect(utils.shouldNotHaveHeader('Last-Modified'))599+ .end(done)600+ })601+602+ it('should not honor if-modified-since', function (done) {603+ var app = express()604+605+ app.use(function (req, res) {606+ res.sendFile(path.resolve(fixtures, 'user.html'), {607+ lastModified: false608+ })609+ })610+611+ 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+ })620+621+ describe('with "maxAge" option', function () {622+ it('should set cache-control max-age to milliseconds', function (done) {623+ var app = express()624+625+ app.use(function (req, res) {626+ res.sendFile(path.resolve(fixtures, 'user.html'), {627+ maxAge: 20000628+ })629+ })630+631+ request(app)632+ .get('/')633+ .expect(200)634+ .expect('Cache-Control', 'public, max-age=20')635+ .end(done)636+ })637+638+ it('should cap cache-control max-age to 1 year', function (done) {639+ var app = express()640+641+ app.use(function (req, res) {642+ res.sendFile(path.resolve(fixtures, 'user.html'), {643+ maxAge: 99999999999644+ })645+ })646+647+ request(app)648+ .get('/')649+ .expect(200)650+ .expect('Cache-Control', 'public, max-age=31536000')651+ .end(done)652+ })653+654+ it('should min cache-control max-age to 0', function (done) {655+ var app = express()656+657+ app.use(function (req, res) {658+ res.sendFile(path.resolve(fixtures, 'user.html'), {659+ maxAge: -20000660+ })661+ })662+663+ request(app)664+ .get('/')665+ .expect(200)666+ .expect('Cache-Control', 'public, max-age=0')667+ .end(done)668+ })669+670+ it('should floor cache-control max-age', function (done) {671+ var app = express()672+673+ app.use(function (req, res) {674+ res.sendFile(path.resolve(fixtures, 'user.html'), {675+ maxAge: 21911.23676+ })677+ })678+679+ request(app)680+ .get('/')681+ .expect(200)682+ .expect('Cache-Control', 'public, max-age=21')683+ .end(done)684+ })685+686+ describe('when cacheControl: false', function () {687+ it('shold not send cache-control', function (done) {688+ var app = express()689+690+ app.use(function (req, res) {691+ res.sendFile(path.resolve(fixtures, 'user.html'), {692+ cacheControl: false,693+ maxAge: 20000694+ })695+ })696+697+ request(app)698+ .get('/')699+ .expect(200)700+ .expect(utils.shouldNotHaveHeader('Cache-Control'))701+ .end(done)702+ })703+ })704+705+ describe('when string', function () {706+ it('should accept plain number as milliseconds', function (done) {707+ var app = express()708+709+ app.use(function (req, res) {710+ res.sendFile(path.resolve(fixtures, 'user.html'), {711+ maxAge: '20000'712+ })713+ })714+715+ request(app)716+ .get('/')717+ .expect(200)718+ .expect('Cache-Control', 'public, max-age=20')719+ .end(done)720+ })721+722+ it('should accept suffix "s" for seconds', function (done) {723+ var app = express()724+725+ app.use(function (req, res) {726+ res.sendFile(path.resolve(fixtures, 'user.html'), {727+ maxAge: '20s'728+ })729+ })730+731+ request(app)732+ .get('/')733+ .expect(200)734+ .expect('Cache-Control', 'public, max-age=20')735+ .end(done)736+ })737+738+ it('should accept suffix "m" for minutes', function (done) {739+ var app = express()740+741+ app.use(function (req, res) {742+ res.sendFile(path.resolve(fixtures, 'user.html'), {743+ maxAge: '20m'744+ })745+ })746+747+ request(app)748+ .get('/')749+ .expect(200)750+ .expect('Cache-Control', 'public, max-age=1200')751+ .end(done)752+ })753+754+ it('should accept suffix "d" for days', function (done) {755+ var app = express()756+757+ app.use(function (req, res) {758+ res.sendFile(path.resolve(fixtures, 'user.html'), {759+ maxAge: '20d'760+ })761+ })762+763+ request(app)764+ .get('/')765+ .expect(200)766+ .expect('Cache-Control', 'public, max-age=1728000')767+ .end(done)768+ })769+ })770+ })771+772+ describe('with "root" option', function () {773+ it('should allow relative path', function (done) {774+ var app = express()775+776+ app.use(function (req, res) {777+ res.sendFile('name.txt', {778+ root: fixtures779+ })780+ })781+782+ request(app)783+ .get('/')784+ .expect(200, 'tobi', done)785+ })786+787+ it('should allow up within root', function (done) {788+ var app = express()789+790+ app.use(function (req, res) {791+ res.sendFile('fake/../name.txt', {792+ root: fixtures793+ })794+ })795+796+ request(app)797+ .get('/')798+ .expect(200, 'tobi', done)799+ })800+801+ it('should reject up outside root', function (done) {802+ var app = express()803+804+ app.use(function (req, res) {805+ res.sendFile('..' + path.sep + path.relative(path.dirname(fixtures), path.join(fixtures, 'name.txt')), {806+ root: fixtures807+ })808+ })809+810+ request(app)811+ .get('/')812+ .expect(403, done)813+ })814+815+ it('should reject reading outside root', function (done) {816+ var app = express()817+818+ app.use(function (req, res) {819+ res.sendFile('../name.txt', {820+ root: fixtures821+ })822+ })823+824+ request(app)825+ .get('/')826+ .expect(403, done)827+ })828+ })829 })830 831 describe('.sendfile(path, fn)', function(){832 it('should invoke the callback when complete', function(done){450 unmodified lines