Skip to content
Myr

More refactoring to allow everything to be request-specific

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

authored by visionmedia · committed by visionmedia · parent 76cf65f · Browse files · 4 files+234 added−227 removed
This commit

State

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

Diff stats

4 files+234 added−227 removed

  • tests:+212 added−212 removed
  • other:+22 added−15 removed

4 files+234 added−227 removed

modified:lib/express/core.js+22 added-15 removed
97 unmodified lines
9899Router = Class({100 101 /**102 * Route the given _request_, returning103 * the value returned by Route#run()102 * Initialize with _request_.103 *105 * @param {object} request106 * @return {mixed}104 * @param {Request} request105 * @api private106 */107 110 route: function(request){108 init: function(request) {109 this.request = request110 },111 112 /**113 * Route #request returning the value returned by Route#run()114 *115 * @return {mixed}116 * @api private117 */118 119 route: function(){120 var route = this.matchingRoute()121 if (route)122 return route.run(this.request.captures.slice(1))115 else if (accepts('html') && set('helpful 404'))116 halt(404, require('express/pages/not-found').render())123 else if (this.request.accepts('html') && set('helpful 404'))124 this.request.halt(404, require('express/pages/not-found').render())125 else118 halt()126 this.request.halt()127 },128 129 /**130 * Attempt to match a route from Express.routes.
96 unmodified lines
227 route: function(request, response){228 request = new Request(request, response)229 request.trigger('request')230 try {223 if (typeof (body = (new Express.router).route(request)) == 'string')224 halt(200, body)231 if (typeof (body = (new Router(request)).route()) == 'string')232 request.halt(200, body)233 }234 catch (e) {235 if (e instanceof ExpressError)236 throw e229 if (accepts('html') && set('show exceptions'))230 halt(500, require('express/pages/show-exceptions').render(e))237 if (request.accepts('html') && set('show exceptions'))238 request.halt(500, require('express/pages/show-exceptions').render(e))239 else232 halt(500)240 request.halt(500)241 if (set('throw exceptions'))242 throw e243 }244 },
22 unmodified lines
267 config: [],268 routes: [],269 plugins: [],270 settings: {},263 server: new Server,264 router: Router271 server: new Server272}273274// --- Defaults275
3 unmodified lines
deleted:spec/spec.helpers.js-211 removed
12describe 'Express'3 before_each4 reset()5 end6 7 describe 'parseParams()'8 it 'should parse simple query string key / value pairs'9 parseParams('foo=bar').should.eql { foo: 'bar' }10 parseParams('foo=bar&baz=1').should.eql { foo: 'bar', baz: '1' }11 end12 13 it 'should parse named nested params'14 var user = { user: { name: 'tj', email: 'tj@vision-media.ca' }}15 parseParams('user[name]=tj&user[email]=tj@vision-media.ca').should.eql user16 end17 18 it 'should parse several levels of nesting'19 var user = { user: { name: 'tj', email: { primary: 'tj@vision-media.ca' }}}20 parseParams('user[name]=tj&user[email][primary]=tj@vision-media.ca').should.eql user21 end22 23 it 'should convert + to literal space'24 parseParams('foo=bar+baz++that').should.eql { foo: 'bar baz that' }25 parseParams('user[name]=tj+holowaychuk').should.eql { user: { name: 'tj holowaychuk' }}26 end27 28 it 'should decode hex literals'29 parseParams('foo=bar%20baz%20%20that').should.eql { foo: 'bar baz that' }30 parseParams('user[name]=tj%20holowaychuk').should.eql { user: { name: 'tj holowaychuk' }}31 end32 end33 34 describe 'toArray()'35 describe 'when given an array'36 it 'should return the array'37 toArray([1,2,3]).should.eql [1,2,3]38 end39 end40 41 describe 'when given an object with indexed values and length'42 it 'should return an array'43 var args = -{ return arguments }('foo', 'bar')44 toArray(args).should.eql ['foo', 'bar']45 end46 end47 end48 49 describe 'escape()'50 it 'should escape html'51 escape('<p>this & that').should.eql '&lt;p&gt;this &amp; that'52 end53 end54 55 describe 'extname()'56 it 'should return the a files extension'57 extname('image.png').should.eql 'png'58 extname('image.large.png').should.eql 'png'59 extname('/path/to/image.large.png').should.eql 'png'60 end61 62 it 'should return null when not found'63 extname('path').should.be_null64 extname('/just/a/path').should.be_null65 end66 end67 68 describe 'dirname()'69 it 'should return the directory path'70 dirname('/path/to/images/foo.bar.png').should.eql '/path/to/images'71 end72 end73 74 describe 'status()'75 it 'should set the response status'76 get('/user', function(){ status(500) })77 get('/user').status.should.eql 50078 end79 end80 81 describe 'header()'82 describe 'when given a field name and value'83 it 'should set the header'84 get('/user', function(){85 header('x-foo', 'bar')86 })87 get('/user').headers.should.have_property 'x-foo', 'bar'88 end 89 end90 91 describe 'when given a field name'92 it 'should return a request header value'93 get('/user', function(){94 return header('host')95 })96 get('/user').body.should.eql 'localhost'97 end98 99 it 'should be case-insensitive'100 get('/user', function(){101 return header('Host')102 })103 get('/user').body.should.eql 'localhost'104 end105 end106 end107 108 describe 'halt()'109 describe 'when given no arguments'110 it 'should respond with 404 Not Found'111 get('/user', function(){ halt() })112 get('/user').status.should.eql 404113 get('/user').body.should.include('Not Found')114 end115 end116 117 describe 'when given a status code'118 it 'should respond with that status and its associated default body'119   get('/user', function(){ halt(400) })120   get('/user').status.should.eql 400121   get('/user').body.should.include('Bad Request')122 end123 end124 125 describe 'when given a status code and body'126 it 'should respond with the status and its body'127 get('/user', function(){ halt(400, 'Oh noes!') })128 get('/user').status.should.eql 400129 get('/user').body.should.include('Oh noes!')130 end131 end132 133 describe 'when given an invalid status code'134 it 'should throw an InvalidStatusCode exception'135 // TODO: throw_error(InvalidStatusCode, ...) when jspec is fixed136 get('/user', function(){ halt(123123) })137 -{ get('/user') }.should.throw_error(/InvalidStatusCode: 123123 is an invalid HTTP response code/)138 try { get('/user') }139 catch (e) {140 e.should.be_an_instance_of ExpressError141 e.should.be_an_instance_of InvalidStatusCode142 }143 end144 end145 end146 147 describe 'contentType()'148 it 'should set Content-Type header with mime type passed'149 get('/style.css', function(){150 contentType('css')151 return 'body { background: white; }'152 })153 get('/style.css').headers['content-type'].should.eql 'text/css'154 end155 end156 157 describe 'param()'158 it 'should return a route placeholder value'159 get('/user/:id', function(){160 return 'user ' + param('id')161 })162 get('/user/12').body.should.eql 'user 12'163 end164 165 it 'should return several route placeholder values'166 get('/user/:id/:operation', function(){167 return param('operation') + 'ing user ' + param('id') 168 })169 get('/user/12/edit').body.should.eql 'editing user 12'170 end171 172 it 'should allow optional placeholders'173 get('/user/:id?', function(){174 return param('id') ? 'user ' + param('id') : 'users'175 })176 get('/user/12').body.should.eql 'user 12'177 get('/user').body.should.eql 'users'178 end179 180 it 'should allow placeholders as part of a segment'181 get('/report.:format', function(){182 return 'report as ' + param('format')183 })184 get('/report.csv').body.should.eql 'report as csv'185 get('/report.pdf').body.should.eql 'report as pdf'186 end187 188 it 'should allow optional placeholders in middle segments'189 get('/user/:id?/edit', function(){190 return param('id') ? 'editing ' + param('id') : 'editing your account'191 })192 get('/user/12/edit').body.should.eql 'editing 12'193 get('/user/edit').body.should.eql 'editing your account'194 end195 196 it 'should work with several routes'197 get('/user/:id', function(){ return param('id') })198 get('/product/:name', function(){ return param('name') })199 get('/user/12').body.should.eql '12'200 get('/product/ipod').body.should.eql 'ipod'201 end202 203 it 'should access request.uri.params'204 get('/user', function(){205 return param('page') || 'First page'206 })207 get('/user').body.should.eql 'First page'208 get('/user', { uri: { params: { page: '2' }}}).body.should.eql '2'209 end210 end211end
modified:spec/spec.node.js+1 added-1 removed
added:spec/spec.request.js+211 added
12describe 'Express'3 before_each4 reset()5 end6 7 describe 'parseParams()'8 it 'should parse simple query string key / value pairs'9 parseParams('foo=bar').should.eql { foo: 'bar' }10 parseParams('foo=bar&baz=1').should.eql { foo: 'bar', baz: '1' }11 end12 13 it 'should parse named nested params'14 var user = { user: { name: 'tj', email: 'tj@vision-media.ca' }}15 parseParams('user[name]=tj&user[email]=tj@vision-media.ca').should.eql user16 end17 18 it 'should parse several levels of nesting'19 var user = { user: { name: 'tj', email: { primary: 'tj@vision-media.ca' }}}20 parseParams('user[name]=tj&user[email][primary]=tj@vision-media.ca').should.eql user21 end22 23 it 'should convert + to literal space'24 parseParams('foo=bar+baz++that').should.eql { foo: 'bar baz that' }25 parseParams('user[name]=tj+holowaychuk').should.eql { user: { name: 'tj holowaychuk' }}26 end27 28 it 'should decode hex literals'29 parseParams('foo=bar%20baz%20%20that').should.eql { foo: 'bar baz that' }30 parseParams('user[name]=tj%20holowaychuk').should.eql { user: { name: 'tj holowaychuk' }}31 end32 end33 34 describe 'toArray()'35 describe 'when given an array'36 it 'should return the array'37 toArray([1,2,3]).should.eql [1,2,3]38 end39 end40 41 describe 'when given an object with indexed values and length'42 it 'should return an array'43 var args = -{ return arguments }('foo', 'bar')44 toArray(args).should.eql ['foo', 'bar']45 end46 end47 end48 49 describe 'escape()'50 it 'should escape html'51 escape('<p>this & that').should.eql '&lt;p&gt;this &amp; that'52 end53 end54 55 describe 'extname()'56 it 'should return the a files extension'57 extname('image.png').should.eql 'png'58 extname('image.large.png').should.eql 'png'59 extname('/path/to/image.large.png').should.eql 'png'60 end61 62 it 'should return null when not found'63 extname('path').should.be_null64 extname('/just/a/path').should.be_null65 end66 end67 68 describe 'dirname()'69 it 'should return the directory path'70 dirname('/path/to/images/foo.bar.png').should.eql '/path/to/images'71 end72 end73 74 describe 'status()'75 it 'should set the response status'76 get('/user', function(){ status(500) })77 get('/user').status.should.eql 50078 end79 end80 81 describe 'header()'82 describe 'when given a field name and value'83 it 'should set the header'84 get('/user', function(){85 header('x-foo', 'bar')86 })87 get('/user').headers.should.have_property 'x-foo', 'bar'88 end 89 end90 91 describe 'when given a field name'92 it 'should return a request header value'93 get('/user', function(){94 return header('host')95 })96 get('/user').body.should.eql 'localhost'97 end98 99 it 'should be case-insensitive'100 get('/user', function(){101 return header('Host')102 })103 get('/user').body.should.eql 'localhost'104 end105 end106 end107 108 describe 'halt()'109 describe 'when given no arguments'110 it 'should respond with 404 Not Found'111 get('/user', function(){ halt() })112 get('/user').status.should.eql 404113 get('/user').body.should.include('Not Found')114 end115 end116 117 describe 'when given a status code'118 it 'should respond with that status and its associated default body'119   get('/user', function(){ halt(400) })120   get('/user').status.should.eql 400121   get('/user').body.should.include('Bad Request')122 end123 end124 125 describe 'when given a status code and body'126 it 'should respond with the status and its body'127 get('/user', function(){ halt(400, 'Oh noes!') })128 get('/user').status.should.eql 400129 get('/user').body.should.include('Oh noes!')130 end131 end132 133 describe 'when given an invalid status code'134 it 'should throw an InvalidStatusCode exception'135 // TODO: throw_error(InvalidStatusCode, ...) when jspec is fixed136 get('/user', function(){ halt(123123) })137 -{ get('/user') }.should.throw_error(/InvalidStatusCode: 123123 is an invalid HTTP response code/)138 try { get('/user') }139 catch (e) {140 e.should.be_an_instance_of ExpressError141 e.should.be_an_instance_of InvalidStatusCode142 }143 end144 end145 end146 147 describe 'contentType()'148 it 'should set Content-Type header with mime type passed'149 get('/style.css', function(){150 contentType('css')151 return 'body { background: white; }'152 })153 get('/style.css').headers['content-type'].should.eql 'text/css'154 end155 end156 157 describe 'param()'158 it 'should return a route placeholder value'159 get('/user/:id', function(){160 return 'user ' + param('id')161 })162 get('/user/12').body.should.eql 'user 12'163 end164 165 it 'should return several route placeholder values'166 get('/user/:id/:operation', function(){167 return param('operation') + 'ing user ' + param('id') 168 })169 get('/user/12/edit').body.should.eql 'editing user 12'170 end171 172 it 'should allow optional placeholders'173 get('/user/:id?', function(){174 return param('id') ? 'user ' + param('id') : 'users'175 })176 get('/user/12').body.should.eql 'user 12'177 get('/user').body.should.eql 'users'178 end179 180 it 'should allow placeholders as part of a segment'181 get('/report.:format', function(){182 return 'report as ' + param('format')183 })184 get('/report.csv').body.should.eql 'report as csv'185 get('/report.pdf').body.should.eql 'report as pdf'186 end187 188 it 'should allow optional placeholders in middle segments'189 get('/user/:id?/edit', function(){190 return param('id') ? 'editing ' + param('id') : 'editing your account'191 })192 get('/user/12/edit').body.should.eql 'editing 12'193 get('/user/edit').body.should.eql 'editing your account'194 end195 196 it 'should work with several routes'197 get('/user/:id', function(){ return param('id') })198 get('/product/:name', function(){ return param('name') })199 get('/user/12').body.should.eql '12'200 get('/product/ipod').body.should.eql 'ipod'201 end202 203 it 'should access request.uri.params'204 get('/user', function(){205 return param('page') || 'First page'206 })207 get('/user').body.should.eql 'First page'208 get('/user', { uri: { params: { page: '2' }}}).body.should.eql '2'209 end210 end211end