Merge branch 'etag'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parents b40325c, 0fb102e · Browse files · 16 files·+101 added−62 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
16 files+101 added−62 removed
- tests:+21 added−21 removed
- other:+80 added−41 removed
Provenance
- Commit
b5d6f1a- Tree
d212020- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
b40325c- Parent 2
0fb102e
16 files changed+101 added−62 removed
modified:lib/express/plugins/body-decoder.js+2 added-2 removed
18 unmodified lines19202122232423242526272818 unmodified lines */ request: function(event) { var request = event.request if (request.header('content-type') && request.header('content-type').includes('application/x-www-form-urlencoded')) if (request.header('Content-Type') && request.header('Content-Type').includes('application/x-www-form-urlencoded')) request.params.post = queryString.parseQuery(request.body) } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/content-length.js+2 added-3 removed
9 unmodified lines1011121314151614151617189 unmodified lines response: function(event) { var response = event.request.response if (!response.chunkedEncoding) response.headers['content-length'] = response.headers['content-length'] || response.body.length if (!response.headers['Content-Length'] && response.body) response.headers['Content-Length'] = response.body.length } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/cookie.js+1 added-1 removed
106 unmodified linesExpand all107108109110111111112113114106 unmodified linesExpand all response: function(event) { if (event.response.cookies && event.response.cookies.length) event.request.header('set-cookie', event.response.cookies.join('\nset-cookie: ')) event.request.header('Set-Cookie', event.response.cookies.join('\nSet-Cookie: ')) } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/redirect.js+2 added-2 removed
39 unmodified lines40 */41 42 redirect: function(url, code) {43 if (url === 'back' || url === 'home') url = this[url]44- this.header('location', url)44+ this.header('Location', url)45 this.halt(code || 303)46 } 47 })48 }9 unmodified lines58 */59 60 request: function(event) {61 event.request.home = set('home') || set('basepath') || '/'62- event.request.back = event.request.header('referrer') || event.request.header('referer')62+ event.request.back = event.request.header('Referrer') || event.request.header('Referer')63 }64 }65 })modified:lib/express/plugins/static.js+25 added-5 removed
38 unmodified lines39 40 Request.include({41 42 /**43- * Transfer static file at the given _path_. All _options_44- * are passed to fs.createReadStream().43+ * Transfer static file at the given _path_ with optional _callback_.44+ * _options_ are passed to fs.createReadStream().45 *46 * @param {string} path47 * @param {object} options48+ * @param {function} callback49 * @return {Request}50 * @api public51 */52 52- sendfile: function(path, options) {53+ sendfile: function(path, options, callback) {54+ var self = this55+ if (options instanceof Function)56+ callback = options,57+ options = {}58 if (path.indexOf('..') !== -1)59 Error.raise('InvalidPathError', "`" + path + "' is not a valid path")55- this.response.chunkedEncoding = true56- return this.stream(fs.createReadStream(path, options))60+ fs.stat(path, function(err, stat){61+ if (err)62+ if (callback !== undefined)63+ return callback(err)64+ else65+ return 'errno' in err && err.errno === 266+ ? self.notFound()67+ : self.error(err)68+ var etag = stat.ino + '-' + stat.size + '-' + Number(stat.mtime)69+ if (self.header('If-None-Match') && 70+ self.header('If-None-Match') == etag)71+ return self.halt(304, null)72+ self.header('Content-Length', stat.size)73+ self.header('ETag', etag)74+ self.stream(fs.createReadStream(path, options))75+ })76+ return this77 },78 79 /**80 * Transfer static _file_ as an attachment.17 unmodified linesmodified:lib/express/spec/mocks.js+1 added-1 removed
89 unmodified lines90 /**91 * Store _code_ and _headers_.92 */93 94- writeHeader: function(code, headers) {94+ writeHead: function(code, headers) {95 this.status = code96 this.headers = headers97 },98 80 unmodified linesmodified:lib/express/core.js+3 added-3 removed
225 unmodified lines226 request.error(err)227 else if (!pendingFiles)228 self.route(request)229 }230- if (request.header('content-type') &&231- request.header('content-type').includes('multipart/form-data')) {230+ if (request.header('Content-Type') &&231+ request.header('Content-Type').includes('multipart/form-data')) {232 var stream,233- contentLength = parseInt(request.header('content-length')),233+ contentLength = parseInt(request.header('Content-Length')),234 maxBodyLength = set('max upload size')235 if (maxBodyLength && contentLength > maxBodyLength)236 return callback(new Error('upload size limit exceeded'))237 stream = multipart.parse(req)98 unmodified linesmodified:lib/express/http.js+2 added-2 removed
37 unmodified lines38 data = queryString.stringify(data)39 if (method === 'GET')40 search += (search ? '&' : '?') + data41 else42- headers['content-length'] = data.length,43- headers['content-type'] = 'application/x-www-form-urlencoded'42+ headers['Content-Length'] = data.length,43+ headers['Content-Type'] = 'application/x-www-form-urlencoded'44 }45 var req = client.request(method, path + search + hash, headers)46 if (data && method !== 'GET') req.write(data)47 req.addListener('response', function(res){45 unmodified linesmodified:lib/express/request.js+43 added-23 removed
72 unmodified lines73 74 header: function(key, val) {75 return val === undefined76 ? this.headers[key.lowercase]77- : this.response.headers[key.lowercase] = val77+ : this.response.headers[key] = val78 },79 80 /**81 * Return the param _key_ value or undefined.52 unmodified lines134 * @api public135 */136 137 get isXHR() {138- return (this.header('x-requested-with') || '').lowercase === 'xmlhttprequest'138+ return (this.header('X-Requested-With') || '').lowercase === 'xmlhttprequest'139 },140 141 /**142 * Set response status to _code_.30 unmodified lines173 if (encoding instanceof Function)174 callback = encoding,175 encoding = null176 this.status(code = code || 404)177- if (body = body || statusBodies[code])178- return this.respond(body, encoding, callback)177+ if (body !== null)178+ body = body || statusBodies[code]179+ return this.respond(body, encoding, callback)180 },181 182 /**183 * Respond with _body_ and optional _encoding_.7 unmodified lines191 respond: function(body, encoding, callback) {192 var self = this193 this.response.body = body194 this.trigger('response', function(err) {194- if (err)195- if (callback !== undefined) callback(err)196- else self.error(err)197- self.response.writeHeader(self.response.status, self.response.headers)198- self.response.write(self.response.body, encoding)195+ if (err) self.error(err, callback)196+ // TODO: revisit. when this is true node will add Transfer-Encoding: chunked197+ // along with a response body, which is not ideal when the user attemps198+ // to respond with no body199+ if (!body) self.response.useChunkedEncodingByDefault = false200+ self.sendHead()201+ if (body) self.response.write(self.response.body, encoding)202 self.response.end()203 }, true)204 },205 206 /**207 * Transfer the given _stream_ with optional _callback_.208 *209 * @param {Stream} stream210+ * @param {function} callback211 * @return {Request}212 * @api public213 */214 2 unmodified lines217 first = true218 stream219 .addListener('error', function(err){220 if (first)217- return 'errno' in err && err.errno === 2218- ? self.notFound()219- : self.error(err)221+ return self.error(err, callback)222 self.response.end()223 if (callback !== undefined) callback(err)224 })225 .addListener('data', function(data){226 if (first) {225- self.status(200)227 first = false228+ self.header('Transfer-Encoding', 'chunked')229+ self.status(200)230 self.contentType(stream.path)228- self.response.writeHeader(self.response.status, self.response.headers)231+ self.sendHead()232 }233 self.response.write(data, 'binary')234 })235 .addListener('end', function(){233- self.response.end()236 self.trigger('response', function(err){235- if (err) {236- stream.destroy()237- if (callback !== undefined) callback(err)238- else self.error(err)239- }237+ if (err)238+ stream.destroy(),239+ self.error(err, callback)240+ else241+ self.response.end()242 }, true)243 })244 },245 246 /**247+ * Send the response header.248+ *249+ * @return {Request}250+ * @api public251+ */252+ 253+ sendHead: function(){254+ this.response.writeHead(this.response.status, this.response.headers)255+ return this256+ },257+ 258+ /**259 * Pass control to the next matching route, or260 * the given _path_.261 *262 * NOTE: _path_ may be the request pathname only,17 unmodified lines280 * @api public281 */282 283 contentType: function(path) {270- this.header('content-type', mime.type(path))284+ this.header('Content-Type', mime.type(path))285 return this286 },287 288 /**40 unmodified lines329 * @api public330 */331 332 attachment: function(filename) {319- this.header('content-disposition', filename333+ this.header('Content-Disposition', filename334 ? 'attachment; filename="' + filename + '"'335 : 'attachment')336 return this337 },15 unmodified lines353 *354 * When "throw exceptions" is enabled the error will be355 * re-thrown, terminating the process (unless otherwise caught).356 *357+ * Also a _callback_ function may be supplied, which when defined358+ * will be called, by-passing the process mentioned above.359+ *360 * @param {Error} err361+ * @param {function} callback362 * @return {Request}363 * @settings 'throw exceptions', 'show exceptions'364 * @api public365 */366 349- error: function(err) {367+ error: function(err, callback) {368+ if (callback)369+ return callback(err)370 if (Express.error)371 Express.error.call(this, err)372 else if (this.accepts('html') && set('show exceptions'))373 this.halt(500, showExceptions.render(this, err))36 unmodified linesmodified:spec/spec.plugins.content-length.js+3 added-3 removed
10 unmodified lines11 it 'should set the content-length header'12 get('/style.css', function(){13 return 'body { background: #000; }'14 })15- get('/style.css').headers['content-length'].should.eql 2615+ get('/style.css').headers['Content-Length'].should.eql 2616 end17 18 it 'should not override when manually set'19 get('/style.css', function(){20- this.header('content-length', 10)20+ this.header('Content-Length', 10)21 return 'body { background: #000; }'22 })23- get('/style.css').headers['content-length'].should.eql 1023+ get('/style.css').headers['Content-Length'].should.eql 1024 end25 end26 end27 end1 unmodified linemodified:spec/spec.plugins.cookie.js+2 added-2 removed
71 unmodified lines72 get('/user', function(){73 this.cookie('SID', '732423sdfs73243', { path: '/', secure: true })74 return '' 75 })76- get('/user').headers['set-cookie'].should.eql 'SID=732423sdfs73243; path=/; secure'76+ get('/user').headers['Set-Cookie'].should.eql 'SID=732423sdfs73243; path=/; secure'77 end78 79 it 'should set multiple cookies'80 get('/user', function(){81 this.cookie('SID', '732423sdfs73243', { path: '/', secure: true })82 this.cookie('foo', 'bar')83 return '' 84 })85- get('/user').headers['set-cookie'].should.eql 'SID=732423sdfs73243; path=/; secure\nset-cookie: foo=bar; path=/'85+ get('/user').headers['Set-Cookie'].should.eql 'SID=732423sdfs73243; path=/; secure\nSet-Cookie: foo=bar; path=/'86 end87 end88 end89 11 unmodified linesmodified:spec/spec.plugins.js+1 added-1 removed
7 unmodified lines8 }9 },10 on: {11 response: function(event) {12- if (event.response.headers['content-type'] == mime.type('css'))12+ if (event.response.headers['Content-Type'] === mime.type('css'))13 event.response.body = event.response.body.replace('black', '#000')14 }15 }16 })25 unmodified linesmodified:spec/spec.plugins.redirect.js+7 added-7 removed
10 unmodified lines11 get('/logout', function(){12 this.redirect('/')13 })14 get('/logout').status.should.eql 30315- get('/logout').headers['location'].should.eql '/'15+ get('/logout').headers['Location'].should.eql '/'16 end17 18 it 'should allow optional status code'19 get('/logout', function(){20 this.redirect('/', 304)21 })22 get('/logout').status.should.eql 30423- get('/logout').headers['location'].should.eql '/'23+ get('/logout').headers['Location'].should.eql '/'24 end25 end26 27 describe 'redirect("back")'1 unmodified line29 get('/logout', function(){30 this.redirect('back')31 })32 var response = get('/logout', { headers: { referrer: '/login' }})33- response.headers['location'].should.eql '/login'33+ response.headers['Location'].should.eql '/login'34 end35 36 it 'should check referer header'37 get('/logout', function(){38 this.redirect('back')39 })40 var response = get('/logout', { headers: { referer: '/login' }})41- response.headers['location'].should.eql '/login'41+ response.headers['Location'].should.eql '/login'42 end43 end44 45 describe 'redirect(home)'1 unmodified line47 set('home', '/login')48 get('/logout', function(){49 this.redirect('home')50 })51- get('/logout').headers['location'].should.eql '/login'51+ get('/logout').headers['Location'].should.eql '/login'52 end53 54 it 'should check set("basepath")'55 set('basepath', '/web/')56 get('/logout', function(){57 this.redirect('home')58 })59- get('/logout').headers['location'].should.eql '/web/'59+ get('/logout').headers['Location'].should.eql '/web/'60 end61 62 it 'should default to "/"'63 get('/logout', function(){64 this.redirect('home')65 })66- get('/logout').headers['location'].should.eql '/'66+ get('/logout').headers['Location'].should.eql '/'67 end68 end69 end70 endmodified:spec/spec.plugins.session.js+3 added-3 removed
10 unmodified lines11 describe 'Session'12 describe 'when sid cookie is not present'13 it 'should set sid cookie'14 get('/login', function(){ return '' })15- get('/login').headers['set-cookie'].should.match(/^sid=(\w+);/)15+ get('/login').headers['Set-Cookie'].should.match(/^sid=(\w+);/)16 end17 18 describe 'and requesting /favicon.ico'19 it 'should not set sid cookie'6 unmodified lines26 describe 'when existing sid cookie is present'27 it 'should not set sid'28 Session.store.commit(new Base(123))29 get('/login', function(){ return '' })30- get('/login', { headers: { cookie: 'sid=123' }}).headers.should.not.have_property 'set-cookie'30+ get('/login', { headers: { cookie: 'sid=123' }}).headers.should.not.have_property 'Set-Cookie'31 end32 end33 34 describe 'when unknown sid cookie is present'35 it 'should set new sid'36 get('/login', function(){ return '' })37 var headers= get('/login', { headers: { cookie: 'sid=123' }}).headers38 39- headers.should.have_property 'set-cookie'39+ headers.should.have_property 'Set-Cookie'40 headers['set-cookie'].should.not.be '123'41 end42 end 43 97 unmodified linesmodified:spec/spec.plugins.view.js+1 added-1 removed
109 unmodified lines110 it 'should set the content type based on the last path segment'111 get('/', function(){112 this.render('hello.html.haml')113 })114- get('/').headers['content-type'].should.eql 'text/html'114+ get('/').headers['Content-Type'].should.eql 'text/html'115 end116 end117 118 describe 'and layout of the same type does not exist'86 unmodified linesmodified:spec/spec.request.js+3 added-3 removed
130 unmodified lines131 get('/style.css', function(){132 this.contentType('css')133 return 'body { background: white; }'134 })135- get('/style.css').headers['content-type'].should.eql 'text/css'135+ get('/style.css').headers['Content-Type'].should.eql 'text/css'136 end137 end138 139 describe '#attachment()'1 unmodified line141 get('/report', function(){142 this.attachment()143 return 'foo'144 })145- get('/report').headers['content-disposition'].should.eql 'attachment'145+ get('/report').headers['Content-Disposition'].should.eql 'attachment'146 end147 148 it 'should set attachment filename'149 get('/report', function(){150 this.attachment('report.pdf')151 return 'foo'152 })153- get('/report').headers['content-disposition'].should.eql 'attachment; filename="report.pdf"'153+ get('/report').headers['Content-Disposition'].should.eql 'attachment; filename="report.pdf"'154 end155 end156 157 describe '#param()'212 unmodified lines