bmizerany-sinatra 0.9.0 → 0.9.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/test/reload_test.rb CHANGED
@@ -1,13 +1,9 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  $reload_count = 0
6
4
  $reload_app = nil
7
5
 
8
6
  describe "Reloading" do
9
- include Sinatra::Test
10
-
11
7
  before {
12
8
  @app = mock_app(Sinatra::Default)
13
9
  $reload_app = @app
@@ -20,31 +16,31 @@ describe "Reloading" do
20
16
  it 'is enabled by default when in development and the app_file is set' do
21
17
  @app.set :app_file, __FILE__
22
18
  @app.set :environment, :development
23
- @app.reload.should.be true
24
- @app.reload?.should.be true
19
+ assert_same true, @app.reload
20
+ assert_same true, @app.reload?
25
21
  end
26
22
 
27
23
  it 'is disabled by default when running in non-development environment' do
28
24
  @app.set :app_file, __FILE__
29
25
  @app.set :environment, :test
30
- @app.reload.should.not.be true
31
- @app.reload?.should.be false
26
+ assert !@app.reload
27
+ assert_same false, @app.reload?
32
28
  end
33
29
 
34
30
  it 'is disabled by default when no app_file is available' do
35
31
  @app.set :app_file, nil
36
32
  @app.set :environment, :development
37
- @app.reload.should.not.be true
38
- @app.reload?.should.be false
33
+ assert !@app.reload
34
+ assert_same false, @app.reload?
39
35
  end
40
36
 
41
37
  it 'can be turned off explicitly' do
42
38
  @app.set :app_file, __FILE__
43
39
  @app.set :environment, :development
44
- @app.reload.should.be true
40
+ assert_same true, @app.reload
45
41
  @app.set :reload, false
46
- @app.reload.should.be false
47
- @app.reload?.should.be false
42
+ assert_same false, @app.reload
43
+ assert_same false, @app.reload?
48
44
  end
49
45
 
50
46
  it 'reloads the app_file each time a request is made' do
@@ -53,13 +49,13 @@ describe "Reloading" do
53
49
  @app.get('/') { 'Hello World' }
54
50
 
55
51
  get '/'
56
- status.should.equal 200
57
- body.should.equal 'Hello from reload file'
58
- $reload_count.should.equal 1
52
+ assert_equal 200, status
53
+ assert_equal 'Hello from reload file', body
54
+ assert_equal 1, $reload_count
59
55
 
60
56
  get '/'
61
- status.should.equal 200
62
- body.should.equal 'Hello from reload file'
63
- $reload_count.should.equal 2
57
+ assert_equal 200, status
58
+ assert_equal 'Hello from reload file', body
59
+ assert_equal 2, $reload_count
64
60
  end
65
61
  end
data/test/request_test.rb CHANGED
@@ -1,11 +1,18 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  describe 'Sinatra::Request' do
6
4
  it 'responds to #user_agent' do
7
5
  request = Sinatra::Request.new({'HTTP_USER_AGENT' => 'Test'})
8
- request.should.respond_to :user_agent
9
- request.user_agent.should.equal 'Test'
6
+ assert request.respond_to?(:user_agent)
7
+ assert_equal 'Test', request.user_agent
8
+ end
9
+
10
+ it 'parses POST params when Content-Type is form-dataish' do
11
+ request = Sinatra::Request.new(
12
+ 'REQUEST_METHOD' => 'PUT',
13
+ 'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
14
+ 'rack.input' => StringIO.new('foo=bar')
15
+ )
16
+ assert_equal 'bar', request.params['foo']
10
17
  end
11
18
  end
data/test/result_test.rb CHANGED
@@ -1,10 +1,6 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  describe 'Result Handling' do
6
- include Sinatra::Test
7
-
8
4
  it "sets response.body when result is a String" do
9
5
  mock_app {
10
6
  get '/' do
@@ -13,8 +9,8 @@ describe 'Result Handling' do
13
9
  }
14
10
 
15
11
  get '/'
16
- should.be.ok
17
- body.should.equal 'Hello World'
12
+ assert ok?
13
+ assert_equal 'Hello World', body
18
14
  end
19
15
 
20
16
  it "sets response.body when result is an Array of Strings" do
@@ -25,8 +21,8 @@ describe 'Result Handling' do
25
21
  }
26
22
 
27
23
  get '/'
28
- should.be.ok
29
- body.should.equal 'HelloWorld'
24
+ assert ok?
25
+ assert_equal 'HelloWorld', body
30
26
  end
31
27
 
32
28
  it "sets response.body when result responds to #each" do
