padrino-core 0.9.29 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,7 +18,7 @@ SimpleDemo.controllers do
18
18
  end
19
19
 
20
20
  get "/rand" do
21
- rand(99).to_s
21
+ rand(2 ** 256).to_s
22
22
  end
23
23
  end
24
24
 
@@ -0,0 +1,13 @@
1
+ class E
2
+ def self.fields
3
+ @fields ||= []
4
+ end
5
+
6
+ def self.inherited(subclass)
7
+ subclass.fields.replace fields.dup
8
+ end
9
+
10
+ G
11
+
12
+ fields << "name"
13
+ end
@@ -0,0 +1,2 @@
1
+ class F < E
2
+ end
@@ -0,0 +1,2 @@
1
+ class G
2
+ end
@@ -9,6 +9,10 @@ require 'rack'
9
9
  require 'shoulda'
10
10
  require 'phocus'
11
11
 
12
+ # Rubies < 1.9 don't handle hashes in the properly order so to prevent
13
+ # this issue for now we remove extra values from mimetypes.
14
+ Rack::Mime::MIME_TYPES.delete(".xsl") # In this way application/xml respond only to .xml
15
+
12
16
  module Kernel
13
17
  # Silences the output by redirecting to stringIO
14
18
  # silence_logger { ...commands... } => "...output..."
@@ -25,8 +29,7 @@ module Kernel
25
29
  yield
26
30
  ensure
27
31
  $VERBOSE = old_verbose
28
- end unless respond_to?(:silence_warnings)
29
-
32
+ end
30
33
  end
31
34
 
32
35
  class Class
@@ -33,7 +33,7 @@ class TestCore < Test::Unit::TestCase
33
33
  assert_equal 'UTF8', $KCODE
34
34
  else
35
35
  assert_equal Encoding.default_external, Encoding::UTF_8
36
- assert_equal Encoding.default_internal, Encoding::UTF_8
36
+ assert_equal Encoding.default_internal, nil # Encoding::UTF_8
37
37
  end
38
38
  end
39
39
 
@@ -28,5 +28,17 @@ class TestDependencies < Test::Unit::TestCase
28
28
  assert_equal ["B", "C"], A_result
29
29
  assert_equal "C", B_result
30
30
  end
31
+
32
+ should 'remove partially loaded constants' do
33
+ silence_warnings do
34
+ Padrino.require_dependencies(
35
+ Padrino.root("fixtures/dependencies/circular/e.rb"),
36
+ Padrino.root("fixtures/dependencies/circular/f.rb"),
37
+ Padrino.root("fixtures/dependencies/circular/g.rb")
38
+ )
39
+ end
40
+
41
+ assert_equal ["name"], F.fields
42
+ end
31
43
  end
