rtomayko-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/AUTHORS +40 -0
- data/CHANGES +189 -0
- data/README.rdoc +142 -95
- data/Rakefile +14 -49
- data/compat/events_test.rb +10 -7
- data/compat/helper.rb +13 -1
- data/lib/sinatra.rb +5 -0
- data/lib/sinatra/base.rb +138 -47
- data/lib/sinatra/compat.rb +142 -44
- data/lib/sinatra/test.rb +92 -91
- data/lib/sinatra/test/bacon.rb +17 -0
- data/lib/sinatra/test/rspec.rb +9 -0
- data/lib/sinatra/test/spec.rb +7 -0
- data/lib/sinatra/test/unit.rb +2 -2
- data/sinatra.gemspec +8 -5
- data/test/base_test.rb +33 -14
- data/test/builder_test.rb +12 -16
- data/test/erb_test.rb +11 -16
- data/test/filter_test.rb +48 -12
- data/test/haml_test.rb +14 -18
- data/test/helper.rb +25 -0
- data/test/helpers_test.rb +55 -62
- data/test/mapped_error_test.rb +43 -24
- data/test/middleware_test.rb +8 -13
- data/test/options_test.rb +29 -35
- data/test/reload_test.rb +16 -20
- data/test/request_test.rb +12 -5
- data/test/result_test.rb +16 -20
- data/test/routing_test.rb +150 -64
- data/test/sass_test.rb +8 -12
- data/test/sinatra_test.rb +2 -4
- data/test/static_test.rb +16 -19
- data/test/templates_test.rb +23 -19
- metadata +8 -6
- data/ChangeLog +0 -96
data/test/helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
begin
|
2
|
+
require 'test/spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems'
|
5
|
+
require 'test/spec'
|
6
|
+
end
|
7
|
+
|
8
|
+
$:.unshift File.dirname(File.dirname(__FILE__)) + '/lib'
|
9
|
+
require 'sinatra/base'
|
10
|
+
require 'sinatra/test'
|
11
|
+
require 'sinatra/test/spec'
|
12
|
+
|
13
|
+
module Sinatra::Test
|
14
|
+
# Sets up a Sinatra::Base subclass defined with the block
|
15
|
+
# given. Used in setup or individual spec methods to establish
|
16
|
+
# the application.
|
17
|
+
def mock_app(base=Sinatra::Base, &block)
|
18
|
+
@app = Sinatra.new(base, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Sinatra::Base
|
23
|
+
# Allow assertions in request context
|
24
|
+
include Test::Unit::Assertions
|
25
|
+
end
|
data/test/helpers_test.rb
CHANGED
@@ -1,10 +1,4 @@
|
|
1
|
-
require '
|
2
|
-
require 'sinatra/base'
|
3
|
-
require 'sinatra/test'
|
4
|
-
|
5
|
-
class Test::Unit::TestCase
|
6
|
-
include Sinatra::Test
|
7
|
-
end
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
8
2
|
|
9
3
|
describe 'Sinatra::Helpers' do
|
10
4
|
describe '#status' do
|
@@ -19,7 +13,7 @@ describe 'Sinatra::Helpers' do
|
|
19
13
|
|
20
14
|
it 'sets the response status code' do
|
21
15
|
get '/'
|
22
|
-
response.status
|
16
|
+
assert_equal 207, response.status
|
23
17
|
end
|
24
18
|
end
|
25
19
|
|
@@ -32,7 +26,7 @@ describe 'Sinatra::Helpers' do
|
|
32
26
|
}
|
33
27
|
|
34
28
|
get '/'
|
35
|
-
|
29
|
+
assert_equal 'Hello World', body
|
36
30
|
end
|
37
31
|
|
38
32
|
it 'takes a String, Array, or other object responding to #each' do
|
@@ -43,7 +37,7 @@ describe 'Sinatra::Helpers' do
|
|
43
37
|
}
|
44
38
|
|
45
39
|
get '/'
|
46
|
-
|
40
|
+
assert_equal 'Hello World', body
|
47
41
|
end
|
48
42
|
end
|
49
43
|
|
@@ -57,9 +51,9 @@ describe 'Sinatra::Helpers' do
|
|
57
51
|
}
|
58
52
|
|
59
53
|
get '/'
|
60
|
-
|
61
|
-
body
|
62
|
-
response['Location']
|
54
|
+
assert_equal 302, status
|
55
|
+
assert_equal '', body
|
56
|
+
assert_equal '/foo', response['Location']
|
63
57
|
end
|
64
58
|
|
65
59
|
it 'uses the code given when specified' do
|
@@ -71,9 +65,9 @@ describe 'Sinatra::Helpers' do
|
|
71
65
|
}
|
72
66
|
|
73
67
|
get '/'
|
74
|
-
|
75
|
-
body
|
76
|
-
response['Location']
|
68
|
+
assert_equal 301, status
|
69
|
+
assert_equal '', body
|
70
|
+
assert_equal '/foo', response['Location']
|
77
71
|
end
|
78
72
|
end
|
79
73
|
|
@@ -87,8 +81,8 @@ describe 'Sinatra::Helpers' do
|
|
87
81
|
}
|
88
82
|
|
89
83
|
get '/'
|
90
|
-
|
91
|
-
body
|
84
|
+
assert_equal 501, status
|
85
|
+
assert_equal '', body
|
92
86
|
end
|
93
87
|
|
94
88
|
it 'takes an optional body' do
|
@@ -100,8 +94,8 @@ describe 'Sinatra::Helpers' do
|
|
100
94
|
}
|
101
95
|
|
102
96
|
get '/'
|
103
|
-
|
104
|
-
|
97
|
+
assert_equal 501, status
|
98
|
+
assert_equal 'FAIL', body
|
105
99
|
end
|
106
100
|
|
107
101
|
it 'uses a 500 status code when first argument is a body' do
|
@@ -113,8 +107,8 @@ describe 'Sinatra::Helpers' do
|
|
113
107
|
}
|
114
108
|
|
115
109
|
get '/'
|
116
|
-
|
117
|
-
|
110
|
+
assert_equal 500, status
|
111
|
+
assert_equal 'FAIL', body
|
118
112
|
end
|
119
113
|
end
|
120
114
|
|
@@ -128,8 +122,8 @@ describe 'Sinatra::Helpers' do
|
|
128
122
|
}
|
129
123
|
|
130
124
|
get '/'
|
131
|
-
|
132
|
-
body
|
125
|
+
assert_equal 404, status
|
126
|
+
assert_equal '', body
|
133
127
|
end
|
134
128
|
end
|
135
129
|
|
@@ -142,20 +136,20 @@ describe 'Sinatra::Helpers' do
|
|
142
136
|
}
|
143
137
|
|
144
138
|
get '/', :env => { 'rack.session' => { :foo => 'bar' } }
|
145
|
-
|
139
|
+
assert_equal 'bar', body
|
146
140
|
end
|
147
141
|
|
148
142
|
it 'creates a new session when none provided' do
|
149
143
|
mock_app {
|
150
144
|
get '/' do
|
151
|
-
session.
|
145
|
+
assert session.empty?
|
152
146
|
session[:foo] = 'bar'
|
153
147
|
'Hi'
|
154
148
|
end
|
155
149
|
}
|
156
150
|
|
157
151
|
get '/'
|
158
|
-
|
152
|
+
assert_equal 'Hi', body
|
159
153
|
end
|
160
154
|
end
|
161
155
|
|
@@ -163,18 +157,18 @@ describe 'Sinatra::Helpers' do
|
|
163
157
|
include Sinatra::Helpers
|
164
158
|
it "looks up media types in Rack's MIME registry" do
|
165
159
|
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
|
166
|
-
|
167
|
-
media_type('.foo')
|
168
|
-
|
160
|
+
assert_equal 'application/foo', media_type('foo')
|
161
|
+
assert_equal 'application/foo', media_type('.foo')
|
162
|
+
assert_equal 'application/foo', media_type(:foo)
|
169
163
|
end
|
170
164
|
it 'returns nil when given nil' do
|
171
|
-
media_type(nil).
|
165
|
+
assert media_type(nil).nil?
|
172
166
|
end
|
173
167
|
it 'returns nil when media type not registered' do
|
174
|
-
media_type(:bizzle).
|
168
|
+
assert media_type(:bizzle).nil?
|
175
169
|
end
|
176
170
|
it 'returns the argument when given a media type string' do
|
177
|
-
|
171
|
+
assert_equal 'text/plain', media_type('text/plain')
|
178
172
|
end
|
179
173
|
end
|
180
174
|
|
@@ -188,8 +182,8 @@ describe 'Sinatra::Helpers' do
|
|
188
182
|
}
|
189
183
|
|
190
184
|
get '/'
|
191
|
-
response['Content-Type']
|
192
|
-
|
185
|
+
assert_equal 'text/plain', response['Content-Type']
|
186
|
+
assert_equal 'Hello World', body
|
193
187
|
end
|
194
188
|
|
195
189
|
it 'takes media type parameters (like charset=)' do
|
@@ -201,9 +195,9 @@ describe 'Sinatra::Helpers' do
|
|
201
195
|
}
|
202
196
|
|
203
197
|
get '/'
|
204
|
-
|
205
|
-
|
206
|
-
|
198
|
+
assert ok?
|
199
|
+
assert_equal 'text/html;charset=utf-8', response['Content-Type']
|
200
|
+
assert_equal "<h1>Hello, World</h1>", body
|
207
201
|
end
|
208
202
|
|
209
203
|
it "looks up symbols in Rack's mime types dictionary" do
|
@@ -216,9 +210,9 @@ describe 'Sinatra::Helpers' do
|
|
216
210
|
}
|
217
211
|
|
218
212
|
get '/foo.xml'
|
219
|
-
|
220
|
-
response['Content-Type']
|
221
|
-
|
213
|
+
assert ok?
|
214
|
+
assert_equal 'application/foo', response['Content-Type']
|
215
|
+
assert_equal 'I AM FOO', body
|
222
216
|
end
|
223
217
|
|
224
218
|
it 'fails when no mime type is registered for the argument provided' do
|
@@ -228,8 +222,7 @@ describe 'Sinatra::Helpers' do
|
|
228
222
|
"I AM FOO"
|
229
223
|
end
|
230
224
|
}
|
231
|
-
|
232
|
-
lambda { get '/foo.xml' }.should.raise RuntimeError
|
225
|
+
assert_raise(RuntimeError) { get '/foo.xml' }
|
233
226
|
end
|
234
227
|
end
|
235
228
|
|
@@ -255,26 +248,26 @@ describe 'Sinatra::Helpers' do
|
|
255
248
|
it "sends the contents of the file" do
|
256
249
|
send_file_app
|
257
250
|
get '/file.txt'
|
258
|
-
|
259
|
-
|
251
|
+
assert ok?
|
252
|
+
assert_equal 'Hello World', body
|
260
253
|
end
|
261
254
|
|
262
255
|
it 'sets the Content-Type response header if a mime-type can be located' do
|
263
256
|
send_file_app
|
264
257
|
get '/file.txt'
|
265
|
-
response['Content-Type']
|
258
|
+
assert_equal 'text/plain', response['Content-Type']
|
266
259
|
end
|
267
260
|
|
268
261
|
it 'sets the Content-Length response header' do
|
269
262
|
send_file_app
|
270
263
|
get '/file.txt'
|
271
|
-
|
264
|
+
assert_equal 'Hello World'.length.to_s, response['Content-Length']
|
272
265
|
end
|
273
266
|
|
274
267
|
it 'sets the Last-Modified response header' do
|
275
268
|
send_file_app
|
276
269
|
get '/file.txt'
|
277
|
-
|
270
|
+
assert_equal File.mtime(@file).httpdate, response['Last-Modified']
|
278
271
|
end
|
279
272
|
|
280
273
|
it "returns a 404 when not found" do
|
@@ -284,7 +277,7 @@ describe 'Sinatra::Helpers' do
|
|
284
277
|
end
|
285
278
|
}
|
286
279
|
get '/'
|
287
|
-
|
280
|
+
assert not_found?
|
288
281
|
end
|
289
282
|
end
|
290
283
|
|
@@ -303,19 +296,19 @@ describe 'Sinatra::Helpers' do
|
|
303
296
|
|
304
297
|
it 'sets the Last-Modified header to a valid RFC 2616 date value' do
|
305
298
|
get '/'
|
306
|
-
response['Last-Modified']
|
299
|
+
assert_equal @now.httpdate, response['Last-Modified']
|
307
300
|
end
|
308
301
|
|
309
302
|
it 'returns a body when conditional get misses' do
|
310
303
|
get '/'
|
311
|
-
|
312
|
-
|
304
|
+
assert_equal 200, status
|
305
|
+
assert_equal 'Boo!', body
|
313
306
|
end
|
314
307
|
|
315
308
|
it 'halts when a conditional GET matches' do
|
316
309
|
get '/', :env => { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
|
317
|
-
|
318
|
-
body
|
310
|
+
assert_equal 304, status
|
311
|
+
assert_equal '', body
|
319
312
|
end
|
320
313
|
end
|
321
314
|
|
@@ -332,25 +325,25 @@ describe 'Sinatra::Helpers' do
|
|
332
325
|
|
333
326
|
it 'sets the ETag header' do
|
334
327
|
get '/'
|
335
|
-
response['ETag']
|
328
|
+
assert_equal '"FOO"', response['ETag']
|
336
329
|
end
|
337
330
|
|
338
331
|
it 'returns a body when conditional get misses' do
|
339
332
|
get '/'
|
340
|
-
|
341
|
-
|
333
|
+
assert_equal 200, status
|
334
|
+
assert_equal 'Boo!', body
|
342
335
|
end
|
343
336
|
|
344
337
|
it 'halts when a conditional GET matches' do
|
345
338
|
get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"FOO"' }
|
346
|
-
|
347
|
-
body
|
339
|
+
assert_equal 304, status
|
340
|
+
assert_equal '', body
|
348
341
|
end
|
349
342
|
|
350
343
|
it 'should handle multiple ETag values in If-None-Match header' do
|
351
344
|
get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"BAR", *' }
|
352
|
-
|
353
|
-
body
|
345
|
+
assert_equal 304, status
|
346
|
+
assert_equal '', body
|
354
347
|
end
|
355
348
|
|
356
349
|
it 'uses a weak etag with the :weak option' do
|
@@ -361,7 +354,7 @@ describe 'Sinatra::Helpers' do
|
|
361
354
|
end
|
362
355
|
}
|
363
356
|
get '/'
|
364
|
-
|
357
|
+
assert_equal 'W/"FOO"', response['ETag']
|
365
358
|
end
|
366
359
|
|
367
360
|
end
|
data/test/mapped_error_test.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
require '
|
2
|
-
require 'sinatra/base'
|
3
|
-
require 'sinatra/test'
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
4
2
|
|
5
3
|
describe 'Exception Mappings' do
|
6
|
-
include Sinatra::Test
|
7
|
-
|
8
4
|
class FooError < RuntimeError
|
9
5
|
end
|
10
6
|
|
@@ -17,8 +13,8 @@ describe 'Exception Mappings' do
|
|
17
13
|
end
|
18
14
|
}
|
19
15
|
get '/'
|
20
|
-
|
21
|
-
|
16
|
+
assert_equal 500, status
|
17
|
+
assert_equal 'Foo!', body
|
22
18
|
end
|
23
19
|
|
24
20
|
it 'uses the Exception handler if no matching handler found' do
|
@@ -30,16 +26,16 @@ describe 'Exception Mappings' do
|
|
30
26
|
end
|
31
27
|
}
|
32
28
|
get '/'
|
33
|
-
|
34
|
-
|
29
|
+
assert_equal 500, status
|
30
|
+
assert_equal 'Exception!', body
|
35
31
|
end
|
36
32
|
|
37
33
|
it "sets env['sinatra.error'] to the rescued exception" do
|
38
34
|
mock_app {
|
39
35
|
set :raise_errors, false
|
40
36
|
error(FooError) {
|
41
|
-
env.
|
42
|
-
env['sinatra.error'].
|
37
|
+
assert env.include?('sinatra.error')
|
38
|
+
assert env['sinatra.error'].kind_of?(FooError)
|
43
39
|
'looks good'
|
44
40
|
}
|
45
41
|
get '/' do
|
@@ -47,7 +43,19 @@ describe 'Exception Mappings' do
|
|
47
43
|
end
|
48
44
|
}
|
49
45
|
get '/'
|
50
|
-
|
46
|
+
assert_equal 'looks good', body
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'dumps errors to rack.errors when dump_errors is enabled' do
|
50
|
+
mock_app {
|
51
|
+
set :raise_errors, false
|
52
|
+
set :dump_errors, true
|
53
|
+
get('/') { raise FooError, 'BOOM!' }
|
54
|
+
}
|
55
|
+
|
56
|
+
get '/'
|
57
|
+
assert_equal 500, status
|
58
|
+
assert @response.errors =~ /FooError - BOOM!:/
|
51
59
|
end
|
52
60
|
|
53
61
|
it "raises without calling the handler when the raise_errors options is set" do
|
@@ -58,7 +66,7 @@ describe 'Exception Mappings' do
|
|
58
66
|
raise FooError
|
59
67
|
end
|
60
68
|
}
|
61
|
-
|
69
|
+
assert_raise(FooError) { get '/' }
|
62
70
|
end
|
63
71
|
|
64
72
|
it "never raises Sinatra::NotFound beyond the application" do
|
@@ -68,8 +76,8 @@ describe 'Exception Mappings' do
|
|
68
76
|
raise Sinatra::NotFound
|
69
77
|
end
|
70
78
|
}
|
71
|
-
|
72
|
-
|
79
|
+
assert_nothing_raised { get '/' }
|
80
|
+
assert_equal 404, status
|
73
81
|
end
|
74
82
|
|
75
83
|
class FooNotFound < Sinatra::NotFound
|
@@ -83,11 +91,22 @@ describe 'Exception Mappings' do
|
|
83
91
|
raise FooNotFound
|
84
92
|
end
|
85
93
|
}
|
86
|
-
|
87
|
-
|
88
|
-
|
94
|
+
assert_nothing_raised { get '/' }
|
95
|
+
assert_equal 404, status
|
96
|
+
assert_equal 'foo! not found.', body
|
89
97
|
end
|
90
98
|
|
99
|
+
it 'has a not_found method for backwards compatibility' do
|
100
|
+
mock_app {
|
101
|
+
not_found do
|
102
|
+
"Lost, are we?"
|
103
|
+
end
|
104
|
+
}
|
105
|
+
|
106
|
+
get '/test'
|
107
|
+
assert_equal 404, status
|
108
|
+
assert_equal "Lost, are we?", body
|
109
|
+
end
|
91
110
|
end
|
92
111
|
|
93
112
|
describe 'Custom Error Pages' do
|
@@ -100,8 +119,8 @@ describe 'Custom Error Pages' do
|
|
100
119
|
end
|
101
120
|
}
|
102
121
|
get '/'
|
103
|
-
|
104
|
-
|
122
|
+
assert_equal 500, status
|
123
|
+
assert_equal 'Foo!', body
|
105
124
|
end
|
106
125
|
|
107
126
|
it 'allows ranges of status code mappings to be registered with :error' do
|
@@ -113,8 +132,8 @@ describe 'Custom Error Pages' do
|
|
113
132
|
end
|
114
133
|
}
|
115
134
|
get '/'
|
116
|
-
|
117
|
-
|
135
|
+
assert_equal 507, status
|
136
|
+
assert_equal 'Error: 507', body
|
118
137
|
end
|
119
138
|
|
120
139
|
class FooError < RuntimeError
|
@@ -135,7 +154,7 @@ describe 'Custom Error Pages' do
|
|
135
154
|
end
|
136
155
|
}
|
137
156
|
get '/'
|
138
|
-
|
139
|
-
|
157
|
+
assert_equal 502, status
|
158
|
+
assert_equal 'from custom error page', body
|
140
159
|
end
|
141
160
|
end
|