@@ -39,8 +35,8 @@ describe 'Result Handling' do
39
35
  }
40
36
 
41
37
  get '/'
42
- should.be.ok
43
- body.should.equal 'Hello World'
38
+ assert ok?
39
+ assert_equal 'Hello World', body
44
40
  end
45
41
 
46
42
  it "sets response.body to [] when result is nil" do
@@ -51,8 +47,8 @@ describe 'Result Handling' do
51
47
  }
52
48
 
53
49
  get '/'
54
- should.be.ok
55
- body.should.equal ''
50
+ assert ok?
51
+ assert_equal '', body
56
52
  end
57
53
 
58
54
  it "sets status, headers, and body when result is a Rack response tuple" do
@@ -63,9 +59,9 @@ describe 'Result Handling' do
63
59
  }
64
60
 
65
61
  get '/'
66
- status.should.equal 205
67
- response['Content-Type'].should.equal 'foo/bar'
68
- body.should.equal 'Hello World'
62
+ assert_equal 205, status
63
+ assert_equal 'foo/bar', response['Content-Type']
64
+ assert_equal 'Hello World', body
69
65
  end
70
66
 
71
67
  it "sets status and body when result is a two-tuple" do
@@ -76,8 +72,8 @@ describe 'Result Handling' do
76
72
  }
77
73
 
78
74
  get '/'
79
- status.should.equal 409
80
- body.should.equal 'formula of'
75
+ assert_equal 409, status
76
+ assert_equal 'formula of', body
81
77
  end
82
78
 
83
79
  it "sets status when result is a Fixnum status code" do
@@ -86,7 +82,7 @@ describe 'Result Handling' do
86
82
  }
87
83
 
88
84
  get '/'
89
- status.should.equal 205
90
- body.should.be.empty
85
+ assert_equal 205, status
86
+ assert_equal '', body
91
87
  end
92
88
  end
data/test/routing_test.rb CHANGED
@@ -1,10 +1,6 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  describe "Routing" do
6
- include Sinatra::Test
7
-
8
4
  %w[get put post delete head].each do |verb|
9
5
  it "defines #{verb.upcase} request handlers with #{verb}" do
10
6
  mock_app {
@@ -15,8 +11,8 @@ describe "Routing" do
15
11
 
16
12
  request = Rack::MockRequest.new(@app)
17
13
  response = request.request(verb.upcase, '/hello', {})
18
- response.should.be.ok
19
- response.body.should.equal 'Hello World'
14
+ assert response.ok?
15
+ assert_equal 'Hello World', response.body
20
16
  end
21
17
  end
22
18
 
@@ -25,30 +21,30 @@ describe "Routing" do
25
21
  get('/foo') { }
26
22
  }
27
23
  get '/bar'
28
- status.should.equal 404
24
+ assert_equal 404, status
29
25
  end
30
26
 
31
27
  it "exposes params with indifferent hash" do
32
28
  mock_app {
33
29
  get '/:foo' do
34
- params['foo'].should.equal 'bar'
35
- params[:foo].should.equal 'bar'
30
+ assert_equal 'bar', params['foo']
31
+ assert_equal 'bar', params[:foo]
36
32
  'well, alright'
37
33
  end
38
34
  }
39
35
  get '/bar'
40
- body.should.equal 'well, alright'
36
+ assert_equal 'well, alright', body
41
37
  end
42
38
 
43
39
  it "merges named params and query string params in params" do
44
40
  mock_app {
45
41
  get '/:foo' do
46
- params['foo'].should.equal 'bar'
47
- params['baz'].should.equal 'biz'
42
+ assert_equal 'bar', params['foo']
43
+ assert_equal 'biz', params['baz']
48
44
  end
49
45
  }
50
46
  get '/bar?baz=biz'
51
- should.be.ok
47
+ assert ok?
52
48
  end
53
49
 
54
50
  it "supports named params like /hello/:person" do
@@ -58,7 +54,7 @@ describe "Routing" do
58
54
  end
59
55
  }
60
56
  get '/hello/Frank'
61
- body.should.equal 'Hello Frank'
57
+ assert_equal 'Hello Frank', body
62
58
  end
63
59
 
64
60
  it "supports optional named params like /?:foo?/?:bar?" do
@@ -69,58 +65,115 @@ describe "Routing" do
69
65
  }
70
66
 
71
67
  get '/hello/world'
72
- should.be.ok
73
- body.should.equal "foo=hello;bar=world"
68
+ assert ok?
69
+ assert_equal "foo=hello;bar=world", body
74
70
 
75
71
  get '/hello'
