Skip to content
Myr

spec.request.js

This file

Path

spec/spec.request.js

367 lines · 12.1 KiB · JavaScript

Last changed by

error() is passed the exceptionvisionmediabefore adoption1c0e2ce

History of this file

Take it

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

Hooks (before / after) pass request as argvisionmediabefore adoptionb333dcc

lines 1–41why, 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 end1415 describe '#header()'16 describe 'when given a field name and value'17 it 'should set the header'18 get('/user', function(){19 this.header('x-foo', 'bar')20 })21 get('/user').headers.should.have_property 'x-foo', 'bar'22 end 23 end2425 describe 'when given a field name'26 it 'should return a request header value'27 get('/user', function(){28 return this.header('host')29 })30 get('/user').body.should.eql 'localhost'31 end3233 it 'should be case-insensitive'34 get('/user', function(){35 return this.header('Host')36 })37 get('/user').body.should.eql 'localhost'38 end39 end40 end41

Added Request#isXHR. Closes #229visionmediabefore adoption884d333

lines 42–58why, one step back
42 describe '#isXHR'
43 it 'should return false unless X-Requested-With is "XMLHttpRequest"'44 get('/', function(){ return this.isXHR ? 'yay' : 'nope' })45 get('/').body.should.eql 'nope'46 end47 48 it 'should return true when X-Requested-With is "XMLHttpRequest"'49 get('/', function(){ return this.isXHR ? 'yay' : 'nope' })50 get('/', { headers: { 'x-requested-with': 'XMLHttpRequest' }}).body.should.eql 'yay'51 end52 53 it 'should be case insensitive'54 get('/', function(){ return this.isXHR ? 'yay' : 'nope' })55 get('/', { headers: { 'x-requested-with': 'xmlhttprequest' }}).body.should.eql 'yay'56 end57 end58

Hooks (before / after) pass request as argvisionmediabefore adoptionb333dcc

lines 59–74why, one step back
59 describe '#accepts()'
60 describe 'when the Accept header is present'61 it 'should return true if the mime type is acceptable'62 get('/user', function(){ return this.accepts('jpeg').toString() })63 get('/user', { headers: { accept: 'image/jpeg' }}).body.should.eql 'true'64 end65 66 it 'should return false if the mime type is not present'67 get('/user', function(){ return this.accepts('html').toString() })68 get('/user', { headers: { accept: 'image/jpeg' }}).body.should.eql 'false'69 end70 end71 72 describe 'when the Accept header is not present'73 it 'should return true'74 get('/user', function(){ return this.accepts('jpeg').toString() })

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

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

Hooks (before / after) pass request as argvisionmediabefore adoptionb333dcc

lines 76–77why, one step back
76 end
77 end

Merge branch 'integration'visionmediabefore adoption406a7f4

lines 78–100why, one step back
78
79 describe 'should allow multiple arguments'80 it 'should return true if any mime type is present'81 get('/user', function(){ return this.accepts('jpeg', 'png').toString() })82 get('/user', { headers: { accept: 'image/gif,image/png' }}).body.should.eql 'true'83 end84 85 it 'should return false if none of the mime types are present'86 get('/user', function(){ return this.accepts('jpeg', 'png').toString() })87 get('/user', { headers: { accept: 'text/plain,text/html' }}).body.should.eql 'false'88 end89 end90 91 describe 'when a media type range was sent'92 it 'should return true if the group media type matches'93 get('/user', function(){ return this.accepts('html').toString() })94 get('/user', { headers: { accept: 'text/plain,text/*' }}).body.should.eql 'true'95 end96 it 'should return false if the group media type does not match'97 get('/user', function(){ return this.accepts('ogg').toString() })98 get('/user', { headers: { accept: 'text/plain,text/*' }}).body.should.eql 'false'99 end100 end

Hooks (before / after) pass request as argvisionmediabefore adoptionb333dcc