32
- end
44
+ end
@@ -0,0 +1,266 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ class TestFilters < Test::Unit::TestCase
4
+ should "filters by accept header" do
5
+ mock_app do
6
+ get '/foo', :provides => [:xml, :js] do
7
+ request.env['HTTP_ACCEPT']
8
+ end
9
+ end
10
+
11
+ get '/foo', {}, { 'HTTP_ACCEPT' => 'application/xml' }
12
+ assert ok?
13
+ assert_equal 'application/xml', body
14
+ assert_equal 'application/xml;charset=utf-8', response.headers['Content-Type']
15
+
16
+ get '/foo.xml'
17
+ assert ok?
18
+ assert_equal 'application/xml;charset=utf-8', response.headers['Content-Type']
19
+
20
+ get '/foo', {}, { 'HTTP_ACCEPT' => 'application/javascript' }
21
+ assert ok?
22
+ assert_equal 'application/javascript', body
23
+ assert_equal 'application/javascript;charset=utf-8', response.headers['Content-Type']
24
+
25
+ get '/foo.js'
26
+ assert ok?
27
+ assert_equal 'application/javascript;charset=utf-8', response.headers['Content-Type']
28
+
29
+ get '/foo', {}, { "HTTP_ACCEPT" => 'text/html' }
30
+ assert_equal 406, status
31
+ end
32
+
33
+ should "allow passing & halting in before filters" do
34
+ mock_app do
35
+ controller do
36
+ before { env['QUERY_STRING'] == 'secret' or pass }
37
+ get :index do
38
+ "secret index"
39
+ end
40
+ end
41
+
42
+ controller do
43
+ before { env['QUERY_STRING'] == 'halt' and halt 401, 'go away!' }
44
+ get :index do
45
+ "index"
46
+ end
47
+ end
48
+ end
49
+
50
+ get "/?secret"
51
+ assert_equal "secret index", body
52
+
53
+ get "/?halt"
54
+ assert_equal "go away!", body
55
+ assert_equal 401, status
56
+
57
+ get "/"
58
+ assert_equal "index", body
59
+ end
60
+
61
+ should 'scope filters in the given controller' do
62
+ mock_app do
63
+ before { @global = 'global' }
64
+ after { @global = nil }
65
+
66
+ controller :foo do
67
+ before { @foo = :foo }
68
+ after { @foo = nil }
69
+ get("/") { [@foo, @bar, @global].compact.join(" ") }
70
+ end
71
+
72
+ get("/") { [@foo, @bar, @global].compact.join(" ") }
73
+
74
+ controller :bar do
75
+ before { @bar = :bar }
76
+ after { @bar = nil }
77
+ get("/") { [@foo, @bar, @global].compact.join(" ") }
78
+ end
79
+ end
80
+
81
+ get "/bar"
82
+ assert_equal "bar global", body
83
+
84
+ get "/foo"
85
+ assert_equal "foo global", body
86
+
87
+ get "/"
88
+ assert_equal "global", body
89
+ end
90
+
91
+ should 'be able to access params in a before filter' do
92
+ username_from_before_filter = nil
93
+
94
+ mock_app do
95
+ before do
96
+ username_from_before_filter = params[:username]
97
+ end
98
+
99
+ get :users, :with => :username do
100
+ end
101
+ end
102
+ get '/users/josh'
103
+ assert_equal 'josh', username_from_before_filter
104
+ end
105
+
106
+ should "be able to access params normally when a before filter is specified" do
107
+ mock_app do
108
+ before { }
109
+ get :index do
110
+ params.inspect
111
+ end
112
+ end
113
+ get '/?test=what'
114
+ assert_equal '{"test"=>"what"}', body
115
+ end
116
+
117
+ should "be able to filter based on a path" do
118
+ mock_app do
119
+ before('/') { @test = "#{@test}before"}
120
+ get :index do
121
+ @test
122
+ end
123
+ get :main do
124
+ @test
125
+ end
126
+ end
127
+ get '/'
128
+ assert_equal 'before', body
129
+ get '/main'
130
+ assert_equal '', body
131
+ end
132
+
133
+ should "be able to filter based on a symbol" do
134
+ mock_app do
135
+ before(:index) { @test = 'before'}
136
+ get :index do
137
+ @test
138
+ end
139
+ get :main do
140
+ @test
141
+ end
142
+ end
143
+ get '/'
144
+ assert_equal 'before', body
145
+ get '/main'
146
+ assert_equal '', body
147
+ end
148
+
149
+ should "be able to filter based on a symbol for a controller" do
150
+ mock_app do
151
+ controller :foo do
152
+ before(:test) { @test = 'foo'}
153
+ get :test do
154
+ @test.to_s + " response"
155
+ end
156
+ end
157
+ controller :bar do
158
+ before(:test) { @test = 'bar'}
159
+ get :test do
160
+ @test.to_s + " response"
161
+ end
162
+ end
163
+ end
164
+ get '/foo/test'
165
+ assert_equal 'foo response', body
166
+ get '/bar/test'
167
+ assert_equal 'bar response', body
168
+ end
169
+
170
+ should "be able to filter based on a symbol or path" do
171
+ mock_app do
172
+ before(:index, '/main') { @test = 'before'}
173
+ get :index do
174
+ @test
175
+ end
176
+ get :main do
177
+ @test
178
+ end
179
+ end
180
+ get '/'
181
+ assert_equal 'before', body
182
+ get '/main'
183
+ assert_equal 'before', body
184
+ end
185
+
186
+ should "be able to filter based on a symbol or regexp" do
187
+ mock_app do
188
+ before(:index, /main/) { @test = 'before'}
189
+ get :index do
190
+ @test
191
+ end
192
+ get :main do
193
+ @test
194
+ end
195
+ get :profile do
196
+ @test
197
+ end
198
+ end
199
+ get '/'
200
+ assert_equal 'before', body
201
+ get '/main'
202
+ assert_equal 'before', body
203
+ get '/profile'
204
+ assert_equal '', body
205
+ end
206
+
207
+ should "be able to filter excluding based on a symbol" do
208
+ mock_app do
209
+ before(:except => :index) { @test = 'before'}
210
+ get :index do
211
+ @test
212
+ end
213
+ get :main do
214
+ @test
215
+ end
216
+ end
217
+ get '/'
218
+ assert_equal '', body
219
+ get '/main'
220
+ assert_equal 'before', body
221
+ end
222
+
223
+ should "be able to filter based on a request param" do
224
+ mock_app do
225
+ before(:agent => /IE/) { @test = 'before'}
226
+ get :index do
227
+ @test
228
+ end
229
+ end
230
+ get '/'
231
+ assert_equal '', body
232
+ get "/", {}, {'HTTP_USER_AGENT' => 'This is IE'}
233
+ assert_equal 'before', body
234
+ end
235
+
236
+ should "be able to filter based on a symbol or path in multiple controller" do
237
+ mock_app do
238
+ controllers :foo do
239
+ before(:index, '/foo/main') { @test = 'before' }
240
+ get :index do
241
+ @test
242
+ end
243
+ get :main do
244
+ @test
245
+ end
246
+ end
247
+ controllers :bar do
248
+ before(:index, '/bar/main') { @test = 'also before' }
249
+ get :index do
250
+ @test
251
+ end
252
+ get :main do
253
+ @test
254
+ end
255
+ end
256
+ end
257
+ get '/foo'
258
+ assert_equal 'before', body
259
+ get '/bar'
260
+ assert_equal 'also before', body
261
+ get '/foo/main'
262
+ assert_equal 'before', body
263
+ get '/bar/main'
264
+ assert_equal 'also before', body
265
+ end
266
+ end
@@ -2,6 +2,10 @@ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
  require 'i18n'
