Skip to content
Myr

spec.request.js

This file

Path

spec/spec.request.js

420 lines · 13.9 KiB · JavaScript

Last changed by

Added "encoding" option to Request#render(). Closes #299visionmediabefore adoptionc93cfa0

History of this file

Take it

The walk stopped after 200 commits: lines older than 4c246a4 point at it.

Merge branch 'async-plugins'visionmediabefore adoption4c246a4

lines 1–13why, one step back
1
2describe 'Express'3 before_each4 reset()5 end6 7 describe 'Request'8 describe '#status()'9 it 'should set the response status'10 get('/user', function(){ this.status(500) })11 get('/user').status.should.eql 50012 end13 end

Added "encoding" option to Request#render(). Closes #299visionmediabefore adoptionc93cfa0

lines 14–26why, one step back
14
15 describe '#charset'16 describe 'when defined'17 it 'should append "; charset=CHARSET'18 get('/user', function(){19 this.contentType('html')20 this.charset = 'UTF-8'21 return '∂'22 })23 get('/user').headers['Content-Type'].should.eql 'text/html; charset=UTF-8'24 end25 end26 end

Merge branch 'async-plugins'visionmediabefore adoption4c246a4

lines 27–54why, one step back
27
28 describe '#header()'29 describe 'when given a field name and value'30 it 'should set the header'31 get('/user', function(){32 this.header('x-foo', 'bar')33 })34 get('/user').headers.should.have_property 'x-foo', 'bar'35 end 36 end3738 describe 'when given a field name'39 it 'should return a request header value'40 get('/user', function(){41 return this.header('host')42 })43 get('/user').body.should.eql 'localhost'44 end4546 it 'should be case-insensitive'47 get('/user', function(){48 return this.header('Host')49 })50 get('/user').body.should.eql 'localhost'51 end52 end53 end54

Added Request#isXHR. Closes #229visionmediabefore adoption884d333

lines 55–71why, one step back
55 describe '#isXHR'
56 it 'should return false unless X-Requested-With is "XMLHttpRequest"'57 get('/', function(){ return this.isXHR ? 'yay' : 'nope' })58 get('/').body.should.eql 'nope'59 end60 61 it 'should return true when X-Requested-With is "XMLHttpRequest"'62 get('/', function(){ return this.isXHR ? 'yay' : 'nope' })63 get('/', { headers: { 'x-requested-with': 'XMLHttpRequest' }}).body.should.eql 'yay'64 end65 66 it 'should be case insensitive'67 get('/', function(){ return this.isXHR ? 'yay' : 'nope' })68 get('/', { headers: { 'x-requested-with': 'xmlhttprequest' }}).body.should.eql 'yay'69 end70 end71

Merge branch 'async-plugins'visionmediabefore adoption4c246a4

lines 72–87why, one step back
72 describe '#accepts()'
73 describe 'when the Accept header is present'74 it 'should return true if the mime type is acceptable'75 get('/user', function(){ return this.accepts('jpeg').toString() })76 get('/user', { headers: { accept: 'image/jpeg' }}).body.should.eql 'true'77 end78 79 it 'should return false if the mime type is not present'80 get('/user', function(){ return this.accepts('html').toString() })81 get('/user', { headers: { accept: 'image/jpeg' }}).body.should.eql 'false'82 end83 end84 85 describe 'when the Accept header is not present'86 it 'should return true'87 get('/user', function(){ return this.accepts('jpeg').toString() })

Removed utils.mixin() use Object#mergeDeep()visionmediabefore adoption33277c3

line 88why, one step back
88 get('/user', { headers: { accept: '' }}).body.should.eql 'true'

Merge branch 'async-plugins'visionmediabefore adoption4c246a4

