Added Request#sendfile() specs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent ebcfb0e · Browse files · 3 files·+82 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+82 added−8 removed
- tests:+78 added−4 removed
- other:+4 added−4 removed
Provenance
- Commit
31e608e- Tree
f82ff7b- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
ebcfb0e
3 files changed+82 added−8 removed
modified:lib/express/spec/mocks.js+32 added-3 removed
4 unmodified lines5678910111212131415151617181920172122232425262728293031323334353637383940414243444546474849505152535455122 unmodified linesExpand all4 unmodified lines * Module dependencies. */ var utils = require('express/utils'), path = require('path'), fs = require('fs')
/** * Mock fs.readFile() * Sync fs.readFile() */ fs.readFile = function(path, fn) {fs.readFile = function(path, encoding, fn) { if (encoding instanceof Function) fn = encoding, encoding = null try { fn(null, fs.readFileSync(path)) fn(null, fs.readFileSync(path, encoding)) } catch (e) { fn(e) }}
/** * Sync path.exists() */ path.exists = function(path, fn) { try { fs.statSync(path) fn(true) } catch (e) { fn(false) }}
/** * Sync fs.stat() */ fs.stat = function(path, fn) { try { fn(null, fs.statSync(path)) } catch (e) { fn(e) }}
// --- MockRequest
var MockRequest = new Class({ 122 unmodified linesExpand allmodified:lib/express/static.js+4 added-4 removed
43 unmodified lines44 return request.contentType(cache.type),45 request.halt(200, cache.content, 'binary')46 path.exists(file, function(exists){47 if (!exists) return request.halt()48- fs.stat(file, function(e, stats){49- if (e) throw e48+ fs.stat(file, function(err, stats){49+ if (err) throw err50 if (!stats.isFile()) return request.halt()51- fs.readFile(file, 'binary', function(e, content){52- if (e) throw e51+ fs.readFile(file, 'binary', function(err, content){52+ if (err) throw err53 request.contentType(file)54 if (set('cache static files'))55 request.cache.set(file, { type: file, content: content })56 request.halt(200, content, 'binary')5 unmodified linesmodified:spec/spec.static.js+46 added-1 removed
3 unmodified lines4 before5 StaticFile = require('express/static').File6 end7 8- describe '#init'8+ describe '#constructor'9 it 'should accept and assign #path'10 (new StaticFile('/foo/bar')).path.should.eql '/foo/bar'11 end12 5 unmodified lines18 e.message.should.eql "`/../foobar' is not a valid path"19 }20 end21 end22+ 23+ describe '#sendfile()'24+ describe 'when the file exists'25+ it 'should transfer the file'26+ get('/public/*', function(file){27+ this.sendfile('spec/fixtures/' + file)28+ })29+ get('/public/user.json').body.should.include '"name":'30+ end31+ 32+ it 'should automatically set the content type based on extension'33+ get('/public/*', function(file){34+ this.sendfile('spec/fixtures/' + file)35+ })36+ get('/public/user.json').headers['content-type'].should.eql 'application/json'37+ end38+ end39+ 40+ describe 'when "cache static files" is enabled'41+ it 'should transfer the file'42+ enable('cache static files')43+ get('/public/*', function(file){44+ this.sendfile('spec/fixtures/' + file)45+ })46+ get('/cached', function(){47+ return this.cache.get('spec/fixtures/user.json') ? 'yes' : 'no'48+ })49+ get('/cached').body.should.eql 'no'50+ get('/public/user.json').body.should.include '"name":'51+ get('/cached').body.should.eql 'yes'52+ get('/cached').body.should.eql 'yes'53+ end54+ 55+ it 'should automatically set the content type based on extension'56+ enable('cache static files')57+ get('/public/*', function(file){58+ this.sendfile('spec/fixtures/' + file)59+ })60+ get('/public/user.json').headers['content-type'].should.eql 'application/json'61+ get('/public/user.json').headers['content-type'].should.eql 'application/json'62+ get('/public/user.json').headers['content-type'].should.eql 'application/json'63+ end64+ end65+ end66+ 67 end68 end