padrino-core 0.9.10 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,34 +1,51 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
 
3
+ class PadrinoTestApp < Padrino::Application; end
4
+
3
5
  class TestApplication < Test::Unit::TestCase
4
6
  def teardown
5
7
  remove_views
6
8
  end
7
9
 
8
- class PadrinoTestApp < Padrino::Application; end
9
-
10
10
  context 'for application functionality' do
11
11
 
12
12
  should 'check default options' do
13
- assert_match __FILE__, PadrinoTestApp.app_file
13
+ assert File.identical?(__FILE__, PadrinoTestApp.app_file)
14
+ assert_equal :padrino_test_app, PadrinoTestApp.app_name
14
15
  assert_equal :test, PadrinoTestApp.environment
15
16
  assert_equal Padrino.root("views"), PadrinoTestApp.views
16
17
  assert PadrinoTestApp.raise_errors
17
18
  assert !PadrinoTestApp.logging
18
19
  assert !PadrinoTestApp.sessions
19
- assert 'PadrinoTestApp', PadrinoTestApp.app_name
20
20
  end
21
21
 
22
22
  should 'check padrino specific options' do
23
23
  assert !PadrinoTestApp.instance_variable_get(:@_configured)
24
24
  PadrinoTestApp.send(:setup_application!)
25
+ assert_equal :padrino_test_app, PadrinoTestApp.app_name
26
+ assert_equal 'StandardFormBuilder', PadrinoTestApp.default_builder
25
27
  assert PadrinoTestApp.instance_variable_get(:@_configured)
26
28
  assert !PadrinoTestApp.reload?
27
- assert 'padrino_test_app', PadrinoTestApp.app_name
28
- assert 'StandardFormBuilder', PadrinoTestApp.default_builder
29
29
  assert !PadrinoTestApp.flash
30
- assert !PadrinoTestApp.padrino_mailer
31
- assert !PadrinoTestApp.padrino_helpers
32
30
  end
31
+
32
+ #compare to: test_routing: allow global provides
33
+ should "set content_type to :html if none can be determined" do
34
+ mock_app do
35
+ provides :xml
36
+
37
+ get("/foo"){ "Foo in #{content_type}" }
38
+ get("/bar"){ "Foo in #{content_type}" }
39
+ end
40
+
41
+ get '/foo', {}, { 'HTTP_ACCEPT' => 'application/xml' }
42
+ assert_equal 'Foo in xml', body
43
+ get '/foo'
44
+ assert not_found?
45
+
46
+ get '/bar', {}, { 'HTTP_ACCEPT' => 'application/xml' }
47
+ assert_equal "Foo in html", body
48
+ end
49
+
33
50
  end
34
51
  end
data/test/test_mounter.rb CHANGED
@@ -23,11 +23,11 @@ class TestMounter < Test::Unit::TestCase
23
23
  end
24
24
 
25
25
  should 'check locate_app_file with __FILE__' do
26
- mounter = Padrino::Mounter.new("test")
26
+ mounter = Padrino::Mounter.new("test", :app_file => __FILE__)
27
27
  mounter.to("/test")
28
28
  assert_equal "test", mounter.name
29
29
  assert_equal "Test", mounter.app_class
30
- assert_match %r{test/app.rb}, mounter.app_file
30
+ assert_equal __FILE__, mounter.app_file
31
31
  assert_equal "/test", mounter.uri_root
32
32
  assert_equal File.dirname(mounter.app_file), mounter.app_root
33
33
  end
@@ -40,15 +40,25 @@ class TestMounter < Test::Unit::TestCase
40
40
  end
41
41
 
42
42
  should 'mount a core' do
43
- mounter = Padrino.mount_core("test")
43
+ mounter = Padrino.mount_core("test", :app_file => __FILE__)
44
44
  assert_equal "core", mounter.name
45
45
  assert_equal "Test", mounter.app_class
46
46
  assert_equal Test, mounter.app_obj
47
- assert_equal Padrino.root('app/app.rb'), mounter.app_file
47
+ assert_equal __FILE__, mounter.app_file
48
48
  assert_equal "/", mounter.uri_root
49
49
  assert_equal File.dirname(mounter.app_file), mounter.app_root
50
50
  end
51
51
 