lines 89–147why, one step back
89 end
90 end91 92 describe 'should allow multiple arguments'93 it 'should return true if any mime type is present'94 get('/user', function(){ return this.accepts('jpeg', 'png').toString() })95 get('/user', { headers: { accept: 'image/gif,image/png' }}).body.should.eql 'true'96 end97 98 it 'should return false if none of the mime types are present'99 get('/user', function(){ return this.accepts('jpeg', 'png').toString() })100 get('/user', { headers: { accept: 'text/plain,text/html' }}).body.should.eql 'false'101 end102 end103 104 describe 'when a media type range was sent'105 it 'should return true if the group media type matches'106 get('/user', function(){ return this.accepts('html').toString() })107 get('/user', { headers: { accept: 'text/plain,text/*' }}).body.should.eql 'true'108 end109 it 'should return false if the group media type does not match'110 get('/user', function(){ return this.accepts('ogg').toString() })111 get('/user', { headers: { accept: 'text/plain,text/*' }}).body.should.eql 'false'112 end113 end114 end115 116 describe '#halt()'117 describe 'when given no arguments'118 it 'should respond with 404 Not Found'119 get('/user', function(){ this.halt() })120 get('/user').status.should.eql 404121 get('/user').body.should.include('Not Found')122 end123 end124125 describe 'when given a status code'126 it 'should respond with that status and its associated default body'127   get('/user', function(){ this.halt(400) })128   get('/user').status.should.eql 400129   get('/user').body.should.include('Bad Request')130 end131 end132133 describe 'when given a status code and body'134 it 'should respond with the status and its body'135 get('/user', function(){ this.halt(400, 'Oh noes!') })136 get('/user').status.should.eql 400137 get('/user').body.should.include('Oh noes!')138 end139 end140 end141142 describe '#contentType()'143 it 'should set Content-Type header with mime type passed'144 get('/style.css', function(){145 this.contentType('css')146 return 'body { background: white; }'147 })

Merge branch 'etag'visionmediabefore adoptionb5d6f1a

line 148why, one step back
148 get('/style.css').headers['Content-Type'].should.eql 'text/css'

Merge branch 'async-plugins'visionmediabefore adoption4c246a4

lines 149–157why, one step back
149 end
150 end151 152 describe '#attachment()'153 it 'should set Content-Disposition to attachment'154 get('/report', function(){155 this.attachment()156 return 'foo'157 })

Merge branch 'etag'visionmediabefore adoptionb5d6f1a

line 158why, one step back
158 get('/report').headers['Content-Disposition'].should.eql 'attachment'

Merge branch 'async-plugins'visionmediabefore adoption4c246a4

lines 159–165why, one step back
159 end
160161 it 'should set attachment filename'162 get('/report', function(){163 this.attachment('report.pdf')164 return 'foo'165 })

Merge branch 'etag'visionmediabefore adoptionb5d6f1a

line 166why, one step back
166 get('/report').headers['Content-Disposition'].should.eql 'attachment; filename="report.pdf"'

Merge branch 'async-plugins'visionmediabefore adoption4c246a4

lines 167–217why, one step back
167 end
168 end169170 describe '#param()'171 it 'should return a route placeholder value'172 get('/user/:id', function(){173 return 'user ' + this.param('id')174 })175 get('/user/12').body.should.eql 'user 12'176 end177178 it 'should return several route placeholder values'179 get('/user/:id/:operation', function(){180 return this.param('operation') + 'ing user ' + this.param('id') 181 })182 get('/user/12/edit').body.should.eql 'editing user 12'183 end184185 it 'should allow optional placeholders'186 get('/user/:id?', function(){187 return this.param('id') ? 'user ' + this.param('id') : 'users'188 })189 get('/user/12').body.should.eql 'user 12'190 get('/user').body.should.eql 'users'191 end192193 it 'should allow placeholders as part of a segment'194 get('/report.:format', function(){195 return 'report as ' + this.param('format')196 })197 get('/report.csv').body.should.eql 'report as csv'198 get('/report.pdf').body.should.eql 'report as pdf'199 end200201 it 'should allow optional placeholders in middle segments'202 get('/user/:id?/edit', function(){203 return this.param('id') ? 'editing ' + this.param('id') : 'editing your account'204 })205 get('/user/12/edit').body.should.eql 'editing 12'206 get('/user/edit').body.should.eql 'editing your account'207 end208209 it 'should work with several routes'210 get('/user/:id', function(){ return this.param('id') })211 get('/product/:name', function(){ return this.param('name') })212 get('/user/12').body.should.eql '12'213 get('/product/ipod').body.should.eql 'ipod'214 end215216 it 'should work with a query string'217 get('/user', function(){

Merge branch 'fix-querystring-0'visionmediabefore adoption7cf18c0

lines 218–219why, one step back
218 var page = this.param('page')
219 return page === undefined ? 'First page' : String(page)

Merge branch 'async-plugins'visionmediabefore adoption4c246a4

lines 220–221why, one step back
220 })
221 get('/user').body.should.eql 'First page'

Merge branch 'fix-querystring-0'visionmediabefore adoption7cf18c0

line 222why, one step back
222 get('/user?page=0').body.should.eql '0'

Merge branch 'async-plugins'visionmediabefore adoption4c246a4