lines 101–217why, one step back
101 end
102 103 describe '#halt()'104 describe 'when given no arguments'105 it 'should respond with 404 Not Found'106 get('/user', function(){ this.halt() })107 get('/user').status.should.eql 404108 get('/user').body.should.include('Not Found')109 end110 end111112 describe 'when given a status code'113 it 'should respond with that status and its associated default body'114   get('/user', function(){ this.halt(400) })115   get('/user').status.should.eql 400116   get('/user').body.should.include('Bad Request')117 end118 end119120 describe 'when given a status code and body'121 it 'should respond with the status and its body'122 get('/user', function(){ this.halt(400, 'Oh noes!') })123 get('/user').status.should.eql 400124 get('/user').body.should.include('Oh noes!')125 end126 end127 end128129 describe '#contentType()'130 it 'should set Content-Type header with mime type passed'131 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'136 end137 end138 139 describe '#attachment()'140 it 'should set Content-Disposition to attachment'141 get('/report', function(){142 this.attachment()143 return 'foo'144 })145 get('/report').headers['content-disposition'].should.eql 'attachment'146 end147148 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"'154 end155 end156157 describe '#param()'158 it 'should return a route placeholder value'159 get('/user/:id', function(){160 return 'user ' + this.param('id')161 })162 get('/user/12').body.should.eql 'user 12'163 end164165 it 'should return several route placeholder values'166 get('/user/:id/:operation', function(){167 return this.param('operation') + 'ing user ' + this.param('id') 168 })169 get('/user/12/edit').body.should.eql 'editing user 12'170 end171172 it 'should allow optional placeholders'173 get('/user/:id?', function(){174 return this.param('id') ? 'user ' + this.param('id') : 'users'175 })176 get('/user/12').body.should.eql 'user 12'177 get('/user').body.should.eql 'users'178 end179180 it 'should allow placeholders as part of a segment'181 get('/report.:format', function(){182 return 'report as ' + this.param('format')183 })184 get('/report.csv').body.should.eql 'report as csv'185 get('/report.pdf').body.should.eql 'report as pdf'186 end187188 it 'should allow optional placeholders in middle segments'189 get('/user/:id?/edit', function(){190 return this.param('id') ? 'editing ' + this.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 end195196 it 'should work with several routes'197 get('/user/:id', function(){ return this.param('id') })198 get('/product/:name', function(){ return this.param('name') })199 get('/user/12').body.should.eql '12'200 get('/product/ipod').body.should.eql 'ipod'201 end202203 it 'should work with a query string'204 get('/user', function(){205 return String(this.param('page') || 'First page')206 })207 get('/user').body.should.eql 'First page'208 get('/user?page=2').body.should.eql '2'209 get('/user?foo[]=bar&page=5').body.should.eql '5'210 end211212 it 'should work with splats'213 get('/:path/*.*', function(path, file, ext){214 return [path, file, ext].join(', ')215 })216 get('/public/app.js').body.should.eql 'public, app, js'217 end

Merge branch 'pull'visionmediabefore adoptionf008af0

lines 218–221why, one step back
218 end
219 220 describe '#pass()'221 it 'should pass control to the next matching route'

Request#pass() accepts a pathname stringvisionmediabefore adoption300cfe7

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

Merge branch 'pull'visionmediabefore adoptionf008af0

lines 223–224why, one step back
223 this.pass()
224 })

Request#pass() accepts a pathname stringvisionmediabefore adoption300cfe7

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

Merge branch 'pull'visionmediabefore adoptionf008af0

lines 226–228why, one step back
226 this.pass()
227 return 'nodejs'228 })

Request#pass() accepts a pathname stringvisionmediabefore adoption300cfe7

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

Merge branch 'pull'visionmediabefore adoptionf008af0

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

Request#pass() accepts a pathname stringvisionmediabefore adoption300cfe7

lines 232–244why, one step back
232
233 describe 'given a string'234 it 'should pass to the given route'235 get('/user', function(){236 this.pass('/user/1')237 })238 get('/user/:id', function(){239 return 'Supa doopa usa'240 })241 get('/user').body.should.eql 'Supa doopa usa'242 end243 end244 end

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 245–246why, one step back
245
246 describe '#error()'

Added DSL level error() route supportvisionmediabefore adoptionb958393

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

error() is passed the exceptionvisionmediabefore adoption1c0e2ce

line 249why, one step back
249 var err

Added DSL level error() route supportvisionmediabefore adoptionb958393

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

error() is passed the exceptionvisionmediabefore adoption1c0e2ce

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

Added DSL level error() route supportvisionmediabefore adoptionb958393