76
- should.be.ok
77
- body.should.equal "foo=hello;bar="
72
+ assert ok?
73
+ assert_equal "foo=hello;bar=", body
78
74
 
79
75
  get '/'
80
- should.be.ok
81
- body.should.equal "foo=;bar="
76
+ assert ok?
77
+ assert_equal "foo=;bar=", body
82
78
  end
83
79
 
84
80
  it "supports single splat params like /*" do
85
81
  mock_app {
86
82
  get '/*' do
87
- params['splat'].should.be.kind_of Array
83
+ assert params['splat'].kind_of?(Array)
88
84
  params['splat'].join "\n"
89
85
  end
90
86
  }
91
87
 
92
88
  get '/foo'
93
- body.should.equal "foo"
89
+ assert_equal "foo", body
94
90
 
95
91
  get '/foo/bar/baz'
96
- body.should.equal "foo/bar/baz"
92
+ assert_equal "foo/bar/baz", body
97
93
  end
98
94
 
99
95
  it "supports mixing multiple splat params like /*/foo/*/*" do
100
96
  mock_app {
101
97
  get '/*/foo/*/*' do
102
- params['splat'].should.be.kind_of Array
98
+ assert params['splat'].kind_of?(Array)
103
99
  params['splat'].join "\n"
104
100
  end
105
101
  }
106
102
 
107
103
  get '/bar/foo/bling/baz/boom'
108
- body.should.equal "bar\nbling\nbaz/boom"
104
+ assert_equal "bar\nbling\nbaz/boom", body
109
105
 
110
106
  get '/bar/foo/baz'
111
- should.be.not_found
107
+ assert not_found?
112
108
  end
113
109
 
114
110
  it "supports mixing named and splat params like /:foo/*" do
115
111
  mock_app {
116
112
  get '/:foo/*' do
117
- params['foo'].should.equal 'foo'
118
- params['splat'].should.equal ['bar/baz']
113
+ assert_equal 'foo', params['foo']
114
+ assert_equal ['bar/baz'], params['splat']
119
115
  end
120
116
  }
121
117
 
122
118
  get '/foo/bar/baz'
123
- should.be.ok
119
+ assert ok?
120
+ end
121
+
122
+ it "supports basic nested params" do
123
+ mock_app {
124
+ get '/hi' do
125
+ params["person"]["name"]
126
+ end
127
+ }
128
+
129
+ get "/hi?person[name]=John+Doe"
130
+ assert ok?
131
+ assert_equal "John Doe", body
132
+ end
133
+
134
+ it "exposes nested params with indifferent hash" do
135
+ mock_app {
136
+ get '/testme' do
137
+ assert_equal 'baz', params['bar']['foo']
138
+ assert_equal 'baz', params['bar'][:foo]
139
+ 'well, alright'
140
+ end
141
+ }
142
+ get '/testme?bar[foo]=baz'
143
+ assert_equal 'well, alright', body
144
+ end
145
+
146
+ it "supports deeply nested params" do
147
+ input = {
148
+ 'browser[chrome][engine][name]' => 'V8',
149
+ 'browser[chrome][engine][version]' => '1.0',
150
+ 'browser[firefox][engine][name]' => 'spidermonkey',
151
+ 'browser[firefox][engine][version]' => '1.7.0',
152
+ 'emacs[map][goto-line]' => 'M-g g',
153
+ 'emacs[version]' => '22.3.1',
154
+ 'paste[name]' => 'hello world',
155
+ 'paste[syntax]' => 'ruby'
156
+ }
157
+ expected = {
158
+ "emacs" => {
159
+ "map" => { "goto-line" => "M-g g" },
160
+ "version" => "22.3.1"
161
+ },
162
+ "browser" => {
163
+ "firefox" => {"engine" => {"name"=>"spidermonkey", "version"=>"1.7.0"}},
164
+ "chrome" => {"engine" => {"name"=>"V8", "version"=>"1.0"}}
165
+ },
166
+ "paste" => {"name"=>"hello world", "syntax"=>"ruby"}
167
+ }
168
+ mock_app {
169
+ get '/foo' do
170
+ assert_equal expected, params
171
+ 'looks good'
172
+ end
173
+ }
174
+ get "/foo?#{param_string(input)}"
175
+ assert ok?
176
+ assert_equal 'looks good', body
124
177
  end
125
178
 
126
179
  it "supports paths that include spaces" do
@@ -131,21 +184,21 @@ describe "Routing" do
131
184
  }
132
185
 
133
186
  get '/path%20with%20spaces'