lines 223–232why, one step back
223 get('/user?page=2').body.should.eql '2'
224 get('/user?foo[]=bar&page=5').body.should.eql '5'225 end226227 it 'should work with splats'228 get('/:path/*.*', function(path, file, ext){229 return [path, file, ext].join(', ')230 })231 get('/public/app.js').body.should.eql 'public, app, js'232 end

Merge branch 'pull'visionmediabefore adoptionf008af0

lines 233–236why, one step back
233 end
234 235 describe '#pass()'236 it 'should pass control to the next matching route'

Request#pass() accepts a pathname stringvisionmediabefore adoption300cfe7

line 237why, one step back
237 get('/user', function(){

Merge branch 'pull'visionmediabefore adoptionf008af0

lines 238–239why, one step back
238 this.pass()
239 })

Request#pass() accepts a pathname stringvisionmediabefore adoption300cfe7

line 240why, one step back
240 get('/user', function(){

Merge branch 'pull'visionmediabefore adoptionf008af0

lines 241–243why, one step back
241 this.pass()
242 return 'nodejs'243 })

Request#pass() accepts a pathname stringvisionmediabefore adoption300cfe7

line 244why, one step back
244 get('/user', function(){ return 'success'})

Merge branch 'pull'visionmediabefore adoptionf008af0

lines 245–246why, one step back
245 get('/user').body.should.eql 'success'
246 end

Request#pass() accepts a pathname stringvisionmediabefore adoption300cfe7

lines 247–259why, one step back
247
248 describe 'given a string'249 it 'should pass to the given route'250 get('/user', function(){251 this.pass('/user/1')252 })253 get('/user/:id', function(){254 return 'Supa doopa usa'255 })256 get('/user').body.should.eql 'Supa doopa usa'257 end258 end259 end

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 260–261why, one step back
260
261 describe '#error()'

Added DSL level error() route supportvisionmediabefore adoptionb958393

lines 262–263why, one step back
262 describe 'when an error route is defined'
263 it 'should be called'

error() is passed the exceptionvisionmediabefore adoption1c0e2ce

line 264why, one step back
264 var err

Added DSL level error() route supportvisionmediabefore adoptionb958393

line 265why, one step back
265 disable('throw exceptions')

error() is passed the exceptionvisionmediabefore adoption1c0e2ce

lines 266–267why, one step back
266 global.error(function(e){
267 err = e

Added DSL level error() route supportvisionmediabefore adoptionb958393

lines 268–271why, one step back
268 this.halt(500, 'FAIL!')
269 })270 get('/', function(){ this.error(new Error('whoop')) })271 get('/').body.should.eql 'FAIL!'

error() is passed the exceptionvisionmediabefore adoption1c0e2ce

line 272why, one step back
272 err.message.should.eql 'whoop'

Added DSL level error() route supportvisionmediabefore adoptionb958393

lines 273–275why, one step back
273 end
274 end275

Added specs for errors with text/plain and application/jsonvisionmediabefore adoption132730a

lines 276–277why, one step back
276 describe 'when not accepting common mime types'
277 it 'should render the default 500 status body'

Added error reponse support for text/plain, application/json. Closes #297visionmediabefore adoptione9fdfc3

line 278why, one step back
278 var headers = { headers: { accept: 'text/xml' }}

Added specs for errors with text/plain and application/jsonvisionmediabefore adoption132730a

lines 279–288why, one step back
279 disable('throw exceptions')
280 enable('show exceptions')281 get('/', function(){282 this.error(new Error('fail!'))283 })284 get('/', headers).body.should.eql 'Internal Server Error'285 get('/', headers).status.should.eql 500286 end287 end288

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 289–299why, one step back
289 describe 'when accepting "html"'
290 describe 'with "show exceptions" enabled'291 it 'should render the show-exceptions page'292 disable('throw exceptions')293 enable('show exceptions')294 get('/', function(){295 this.error(new Error('fail!'))296 })297 get('/').body.should.include '<em>500</em> Error: fail!'298 get('/').status.should.eql 500299 end

Added error reponse support for text/plain, application/json. Closes #297visionmediabefore adoptione9fdfc3

lines 300–309why, one step back
300
301 it 'should render the show-exceptions page with errors thrown within the route'302 disable('throw exceptions')303 enable('show exceptions')304 get('/', function(){305 throw new Error('fail!')306 })307 get('/').body.should.include '<em>500</em> Error: fail!'308 get('/').status.should.eql 500309 end

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 310–311why, one step back
310 end
311

Added specs for errors with text/plain and application/jsonvisionmediabefore adoption132730a