52
+ should 'mount a core to url' do
53
+ mounter = Padrino.mount_core("test", :app_file => __FILE__).to('/me')
54
+ assert_equal "core", mounter.name
55
+ assert_equal "Test", mounter.app_class
56
+ assert_equal Test, mounter.app_obj
57
+ assert_equal __FILE__, mounter.app_file
58
+ assert_equal "/me", mounter.uri_root
59
+ assert_equal File.dirname(mounter.app_file), mounter.app_root
60
+ end
61
+
52
62
  should 'mount multiple apps' do
53
63
  class ::OneApp < Padrino::Application; end
54
64
  class ::TwoApp < Padrino::Application; end
@@ -55,7 +55,7 @@ class TestSimpleReloader < Test::Unit::TestCase
55
55
  should 'correctly reload SimpleDemo fixture' do
56
56
  @app = SimpleDemo
57
57
  get "/"
58
- assert_equal 200, status
58
+ assert ok?
59
59
  new_phrase = "The magick number is: #{rand(100)}!"
60
60
  buffer = File.read(SimpleDemo.app_file)
61
61
  new_buffer = buffer.gsub(/The magick number is: \d+!/, new_phrase)
@@ -66,6 +66,31 @@ class TestSimpleReloader < Test::Unit::TestCase
66
66
 
67
67
  # Now we need to prevent to commit a new changed file so we revert it
68
68
  File.open(SimpleDemo.app_file, "w") { |f| f.write(buffer) }
69
+ Padrino.reload!
70
+ end
71
+
72
+ should 'correctly reset SimpleDemo fixture' do
73
+ @app = SimpleDemo
74
+ get "/rand"
75
+ assert ok?
76
+ last_body = body
77
+ assert_equal 2, @app.before_filters.size # one is ours the other is default_filter for content type
78
+ assert_equal 1, @app.errors.size
79
+ assert_equal 1, @app.after_filters.size
80
+ assert_equal 2, @app.middleware.size # [Padrino::Logger::Rack, Padrino::Reloader::Rack]
81
+ assert_equal 4, @app.routes.size # GET+HEAD of "/" + GET+HEAD of "/rand" = 4
82
+ assert_equal 2, @app.extensions.size # [Padrino::Routing, Padrino::Rendering]
83
+ assert_equal 0, @app.templates.size
84
+ @app.reload!
85
+ get "/rand"
86
+ assert_not_equal last_body, body
87
+ assert_equal 2, @app.before_filters.size # one is ours the other is default_filter for content type
88
+ assert_equal 1, @app.errors.size
89
+ assert_equal 1, @app.after_filters.size
90
+ assert_equal 2, @app.middleware.size # only logger Padrino::Logger::Rack
91
+ assert_equal 4, @app.routes.size # GET+HEAD of "/" = 2
92
+ assert_equal 2, @app.extensions.size # [Padrino::Routing, Padrino::Rendering]
93
+ assert_equal 0, @app.templates.size
69
94
  end
70
95
  end
71
96
  end
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+ require 'i18n'
2
3
 
3
4
  class TestRendering < Test::Unit::TestCase
4
5
  def teardown
@@ -72,7 +73,7 @@ class TestRendering < Test::Unit::TestCase
72
73
  create_view :foo, "erb file"
73
74
  create_view :foo, "js file", :format => :js
74
75
  mock_app do
75
- get('/layout_test', :respond_to => [:html, :js]){ render :foo }
76
+ get('/layout_test', :provides => [:html, :js]){ render :foo }
76
77
  end
77
78
  get "/layout_test"
78
79
  assert_equal "this is an erb file", body
@@ -86,7 +87,7 @@ class TestRendering < Test::Unit::TestCase
86
87
  create_view :foo, "erb file"
87
88
  create_view :foo, "xml file", :format => :xml
88
89
  mock_app do
89
- get('/layout_test', :respond_to => [:html, :xml]){ render :foo }
90
+ get('/layout_test', :provides => [:html, :xml]){ render :foo }
90
91
  end
91
92
  get "/layout_test"
92
93
  assert_equal "this is an erb file", body
@@ -94,6 +95,38 @@ class TestRendering < Test::Unit::TestCase
94
95
  assert_equal "document start xml file end", body
95
96
  end
96
97
 