134
- should.be.ok
135
- body.should.equal 'looks good'
187
+ assert ok?
188
+ assert_equal 'looks good', body
136
189
  end
137
190
 
138
191
  it "URL decodes named parameters and splats" do
139
192
  mock_app {
140
193
  get '/:foo/*' do
141
- params['foo'].should.equal 'hello world'
142
- params['splat'].should.equal ['how are you']
194
+ assert_equal 'hello world', params['foo']
195
+ assert_equal ['how are you'], params['splat']
143
196
  nil
144
197
  end
145
198
  }
146
199
 
147
200
  get '/hello%20world/how%20are%20you'
148
- should.be.ok
201
+ assert ok?
149
202
  end
150
203
 
151
204
  it 'supports regular expressions' do
@@ -156,21 +209,21 @@ describe "Routing" do
156
209
  }
157
210
 
158
211
  get '/foooom/bar'
159
- should.be.ok
160
- body.should.equal 'Hello World'
212
+ assert ok?
213
+ assert_equal 'Hello World', body
161
214
  end
162
215
 
163
216
  it 'makes regular expression captures available in params[:captures]' do
164
217
  mock_app {
165
218
  get(/^\/fo(.*)\/ba(.*)/) do
166
- params[:captures].should.equal ['orooomma', 'f']
219
+ assert_equal ['orooomma', 'f'], params[:captures]
167
220
  'right on'
168
221
  end
169
222
  }
170
223
 
171
224
  get '/foorooomma/baf'
172
- should.be.ok
173
- body.should.equal 'right on'
225
+ assert ok?
226
+ assert_equal 'right on', body
174
227
  end
175
228
 
176
229
  it "returns response immediately on halt" do
@@ -182,8 +235,8 @@ describe "Routing" do
182
235
  }
183
236
 
184
237
  get '/'
185
- should.be.ok
186
- body.should.equal 'Hello World'
238
+ assert ok?
239
+ assert_equal 'Hello World', body
187
240
  end
188
241
 
189
242
  it "transitions to the next matching route on pass" do
@@ -194,14 +247,14 @@ describe "Routing" do
194
247
  end
195
248
 
196
249
  get '/*' do
197
- params.should.not.include 'foo'
250
+ assert !params.include?('foo')
198
251
  'Hello World'
199
252
  end
200
253
  }
201
254
 
202
255
  get '/bar'
203
- should.be.ok
204
- body.should.equal 'Hello World'
256
+ assert ok?
257
+ assert_equal 'Hello World', body
205
258
  end
206
259
 
207
260
  it "transitions to 404 when passed and no subsequent route matches" do
@@ -213,7 +266,7 @@ describe "Routing" do
213
266
  }
214
267
 
215
268
  get '/bar'
216
- should.be.not_found
269
+ assert not_found?
217
270
  end
218
271
 
219
272
  it "passes when matching condition returns false" do
@@ -225,11 +278,11 @@ describe "Routing" do
225
278
  }
226
279
 
227
280
  get '/bar'
228
- should.be.ok
229
- body.should.equal 'Hello World'
281
+ assert ok?
282
+ assert_equal 'Hello World', body
230
283
 
231
284
  get '/foo'
232
- should.be.not_found
285
+ assert not_found?
233
286
  end
234
287
 
235
288
  it "does not pass when matching condition returns nil" do
@@ -241,8 +294,8 @@ describe "Routing" do
241
294
  }
242
295
 
243
296
  get '/bar'
244
- should.be.ok
245
- body.should.equal 'Hello World'
297
+ assert ok?
298
+ assert_equal 'Hello World', body
246
299
  end
247
300
 
248
301
  it "passes to next route when condition calls pass explicitly" do
@@ -254,11 +307,11 @@ describe "Routing" do
254
307
  }
255
308
 
256
309
  get '/bar'
257
- should.be.ok
258
- body.should.equal 'Hello World'
310
+ assert ok?
311
+ assert_equal 'Hello World', body
259
312
 
260
313
  get '/foo'
261
- should.be.not_found
314
+ assert not_found?
262
315
  end
263
316
 
264
317
  it "passes to the next route when host_name does not match" do
@@ -269,38 +322,38 @@ describe "Routing" do
269
322
  end
270
323
  }
271
324
  get '/foo'
272
- should.be.not_found
325
+ assert not_found?
273
326
 
274
327
  get '/foo', :env => { 'HTTP_HOST' => 'example.com' }
275
- status.should.equal 200
276
- body.should.equal 'Hello World'
328
+ assert_equal 200, status
329
+ assert_equal 'Hello World', body
277
330
  end
278
331
 
