sinatra 1.4.8 → 2.0.8.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.

Files changed (129) hide show
  1. checksums.yaml +5 -5
  2. data/AUTHORS.md +1 -0
  3. data/CHANGELOG.md +238 -54
  4. data/CONTRIBUTING.md +8 -8
  5. data/Gemfile +47 -47
  6. data/LICENSE +4 -1
  7. data/MAINTENANCE.md +42 -0
  8. data/README.de.md +650 -442
  9. data/README.es.md +738 -357
  10. data/README.fr.md +15 -15
  11. data/README.hu.md +37 -3
  12. data/README.ja.md +124 -66
  13. data/README.ko.md +14 -14
  14. data/README.malayalam.md +3141 -0
  15. data/README.md +530 -403
  16. data/README.pt-br.md +2361 -334
  17. data/README.pt-pt.md +2 -2
  18. data/README.ru.md +856 -607
  19. data/README.zh.md +90 -28
  20. data/Rakefile +77 -51
  21. data/SECURITY.md +35 -0
  22. data/VERSION +1 -0
  23. data/lib/sinatra/base.rb +177 -239
  24. data/lib/sinatra/indifferent_hash.rb +200 -0
  25. data/lib/sinatra/main.rb +30 -10
  26. data/lib/sinatra/show_exceptions.rb +102 -62
  27. data/lib/sinatra/version.rb +1 -1
  28. data/sinatra.gemspec +44 -8
  29. metadata +41 -166
  30. data/lib/sinatra/ext.rb +0 -17
  31. data/test/asciidoctor_test.rb +0 -72
  32. data/test/base_test.rb +0 -167
  33. data/test/builder_test.rb +0 -91
  34. data/test/coffee_test.rb +0 -96
  35. data/test/compile_test.rb +0 -183
  36. data/test/contest.rb +0 -91
  37. data/test/creole_test.rb +0 -65
  38. data/test/delegator_test.rb +0 -160
  39. data/test/encoding_test.rb +0 -20
  40. data/test/erb_test.rb +0 -116
  41. data/test/extensions_test.rb +0 -98
  42. data/test/filter_test.rb +0 -487
  43. data/test/haml_test.rb +0 -109
  44. data/test/helper.rb +0 -132
  45. data/test/helpers_test.rb +0 -1917
  46. data/test/integration/app.rb +0 -79
  47. data/test/integration_helper.rb +0 -236
  48. data/test/integration_test.rb +0 -104
  49. data/test/less_test.rb +0 -69
  50. data/test/liquid_test.rb +0 -77
  51. data/test/mapped_error_test.rb +0 -285
  52. data/test/markaby_test.rb +0 -80
  53. data/test/markdown_test.rb +0 -85
  54. data/test/mediawiki_test.rb +0 -68
  55. data/test/middleware_test.rb +0 -68
  56. data/test/nokogiri_test.rb +0 -67
  57. data/test/public/favicon.ico +0 -0
  58. data/test/public/hello+world.txt +0 -1
  59. data/test/rabl_test.rb +0 -89
  60. data/test/rack_test.rb +0 -45
  61. data/test/radius_test.rb +0 -59
  62. data/test/rdoc_test.rb +0 -66
  63. data/test/readme_test.rb +0 -130
  64. data/test/request_test.rb +0 -100
  65. data/test/response_test.rb +0 -63
  66. data/test/result_test.rb +0 -76
  67. data/test/route_added_hook_test.rb +0 -59
  68. data/test/routing_test.rb +0 -1456
  69. data/test/sass_test.rb +0 -115
  70. data/test/scss_test.rb +0 -88
  71. data/test/server_test.rb +0 -56
  72. data/test/settings_test.rb +0 -582
  73. data/test/sinatra_test.rb +0 -12
  74. data/test/slim_test.rb +0 -102
  75. data/test/static_test.rb +0 -266
  76. data/test/streaming_test.rb +0 -149
  77. data/test/stylus_test.rb +0 -90
  78. data/test/templates_test.rb +0 -382
  79. data/test/textile_test.rb +0 -65
  80. data/test/views/a/in_a.str +0 -1
  81. data/test/views/ascii.erb +0 -2
  82. data/test/views/b/in_b.str +0 -1
  83. data/test/views/calc.html.erb +0 -1
  84. data/test/views/error.builder +0 -3
  85. data/test/views/error.erb +0 -3
  86. data/test/views/error.haml +0 -3
  87. data/test/views/error.sass +0 -2
  88. data/test/views/explicitly_nested.str +0 -1
  89. data/test/views/foo/hello.test +0 -1
  90. data/test/views/hello.asciidoc +0 -1
  91. data/test/views/hello.builder +0 -1
  92. data/test/views/hello.coffee +0 -1
  93. data/test/views/hello.creole +0 -1
  94. data/test/views/hello.erb +0 -1
  95. data/test/views/hello.haml +0 -1
  96. data/test/views/hello.less +0 -5
  97. data/test/views/hello.liquid +0 -1
  98. data/test/views/hello.mab +0 -1
  99. data/test/views/hello.md +0 -1
  100. data/test/views/hello.mediawiki +0 -1
  101. data/test/views/hello.nokogiri +0 -1
  102. data/test/views/hello.rabl +0 -2
  103. data/test/views/hello.radius +0 -1
  104. data/test/views/hello.rdoc +0 -1
  105. data/test/views/hello.sass +0 -2
  106. data/test/views/hello.scss +0 -3
  107. data/test/views/hello.slim +0 -1
  108. data/test/views/hello.str +0 -1
  109. data/test/views/hello.styl +0 -2
  110. data/test/views/hello.test +0 -1
  111. data/test/views/hello.textile +0 -1
  112. data/test/views/hello.wlang +0 -1
  113. data/test/views/hello.yajl +0 -1
  114. data/test/views/layout2.builder +0 -3
  115. data/test/views/layout2.erb +0 -2
  116. data/test/views/layout2.haml +0 -2
  117. data/test/views/layout2.liquid +0 -2
  118. data/test/views/layout2.mab +0 -2
  119. data/test/views/layout2.nokogiri +0 -3
  120. data/test/views/layout2.rabl +0 -3
  121. data/test/views/layout2.radius +0 -2
  122. data/test/views/layout2.slim +0 -3
  123. data/test/views/layout2.str +0 -2
  124. data/test/views/layout2.test +0 -1
  125. data/test/views/layout2.wlang +0 -2
  126. data/test/views/nested.str +0 -1
  127. data/test/views/utf8.erb +0 -2
  128. data/test/wlang_test.rb +0 -87
  129. data/test/yajl_test.rb +0 -86