98
+ should 'by default use html file when no other is given' do
99
+ create_layout :foo, "html file", :format => :html
100
+
101
+ mock_app do
102
+ get('/content_type_test', :provides => [:html, :xml]) { render :foo }
103
+ end
104
+
105
+ get "/content_type_test"
106
+ assert_equal "html file", body
107
+ get "/content_type_test.xml"
108
+ assert_equal "html file", body
109
+ end
110
+
111
+ should 'not use html file when DEFAULT_RENDERING_OPTIONS[:strict_format] == true' do
112
+ create_layout :foo, "html file", :format => :html
113
+
114
+ mock_app do
115
+ get('/default_rendering_test', :provides => [:html, :xml]) { render :foo }
116
+ end
117
+
118
+ @save = Padrino::Rendering::DEFAULT_RENDERING_OPTIONS
119
+ Padrino::Rendering::DEFAULT_RENDERING_OPTIONS[:strict_format] = true
120
+
121
+ get "/default_rendering_test"
122
+ assert_equal "html file", body
123
+ assert_raise Padrino::Rendering::TemplateNotFound do
124
+ get "/default_rendering_test.xml"
125
+ end
126
+
127
+ Padrino::Rendering::DEFAULT_RENDERING_OPTIONS.merge(@save)
128
+ end
129
+
97
130
  should 'use correct layout with each controller' do
98
131
  create_layout :foo, "foo layout at <%= yield %>"
99
132
  create_layout :bar, "bar layout at <%= yield %>"
@@ -175,7 +208,7 @@ class TestRendering < Test::Unit::TestCase
175
208
  create_view :foo, "Im Js", :format => :js
176
209
  create_view :foo, "Im Erb"
177
210
  mock_app do
178
- get("/foo", :respond_to => :js) { render :foo }
211
+ get("/foo", :provides => :js) { render :foo }
179
212
  get("/bar.js") { render :foo }
180
213
  end
181
214
  get "/foo.js"
@@ -190,9 +223,9 @@ class TestRendering < Test::Unit::TestCase
190
223
  create_view :foo, "Im Haml", :format => :haml
191
224
  create_view :foo, "Im Xml", :format => :xml
192
225
  mock_app do
193
- get("/foo_normal", :respond_to => :js) { render 'foo' }
194
- get("/foo_haml", :respond_to => :js) { render 'foo.haml' }
195
- get("/foo_xml", :respond_to => :js) { render 'foo.xml' }
226
+ get("/foo_normal", :provides => :js) { render 'foo' }
227
+ get("/foo_haml", :provides => :js) { render 'foo.haml' }
228
+ get("/foo_xml", :provides => :js) { render 'foo.xml' }
196
229
  end
197
230
  get "/foo_normal.js"
198
231
  assert_equal "Im Js", body
@@ -202,6 +235,18 @@ class TestRendering < Test::Unit::TestCase
202
235
  assert_equal "Im Xml", body
203
236
  end
204
237
 
238
+ should 'resolve without explict template format' do
239
+ create_view :foo, "Im Html"
240
+ create_view :foo, "xml.rss", :format => :rss
241
+ mock_app do
242
+ get(:index, :map => "/", :provides => [:html, :rss]){ render 'foo' }
243
+ end
244
+ get "/", {}, { 'HTTP_ACCEPT' => 'text/html;q=0.9' }
245
+ assert_equal "Im Html", body
246
+ get ".rss"
247
+ assert_equal "<rss/>\n", body
248
+ end
249
+
205
250
  should "ignore files ending in tilde and not render them" do
206
251
  create_view :foo, "Im Wrong", :format => 'haml~'
207
252
  create_view :foo, "Im Haml", :format => :haml
@@ -237,7 +282,7 @@ class TestRendering < Test::Unit::TestCase
237
282
  create_view :foo, "Im English Js", :format => :js, :locale => :en
238
283
  create_view :foo, "Im Italian Js", :format => :js, :locale => :it
239
284
  mock_app do
240
- get("/foo", :respond_to => [:html, :js]) { render :foo }
285
+ get("/foo", :provides => [:html, :js]) { render :foo }
241
286
  end
242
287
  I18n.locale = :none
243
288
  get "/foo.js"
@@ -276,7 +321,7 @@ class TestRendering < Test::Unit::TestCase
276
321
  create_view :bar, "Im a json", :format => :json
277
322
  mock_app do
278
323
  layout :foo