lines 312–326why, one step back
312 describe 'with "throw exceptions" enabled'
313 it 'should re-throw the exception'314 enable('show exceptions')315 enable('throw exceptions')316 get('/', function(){317 this.error(new Error('fail!'))318 })319 -{ get('/') }.should.throw_error Error, 'fail!'320 end321 end322 end323 324 describe 'when accepting "text/plain"'325 describe 'with "show exceptions" enabled'326 it 'should render a text representation of the error'

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 327–331why, one step back
327 disable('throw exceptions')
328 enable('show exceptions')329 get('/', function(){330 this.error(new Error('fail!'))331 })

Added specs for errors with text/plain and application/jsonvisionmediabefore adoption132730a

lines 332–333why, one step back
332 get('/', { headers: { accept: 'text/plain' }}).body.should.include '500 Error: fail!'
333 get('/').status.should.eql 500

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 334–335why, one step back
334 end
335 end

Added specs for errors with text/plain and application/jsonvisionmediabefore adoption132730a

lines 336–341why, one step back
336 end
337 338 describe 'when accepting "application/json"'339 describe 'with "show exceptions" enabled'340 it 'should render a text representation of the error'341 disable('throw exceptions')

More Request#error() specsvisionmediabefore adoption54fa643

lines 342–345why, one step back
342 enable('show exceptions')
343 get('/', function(){344 this.error(new Error('fail!'))345 })

Added error reponse support for text/plain, application/json. Closes #297visionmediabefore adoptione9fdfc3

line 346why, one step back
346 get('/', { headers: { accept: 'application/json' }}).body.should.include '{"error":{"message":"fail!"'

Added specs for errors with text/plain and application/jsonvisionmediabefore adoption132730a

line 347why, one step back
347 get('/').status.should.eql 500

More Request#error() specsvisionmediabefore adoption54fa643

lines 348–349why, one step back
348 end
349 end

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 350–359why, one step back
350 end
351 352 describe 'with "show exceptions" disabled'353 it 'should render the default 500 status body'354 disable('throw exceptions')355 get('/', function(){356 this.error(new Error('fail!'))357 })358 get('/').body.should.eql 'Internal Server Error'359 get('/').status.should.eql 500

More Request#error() specsvisionmediabefore adoption54fa643

lines 360–369why, one step back
360 end
361 end362 363 describe 'with "throw exceptions" enabled'364 it 'should re-throw the exception'365 enable('throw exceptions')366 get('/', function(){367 this.error(new Error('fail!'))368 })369 -{ get('/') }.should.throw_error Error, 'fail!'

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 370–372why, one step back
370 end
371 end372 end

Added specs for Request#notFound()visionmediabefore adoptiona4833e7

lines 373–374why, one step back
373
374 describe '#notFound()'

Added DSL level notFound() route supportvisionmediabefore adoptiona6dd697

lines 375–384why, one step back
375 describe 'when a notFound route is defined'
376 it 'should be called'377 notFound(function(){378 this.halt(404, 'Sorry your page was not found')379 })380 get('/', function(){ this.notFound() })381 get('/').body.should.eql 'Sorry your page was not found'382 end383 end384

Added specs for Request#notFound()visionmediabefore adoptiona4833e7

lines 385–417why, one step back
385 describe 'when accepting "html"'
386 describe 'with "helpful 404" enabled'387 it 'should render the not-found page'388 enable('helpful 404')389 get('/', function(){ this.notFound() })390 get('/').body.should.include '<em>404</em> Not Found'391 get('/').status.should.eql 404392 end393 end394 end395 396 describe 'when not accepting "html"'397 describe 'with "helpful 404" enabled'398 it 'should render defaulat 404 status body'399 var headers = { headers: { accept: 'text/plain' }}400 enable('helpful 404')401 get('/', function(){ this.notFound() })402 get('/', headers).body.should.eql 'Not Found'403 get('/', headers).status.should.eql 404404 end405 end406 end407 408 describe 'when "helpful 404" is disabled'409 it 'should render defaulat 404 status body'410 var headers = { headers: { accept: 'text/plain' }}411 enable('helpful 404')412 get('/', function(){ this.notFound() })413 get('/', headers).body.should.eql 'Not Found'414 get('/', headers).status.should.eql 404415 end416 end417 end

Request#pass() accepts a pathname stringvisionmediabefore adoption300cfe7

line 418why, one step back
418

Merge branch 'async-plugins'visionmediabefore adoption4c246a4

lines 419–420why, one step back
419 end
420end