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 fileTake it
12describe '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 end14 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 end2728 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 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 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() })88 get('/user', { headers: { accept: '' }}).body.should.eql 'true'89 end90 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 })148 get('/style.css').headers['Content-Type'].should.eql 'text/css'149 end150 end151 152 describe '#attachment()'153 it 'should set Content-Disposition to attachment'154 get('/report', function(){155 this.attachment()156 return 'foo'157 })158 get('/report').headers['Content-Disposition'].should.eql 'attachment'159 end160161 it 'should set attachment filename'162 get('/report', function(){163 this.attachment('report.pdf')164 return 'foo'165 })166 get('/report').headers['Content-Disposition'].should.eql 'attachment; filename="report.pdf"'167 end168 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(){218 var page = this.param('page')219 return page === undefined ? 'First page' : String(page)220 })221 get('/user').body.should.eql 'First page'222 get('/user?page=0').body.should.eql '0'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 end233 end234 235 describe '#pass()'236 it 'should pass control to the next matching route'237 get('/user', function(){ 238 this.pass()239 })240 get('/user', function(){241 this.pass()242 return 'nodejs'243 })244 get('/user', function(){ return 'success'})245 get('/user').body.should.eql 'success'246 end247 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 end260 261 describe '#error()'262 describe 'when an error route is defined'263 it 'should be called'264 var err265 disable('throw exceptions')266 global.error(function(e){267 err = e268 this.halt(500, 'FAIL!')269 })270 get('/', function(){ this.error(new Error('whoop')) })271 get('/').body.should.eql 'FAIL!'272 err.message.should.eql 'whoop'273 end274 end275 276 describe 'when not accepting common mime types'277 it 'should render the default 500 status body'278 var headers = { headers: { accept: 'text/xml' }}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 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 end300 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 end310 end311 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'327 disable('throw exceptions')328 enable('show exceptions')329 get('/', function(){330 this.error(new Error('fail!'))331 })332 get('/', { headers: { accept: 'text/plain' }}).body.should.include '500 Error: fail!'333 get('/').status.should.eql 500334 end335 end336 end337 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')342 enable('show exceptions')343 get('/', function(){344 this.error(new Error('fail!'))345 })346 get('/', { headers: { accept: 'application/json' }}).body.should.include '{"error":{"message":"fail!"'347 get('/').status.should.eql 500 348 end349 end350 end351 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 500360 end361 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!'370 end371 end372 end373 374 describe '#notFound()'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 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 end418 419 end420end