279
- get("/bar", :respond_to => [:html, :js, :json]) { render :bar }
324
+ get("/bar", :provides => [:html, :js, :json]) { render :bar }
280
325
  end
281
326
  I18n.locale = :none
282
327
  get "/bar.js"
data/test/test_router.rb CHANGED
@@ -26,7 +26,7 @@ class TestRouter < Test::Unit::TestCase
26
26
  res = Rack::MockRequest.new(map).get("/foo")
27
27
  assert res.ok?
28
28
  assert_equal "/foo", res["X-ScriptName"]
29
- assert_equal "", res["X-PathInfo"]
29
+ assert_equal "/", res["X-PathInfo"]
30
30
 
31
31
  res = Rack::MockRequest.new(map).get("/foo/")
32
32
  assert res.ok?
@@ -36,7 +36,7 @@ class TestRouter < Test::Unit::TestCase
36
36
  res = Rack::MockRequest.new(map).get("/foo/bar")
37
37
  assert res.ok?
38
38
  assert_equal "/foo/bar", res["X-ScriptName"]
39
- assert_equal "", res["X-PathInfo"]
39
+ assert_equal "/", res["X-PathInfo"]
40
40
 
41
41
  res = Rack::MockRequest.new(map).get("/foo/bar/")
42
42
  assert res.ok?
@@ -57,7 +57,7 @@ class TestRouter < Test::Unit::TestCase
57
57
  res = Rack::MockRequest.new(map).get("/bar", 'HTTP_HOST' => 'foo.org')
58
58
  assert res.ok?
59
59
  assert_equal "/bar", res["X-ScriptName"]
60
- assert_equal "", res["X-PathInfo"]
60
+ assert_equal "/", res["X-PathInfo"]
61
61
 
62
62
  res = Rack::MockRequest.new(map).get("/bar/", 'HTTP_HOST' => 'foo.org')
63
63
  assert res.ok?
@@ -136,7 +136,7 @@ class TestRouter < Test::Unit::TestCase
136
136
  assert res.not_found?
137
137
 
138
138
  res = Rack::MockRequest.new(Padrino.application).get("/foo", "HTTP_HOST" => "bar.padrino.org")
139
- assert res.not_found?
139
+ assert res.ok?
140
140
 
141
141
  res = Rack::MockRequest.new(Padrino.application).get("/foo/", "HTTP_HOST" => "bar.padrino.org")
142
142
  assert res.ok?
data/test/test_routing.rb CHANGED
@@ -1,15 +1,19 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
 
3
3
  class TestRouting < Test::Unit::TestCase
4
-
5
- should 'ignore trailing delimiters' do
4
+ should 'ignore trailing delimiters for basic route' do
6
5
  mock_app do
7
6
  get("/foo"){ "okey" }
7
+ get(:test) { "tester" }
8
8
  end
9
9
  get "/foo"
10
10
  assert_equal "okey", body
11
11
  get "/foo/"
12
12
  assert_equal "okey", body
13
+ get "/test"
14
+ assert_equal "tester", body
15
+ get "/test/"
16
+ assert_equal "tester", body
13
17
  end
14
18
 
15
19
  should 'fail with unrecognized route exception when not found' do
@@ -49,18 +53,41 @@ class TestRouting < Test::Unit::TestCase
49
53
  assert_equal "2", body
50
54
  end
51
55
 
56
+ should "not generate overlapping head urls" do
57
+ app = mock_app do
58
+ get("/main"){ "hello" }
59
+ post("/main"){ "hello" }
60
+ end
61
+ assert_equal 3, app.routes.size, "should generate GET, HEAD and PUT"
62
+ assert_equal ["GET"], app.routes[0].as_options[:conditions][:request_method]
63
+ assert_equal ["HEAD"], app.routes[1].as_options[:conditions][:request_method]
64
+ assert_equal ["POST"], app.routes[2].as_options[:conditions][:request_method]
65
+ end
66
+
52
67
  should 'generate basic urls'do
53
68
  mock_app do
54
- get(:foo){ url(:foo) }
55
- get(:bar, :with => :id){ url(:bar, :id => 1) }
69
+ get(:foo){ "/foo" }
70
+ get(:foo, :with => :id){ |id| "/foo/#{id}" }
71
+ get(:hash, :with => :id){ url(:hash, :id => 1) }
72
+ get(:array, :with => :id){ url(:array, 23) }
73
+ get(:hash_with_extra, :with => :id){ url(:hash_with_extra, :id => 1, :query => 'string') }
74
+ get(:array_with_extra, :with => :id){ url(:array_with_extra, 23, :query => 'string') }
56
75
  get("/old-bar/:id"){ params[:id] }