data/test/erb_test.rb DELETED
@@ -1,116 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class ERBTest < Minitest::Test
4
- def engine
5
- Tilt::ERBTemplate
6
- end
7
-
8
- def setup
9
- Tilt.prefer engine, :erb
10
- super
11
- end
12
-
13
- def erb_app(&block)
14
- mock_app do
15
- set :views, File.dirname(__FILE__) + '/views'
16
- get('/', &block)
17
- end
18
- get '/'
19
- end
20
-
21
- it 'uses the correct engine' do
22
- assert_equal engine, Tilt[:erb]
23
- end
24
-
25
- it 'renders inline ERB strings' do
26
- erb_app { erb '<%= 1 + 1 %>' }
27
- assert ok?
28
- assert_equal '2', body
29
- end
30
-
31
- it 'renders .erb files in views path' do
32
- erb_app { erb :hello }
33
- assert ok?
34
- assert_equal "Hello World\n", body
35
- end
36
-
37
- it 'takes a :locals option' do
38
- erb_app do
39
- locals = {:foo => 'Bar'}
40
- erb '<%= foo %>', :locals => locals
41
- end
42
- assert ok?
43
- assert_equal 'Bar', body
44
- end
45
-
46
- it "renders with inline layouts" do
47
- mock_app do
48
- layout { 'THIS. IS. <%= yield.upcase %>!' }
49
- get('/') { erb 'Sparta' }
50
- end
51
- get '/'
52
- assert ok?
53
- assert_equal 'THIS. IS. SPARTA!', body
54
- end
55
-
56
- it "renders with file layouts" do
57
- erb_app { erb 'Hello World', :layout => :layout2 }
58
- assert ok?
59
- assert_body "ERB Layout!\nHello World"
60
- end
61
-
62
- it "renders erb with blocks" do
63
- mock_app do
64
- def container
65
- @_out_buf << "THIS."
66
- yield
67
- @_out_buf << "SPARTA!"
68
- end
69
- def is; "IS." end
70
- get('/') { erb '<% container do %> <%= is %> <% end %>' }
71
- end
72
- get '/'
73
- assert ok?
74
- assert_equal 'THIS. IS. SPARTA!', body
75
- end
76
-
77
- it "can be used in a nested fashion for partials and whatnot" do
78
- mock_app do
79
- template(:inner) { "<inner><%= 'hi' %></inner>" }
80
- template(:outer) { "<outer><%= erb :inner %></outer>" }
81
- get('/') { erb :outer }
82
- end
83
-
84
- get '/'
85
- assert ok?
86
- assert_equal '<outer><inner>hi</inner></outer>', body
87
- end
88
-
89
- it "can render truly nested layouts by accepting a layout and a block with the contents" do
90
- mock_app do
91
- template(:main_outer_layout) { "<h1>Title</h1>\n<%= yield %>" }
92
- template(:an_inner_layout) { "<h2>Subtitle</h2>\n<%= yield %>" }
93
- template(:a_page) { "<p>Contents.</p>\n" }
94
- get('/') do
95
- erb :main_outer_layout, :layout => false do
96
- erb :an_inner_layout do
97
- erb :a_page
98
- end
99
- end
100
- end
101
- end
102
- get '/'
103
- assert ok?
104
- assert_body "<h1>Title</h1>\n<h2>Subtitle</h2>\n<p>Contents.</p>\n"
105
- end
106
- end
107
-
108
-
109
- begin
110
- require 'erubis'
111
- class ErubisTest < ERBTest
112
- def engine; Tilt::ErubisTemplate end
113
- end
114
- rescue LoadError
115
- warn "#{$!.to_s}: skipping erubis tests"
116
- end
@@ -1,98 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class ExtensionsTest < Minitest::Test
4
- module FooExtensions
5
- def foo
6
- end
7
-
8
- private
9
- def im_hiding_in_ur_foos
10
- end
11
- end
12
-
13
- module BarExtensions
14
- def bar
15
- end
16
- end
17
-
18
- module BazExtensions
19
- def baz
20
- end
21
- end
22
-
23
- module QuuxExtensions
24
- def quux
25
- end
26
- end
27
-
28
- module PainExtensions
29
- def foo=(name); end
30
- def bar?(name); end
31
- def fizz!(name); end
32
- end
33
-
34
- it 'will add the methods to the DSL for the class in which you register them and its subclasses' do
35
- Sinatra::Base.register FooExtensions
36
- assert Sinatra::Base.respond_to?(:foo)
37
-
38
- Sinatra::Application.register BarExtensions
39
- assert Sinatra::Application.respond_to?(:bar)
40
- assert Sinatra::Application.respond_to?(:foo)
41
- assert !Sinatra::Base.respond_to?(:bar)
42
- end
43
-
44
- it 'allows extending by passing a block' do
45
- Sinatra::Base.register { def im_in_ur_anonymous_module; end }
46
- assert Sinatra::Base.respond_to?(:im_in_ur_anonymous_module)
47
- end
48
-
49
- it 'will make sure any public methods added via Application#register are delegated to Sinatra::Delegator' do
50
- Sinatra::Application.register FooExtensions
51
- assert Sinatra::Delegator.private_instance_methods.
52
- map { |m| m.to_sym }.include?(:foo)
53
- assert !Sinatra::Delegator.private_instance_methods.
54
- map { |m| m.to_sym }.include?(:im_hiding_in_ur_foos)
55
- end
56
-
57
- it 'will handle special method names' do
58
- Sinatra::Application.register PainExtensions
59
- assert Sinatra::Delegator.private_instance_methods.
60
- map { |m| m.to_sym }.include?(:foo=)
61
- assert Sinatra::Delegator.private_instance_methods.
62
- map { |m| m.to_sym }.include?(:bar?)
63
- assert Sinatra::Delegator.private_instance_methods.
64
- map { |m| m.to_sym }.include?(:fizz!)
65
- end
66
-
67
- it 'will not delegate methods on Base#register' do
68
- Sinatra::Base.register QuuxExtensions
69
- assert !Sinatra::Delegator.private_instance_methods.include?("quux")
70
- end
71
-
72
- it 'will extend the Sinatra::Application application by default' do
73
- Sinatra.register BazExtensions
74
- assert !Sinatra::Base.respond_to?(:baz)
75
- assert Sinatra::Application.respond_to?(:baz)
76
- end
77
-
78
- module BizzleExtension
79
- def bizzle
80
- bizzle_option
81
- end
82
-
83
- def self.registered(base)
84
- fail "base should be BizzleApp" unless base == BizzleApp
85
- fail "base should have already extended BizzleExtension" unless base.respond_to?(:bizzle)
86
- base.set :bizzle_option, 'bizzle!'
87
- end
88
- end
89
-
90
- class BizzleApp < Sinatra::Base
91
- end
92
-
93
- it 'sends .registered to the extension module after extending the class' do
94
- BizzleApp.register BizzleExtension
95
- assert_equal 'bizzle!', BizzleApp.bizzle_option
96
- assert_equal 'bizzle!', BizzleApp.bizzle
97
- end
98
- end
data/test/filter_test.rb DELETED
@@ -1,487 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class BeforeFilterTest < Minitest::Test
4
- it "executes filters in the order defined" do
5
- count = 0
6
- mock_app do
7
- get('/') { 'Hello World' }
8
- before do
9
- assert_equal 0, count
10
- count = 1
11
- end
12
- before do
13
- assert_equal 1, count
14
- count = 2
15
- end
16
- end
17
-
18
- get '/'
19
- assert ok?
20
- assert_equal 2, count
21
- assert_equal 'Hello World', body
22
- end
23
-
24
- it "can modify the request" do
25
- mock_app do
26
- get('/foo') { 'foo' }
27
- get('/bar') { 'bar' }
28
- before { request.path_info = '/bar' }
29
- end
30
-
31
- get '/foo'
32
- assert ok?
33
- assert_equal 'bar', body
34
- end
35
-
36
- it "can modify instance variables available to routes" do
37
- mock_app do
38
- before { @foo = 'bar' }
39
- get('/foo') { @foo }
40
- end
41
-
42
- get '/foo'
43
- assert ok?
44
- assert_equal 'bar', body
45
- end
46
-
47
- it "allows redirects" do
48
- mock_app do
49
- before { redirect '/bar' }
50
- get('/foo') do
51
- fail 'before block should have halted processing'
52
- 'ORLY?!'
53
- end
54
- end
55
-
56
- get '/foo'
57
- assert redirect?
58
- assert_equal 'http://example.org/bar', response['Location']
59
- assert_equal '', body
60
- end
61
-
62
- it "does not modify the response with its return value" do
63
- mock_app do
64
- before { 'Hello World!' }
65
- get('/foo') do
66
- assert_equal [], response.body
67
- 'cool'
68
- end
69
- end
70
-
71
- get '/foo'
72
- assert ok?
73
- assert_equal 'cool', body
74
- end
75
-
76
- it "does modify the response with halt" do
77
- mock_app do
78
- before { halt 302, 'Hi' }
79
- get '/foo' do
80
- "should not happen"
81
- end
82
- end
83
-
84
- get '/foo'
85
- assert_equal 302, response.status
86
- assert_equal 'Hi', body
87
- end
88
-
89
- it "gives you access to params" do
90
- mock_app do
91
- before { @foo = params['foo'] }
92
- get('/foo') { @foo }
93
- end
94
-
95
- get '/foo?foo=cool'
96
- assert ok?
97
- assert_equal 'cool', body
98
- end
99
-
100
- it "properly unescapes parameters" do
101
- mock_app do
102
- before { @foo = params['foo'] }
103
- get('/foo') { @foo }
104
- end
105
-
106
- get '/foo?foo=bar%3Abaz%2Fbend'
107
- assert ok?
108
- assert_equal 'bar:baz/bend', body
109
- end
110
-
111
- it "runs filters defined in superclasses" do
112
- base = Class.new(Sinatra::Base)
113
- base.before { @foo = 'hello from superclass' }
114
-
115
- mock_app(base) { get('/foo') { @foo } }
116
-
117
- get '/foo'
118
- assert_equal 'hello from superclass', body
119
- end
120
-
121
- it 'does not run before filter when serving static files' do
122
- ran_filter = false
123
- mock_app do
124
- before { ran_filter = true }
125
- set :static, true
126
- set :public_folder, File.dirname(__FILE__)
127
- end
128
- get "/#{File.basename(__FILE__)}"
129
- assert ok?
130
- assert_equal File.read(__FILE__), body
131
- assert !ran_filter
132
- end
133
-
134
- it 'takes an optional route pattern' do
135
- ran_filter = false
136
- mock_app do
137
- before("/b*") { ran_filter = true }
138
- get('/foo') { }
139
- get('/bar') { }
140
- end
141
- get '/foo'
142
- assert !ran_filter
143
- get '/bar'
144
- assert ran_filter
145
- end
146
-
147
- it 'generates block arguments from route pattern' do
148
- subpath = nil
149
- mock_app do
150
- before("/foo/:sub") { |s| subpath = s }
151
- get('/foo/*') { }
152
- end
153
- get '/foo/bar'
154
- assert_equal subpath, 'bar'
155
- end
156
-
157
- it 'can catch exceptions in before filters and handle them properly' do
158
- doodle = ''
159
- mock_app do
160
- before do
161
- doodle += 'This begins'
162
- raise StandardError, "before"
163
- end
164
- get "/" do
165
- doodle = 'and runs'
166
- end
167
- error 500 do
168
- "Error handled #{env['sinatra.error'].message}"
169
- end
170
- end
171
-
172
- doodle = ''
173
- get '/'
174
- assert_equal 'Error handled before', body
175
- assert_equal 'This begins', doodle
176
- end
177
- end
178
-
179
- class AfterFilterTest < Minitest::Test
180
- it "executes before and after filters in correct order" do
181
- invoked = 0
182
- mock_app do
183
- before { invoked = 2 }
184
- get('/') { invoked += 2; 'hello' }
185
- after { invoked *= 2 }
186
- end
187
-
188
- get '/'
189
- assert ok?
190
-
191
- assert_equal 8, invoked
192
- end
193
-
194
- it "executes filters in the order defined" do
195
- count = 0
196
- mock_app do
197
- get('/') { 'Hello World' }
198
- after do
199
- assert_equal 0, count
200
- count = 1
201
- end
202
- after do
203
- assert_equal 1, count
204
- count = 2
205
- end
206
- end
207
-
208
- get '/'
209
- assert ok?
210
- assert_equal 2, count
211
- assert_equal 'Hello World', body
212
- end
213
-
214
- it "allows redirects" do
215
- mock_app do
216
- get('/foo') { 'ORLY' }
217
- after { redirect '/bar' }
218
- end
219
-
220
- get '/foo'
221
- assert redirect?
222
- assert_equal 'http://example.org/bar', response['Location']
223
- assert_equal '', body
224
- end
225
-
226
- it "does not modify the response with its return value" do
227
- mock_app do
228
- get('/foo') { 'cool' }
229
- after { 'Hello World!' }
230
- end
231
-
232
- get '/foo'
233
- assert ok?
234
- assert_equal 'cool', body
235
- end
236
-
237
- it "does modify the response with halt" do
238
- mock_app do
239
- get '/foo' do
240
- "should not be returned"
241
- end
242
- after { halt 302, 'Hi' }
243
- end
244
-
245
- get '/foo'
246
- assert_equal 302, response.status
247
- assert_equal 'Hi', body
248
- end
249
-
250
- it "runs filters defined in superclasses" do
251
- count = 2
252
- base = Class.new(Sinatra::Base)
253
- base.after { count *= 2 }
254
- mock_app(base) do
255
- get('/foo') do
256
- count += 2
257
- "ok"
258
- end
259
- end
260
-
261
- get '/foo'
262
- assert_equal 8, count
263
- end
264
-
265
- it 'does not run after filter when serving static files' do
266
- ran_filter = false
267
- mock_app do
268
- after { ran_filter = true }
269
- set :static, true
270
- set :public_folder, File.dirname(__FILE__)
271
- end
272
- get "/#{File.basename(__FILE__)}"
273
- assert ok?
274
- assert_equal File.read(__FILE__), body
275
- assert !ran_filter
276
- end
277
-
278
- it 'takes an optional route pattern' do
279
- ran_filter = false
280
- mock_app do
281
- after("/b*") { ran_filter = true }
282
- get('/foo') { }
283
- get('/bar') { }
284
- end
285
- get '/foo'
286
- assert !ran_filter
287
- get '/bar'
288
- assert ran_filter
289
- end
290
-
291
- it 'changes to path_info from a pattern matching before filter are respected when routing' do
292
- mock_app do
293
- before('/foo') { request.path_info = '/bar' }
294
- get('/bar') { 'blah' }
295
- end
296
- get '/foo'
297
- assert ok?
298
- assert_equal 'blah', body
299
- end
300
-
301
- it 'generates block arguments from route pattern' do
302
- subpath = nil
303
- mock_app do
304
- after("/foo/:sub") { |s| subpath = s }
305
- get('/foo/*') { }
306
- end
307
- get '/foo/bar'
308
- assert_equal subpath, 'bar'
309
- end
310
-
311
- it 'is possible to access url params from the route param' do
312
- ran = false
313
- mock_app do
314
- get('/foo/*') { }
315
- before('/foo/:sub') do
316
- assert_equal params[:sub], 'bar'
317
- ran = true
318
- end
319
- end
320
- get '/foo/bar'
321
- assert ran
322
- end
323
-
324
- it 'is possible to apply host_name conditions to before filters with no path' do
325
- ran = false
326
- mock_app do
327
- before(:host_name => 'example.com') { ran = true }
328
- get('/') { 'welcome' }
329
- end
330
- get('/', {}, { 'HTTP_HOST' => 'example.org' })
331
- assert !ran
332
- get('/', {}, { 'HTTP_HOST' => 'example.com' })
333
- assert ran
334
- end
335
-
336
- it 'is possible to apply host_name conditions to before filters with a path' do
337
- ran = false
338
- mock_app do
339
- before('/foo', :host_name => 'example.com') { ran = true }
340
- get('/') { 'welcome' }
341
- end
342
- get('/', {}, { 'HTTP_HOST' => 'example.com' })
343
- assert !ran
344
- get('/foo', {}, { 'HTTP_HOST' => 'example.org' })
345
- assert !ran
346
- get('/foo', {}, { 'HTTP_HOST' => 'example.com' })
347
- assert ran
348
- end
349
-
350
- it 'is possible to apply host_name conditions to after filters with no path' do
351
- ran = false
352
- mock_app do
353
- after(:host_name => 'example.com') { ran = true }
354
- get('/') { 'welcome' }
355
- end
356
- get('/', {}, { 'HTTP_HOST' => 'example.org' })
357
- assert !ran
358
- get('/', {}, { 'HTTP_HOST' => 'example.com' })
359
- assert ran
360
- end
361
-
362
- it 'is possible to apply host_name conditions to after filters with a path' do
363
- ran = false
364
- mock_app do
365
- after('/foo', :host_name => 'example.com') { ran = true }
366
- get('/') { 'welcome' }
367
- end
368
- get('/', {}, { 'HTTP_HOST' => 'example.com' })
369
- assert !ran
370
- get('/foo', {}, { 'HTTP_HOST' => 'example.org' })
371
- assert !ran
372
- get('/foo', {}, { 'HTTP_HOST' => 'example.com' })
373
- assert ran
374
- end
375
-
376
- it 'is possible to apply user_agent conditions to before filters with no path' do
377
- ran = false
378
- mock_app do
379
- before(:user_agent => /foo/) { ran = true }
380
- get('/') { 'welcome' }
381
- end
382
- get('/', {}, { 'HTTP_USER_AGENT' => 'bar' })
383
- assert !ran
384
- get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
385
- assert ran
386
- end
387
-
388
- it 'is possible to apply user_agent conditions to before filters with a path' do
389
- ran = false
390
- mock_app do
391
- before('/foo', :user_agent => /foo/) { ran = true }
392
- get('/') { 'welcome' }
393
- end
394
- get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
395
- assert !ran
396
- get('/foo', {}, { 'HTTP_USER_AGENT' => 'bar' })
397
- assert !ran
398
- get('/foo', {}, { 'HTTP_USER_AGENT' => 'foo' })
399
- assert ran
400
- end
401
-
402
- it 'can add params' do
403
- mock_app do
404
- before { params['foo'] = 'bar' }
405
- get('/') { params['foo'] }
406
- end
407
-
408
- get '/'
409
- assert_body 'bar'
410
- end
411
-
412
- it 'can remove params' do
413
- mock_app do
414
- before { params.delete('foo') }
415
- get('/') { params['foo'].to_s }
416
- end
417
-
418
- get '/?foo=bar'
419
- assert_body ''
420
- end
421
-
422
- it 'is possible to apply user_agent conditions to after filters with no path' do
423
- ran = false
424
- mock_app do
425
- after(:user_agent => /foo/) { ran = true }
426
- get('/') { 'welcome' }
427
- end
428
- get('/', {}, { 'HTTP_USER_AGENT' => 'bar' })
429
- assert !ran
430
- get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
431
- assert ran
432
- end
433
-
434
- it 'is possible to apply user_agent conditions to after filters with a path' do
435
- ran = false
436
- mock_app do
437
- after('/foo', :user_agent => /foo/) { ran = true }
438
- get('/') { 'welcome' }
439
- end
440
- get('/', {}, { 'HTTP_USER_AGENT' => 'foo' })
441
- assert !ran
442
- get('/foo', {}, { 'HTTP_USER_AGENT' => 'bar' })
443
- assert !ran
444
- get('/foo', {}, { 'HTTP_USER_AGENT' => 'foo' })
445
- assert ran
446
- end
447
-
448
- it 'only triggers provides condition if conforms with current Content-Type' do
449
- mock_app do
450
- before(:provides => :txt) { @type = 'txt' }
451
- before(:provides => :html) { @type = 'html' }
452
- get('/') { @type }
453
- end
454
-
455
- get('/', {}, { 'HTTP_ACCEPT' => '*/*' })
456
- assert_body 'txt'
457
- end
458
-
459
- it 'can catch exceptions in after filters and handle them properly' do
460
- doodle = ''
461
- mock_app do
462
- after do
463
- doodle += ' and after'
464
- raise StandardError, "after"
465
- end
466
- get "/foo" do
467
- doodle = 'Been now'
468
- raise StandardError, "now"
469
- end
470
- get "/" do
471
- doodle = 'Been now'
472
- end
473
- error 500 do
474
- "Error handled #{env['sinatra.error'].message}"
475
- end
476
- end
477
-
478
- get '/foo'
479
- assert_equal 'Error handled now', body
480
- assert_equal 'Been now and after', doodle
481
-
482
- doodle = ''
483
- get '/'
484
- assert_equal 'Error handled after', body
485
- assert_equal 'Been now and after', doodle
486
- end
487
- end