lines 253–256why, one step back
253 this.halt(500, 'FAIL!')
254 })255 get('/', function(){ this.error(new Error('whoop')) })256 get('/').body.should.eql 'FAIL!'

error() is passed the exceptionvisionmediabefore adoption1c0e2ce

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

Added DSL level error() route supportvisionmediabefore adoptionb958393

lines 258–260why, one step back
258 end
259 end260

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 261–285why, one step back
261 describe 'when accepting "html"'
262 describe 'with "show exceptions" enabled'263 it 'should render the show-exceptions page'264 disable('throw exceptions')265 enable('show exceptions')266 get('/', function(){267 this.error(new Error('fail!'))268 })269 get('/').body.should.include '<em>500</em> Error: fail!'270 get('/').status.should.eql 500271 end272 end273 274 describe 'when not accepting "html"'275 it 'should render the default 500 status body'276 var headers = { headers: { accept: 'text/plain' }}277 disable('throw exceptions')278 enable('show exceptions')279 get('/', function(){280 this.error(new Error('fail!'))281 })282 get('/', headers).body.should.eql 'Internal Server Error'283 get('/', headers).status.should.eql 500284 end285 end

More Request#error() specsvisionmediabefore adoption54fa643

lines 286–296why, one step back
286
287 describe 'with "throw exceptions" enabled'288 it 'should re-throw the exception'289 enable('show exceptions')290 enable('throw exceptions')291 get('/', function(){292 this.error(new Error('fail!'))293 })294 -{ get('/') }.should.throw_error Error, 'fail!'295 end296 end

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 297–306why, one step back
297 end
298 299 describe 'with "show exceptions" disabled'300 it 'should render the default 500 status body'301 disable('throw exceptions')302 get('/', function(){303 this.error(new Error('fail!'))304 })305 get('/').body.should.eql 'Internal Server Error'306 get('/').status.should.eql 500

More Request#error() specsvisionmediabefore adoption54fa643

lines 307–316why, one step back
307 end
308 end309 310 describe 'with "throw exceptions" enabled'311 it 'should re-throw the exception'312 enable('throw exceptions')313 get('/', function(){314 this.error(new Error('fail!'))315 })316 -{ get('/') }.should.throw_error Error, 'fail!'

Added specs for Request#error()visionmediabefore adoption6047cd4

lines 317–319why, one step back
317 end
318 end319 end

Added specs for Request#notFound()visionmediabefore adoptiona4833e7

lines 320–321why, one step back
320
321 describe '#notFound()'

Added DSL level notFound() route supportvisionmediabefore adoptiona6dd697

lines 322–331why, one step back
322 describe 'when a notFound route is defined'
323 it 'should be called'324 notFound(function(){325 this.halt(404, 'Sorry your page was not found')326 })327 get('/', function(){ this.notFound() })328 get('/').body.should.eql 'Sorry your page was not found'329 end330 end331

Added specs for Request#notFound()visionmediabefore adoptiona4833e7

lines 332–364why, one step back
332 describe 'when accepting "html"'
333 describe 'with "helpful 404" enabled'334 it 'should render the not-found page'335 enable('helpful 404')336 get('/', function(){ this.notFound() })337 get('/').body.should.include '<em>404</em> Not Found'338 get('/').status.should.eql 404339 end340 end341 end342 343 describe 'when not accepting "html"'344 describe 'with "helpful 404" enabled'345 it 'should render defaulat 404 status body'346 var headers = { headers: { accept: 'text/plain' }}347 enable('helpful 404')348 get('/', function(){ this.notFound() })349 get('/', headers).body.should.eql 'Not Found'350 get('/', headers).status.should.eql 404351 end352 end353 end354 355 describe 'when "helpful 404" is disabled'356 it 'should render defaulat 404 status body'357 var headers = { headers: { accept: 'text/plain' }}358 enable('helpful 404')359 get('/', function(){ this.notFound() })360 get('/', headers).body.should.eql 'Not Found'361 get('/', headers).status.should.eql 404362 end363 end364 end

Request#pass() accepts a pathname stringvisionmediabefore adoption300cfe7

line 365why, one step back
365

Hooks (before / after) pass request as argvisionmediabefore adoptionb333dcc

lines 366–367why, one step back
366 end
367end