57
76
  post(:mix, :map => "/mix-bar/:id"){ params[:id] }
58
77
  get(:mix, :map => "/mix-bar/:id"){ params[:id] }
59
78
  end
60
79
  get "/foo"
61
80
  assert_equal "/foo", body
62
- get "/bar/2"
63
- assert_equal "/bar/1", body
81
+ get "/foo/123"
82
+ assert_equal "/foo/123", body
83
+ get "/hash/2"
84
+ assert_equal "/hash/1", body
85
+ get "/array/23"
86
+ assert_equal "/array/23", body
87
+ get "/hash_with_extra/1"
88
+ assert_equal "/hash_with_extra/1?query=string", body
89
+ get "/array_with_extra/23"
90
+ assert_equal "/array_with_extra/23?query=string", body
64
91
  get "/old-bar/3"
65
92
  assert_equal "3", body
66
93
  post "/mix-bar/4"
@@ -88,12 +115,39 @@ class TestRouting < Test::Unit::TestCase
88
115
  assert_equal "/c.json", body
89
116
  get "/c.ru"
90
117
  assert_equal 404, status
91
- get "/d.json"
92
- assert 404, status
93
118
  get "/d"
94
119
  assert_equal "/d.js?foo=bar", body
95
120
  get "/d.js"
96
121
  assert_equal "/d.js?foo=bar", body
122
+ get "/e.xml"
123
+ assert_equal 404, status
124
+ end
125
+
126
+ should "not allow Accept-Headers it does not provide" do
127
+ mock_app do
128
+ get(:a, :provides => [:html, :js]){ content_type }
129
+ end
130
+
131
+ get "/a", {}, {"HTTP_ACCEPT" => "application/yaml"}
132
+ assert_equal 404, status
133
+ end
134
+
135
+ should "not default to HTML if HTML is not provided and no type is given" do
136
+ mock_app do
137
+ get(:a, :provides => [:js]){ content_type }
138
+ end
139
+
140
+ get "/a", {}, {}
141
+ assert_equal 404, status
142
+ end
143
+
144
+ should "not match routes if url_format and http_accept is provided but not included" do
145
+ mock_app do
146
+ get(:a, :provides => [:js, :html]){ content_type }
147
+ end
148
+
149
+ get "/a.xml", {}, {"HTTP_ACCEPT" => "text/html"}
150
+ assert_equal 404, status
97
151
  end
98
152
 
99
153
  should "generate routes for format simple" do
@@ -106,6 +160,31 @@ class TestRouting < Test::Unit::TestCase
106
160
  assert_equal "Test\n", body
107
161
  end
108
162
 
163
+ should "should inject the controller name into the request" do
164
+ mock_app do
165
+ controller :posts do
166
+ get(:index) { request.controller }
167
+ controller :mini do
168
+ get(:index) { request.controller }
169
+ end
170
+ end
171
+ end
172
+ get "/posts"
173
+ assert_equal "posts", body
174
+ get "/mini"
175
+ assert_equal "mini", body
176
+ end
177
+
178
+ should "should inject the route into the request" do
179
+ mock_app do
180
+ controller :posts do
181
+ get(:index) { request.route.named.to_s }
182
+ end
183
+ end
184
+ get "/posts"
185
+ assert_equal "posts_index", body
186
+ end
187
+
109
188
  should "generate routes for format with controller" do
110
189
  mock_app do
111
190
  controller :posts do
@@ -168,6 +247,14 @@ class TestRouting < Test::Unit::TestCase
168
247
  assert_equal "/print/9", @app.url(:print, :id => 9)
169
248
  end
170
249
 
250
+ should '405 on wrong request_method' do
251
+ mock_app do
252
+ post('/bar'){ "bar" }
253
+ end
254
+ get "/bar"
255
+ assert_equal 405, status
256
+ end
257
+
171
258
  should 'respond to' do
172
259
  mock_app do
173
260
  get(:a, :provides => :js){ "js" }
@@ -253,6 +340,52 @@ class TestRouting < Test::Unit::TestCase
253
340
  assert_equal "foo_bar_index", body