3
3
 
4
4
  class TestRendering < Test::Unit::TestCase
5
+ def setup
6
+ Padrino::Application.send(:register, Padrino::Rendering)
7
+ end
8
+
5
9
  def teardown
6
10
  remove_views
7
11
  end
@@ -368,7 +372,7 @@ class TestRendering < Test::Unit::TestCase
368
372
  assert_equal "Im Italian Js", body
369
373
  I18n.locale = :en
370
374
  get "/foo.pk"
371
- assert_equal 404, status
375
+ assert_equal 405, status
372
376
  end
373
377
 
374
378
  should 'resolve template content_type and locale with layout' do
@@ -410,7 +414,7 @@ class TestRendering < Test::Unit::TestCase
410
414
  get "/bar.json"
411
415
  assert_equal "Im a json", body
412
416
  get "/bar.pk"
413
- assert_equal 404, status
417
+ assert_equal 405, status
414
418
  end
415
419
 
416
420
  should 'renders erb with blocks' do
@@ -101,9 +101,9 @@ class TestRouting < Test::Unit::TestCase
101
101
  post("/main"){ "hello" }
102
102
  end
103
103
  assert_equal 3, app.routes.size, "should generate GET, HEAD and PUT"
104
- assert_equal ["GET"], app.routes[0].as_options[:conditions][:request_method]
105
- assert_equal ["HEAD"], app.routes[1].as_options[:conditions][:request_method]
106
- assert_equal ["POST"], app.routes[2].as_options[:conditions][:request_method]
104
+ assert_equal ["GET"], app.routes[0].conditions[:request_method]
105
+ assert_equal ["HEAD"], app.routes[1].conditions[:request_method]
106
+ assert_equal ["POST"], app.routes[2].conditions[:request_method]
107
107
  end
108
108
 
109
109
  should 'generate basic urls' do
@@ -155,13 +155,13 @@ class TestRouting < Test::Unit::TestCase
155
155
  get "/b.js"
156
156
  assert_equal "/b.js", body
157
157
  get "/b.ru"
158
- assert_equal 404, status
158
+ assert_equal 405, status
159
159
  get "/c.js"
160
160
  assert_equal "/c.json", body
161
161
  get "/c.json"
162
162
  assert_equal "/c.json", body
163
163
  get "/c.ru"
164
- assert_equal 404, status
164
+ assert_equal 405, status
165
165
  get "/d"
166
166
  assert_equal "/d.js?foo=bar", body
167
167
  get "/d.js"
@@ -218,7 +218,7 @@ class TestRouting < Test::Unit::TestCase
218
218
  end
219
219
 
220
220
  get "/a.xml", {}, {}
221
- assert_equal 404, status
221
+ assert_equal 405, status
222
222
  end
223
223
 
224
224
  should "not set content_type to :html if Accept */* and html not in provides" do
@@ -288,7 +288,7 @@ class TestRouting < Test::Unit::TestCase
288
288
  end
289
289
 
290
290
  get "/a.xml", {}, {"HTTP_ACCEPT" => "text/html"}
291
- assert_equal 404, status
291
+ assert_equal 405, status
292
292
  end
293
293
 
294
294
  should "generate routes for format simple" do
@@ -634,12 +634,36 @@ class TestRouting < Test::Unit::TestCase
634
634
  should 'reset routes' do