279
332
  it "passes to the next route when user_agent does not match" do
280
333
  mock_app {
281
- user_agent /Foo/
334
+ user_agent(/Foo/)
282
335
  get '/foo' do
283
336
  'Hello World'
284
337
  end
285
338
  }
286
339
  get '/foo'
287
- should.be.not_found
340
+ assert not_found?
288
341
 
289
342
  get '/foo', :env => { 'HTTP_USER_AGENT' => 'Foo Bar' }
290
- status.should.equal 200
291
- body.should.equal 'Hello World'
343
+ assert_equal 200, status
344
+ assert_equal 'Hello World', body
292
345
  end
293
346
 
294
347
  it "makes captures in user agent pattern available in params[:agent]" do
295
348
  mock_app {
296
- user_agent /Foo (.*)/
349
+ user_agent(/Foo (.*)/)
297
350
  get '/foo' do
298
351
  'Hello ' + params[:agent].first
299
352
  end
300
353
  }
301
354
  get '/foo', :env => { 'HTTP_USER_AGENT' => 'Foo Bar' }
302
- status.should.equal 200
303
- body.should.equal 'Hello Bar'
355
+ assert_equal 200, status
356
+ assert_equal 'Hello Bar', body
304
357
  end
305
358
 
306
359
  it "filters by accept header" do
@@ -311,12 +364,12 @@ describe "Routing" do
311
364
  }
312
365
 
313
366
  get '/', :env => { :accept => 'application/xml' }
314
- should.be.ok
315
- body.should.equal 'application/xml'
316
- response.headers['Content-Type'].should.equal 'application/xml'
367
+ assert ok?
368
+ assert_equal 'application/xml', body
369
+ assert_equal 'application/xml', response.headers['Content-Type']
317
370
 
318
371
  get '/', :env => { :accept => 'text/html' }
319
- should.not.be.ok
372
+ assert !ok?
320
373
  end
321
374
 
322
375
  it "allows multiple mime types for accept header" do
@@ -330,9 +383,9 @@ describe "Routing" do
330
383
 
331
384
  types.each do |type|
332
385
  get '/', :env => { :accept => type }
333
- should.be.ok
334
- body.should.equal type
335
- response.headers['Content-Type'].should.equal type
386
+ assert ok?
387
+ assert_equal type, body
388
+ assert_equal type, response.headers['Content-Type']
336
389
  end
337
390
  end
338
391
  end
data/test/sass_test.rb CHANGED
@@ -1,10 +1,6 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  describe "Sass Templates" do
6
- include Sinatra::Test
7
-
8
4
  def sass_app(&block)
9
5
  mock_app {
10
6
  set :views, File.dirname(__FILE__) + '/views'
@@ -15,26 +11,26 @@ describe "Sass Templates" do
15
11
 
16
12
  it 'renders inline Sass strings' do
17
13
  sass_app { sass "#sass\n :background-color #FFF\n" }
18
- should.be.ok
19
- body.should.equal "#sass {\n background-color: #FFF; }\n"
14
+ assert ok?
15
+ assert_equal "#sass {\n background-color: #FFF; }\n", body
20
16
  end
21
17
 
22
18
  it 'renders .sass files in views path' do
23
19
  sass_app { sass :hello }
24
- should.be.ok
25
- body.should.equal "#sass {\n background-color: #FFF; }\n"
20
+ assert ok?
21
+ assert_equal "#sass {\n background-color: #FFF; }\n", body
26
22
  end
27
23
 
28
24
  it 'ignores the layout option' do
29
25
  sass_app { sass :hello, :layout => :layout2 }
30
- should.be.ok
31
- body.should.equal "#sass {\n background-color: #FFF; }\n"
26
+ assert ok?
27
+ assert_equal "#sass {\n background-color: #FFF; }\n", body
32
28
  end
33
29
 
34
30
  it "raises error if template not found" do
35
31
  mock_app {
36
32
  get('/') { sass :no_such_template }
37
33
  }
38
- lambda { get('/') }.should.raise(Errno::ENOENT)
34
+ assert_raise(Errno::ENOENT) { get('/') }
39
35
  end
40
36
  end
data/test/sinatra_test.rb CHANGED
@@ -1,6 +1,4 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  describe 'Sinatra' do
6
4
  it 'creates a new Sinatra::Base subclass on new' do
@@ -10,6 +8,6 @@ describe 'Sinatra' do
10
8
  'Hello World'
11
9
  end
12
10
  end
13
- app.superclass.should.be Sinatra::Base
11
+ assert_same Sinatra::Base, app.superclass
14
12
  end
15
13
  end