254
341
  end
255
342
 
343
+ should "ignore trailing delimiters within a named controller" do
344
+ mock_app do
345
+ controller :posts do
346
+ get(:index, :provides => [:html, :js]){ "index" }
347
+ get(:new) { "new" }
348
+ get(:show, :with => :id){ "show #{params[:id]}" }
349
+ end
350
+ end
351
+ get "/posts"
352
+ assert_equal "index", body
353
+ get "/posts/"
354
+ assert_equal "index", body
355
+ get "/posts.js"
356
+ assert_equal "index", body
357
+ get "/posts.js/"
358
+ assert_equal "index", body
359
+ get "/posts/new"
360
+ assert_equal "new", body
361
+ get "/posts/new/"
362
+ assert_equal "new", body
363
+ end
364
+
365
+ should "ignore trailing delimiters within a named controller for unnamed actions" do
366
+ mock_app do
367
+ controller :accounts do
368
+ get("/") { "account_index" }
369
+ get("/new") { "new" }
370
+ end
371
+ controller :votes do
372
+ get("/") { "vote_index" }
373
+ end
374
+ end
375
+ get "/accounts"
376
+ assert_equal "account_index", body
377
+ get "/accounts/"
378
+ assert_equal "account_index", body
379
+ get "/accounts/new"
380
+ assert_equal "new", body
381
+ get "/accounts/new/"
382
+ assert_equal "new", body
383
+ get "/votes"
384
+ assert_equal "vote_index", body
385
+ get "/votes/"
386
+ assert_equal "vote_index", body
387
+ end
388
+
256
389
  should 'use named controllers with array routes' do
257
390
  mock_app do
258
391
  controller :admin do
@@ -273,6 +406,42 @@ class TestRouting < Test::Unit::TestCase
273
406
  assert_equal "foo_bar_index", body
274
407
  end
275
408
 
409
+ should 'use uri_root' do
410
+ mock_app do
411
+ get(:foo){ "foo" }
412
+ end
413
+ @app.uri_root = '/'
414
+ assert_equal "/foo", @app.url(:foo)
415
+ @app.uri_root = '/testing'
416
+ assert_equal "/testing/foo", @app.url(:foo)
417
+ @app.uri_root = '/testing/'
418
+ assert_equal "/testing/foo", @app.url(:foo)
419
+ @app.uri_root = 'testing/bar///'
420
+ assert_equal "/testing/bar/foo", @app.url(:foo)
421
+ end
422
+
423
+ should 'use uri_root with controllers' do
424
+ mock_app do
425
+ controller :foo do
426
+ get(:bar){ "bar" }
427
+ end
428
+ end
429
+ @app.uri_root = '/testing'
430
+ assert_equal "/testing/foo/bar", @app.url(:foo, :bar)
431
+ end
432
+
433
+ should 'use RACK_BASE_URI' do
434
+ mock_app do
435
+ get(:foo){ "foo" }
436
+ end
437
+ # Wish there was a side-effect free way to test this...
438
+ ENV['RACK_BASE_URI'] = '/'
439
+ assert_equal "/foo", @app.url(:foo)
440
+ ENV['RACK_BASE_URI'] = '/testing'
441
+ assert_equal "/testing/foo", @app.url(:foo)
442
+ ENV['RACK_BASE_URI'] = nil
443
+ end
444
+
276
445
  should 'reset routes' do
277
446
  mock_app do
278
447
  get("/"){ "foo" }
@@ -323,12 +492,21 @@ class TestRouting < Test::Unit::TestCase
323
492
  get(:show, :with => :id, :parent => :product) { "show #{params[:id]} #{params[:user_id]} #{params[:product_id]}"}
324
493
  end
325
494
  end
326
- get "/user/1/project"
495
+
496
+ user_project_url = "/user/1/project"
497
+ get user_project_url
327
498
  assert_equal "index 1", body
328
- get "/user/1/project/edit/2"
499
+ assert_equal user_project_url, @app.url(:project, :index, :user_id => 1)
500
+
501
+ user_project_edit_url = "/user/1/project/edit/2"
502
+ get user_project_edit_url
329
503
  assert_equal "edit 2 1", body