635
635
  mock_app do
636
636
  get("/"){ "foo" }
637
- router.reset!
637
+ reset_router!
638
638
  end
639
639
  get "/"
640
640
  assert_equal 404, status
641
641
  end
642
642
 
643
+ should 'respect priorities' do
644
+ route_order = []
645
+ mock_app do
646
+ get(:index, :priority => :normal) { route_order << :normal; pass }
647
+ get(:index, :priority => :low) { route_order << :low; "hello" }
648
+ get(:index, :priority => :high) { route_order << :high; pass }
649
+ end
650
+ get '/'
651
+ assert_equal [:high, :normal, :low], route_order
652
+ assert_equal "hello", body
653
+ end
654
+
655
+ should 'allow optionals' do
656
+ mock_app do
657
+ get(:show, :map => "/stories/:type(/:category)") do
658
+ "#{params[:type]}/#{params[:category]}"
659
+ end
660
+ end
661
+ get "/stories/foo"
662
+ assert_equal "foo/", body
663
+ get "/stories/foo/bar"
664
+ assert_equal "foo/bar", body
665
+ end
666
+
643
667
  should 'apply maps' do
644
668
  mock_app do
645
669
  controllers :admin do
@@ -1151,11 +1175,11 @@ class TestRouting < Test::Unit::TestCase
1151
1175
  get "/.json"
1152
1176
  assert_equal "This is the get index.json", body
1153
1177
  get "/.js"
1154
- assert_equal 404, status
1178
+ assert_equal 405, status
1155
1179
  post "/.json"
1156
1180
  assert_equal "This is the post index.json", body
1157
1181
  post "/.js"
1158
- assert_equal 404, status
1182
+ assert_equal 405, status
1159
1183
  end
1160
1184
 
1161
1185
  should "allow controller level mapping" do
@@ -1276,6 +1300,46 @@ class TestRouting < Test::Unit::TestCase
1276
1300
  assert_equal "foo", body
1277
1301
  end
1278
1302
 
1303
+ should "pass controller conditions to each route" do
1304
+ counter = 0
1305
+
1306
+ mock_app do
1307
+ self.class.send(:define_method, :increment!) do |*args|
1308
+ condition { counter += 1 }
1309
+ end
1310
+
1311
+ controller :posts, :conditions => {:increment! => true} do
1312
+ get("/foo") { "foo" }
1313
+ get("/bar") { "bar" }
1314
+ end
1315
+
1316
+ end
1317
+
1318
+ get "/posts/foo"
1319
+ get "/posts/bar"
1320
+ assert_equal 2, counter
1321
+ end
1322
+
1323
+ should "allow controller conditions to be overridden" do
1324
+ counter = 0
1325
+
1326
+ mock_app do
1327
+ self.class.send(:define_method, :increment!) do |increment|
1328
+ condition { counter += 1 } if increment
1329
+ end
1330
+
1331
+ controller :posts, :conditions => {:increment! => true} do
1332
+ get("/foo") { "foo" }
1333
+ get("/bar", :increment! => false) { "bar" }
1334
+ end
1335
+
1336
+ end
1337
+
1338
+ get "/posts/foo"
1339
+ get "/posts/bar"
1340
+ assert_equal 1, counter
1341
+ end
1342
+
1279
1343
  should "parse params with class level provides" do
1280
1344
  mock_app do
1281
1345
  controllers :posts, :provides => [:html, :js] do
@@ -1352,12 +1416,12 @@ class TestRouting < Test::Unit::TestCase
1352
1416
 
1353
1417
  should 'parse nested params' do
1354
1418
  mock_app do
1355
- get(:index) { params.inspect }
1419
+ get(:index) { "%s %s" % [params[:account][:name], params[:account][:surname]] }
1356
1420
  end
1357
1421
  get "/?account[name]=foo&account[surname]=bar"
1358
- assert_equal '{"account"=>{"name"=>"foo", "surname"=>"bar"}}', body
1422
+ assert_equal 'foo bar', body
1359
1423
  get @app.url(:index, "account[name]" => "foo", "account[surname]" => "bar")
1360
- assert_equal '{"account"=>{"name"=>"foo", "surname"=>"bar"}}', body
1424
+ assert_equal 'foo bar', body
1361
1425
  end
1362
1426
 
1363
1427
  should 'render sinatra NotFound page' do
@@ -1424,4 +1488,4 @@ class TestRouting < Test::Unit::TestCase
1424
1488
  get @app.url(:index, :page => 10)
1425
1489
  assert_equal "/paginate/66", body
1426
1490
  end
1427
- end
1491
+ end