spec.helpers.js
This file
Path
spec/spec.helpers.js
211 lines · 6.4 KiB · JavaScript
Last changed by
Decoding param values and supporting urlencoded request bodiesvisionmediabefore adoptiona4159a7
History of this fileTake it
Renaming some spec filesvisionmediabefore adoption340cd1d
lines 1–21why, one step back12describe '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 endDecoding param values and supporting urlencoded request bodiesvisionmediabefore adoptiona4159a7
lines 22–31why, one step back22 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 endRenaming some spec filesvisionmediabefore adoption340cd1d
lines 32–146why, one step back32 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 '<p>this & 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 Added contentType() helpervisionmediabefore adoption7f1eeaf
lines 147–156why, one step back147 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 Renaming some spec filesvisionmediabefore adoption340cd1d
lines 157–201why, one step back157 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 endAdded MethodOverridevisionmediabefore adoption938dc06
lines 202–209why, one step back202 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 endRenaming some spec filesvisionmediabefore adoption340cd1d
lines 210–211why, one step back210 end211end