sinatra 0.9.0.5 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sinatra might be problematic. Click here for more details.
- data/AUTHORS +8 -7
- data/CHANGES +70 -0
- data/README.rdoc +86 -155
- data/Rakefile +9 -59
- data/compat/app_test.rb +3 -21
- data/compat/application_test.rb +0 -72
- data/compat/pipeline_test.rb +0 -26
- data/compat/sessions_test.rb +3 -0
- data/compat/streaming_test.rb +15 -3
- data/lib/sinatra/base.rb +290 -142
- data/lib/sinatra/compat.rb +30 -30
- data/lib/sinatra/main.rb +4 -5
- data/lib/sinatra/test/bacon.rb +2 -0
- data/lib/sinatra/test/rspec.rb +2 -0
- data/lib/sinatra/test/spec.rb +2 -0
- data/lib/sinatra/test/unit.rb +2 -0
- data/lib/sinatra/test.rb +62 -50
- data/sinatra.gemspec +7 -3
- data/test/base_test.rb +108 -46
- data/test/erb_test.rb +31 -0
- data/test/extensions_test.rb +84 -0
- data/test/helper.rb +65 -9
- data/test/helpers_test.rb +469 -333
- data/test/mapped_error_test.rb +6 -6
- data/test/middleware_test.rb +13 -3
- data/test/options_test.rb +278 -1
- data/test/reload_test.rb +7 -0
- data/test/response_test.rb +42 -0
- data/test/result_test.rb +10 -0
- data/test/routing_test.rb +269 -2
- data/test/server_test.rb +41 -0
- data/test/static_test.rb +8 -25
- data/test/test_test.rb +144 -0
- metadata +13 -2
data/test/mapped_error_test.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
end
|
3
|
+
class FooError < RuntimeError
|
4
|
+
end
|
6
5
|
|
6
|
+
class FooNotFound < Sinatra::NotFound
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'Exception Mappings' do
|
7
10
|
it 'invokes handlers registered with ::error when raised' do
|
8
11
|
mock_app {
|
9
12
|
set :raise_errors, false
|
@@ -80,9 +83,6 @@ describe 'Exception Mappings' do
|
|
80
83
|
assert_equal 404, status
|
81
84
|
end
|
82
85
|
|
83
|
-
class FooNotFound < Sinatra::NotFound
|
84
|
-
end
|
85
|
-
|
86
86
|
it "cascades for subclasses of Sinatra::NotFound" do
|
87
87
|
mock_app {
|
88
88
|
set :raise_errors, true
|
data/test/middleware_test.rb
CHANGED
@@ -4,7 +4,9 @@ describe "Middleware" do
|
|
4
4
|
before do
|
5
5
|
@app = mock_app(Sinatra::Default) {
|
6
6
|
get '/*' do
|
7
|
-
response.headers['X-Tests'] = env['test.ran'].
|
7
|
+
response.headers['X-Tests'] = env['test.ran'].
|
8
|
+
map { |n| n.split('::').last }.
|
9
|
+
join(', ')
|
8
10
|
env['PATH_INFO']
|
9
11
|
end
|
10
12
|
}
|
@@ -38,7 +40,7 @@ describe "Middleware" do
|
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
41
|
-
|
43
|
+
it "runs in the order defined" do
|
42
44
|
@app.use UpcaseMiddleware
|
43
45
|
@app.use DowncaseMiddleware
|
44
46
|
get '/Foo'
|
@@ -46,7 +48,7 @@ describe "Middleware" do
|
|
46
48
|
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
47
49
|
end
|
48
50
|
|
49
|
-
|
51
|
+
it "resets the prebuilt pipeline when new middleware is added" do
|
50
52
|
@app.use UpcaseMiddleware
|
51
53
|
get '/Foo'
|
52
54
|
assert_equal "/FOO", body
|
@@ -55,4 +57,12 @@ describe "Middleware" do
|
|
55
57
|
assert_equal '/foo', body
|
56
58
|
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
57
59
|
end
|
60
|
+
|
61
|
+
it "works when app is used as middleware" do
|
62
|
+
@app.use UpcaseMiddleware
|
63
|
+
@app = @app.new
|
64
|
+
get '/Foo'
|
65
|
+
assert_equal "/FOO", body
|
66
|
+
assert_equal "UpcaseMiddleware", response['X-Tests']
|
67
|
+
end
|
58
68
|
end
|
data/test/options_test.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/helper'
|
2
2
|
|
3
3
|
describe 'Options' do
|
4
|
-
before
|
4
|
+
before do
|
5
|
+
restore_default_options
|
6
|
+
@app = Sinatra.new
|
7
|
+
end
|
5
8
|
|
6
9
|
it 'sets options to literal values' do
|
7
10
|
@app.set(:foo, 'bar')
|
@@ -95,3 +98,277 @@ describe 'Options' do
|
|
95
98
|
assert_equal 'okay', body
|
96
99
|
end
|
97
100
|
end
|
101
|
+
|
102
|
+
describe_option 'clean_trace' do
|
103
|
+
def clean_backtrace(trace)
|
104
|
+
@base.new.send(:clean_backtrace, trace)
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'is enabled on Base' do
|
108
|
+
assert @base.clean_trace?
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'is enabled on Default' do
|
112
|
+
assert @default.clean_trace?
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'does nothing when disabled' do
|
116
|
+
backtrace = [
|
117
|
+
"./lib/sinatra/base.rb",
|
118
|
+
"./myapp:42",
|
119
|
+
("#{Gem.dir}/some/lib.rb" if defined?(Gem))
|
120
|
+
].compact
|
121
|
+
@base.set :clean_trace, false
|
122
|
+
assert_equal backtrace, clean_backtrace(backtrace)
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'removes sinatra lib paths from backtrace when enabled' do
|
126
|
+
backtrace = [
|
127
|
+
"./lib/sinatra/base.rb",
|
128
|
+
"./lib/sinatra/compat.rb:42",
|
129
|
+
"./lib/sinatra/main.rb:55 in `foo'"
|
130
|
+
]
|
131
|
+
assert clean_backtrace(backtrace).empty?
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'removes ./ prefix from backtrace paths when enabled' do
|
135
|
+
assert_equal ['myapp.rb:42'], clean_backtrace(['./myapp.rb:42'])
|
136
|
+
end
|
137
|
+
|
138
|
+
if defined?(Gem)
|
139
|
+
it 'removes gem lib paths from backtrace when enabled' do
|
140
|
+
assert clean_backtrace(["#{Gem.dir}/some/lib"]).empty?
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe_option 'run' do
|
146
|
+
it 'is disabled on Base' do
|
147
|
+
assert ! @base.run?
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'is enabled on Default when not in test environment' do
|
151
|
+
assert @default.development?
|
152
|
+
assert @default.run?
|
153
|
+
|
154
|
+
@default.set :environment, :development
|
155
|
+
assert @default.run?
|
156
|
+
end
|
157
|
+
|
158
|
+
# TODO: it 'is enabled when $0 == app_file'
|
159
|
+
end
|
160
|
+
|
161
|
+
describe_option 'raise_errors' do
|
162
|
+
it 'is enabled on Base' do
|
163
|
+
assert @base.raise_errors?
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'is enabled on Default only in test' do
|
167
|
+
@default.set(:environment, :development)
|
168
|
+
assert @default.development?
|
169
|
+
assert ! @default.raise_errors?, "disabled development"
|
170
|
+
|
171
|
+
@default.set(:environment, :production)
|
172
|
+
assert ! @default.raise_errors?
|
173
|
+
|
174
|
+
@default.set(:environment, :test)
|
175
|
+
assert @default.raise_errors?
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe_option 'dump_errors' do
|
180
|
+
it 'is disabled on Base' do
|
181
|
+
assert ! @base.dump_errors?
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'is enabled on Default' do
|
185
|
+
assert @default.dump_errors?
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'dumps exception with backtrace to rack.errors' do
|
189
|
+
Sinatra::Default.disable(:raise_errors)
|
190
|
+
|
191
|
+
mock_app(Sinatra::Default) {
|
192
|
+
error do
|
193
|
+
error = @env['rack.errors'].instance_variable_get(:@error)
|
194
|
+
error.rewind
|
195
|
+
|
196
|
+
error.read
|
197
|
+
end
|
198
|
+
|
199
|
+
get '/' do
|
200
|
+
raise
|
201
|
+
end
|
202
|
+
}
|
203
|
+
|
204
|
+
get '/'
|
205
|
+
assert body.include?("RuntimeError") && body.include?("options_test.rb")
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
describe_option 'sessions' do
|
210
|
+
it 'is disabled on Base' do
|
211
|
+
assert ! @base.sessions?
|
212
|
+
end
|
213
|
+
|
214
|
+
it 'is disabled on Default' do
|
215
|
+
assert ! @default.sessions?
|
216
|
+
end
|
217
|
+
|
218
|
+
# TODO: it 'uses Rack::Session::Cookie when enabled' do
|
219
|
+
end
|
220
|
+
|
221
|
+
describe_option 'logging' do
|
222
|
+
it 'is disabled on Base' do
|
223
|
+
assert ! @base.logging?
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'is enabled on Default when not in test environment' do
|
227
|
+
assert @default.logging?
|
228
|
+
|
229
|
+
@default.set :environment, :test
|
230
|
+
assert ! @default.logging
|
231
|
+
end
|
232
|
+
|
233
|
+
# TODO: it 'uses Rack::CommonLogger when enabled' do
|
234
|
+
end
|
235
|
+
|
236
|
+
describe_option 'static' do
|
237
|
+
it 'is disabled on Base' do
|
238
|
+
assert ! @base.static?
|
239
|
+
end
|
240
|
+
|
241
|
+
it 'is enabled on Default' do
|
242
|
+
assert @default.static?
|
243
|
+
end
|
244
|
+
|
245
|
+
# TODO: it setup static routes if public is enabled
|
246
|
+
# TODO: however, that's already tested in static_test so...
|
247
|
+
end
|
248
|
+
|
249
|
+
describe_option 'host' do
|
250
|
+
it 'defaults to 0.0.0.0' do
|
251
|
+
assert_equal '0.0.0.0', @base.host
|
252
|
+
assert_equal '0.0.0.0', @default.host
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
describe_option 'port' do
|
257
|
+
it 'defaults to 4567' do
|
258
|
+
assert_equal 4567, @base.port
|
259
|
+
assert_equal 4567, @default.port
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe_option 'server' do
|
264
|
+
it 'is one of thin, mongrel, webrick' do
|
265
|
+
assert_equal %w[thin mongrel webrick], @base.server
|
266
|
+
assert_equal %w[thin mongrel webrick], @default.server
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
describe_option 'app_file' do
|
271
|
+
it 'is nil' do
|
272
|
+
assert @base.app_file.nil?
|
273
|
+
assert @default.app_file.nil?
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
describe_option 'root' do
|
278
|
+
it 'is nil if app_file is not set' do
|
279
|
+
assert @base.root.nil?
|
280
|
+
assert @default.root.nil?
|
281
|
+
end
|
282
|
+
|
283
|
+
it 'is equal to the expanded basename of app_file' do
|
284
|
+
@base.app_file = __FILE__
|
285
|
+
assert_equal File.expand_path(File.dirname(__FILE__)), @base.root
|
286
|
+
|
287
|
+
@default.app_file = __FILE__
|
288
|
+
assert_equal File.expand_path(File.dirname(__FILE__)), @default.root
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
describe_option 'views' do
|
293
|
+
it 'is nil if root is not set' do
|
294
|
+
assert @base.views.nil?
|
295
|
+
assert @default.views.nil?
|
296
|
+
end
|
297
|
+
|
298
|
+
it 'is set to root joined with views/' do
|
299
|
+
@base.root = File.dirname(__FILE__)
|
300
|
+
assert_equal File.dirname(__FILE__) + "/views", @base.views
|
301
|
+
|
302
|
+
@default.root = File.dirname(__FILE__)
|
303
|
+
assert_equal File.dirname(__FILE__) + "/views", @default.views
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
describe_option 'public' do
|
308
|
+
it 'is nil if root is not set' do
|
309
|
+
assert @base.public.nil?
|
310
|
+
assert @default.public.nil?
|
311
|
+
end
|
312
|
+
|
313
|
+
it 'is set to root joined with public/' do
|
314
|
+
@base.root = File.dirname(__FILE__)
|
315
|
+
assert_equal File.dirname(__FILE__) + "/public", @base.public
|
316
|
+
|
317
|
+
@default.root = File.dirname(__FILE__)
|
318
|
+
assert_equal File.dirname(__FILE__) + "/public", @default.public
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
describe_option 'reload' do
|
323
|
+
it 'is enabled when
|
324
|
+
app_file is set,
|
325
|
+
is not a rackup file,
|
326
|
+
and we are in development' do
|
327
|
+
@base.app_file = __FILE__
|
328
|
+
@base.set(:environment, :development)
|
329
|
+
assert @base.reload?
|
330
|
+
|
331
|
+
@default.app_file = __FILE__
|
332
|
+
@default.set(:environment, :development)
|
333
|
+
assert @default.reload?
|
334
|
+
end
|
335
|
+
|
336
|
+
it 'is disabled if app_file is not set' do
|
337
|
+
assert ! @base.reload?
|
338
|
+
assert ! @default.reload?
|
339
|
+
end
|
340
|
+
|
341
|
+
it 'is disabled if app_file is a rackup file' do
|
342
|
+
@base.app_file = 'config.ru'
|
343
|
+
assert ! @base.reload?
|
344
|
+
|
345
|
+
@default.app_file = 'config.ru'
|
346
|
+
assert ! @base.reload?
|
347
|
+
end
|
348
|
+
|
349
|
+
it 'is disabled if we are not in development' do
|
350
|
+
@base.set(:environment, :foo)
|
351
|
+
assert ! @base.reload
|
352
|
+
|
353
|
+
@default.set(:environment, :bar)
|
354
|
+
assert ! @default.reload
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
describe_option 'lock' do
|
359
|
+
it 'is enabled when reload is enabled' do
|
360
|
+
@base.enable(:reload)
|
361
|
+
assert @base.lock?
|
362
|
+
|
363
|
+
@default.enable(:reload)
|
364
|
+
assert @default.lock?
|
365
|
+
end
|
366
|
+
|
367
|
+
it 'is disabled when reload is disabled' do
|
368
|
+
@base.disable(:reload)
|
369
|
+
assert ! @base.lock?
|
370
|
+
|
371
|
+
@default.disable(:reload)
|
372
|
+
assert ! @default.lock?
|
373
|
+
end
|
374
|
+
end
|
data/test/reload_test.rb
CHANGED
@@ -34,6 +34,13 @@ describe "Reloading" do
|
|
34
34
|
assert_same false, @app.reload?
|
35
35
|
end
|
36
36
|
|
37
|
+
it 'is disabled when app_file is a rackup (.ru) file' do
|
38
|
+
@app.set :app_file, __FILE__.sub(/\.rb$/, '.ru')
|
39
|
+
@app.set :environment, :development
|
40
|
+
assert !@app.reload
|
41
|
+
assert_same false, @app.reload?
|
42
|
+
end
|
43
|
+
|
37
44
|
it 'can be turned off explicitly' do
|
38
45
|
@app.set :app_file, __FILE__
|
39
46
|
@app.set :environment, :development
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/helper'
|
4
|
+
|
5
|
+
describe 'Sinatra::Response' do
|
6
|
+
before do
|
7
|
+
@response = Sinatra::Response.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "initializes with 200, text/html, and empty body" do
|
11
|
+
assert_equal 200, @response.status
|
12
|
+
assert_equal 'text/html', @response['Content-Type']
|
13
|
+
assert_equal [], @response.body
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'uses case insensitive headers' do
|
17
|
+
@response['content-type'] = 'application/foo'
|
18
|
+
assert_equal 'application/foo', @response['Content-Type']
|
19
|
+
assert_equal 'application/foo', @response['CONTENT-TYPE']
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'writes to body' do
|
23
|
+
@response.body = 'Hello'
|
24
|
+
@response.write ' World'
|
25
|
+
assert_equal 'Hello World', @response.body
|
26
|
+
end
|
27
|
+
|
28
|
+
[204, 304].each do |status_code|
|
29
|
+
it "removes the Content-Type header and body when response status is #{status_code}" do
|
30
|
+
@response.status = status_code
|
31
|
+
@response.body = ['Hello World']
|
32
|
+
assert_equal [status_code, {}, []], @response.finish
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'Calculates the Content-Length using the bytesize of the body' do
|
37
|
+
@response.body = ['Hello', 'World!', '✈']
|
38
|
+
status, headers, body = @response.finish
|
39
|
+
assert_equal '14', headers['Content-Length']
|
40
|
+
assert_equal @response.body, body
|
41
|
+
end
|
42
|
+
end
|
data/test/result_test.rb
CHANGED
@@ -76,6 +76,16 @@ describe 'Result Handling' do
|
|
76
76
|
assert_equal 'formula of', body
|
77
77
|
end
|
78
78
|
|
79
|
+
it "raises a TypeError when result is a non two or three tuple Array" do
|
80
|
+
mock_app {
|
81
|
+
get '/' do
|
82
|
+
[409, 'formula of', 'something else', 'even more']
|
83
|
+
end
|
84
|
+
}
|
85
|
+
|
86
|
+
assert_raise(TypeError) { get '/' }
|
87
|
+
end
|
88
|
+
|
79
89
|
it "sets status when result is a Fixnum status code" do
|
80
90
|
mock_app {
|
81
91
|
get('/') { 205 }
|