330
- get "/user/1/product/2/project/show/3"
504
+ assert_equal user_project_edit_url, @app.url(:project, :edit, :user_id => 1, :id => 2)
505
+
506
+ user_product_project_url = "/user/1/product/2/project/show/3"
507
+ get user_product_project_url
331
508
  assert_equal "show 3 1 2", body
509
+ assert_equal user_product_project_url, @app.url(:project, :show, :user_id => 1, :product_id => 2, :id => 3)
332
510
  end
333
511
 
334
512
  should "use default values" do
@@ -407,6 +585,31 @@ class TestRouting < Test::Unit::TestCase
407
585
  assert_equal 'Bar in html', body
408
586
  end
409
587
 
588
+ should "set content_type to :html for both empty Accept as well as Accept text/html" do
589
+ mock_app do
590
+ provides :html
591
+
592
+ get("/foo"){ content_type.to_s }
593
+ end
594
+
595
+ get '/foo', {}, {}
596
+ assert_equal 'html', body
597
+
598
+ get '/foo', {}, { 'HTTP_ACCEPT' => 'text/html' }
599
+ assert_equal 'html', body
600
+ end
601
+
602
+ should "set content_type to :html if Accept */*" do
603
+ mock_app do
604
+ get("/foo", :provides => [:html, :js]) { content_type.to_s }
605
+ end
606
+ get '/foo', {}, {}
607
+ assert_equal 'html', body
608
+
609
+ get '/foo', {}, { 'HTTP_ACCEPT' => '*/*;q=0.5' }
610
+ assert_equal 'html', body
611
+ end
612
+
410
613
  should 'allows custom route-conditions to be set via route options' do
