Skip to content
Myr

spec.request.js

This file

Path

spec/spec.request.js

153 lines · 4.8 KiB · JavaScript

Last changed by

Misc spec refactoringvisionmediabefore adoption399e91d

History of this file

Take it

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

lines 1–5why, one step back
1
2describe 'Express'3 before_each4 reset()5 end

Added specs for requests with content-type of "application/x-www-form-urlencoded"visionmediabefore adoption5a85db1

line 6why, one step back
6

Misc spec refactoringvisionmediabefore adoption399e91d

lines 7–12why, one step back
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 end

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 13why, one step back
13 end

Misc spec refactoringvisionmediabefore adoption399e91d

lines 14–85why, one step back
14
15 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 end4142 describe 'halt()'43 describe 'when given no arguments'44 it 'should respond with 404 Not Found'45 get('/user', function(){ this.halt() })46 get('/user').status.should.eql 40447 get('/user').body.should.include('Not Found')48 end49 end5051 describe 'when given a status code'52 it 'should respond with that status and its associated default body'53   get('/user', function(){ this.halt(400) })54   get('/user').status.should.eql 40055   get('/user').body.should.include('Bad Request')56 end57 end5859 describe 'when given a status code and body'60 it 'should respond with the status and its body'61 get('/user', function(){ this.halt(400, 'Oh noes!') })62 get('/user').status.should.eql 40063 get('/user').body.should.include('Oh noes!')64 end65 end6667 describe 'when given an invalid status code'68 it 'should throw an InvalidStatusCode exception'69 // TODO: throw_error(InvalidStatusCode, ...) when jspec is fixed70 get('/user', function(){ this.halt(123123) })71 -{ get('/user') }.should.throw_error(/InvalidStatusCode: 123123 is an invalid HTTP response code/)72 try { get('/user') }73 catch (e) {74 e.should.be_an_instance_of ExpressError75 e.should.be_an_instance_of InvalidStatusCode76 }77 end78 end79 end8081 describe 'contentType()'82 it 'should set Content-Type header with mime type passed'83 get('/style.css', function(){84 this.contentType('css')85 return 'body { background: white; }'

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 86why, one step back
86 })

Misc spec refactoringvisionmediabefore adoption399e91d

lines 87–88why, one step back
87 get('/style.css').headers['content-type'].should.eql 'text/css'
88 end

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 89why, one step back
89 end

Misc spec refactoringvisionmediabefore adoption399e91d

lines 90–94why, one step back
90
91 describe 'param()'92 it 'should return a route placeholder value'93 get('/user/:id', function(){94 return 'user ' + this.param('id')

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 95why, one step back
95 })

Misc spec refactoringvisionmediabefore adoption399e91d

line 96why, one step back
96 get('/user/12').body.should.eql 'user 12'

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 97why, one step back
97 end

Misc spec refactoringvisionmediabefore adoption399e91d

lines 98–101why, one step back
98
99 it 'should return several route placeholder values'100 get('/user/:id/:operation', function(){101 return this.param('operation') + 'ing user ' + this.param('id')

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 102why, one step back
102 })

Misc spec refactoringvisionmediabefore adoption399e91d

line 103why, one step back
103 get('/user/12/edit').body.should.eql 'editing user 12'

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 104why, one step back
104 end

Misc spec refactoringvisionmediabefore adoption399e91d

lines 105–111why, one step back
105
106 it 'should allow optional placeholders'107 get('/user/:id?', function(){108 return this.param('id') ? 'user ' + this.param('id') : 'users'109 })110 get('/user/12').body.should.eql 'user 12'111 get('/user').body.should.eql 'users'

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 112why, one step back
112 end

Misc spec refactoringvisionmediabefore adoption399e91d

lines 113–119why, one step back
113
114 it 'should allow placeholders as part of a segment'115 get('/report.:format', function(){116 return 'report as ' + this.param('format')117 })118 get('/report.csv').body.should.eql 'report as csv'119 get('/report.pdf').body.should.eql 'report as pdf'

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 120why, one step back
120 end

Misc spec refactoringvisionmediabefore adoption399e91d

lines 121–127why, one step back
121
122 it 'should allow optional placeholders in middle segments'123 get('/user/:id?/edit', function(){124 return this.param('id') ? 'editing ' + this.param('id') : 'editing your account'125 })126 get('/user/12/edit').body.should.eql 'editing 12'127 get('/user/edit').body.should.eql 'editing your account'

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 128why, one step back
128 end

Misc spec refactoringvisionmediabefore adoption399e91d

lines 129–134why, one step back
129
130 it 'should work with several routes'131 get('/user/:id', function(){ return this.param('id') })132 get('/product/:name', function(){ return this.param('name') })133 get('/user/12').body.should.eql '12'134 get('/product/ipod').body.should.eql 'ipod'

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

line 135why, one step back
135 end

Misc spec refactoringvisionmediabefore adoption399e91d

lines 136–151why, one step back
136
137 it 'should access request.uri.params'138 get('/user', function(){139 return this.param('page') || 'First page'140 })141 get('/user').body.should.eql 'First page'142 get('/user', { uri: { params: { page: '2' }}}).body.should.eql '2'143 end144145 it 'should work with splats'146 get('/:path/*.*', function(path, file, ext){147 return [path, file, ext].join(', ')148 })149 get('/public/app.js').body.should.eql 'public, app, js'150 end151 end

More refactoring to allow everything to be request-specificvisionmediabefore adoption34c412e

lines 152–153why, one step back
152 end
153end