411
614
  protector = Module.new {
412
615
  def protect(*args)
@@ -470,4 +673,158 @@ class TestRouting < Test::Unit::TestCase
470
673
  get "/"
471
674
  assert_equal "global", body
472
675
  end
473
- end
676
+
677
+ should 'works with optionals params' do
678
+ mock_app do
679
+ get("/foo(/:bar)") { params[:bar] }
680
+ end
681
+
682
+ get "/foo/bar"
683
+ assert_equal "bar", body
684
+
685
+ get "/foo"
686
+ assert_equal "", body
687
+ end
688
+
689
+ should 'work with multiple dashed params' do
690
+ mock_app do
691
+ get "/route/:foo/:bar/:baz", :provides => :html do
692
+ "#{params[:foo]};#{params[:bar]};#{params[:baz]}"
693
+ end
694
+ end
695
+
696
+ get "/route/foo/bar/baz"
697
+ assert_equal 'foo;bar;baz', body
698
+
699
+ get "/route/foo/bar-whatever/baz"
700
+ assert_equal 'foo;bar-whatever;baz', body
701
+ end
702
+
703
+ should 'work with arbitrary params' do
704
+ mock_app do
705
+ get(:testing) { params[:foo] }
706
+ end
707
+
708
+ url = @app.url(:testing, :foo => 'bar')
709
+ assert_equal "/testing?foo=bar", url
710
+ get url
711
+ assert_equal "bar", body
712
+ end
713
+
714
+ should 'work with controller and arbitrary params' do
715
+ mock_app do
716
+ get(:testing) { params[:foo] }
717
+ controller :test1 do
718
+ get(:url1) { params[:foo] }
719
+ get(:url2, :provides => [:html, :json]) { params[:foo] }
720
+ end
721
+ end
722
+
723
+ url = @app.url(:test1, :url1, :foo => 'bar1')
724
+ assert_equal "/test1/url1?foo=bar1", url
725
+ get url
726
+ assert_equal "bar1", body
727
+
728
+ url = @app.url(:test1, :url2, :foo => 'bar2')
729
+ assert_equal "/test1/url2?foo=bar2", url
730
+ get url
731
+ assert_equal "bar2", body
732
+ end
733
+
734
+ should "work with params and parent options" do
735
+ mock_app do
736
+ controller :test2, :parent => :parent1, :parent1_id => 1 do
737
+ get(:url3) { params[:foo] }
738
+ get(:url4, :with => :with1) { params[:foo] }
739
+ get(:url5, :with => :with2, :provides => [:html]) { params[:foo] }
740
+ end
741
+ end
742
+
743
+ url = @app.url(:test2, :url3, :foo => 'bar3')
744
+ assert_equal "/parent1/1/test2/url3?foo=bar3", url
745
+ get url
746
+ assert_equal "bar3", body
747
+
748
+ url = @app.url(:test2, :url4, :with1 => 'awith1', :foo => 'bar4')
749
+ assert_equal "/parent1/1/test2/url4/awith1?foo=bar4", url
750
+ get url
751
+ assert_equal "bar4", body
752
+
753
+ url = @app.url(:test2, :url5, :with2 => 'awith1', :foo => 'bar5')
754
+ assert_equal "/parent1/1/test2/url5/awith1?foo=bar5", url
755
+ get url
756
+ assert_equal "bar5", body
757
+ end
758
+
759
+ should "parse params without explicit provides for every matching route" do
760
+ mock_app do
761
+ get(:index, :map => "/foos/:bar") { "get bar = #{params[:bar]}" }
762
+ post :create, :map => "/foos/:bar", :provides => [:html, :js] do
763
+ "post bar = #{params[:bar]}"
764
+ end
765
+ end
766
+
767
+ get "/foos/hello"
768
+ assert_equal "get bar = hello", body
769
+ post "/foos/hello"
770
+ assert_equal "post bar = hello", body
771
+ post "/foos/hello.js"
772
+ assert_equal "post bar = hello", body
773
+ end
774
+
775
+ should "properly route to first foo with two similar routes" do
776
+ mock_app do
777
+ controllers do
778
+ get('/foo/') { "this is foo" }
779
+ get(:show, :map => "/foo/:bar/:id") { "/foo/#{params[:bar]}/#{params[:id]}" }
780
+ end
781
+ end
782
+ get "/foo"
783
+ assert_equal "this is foo", body
784
+ # TODO fix this in http_router, should pass
785
+ get "/foo/"
786
+ assert_equal "this is foo", body
787
+ get '/foo/5/10'
788
+ assert_equal "/foo/5/10", body
789
+ end
790
+
791
+ should "parse params with class level provides" do
792
+ mock_app do
793
+ controllers :posts, :provides => [:html, :js] do
794
+ post(:create, :map => "/foo/:bar/:baz/:id") {
795
+ "POST CREATE #{params[:bar]} - #{params[:baz]} - #{params[:id]}"
796
+ }
797
+ end
798
+ controllers :topics, :provides => [:js, :html] do
799
+ get(:show, :map => "/foo/:bar/:baz/:id") { render "topics/show" }
800
+ post(:create, :map => "/foo/:bar/:baz") { "TOPICS CREATE #{params[:bar]} - #{params[:baz]}" }
801
+ end
802
+ end
803
+ post "/foo/bar/baz.js"
804
+ assert_equal "TOPICS CREATE bar - baz", body, "should parse params with explicit .js"
805
+ post @app.url(:topics, :create, :format => :js, :bar => 'bar', :baz => 'baz')
806
+ assert_equal "TOPICS CREATE bar - baz", body, "should parse params from generated url"
807
+ post "/foo/bar/baz/5.js"
808
+ assert_equal "POST CREATE bar - baz - 5", body
809
+ post @app.url(:posts, :create, :format => :js, :bar => 'bar', :baz => 'baz', :id => 5)
810
+ assert_equal "POST CREATE bar - baz - 5", body
811
+ end
812
+
813
+ should "parse params properly with inline provides" do
814
+ mock_app do
815
+ controllers :posts do
816
+ post(:create, :map => "/foo/:bar/:baz/:id", :provides => [:html, :js]) {
817
+ "POST CREATE #{params[:bar]} - #{params[:baz]} - #{params[:id]}"
818
+ }
819
+ end
820
+ controllers :topics do
821
+ get(:show, :map => "/foo/:bar/:baz/:id", :provides => [:html, :js]) { render "topics/show" }
822
+ post(:create, :map => "/foo/:bar/:baz", :provides => [:html, :js]) { "TOPICS CREATE #{params[:bar]} - #{params[:baz]}" }
823
+ end
824
+ end
825
+ post @app.url(:topics, :create, :format => :js, :bar => 'bar', :baz => 'baz')
826
+ assert_equal "TOPICS CREATE bar - baz", body, "should properly post to topics create action"
827
+ post @app.url(:posts, :create, :format => :js, :bar => 'bar', :baz => 'baz', :id => 5)
828
+ assert_equal "POST CREATE bar - baz - 5", body, "should properly post to create action"
829
+ end
830
+ end