actionpack 2.2.3 → 2.3.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of actionpack might be problematic. Click here for more details.
- data/CHANGELOG +433 -375
- data/MIT-LICENSE +1 -1
- data/README +21 -75
- data/Rakefile +1 -1
- data/lib/action_controller.rb +80 -43
- data/lib/action_controller/assertions/model_assertions.rb +1 -0
- data/lib/action_controller/assertions/response_assertions.rb +43 -16
- data/lib/action_controller/assertions/routing_assertions.rb +1 -1
- data/lib/action_controller/assertions/selector_assertions.rb +17 -12
- data/lib/action_controller/assertions/tag_assertions.rb +1 -4
- data/lib/action_controller/base.rb +153 -82
- data/lib/action_controller/benchmarking.rb +9 -9
- data/lib/action_controller/caching.rb +9 -11
- data/lib/action_controller/caching/actions.rb +11 -18
- data/lib/action_controller/caching/fragments.rb +28 -20
- data/lib/action_controller/caching/pages.rb +13 -15
- data/lib/action_controller/caching/sweeping.rb +2 -2
- data/lib/action_controller/cgi_ext.rb +0 -1
- data/lib/action_controller/cgi_ext/cookie.rb +2 -0
- data/lib/action_controller/cgi_process.rb +54 -162
- data/lib/action_controller/cookies.rb +13 -25
- data/lib/action_controller/dispatcher.rb +43 -122
- data/lib/action_controller/failsafe.rb +52 -0
- data/lib/action_controller/flash.rb +38 -47
- data/lib/action_controller/helpers.rb +13 -9
- data/lib/action_controller/http_authentication.rb +203 -23
- data/lib/action_controller/integration.rb +126 -70
- data/lib/action_controller/layout.rb +36 -39
- data/lib/action_controller/middleware_stack.rb +119 -0
- data/lib/action_controller/middlewares.rb +13 -0
- data/lib/action_controller/mime_responds.rb +19 -4
- data/lib/action_controller/mime_type.rb +8 -0
- data/lib/action_controller/params_parser.rb +71 -0
- data/lib/action_controller/performance_test.rb +0 -1
- data/lib/action_controller/polymorphic_routes.rb +36 -30
- data/lib/action_controller/reloader.rb +14 -0
- data/lib/action_controller/request.rb +107 -499
- data/lib/action_controller/request_forgery_protection.rb +7 -39
- data/lib/action_controller/rescue.rb +55 -35
- data/lib/action_controller/resources.rb +34 -31
- data/lib/action_controller/response.rb +99 -57
- data/lib/action_controller/rewindable_input.rb +28 -0
- data/lib/action_controller/routing.rb +7 -7
- data/lib/action_controller/routing/builder.rb +4 -1
- data/lib/action_controller/routing/optimisations.rb +1 -1
- data/lib/action_controller/routing/recognition_optimisation.rb +1 -2
- data/lib/action_controller/routing/route.rb +15 -5
- data/lib/action_controller/routing/route_set.rb +82 -35
- data/lib/action_controller/routing/segments.rb +35 -0
- data/lib/action_controller/session/abstract_store.rb +181 -0
- data/lib/action_controller/session/cookie_store.rb +197 -175
- data/lib/action_controller/session/mem_cache_store.rb +36 -83
- data/lib/action_controller/session_management.rb +26 -134
- data/lib/action_controller/streaming.rb +24 -7
- data/lib/action_controller/templates/rescues/diagnostics.erb +2 -2
- data/lib/action_controller/templates/rescues/template_error.erb +2 -2
- data/lib/action_controller/test_case.rb +87 -30
- data/lib/action_controller/test_process.rb +145 -104
- data/lib/action_controller/uploaded_file.rb +44 -0
- data/lib/action_controller/url_rewriter.rb +3 -6
- data/lib/action_controller/vendor/html-scanner.rb +16 -0
- data/lib/action_controller/vendor/html-scanner/html/selector.rb +1 -1
- data/lib/action_controller/vendor/rack-1.0/rack.rb +89 -0
- data/lib/action_controller/vendor/rack-1.0/rack/adapter/camping.rb +22 -0
- data/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/handler.rb +37 -0
- data/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/request.rb +37 -0
- data/lib/action_controller/vendor/rack-1.0/rack/auth/basic.rb +58 -0
- data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/md5.rb +124 -0
- data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/nonce.rb +51 -0
- data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/params.rb +55 -0
- data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/request.rb +40 -0
- data/lib/action_controller/vendor/rack-1.0/rack/auth/openid.rb +480 -0
- data/lib/action_controller/vendor/rack-1.0/rack/builder.rb +63 -0
- data/lib/action_controller/vendor/rack-1.0/rack/cascade.rb +36 -0
- data/lib/action_controller/vendor/rack-1.0/rack/chunked.rb +49 -0
- data/lib/action_controller/vendor/rack-1.0/rack/commonlogger.rb +61 -0
- data/lib/action_controller/vendor/rack-1.0/rack/conditionalget.rb +45 -0
- data/lib/action_controller/vendor/rack-1.0/rack/content_length.rb +29 -0
- data/lib/action_controller/vendor/rack-1.0/rack/content_type.rb +23 -0
- data/lib/action_controller/vendor/rack-1.0/rack/deflater.rb +85 -0
- data/lib/action_controller/vendor/rack-1.0/rack/directory.rb +153 -0
- data/lib/action_controller/vendor/rack-1.0/rack/file.rb +88 -0
- data/lib/action_controller/vendor/rack-1.0/rack/handler.rb +48 -0
- data/lib/action_controller/vendor/rack-1.0/rack/handler/cgi.rb +61 -0
- data/lib/action_controller/vendor/rack-1.0/rack/handler/evented_mongrel.rb +8 -0
- data/lib/action_controller/vendor/rack-1.0/rack/handler/fastcgi.rb +89 -0
- data/lib/action_controller/vendor/rack-1.0/rack/handler/lsws.rb +55 -0
- data/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb +84 -0
- data/lib/action_controller/vendor/rack-1.0/rack/handler/scgi.rb +59 -0
- data/lib/action_controller/vendor/rack-1.0/rack/handler/swiftiplied_mongrel.rb +8 -0
- data/lib/action_controller/vendor/rack-1.0/rack/handler/thin.rb +18 -0
- data/lib/action_controller/vendor/rack-1.0/rack/handler/webrick.rb +67 -0
- data/lib/action_controller/vendor/rack-1.0/rack/head.rb +19 -0
- data/lib/action_controller/vendor/rack-1.0/rack/lint.rb +462 -0
- data/lib/action_controller/vendor/rack-1.0/rack/lobster.rb +65 -0
- data/lib/action_controller/vendor/rack-1.0/rack/lock.rb +16 -0
- data/lib/action_controller/vendor/rack-1.0/rack/methodoverride.rb +27 -0
- data/lib/action_controller/vendor/rack-1.0/rack/mime.rb +204 -0
- data/lib/action_controller/vendor/rack-1.0/rack/mock.rb +160 -0
- data/lib/action_controller/vendor/rack-1.0/rack/recursive.rb +57 -0
- data/lib/action_controller/vendor/rack-1.0/rack/reloader.rb +64 -0
- data/lib/action_controller/vendor/rack-1.0/rack/request.rb +241 -0
- data/lib/action_controller/vendor/rack-1.0/rack/response.rb +179 -0
- data/lib/action_controller/vendor/rack-1.0/rack/session/abstract/id.rb +142 -0
- data/lib/action_controller/vendor/rack-1.0/rack/session/cookie.rb +91 -0
- data/lib/action_controller/vendor/rack-1.0/rack/session/memcache.rb +109 -0
- data/lib/action_controller/vendor/rack-1.0/rack/session/pool.rb +100 -0
- data/lib/action_controller/vendor/rack-1.0/rack/showexceptions.rb +349 -0
- data/lib/action_controller/vendor/rack-1.0/rack/showstatus.rb +106 -0
- data/lib/action_controller/vendor/rack-1.0/rack/static.rb +38 -0
- data/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb +55 -0
- data/lib/action_controller/vendor/rack-1.0/rack/utils.rb +392 -0
- data/lib/action_controller/verification.rb +1 -1
- data/lib/action_pack.rb +1 -1
- data/lib/action_pack/version.rb +2 -2
- data/lib/action_view.rb +22 -17
- data/lib/action_view/base.rb +53 -79
- data/lib/action_view/erb/util.rb +38 -0
- data/lib/action_view/helpers.rb +24 -5
- data/lib/action_view/helpers/active_record_helper.rb +2 -2
- data/lib/action_view/helpers/asset_tag_helper.rb +81 -50
- data/lib/action_view/helpers/atom_feed_helper.rb +1 -1
- data/lib/action_view/helpers/benchmark_helper.rb +26 -5
- data/lib/action_view/helpers/date_helper.rb +82 -7
- data/lib/action_view/helpers/form_helper.rb +295 -64
- data/lib/action_view/helpers/form_options_helper.rb +160 -18
- data/lib/action_view/helpers/form_tag_helper.rb +2 -2
- data/lib/action_view/helpers/number_helper.rb +31 -18
- data/lib/action_view/helpers/prototype_helper.rb +2 -12
- data/lib/action_view/helpers/sanitize_helper.rb +0 -10
- data/lib/action_view/helpers/scriptaculous_helper.rb +1 -0
- data/lib/action_view/helpers/tag_helper.rb +3 -4
- data/lib/action_view/helpers/text_helper.rb +99 -122
- data/lib/action_view/helpers/translation_helper.rb +19 -1
- data/lib/action_view/helpers/url_helper.rb +25 -2
- data/lib/action_view/inline_template.rb +1 -1
- data/lib/action_view/locale/en.yml +19 -1
- data/lib/action_view/partials.rb +46 -9
- data/lib/action_view/paths.rb +28 -84
- data/lib/action_view/reloadable_template.rb +117 -0
- data/lib/action_view/renderable.rb +28 -35
- data/lib/action_view/renderable_partial.rb +3 -4
- data/lib/action_view/template.rb +172 -31
- data/lib/action_view/template_error.rb +8 -9
- data/lib/action_view/template_handler.rb +1 -1
- data/lib/action_view/template_handlers.rb +9 -6
- data/lib/action_view/template_handlers/erb.rb +2 -39
- data/lib/action_view/template_handlers/rjs.rb +1 -0
- data/lib/action_view/test_case.rb +27 -1
- data/test/abstract_unit.rb +23 -17
- data/test/active_record_unit.rb +5 -4
- data/test/activerecord/active_record_store_test.rb +139 -106
- data/test/activerecord/render_partial_with_record_identification_test.rb +5 -21
- data/test/controller/action_pack_assertions_test.rb +25 -23
- data/test/controller/addresses_render_test.rb +3 -6
- data/test/controller/assert_select_test.rb +83 -70
- data/test/controller/base_test.rb +11 -13
- data/test/controller/benchmark_test.rb +3 -3
- data/test/controller/caching_test.rb +34 -24
- data/test/controller/capture_test.rb +3 -6
- data/test/controller/content_type_test.rb +3 -6
- data/test/controller/cookie_test.rb +31 -66
- data/test/controller/deprecation/deprecated_base_methods_test.rb +9 -11
- data/test/controller/dispatcher_test.rb +23 -28
- data/test/controller/fake_models.rb +8 -0
- data/test/controller/filters_test.rb +6 -2
- data/test/controller/flash_test.rb +2 -6
- data/test/controller/helper_test.rb +15 -1
- data/test/controller/html-scanner/document_test.rb +1 -1
- data/test/controller/html-scanner/sanitizer_test.rb +1 -1
- data/test/controller/http_basic_authentication_test.rb +88 -0
- data/test/controller/http_digest_authentication_test.rb +178 -0
- data/test/controller/integration_test.rb +56 -52
- data/test/controller/layout_test.rb +46 -44
- data/test/controller/middleware_stack_test.rb +90 -0
- data/test/controller/mime_responds_test.rb +7 -11
- data/test/controller/mime_type_test.rb +9 -0
- data/test/controller/polymorphic_routes_test.rb +235 -151
- data/test/controller/rack_test.rb +52 -81
- data/test/controller/redirect_test.rb +6 -14
- data/test/controller/render_test.rb +273 -60
- data/test/controller/request/json_params_parsing_test.rb +45 -0
- data/test/controller/request/multipart_params_parsing_test.rb +223 -0
- data/test/controller/request/query_string_parsing_test.rb +120 -0
- data/test/controller/request/url_encoded_params_parsing_test.rb +184 -0
- data/test/controller/request/xml_params_parsing_test.rb +88 -0
- data/test/controller/request_forgery_protection_test.rb +17 -98
- data/test/controller/request_test.rb +45 -530
- data/test/controller/rescue_test.rb +45 -22
- data/test/controller/resources_test.rb +112 -37
- data/test/controller/routing_test.rb +1442 -1384
- data/test/controller/selector_test.rb +3 -3
- data/test/controller/send_file_test.rb +30 -3
- data/test/controller/session/cookie_store_test.rb +169 -240
- data/test/controller/session/mem_cache_store_test.rb +94 -148
- data/test/controller/session/test_session_test.rb +58 -0
- data/test/controller/test_test.rb +32 -13
- data/test/controller/url_rewriter_test.rb +54 -4
- data/test/controller/verification_test.rb +1 -1
- data/test/controller/view_paths_test.rb +15 -15
- data/test/controller/webservice_test.rb +178 -147
- data/test/fixtures/alternate_helpers/foo_helper.rb +3 -0
- data/test/fixtures/layout_tests/alt/layouts/alt.rhtml +0 -0
- data/test/fixtures/layouts/default_html.html.erb +1 -0
- data/test/fixtures/layouts/xhr.html.erb +2 -0
- data/test/fixtures/multipart/empty +10 -0
- data/test/fixtures/multipart/hello.txt +1 -0
- data/test/fixtures/multipart/none +9 -0
- data/test/fixtures/public/500.da.html +1 -0
- data/test/fixtures/quiz/questions/_question.html.erb +1 -0
- data/test/fixtures/replies.yml +1 -1
- data/test/fixtures/test/_one.html.erb +1 -0
- data/test/fixtures/test/_two.html.erb +1 -0
- data/test/fixtures/test/dont_pick_me +1 -0
- data/test/fixtures/test/hello.builder +1 -1
- data/test/fixtures/test/hello_world.da.html.erb +1 -0
- data/test/fixtures/test/hello_world.erb~ +1 -0
- data/test/fixtures/test/hello_world.pt-BR.html.erb +1 -0
- data/test/fixtures/test/malformed/malformed.en.html.erb~ +1 -0
- data/test/fixtures/test/malformed/malformed.erb~ +1 -0
- data/test/fixtures/test/malformed/malformed.html.erb~ +1 -0
- data/test/fixtures/test/render_explicit_html_template.js.rjs +1 -0
- data/test/fixtures/test/render_implicit_html_template.js.rjs +1 -0
- data/test/fixtures/test/render_implicit_html_template_from_xhr_request.da.html.erb +1 -0
- data/test/fixtures/test/render_implicit_html_template_from_xhr_request.html.erb +1 -0
- data/test/fixtures/test/render_implicit_js_template_without_layout.js.erb +1 -0
- data/test/fixtures/test/utf8.html.erb +2 -0
- data/test/template/active_record_helper_i18n_test.rb +31 -33
- data/test/template/active_record_helper_test.rb +34 -0
- data/test/template/asset_tag_helper_test.rb +52 -14
- data/test/template/atom_feed_helper_test.rb +3 -5
- data/test/template/benchmark_helper_test.rb +50 -24
- data/test/template/compiled_templates_test.rb +177 -33
- data/test/template/date_helper_i18n_test.rb +88 -81
- data/test/template/date_helper_test.rb +427 -43
- data/test/template/form_helper_test.rb +243 -44
- data/test/template/form_options_helper_test.rb +631 -565
- data/test/template/form_tag_helper_test.rb +9 -2
- data/test/template/javascript_helper_test.rb +0 -5
- data/test/template/number_helper_i18n_test.rb +60 -48
- data/test/template/number_helper_test.rb +1 -0
- data/test/template/render_test.rb +117 -35
- data/test/template/test_test.rb +4 -6
- data/test/template/text_helper_test.rb +129 -50
- data/test/template/translation_helper_test.rb +23 -19
- data/test/template/url_helper_test.rb +35 -2
- data/test/view/test_case_test.rb +8 -0
- metadata +197 -23
- data/lib/action_controller/assertions.rb +0 -69
- data/lib/action_controller/caching/sql_cache.rb +0 -18
- data/lib/action_controller/cgi_ext/session.rb +0 -53
- data/lib/action_controller/components.rb +0 -169
- data/lib/action_controller/rack_process.rb +0 -297
- data/lib/action_controller/request_profiler.rb +0 -169
- data/lib/action_controller/session/active_record_store.rb +0 -340
- data/lib/action_controller/session/drb_server.rb +0 -32
- data/lib/action_controller/session/drb_store.rb +0 -35
- data/test/controller/cgi_test.rb +0 -269
- data/test/controller/components_test.rb +0 -156
- data/test/controller/http_authentication_test.rb +0 -54
- data/test/controller/integration_upload_test.rb +0 -43
- data/test/controller/session_fixation_test.rb +0 -89
- data/test/controller/session_management_test.rb +0 -178
- data/test/fixtures/test/hello_world.js +0 -1
@@ -1,7 +1,5 @@
|
|
1
1
|
require 'abstract_unit'
|
2
2
|
|
3
|
-
uses_mocha 'rescue' do
|
4
|
-
|
5
3
|
class RescueController < ActionController::Base
|
6
4
|
class NotAuthorized < StandardError
|
7
5
|
end
|
@@ -67,6 +65,11 @@ class RescueController < ActionController::Base
|
|
67
65
|
render :text => 'no way'
|
68
66
|
end
|
69
67
|
|
68
|
+
before_filter(:only => :before_filter_raises) { raise 'umm nice' }
|
69
|
+
|
70
|
+
def before_filter_raises
|
71
|
+
end
|
72
|
+
|
70
73
|
def raises
|
71
74
|
render :text => 'already rendered'
|
72
75
|
raise "don't panic!"
|
@@ -154,6 +157,16 @@ class RescueControllerTest < ActionController::TestCase
|
|
154
157
|
end
|
155
158
|
end
|
156
159
|
|
160
|
+
def test_rescue_exceptions_raised_by_filters
|
161
|
+
with_rails_root FIXTURE_PUBLIC do
|
162
|
+
with_all_requests_local false do
|
163
|
+
get :before_filter_raises
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
assert_response :internal_server_error
|
168
|
+
end
|
169
|
+
|
157
170
|
def test_rescue_action_locally_if_all_requests_local
|
158
171
|
@controller.expects(:local_request?).never
|
159
172
|
@controller.expects(:rescue_action_locally).with(@exception)
|
@@ -184,6 +197,24 @@ class RescueControllerTest < ActionController::TestCase
|
|
184
197
|
end
|
185
198
|
end
|
186
199
|
|
200
|
+
def test_rescue_action_in_public_with_localized_error_file
|
201
|
+
# Change locale
|
202
|
+
old_locale = I18n.locale
|
203
|
+
I18n.locale = :da
|
204
|
+
|
205
|
+
with_rails_root FIXTURE_PUBLIC do
|
206
|
+
with_all_requests_local false do
|
207
|
+
get :raises
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
assert_response :internal_server_error
|
212
|
+
body = File.read("#{FIXTURE_PUBLIC}/public/500.da.html")
|
213
|
+
assert_equal body, @response.body
|
214
|
+
ensure
|
215
|
+
I18n.locale = old_locale
|
216
|
+
end
|
217
|
+
|
187
218
|
def test_rescue_action_in_public_with_error_file
|
188
219
|
with_rails_root FIXTURE_PUBLIC do
|
189
220
|
with_all_requests_local false do
|
@@ -291,24 +322,6 @@ class RescueControllerTest < ActionController::TestCase
|
|
291
322
|
assert_equal 'template_error', templates[ActionView::TemplateError.name]
|
292
323
|
end
|
293
324
|
|
294
|
-
def test_clean_backtrace
|
295
|
-
with_rails_root nil do
|
296
|
-
# No action if RAILS_ROOT isn't set.
|
297
|
-
cleaned = @controller.send(:clean_backtrace, @exception)
|
298
|
-
assert_equal @exception.backtrace, cleaned
|
299
|
-
end
|
300
|
-
|
301
|
-
with_rails_root Dir.pwd do
|
302
|
-
# RAILS_ROOT is removed from backtrace.
|
303
|
-
cleaned = @controller.send(:clean_backtrace, @exception)
|
304
|
-
expected = @exception.backtrace.map { |line| line.sub(RAILS_ROOT, '') }
|
305
|
-
assert_equal expected, cleaned
|
306
|
-
|
307
|
-
# No action if backtrace is nil.
|
308
|
-
assert_nil @controller.send(:clean_backtrace, Exception.new)
|
309
|
-
end
|
310
|
-
end
|
311
|
-
|
312
325
|
def test_not_implemented
|
313
326
|
with_all_requests_local false do
|
314
327
|
with_rails_public_path(".") do
|
@@ -385,10 +398,21 @@ class RescueControllerTest < ActionController::TestCase
|
|
385
398
|
end
|
386
399
|
|
387
400
|
def test_rescue_dispatcher_exceptions
|
388
|
-
|
401
|
+
env = @request.env
|
402
|
+
env["action_controller.rescue.request"] = @request
|
403
|
+
env["action_controller.rescue.response"] = @response
|
404
|
+
|
405
|
+
RescueController.call_with_exception(env, ActionController::RoutingError.new("Route not found"))
|
389
406
|
assert_equal "no way", @response.body
|
390
407
|
end
|
391
408
|
|
409
|
+
def test_rescue_dispatcher_exceptions_without_request_set
|
410
|
+
@request.env['REQUEST_URI'] = '/no_way'
|
411
|
+
response = RescueController.call_with_exception(@request.env, ActionController::RoutingError.new("Route not found"))
|
412
|
+
assert_kind_of ActionController::Response, response
|
413
|
+
assert_equal "no way", response.body
|
414
|
+
end
|
415
|
+
|
392
416
|
protected
|
393
417
|
def with_all_requests_local(local = true)
|
394
418
|
old_local, ActionController::Base.consider_all_requests_local =
|
@@ -510,4 +534,3 @@ class ControllerInheritanceRescueControllerTest < ActionController::TestCase
|
|
510
534
|
assert_response :created
|
511
535
|
end
|
512
536
|
end
|
513
|
-
end # uses_mocha
|
@@ -29,7 +29,7 @@ module Backoffice
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
class ResourcesTest <
|
32
|
+
class ResourcesTest < ActionController::TestCase
|
33
33
|
# The assertions in these tests are incompatible with the hash method
|
34
34
|
# optimisation. This could indicate user level problems
|
35
35
|
def setup
|
@@ -99,7 +99,7 @@ class ResourcesTest < Test::Unit::TestCase
|
|
99
99
|
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
|
100
100
|
|
101
101
|
with_restful_routing :messages do
|
102
|
-
|
102
|
+
assert_raise(ActionController::RoutingError) do
|
103
103
|
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
|
104
104
|
end
|
105
105
|
end
|
@@ -175,6 +175,24 @@ class ResourcesTest < Test::Unit::TestCase
|
|
175
175
|
end
|
176
176
|
end
|
177
177
|
|
178
|
+
def test_with_collection_actions_and_name_prefix_and_member_action_with_same_name
|
179
|
+
actions = { 'a' => :get }
|
180
|
+
|
181
|
+
with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions, :member => actions do
|
182
|
+
assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
183
|
+
actions.each do |action, method|
|
184
|
+
assert_recognizes(options.merge(:action => action), :path => "/threads/1/messages/#{action}", :method => method)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
189
|
+
actions.keys.each do |action|
|
190
|
+
assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
178
196
|
def test_with_collection_action_and_name_prefix_and_formatted
|
179
197
|
actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
|
180
198
|
|
@@ -187,7 +205,7 @@ class ResourcesTest < Test::Unit::TestCase
|
|
187
205
|
|
188
206
|
assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
189
207
|
actions.keys.each do |action|
|
190
|
-
assert_named_route "/threads/1/messages/#{action}.xml", "
|
208
|
+
assert_named_route "/threads/1/messages/#{action}.xml", "#{action}_thread_messages_path", :action => action, :format => 'xml'
|
191
209
|
end
|
192
210
|
end
|
193
211
|
end
|
@@ -209,6 +227,14 @@ class ResourcesTest < Test::Unit::TestCase
|
|
209
227
|
end
|
210
228
|
end
|
211
229
|
|
230
|
+
def test_with_member_action_and_requirement
|
231
|
+
expected_options = {:controller => 'messages', :action => 'mark', :id => '1.1.1'}
|
232
|
+
|
233
|
+
with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}, :member => { :mark => :get }) do
|
234
|
+
assert_recognizes(expected_options, :path => 'messages/1.1.1/mark', :method => :get)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
212
238
|
def test_member_when_override_paths_for_default_restful_actions_with
|
213
239
|
[:put, :post].each do |method|
|
214
240
|
with_restful_routing :messages, :member => { :mark => method }, :path_names => {:new => 'nuevo'} do
|
@@ -316,7 +342,7 @@ class ResourcesTest < Test::Unit::TestCase
|
|
316
342
|
end
|
317
343
|
|
318
344
|
assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
|
319
|
-
assert_named_route preview_path, :
|
345
|
+
assert_named_route preview_path, :preview_new_thread_message_path, preview_options
|
320
346
|
end
|
321
347
|
end
|
322
348
|
end
|
@@ -325,7 +351,7 @@ class ResourcesTest < Test::Unit::TestCase
|
|
325
351
|
with_restful_routing :messages do
|
326
352
|
assert_restful_routes_for :messages do |options|
|
327
353
|
assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
|
328
|
-
|
354
|
+
assert_raise(ActionController::MethodNotAllowed) do
|
329
355
|
ActionController::Routing::Routes.recognize_path("/messages/new", :method => :post)
|
330
356
|
end
|
331
357
|
end
|
@@ -406,6 +432,34 @@ class ResourcesTest < Test::Unit::TestCase
|
|
406
432
|
end
|
407
433
|
end
|
408
434
|
|
435
|
+
def test_shallow_nested_restful_routes_with_namespaces
|
436
|
+
with_routing do |set|
|
437
|
+
set.draw do |map|
|
438
|
+
map.namespace :backoffice do |map|
|
439
|
+
map.namespace :admin do |map|
|
440
|
+
map.resources :products, :shallow => true do |map|
|
441
|
+
map.resources :images
|
442
|
+
end
|
443
|
+
end
|
444
|
+
end
|
445
|
+
end
|
446
|
+
|
447
|
+
assert_simply_restful_for :products,
|
448
|
+
:controller => 'backoffice/admin/products',
|
449
|
+
:namespace => 'backoffice/admin/',
|
450
|
+
:name_prefix => 'backoffice_admin_',
|
451
|
+
:path_prefix => 'backoffice/admin/',
|
452
|
+
:shallow => true
|
453
|
+
assert_simply_restful_for :images,
|
454
|
+
:controller => 'backoffice/admin/images',
|
455
|
+
:namespace => 'backoffice/admin/',
|
456
|
+
:name_prefix => 'backoffice_admin_product_',
|
457
|
+
:path_prefix => 'backoffice/admin/products/1/',
|
458
|
+
:shallow => true,
|
459
|
+
:options => { :product_id => '1' }
|
460
|
+
end
|
461
|
+
end
|
462
|
+
|
409
463
|
def test_restful_routes_dont_generate_duplicates
|
410
464
|
with_restful_routing :messages do
|
411
465
|
routes = ActionController::Routing::Routes.routes
|
@@ -583,11 +637,11 @@ class ResourcesTest < Test::Unit::TestCase
|
|
583
637
|
options = { :controller => controller_name.to_s }
|
584
638
|
collection_path = "/#{controller_name}"
|
585
639
|
|
586
|
-
|
640
|
+
assert_raise(ActionController::MethodNotAllowed) do
|
587
641
|
assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put)
|
588
642
|
end
|
589
643
|
|
590
|
-
|
644
|
+
assert_raise(ActionController::MethodNotAllowed) do
|
591
645
|
assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete)
|
592
646
|
end
|
593
647
|
end
|
@@ -596,7 +650,7 @@ class ResourcesTest < Test::Unit::TestCase
|
|
596
650
|
def test_should_not_allow_invalid_head_method_for_member_routes
|
597
651
|
with_routing do |set|
|
598
652
|
set.draw do |map|
|
599
|
-
|
653
|
+
assert_raise(ArgumentError) do
|
600
654
|
map.resources :messages, :member => {:something => :head}
|
601
655
|
end
|
602
656
|
end
|
@@ -606,7 +660,7 @@ class ResourcesTest < Test::Unit::TestCase
|
|
606
660
|
def test_should_not_allow_invalid_http_methods_for_member_routes
|
607
661
|
with_routing do |set|
|
608
662
|
set.draw do |map|
|
609
|
-
|
663
|
+
assert_raise(ArgumentError) do
|
610
664
|
map.resources :messages, :member => {:something => :invalid}
|
611
665
|
end
|
612
666
|
end
|
@@ -750,9 +804,17 @@ class ResourcesTest < Test::Unit::TestCase
|
|
750
804
|
end
|
751
805
|
|
752
806
|
def test_with_path_segment
|
753
|
-
with_restful_routing :messages
|
754
|
-
assert_simply_restful_for :messages
|
807
|
+
with_restful_routing :messages do
|
808
|
+
assert_simply_restful_for :messages
|
809
|
+
assert_recognizes({:controller => "messages", :action => "index"}, "/messages")
|
810
|
+
assert_recognizes({:controller => "messages", :action => "index"}, "/messages/")
|
755
811
|
end
|
812
|
+
|
813
|
+
with_restful_routing :messages, :as => 'reviews' do
|
814
|
+
assert_simply_restful_for :messages, :as => 'reviews'
|
815
|
+
assert_recognizes({:controller => "messages", :action => "index"}, "/reviews")
|
816
|
+
assert_recognizes({:controller => "messages", :action => "index"}, "/reviews/")
|
817
|
+
end
|
756
818
|
end
|
757
819
|
|
758
820
|
def test_multiple_with_path_segment_and_controller
|
@@ -942,19 +1004,6 @@ class ResourcesTest < Test::Unit::TestCase
|
|
942
1004
|
end
|
943
1005
|
end
|
944
1006
|
|
945
|
-
def test_nested_resource_inherits_only_show_action
|
946
|
-
with_routing do |set|
|
947
|
-
set.draw do |map|
|
948
|
-
map.resources :products, :only => :show do |product|
|
949
|
-
product.resources :images
|
950
|
-
end
|
951
|
-
end
|
952
|
-
|
953
|
-
assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
|
954
|
-
assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
|
955
|
-
end
|
956
|
-
end
|
957
|
-
|
958
1007
|
def test_nested_resource_has_only_show_and_member_action
|
959
1008
|
with_routing do |set|
|
960
1009
|
set.draw do |map|
|
@@ -971,7 +1020,7 @@ class ResourcesTest < Test::Unit::TestCase
|
|
971
1020
|
end
|
972
1021
|
end
|
973
1022
|
|
974
|
-
def
|
1023
|
+
def test_nested_resource_does_not_inherit_only_option
|
975
1024
|
with_routing do |set|
|
976
1025
|
set.draw do |map|
|
977
1026
|
map.resources :products, :only => :show do |product|
|
@@ -984,7 +1033,20 @@ class ResourcesTest < Test::Unit::TestCase
|
|
984
1033
|
end
|
985
1034
|
end
|
986
1035
|
|
987
|
-
def
|
1036
|
+
def test_nested_resource_does_not_inherit_only_option_by_default
|
1037
|
+
with_routing do |set|
|
1038
|
+
set.draw do |map|
|
1039
|
+
map.resources :products, :only => :show do |product|
|
1040
|
+
product.resources :images
|
1041
|
+
end
|
1042
|
+
end
|
1043
|
+
|
1044
|
+
assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destory], [], 'products/1/images')
|
1045
|
+
assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
|
1046
|
+
end
|
1047
|
+
end
|
1048
|
+
|
1049
|
+
def test_nested_resource_does_not_inherit_except_option
|
988
1050
|
with_routing do |set|
|
989
1051
|
set.draw do |map|
|
990
1052
|
map.resources :products, :except => :show do |product|
|
@@ -997,6 +1059,19 @@ class ResourcesTest < Test::Unit::TestCase
|
|
997
1059
|
end
|
998
1060
|
end
|
999
1061
|
|
1062
|
+
def test_nested_resource_does_not_inherit_except_option_by_default
|
1063
|
+
with_routing do |set|
|
1064
|
+
set.draw do |map|
|
1065
|
+
map.resources :products, :except => :show do |product|
|
1066
|
+
product.resources :images
|
1067
|
+
end
|
1068
|
+
end
|
1069
|
+
|
1070
|
+
assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
|
1071
|
+
assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
|
1072
|
+
end
|
1073
|
+
end
|
1074
|
+
|
1000
1075
|
def test_default_singleton_restful_route_uses_get
|
1001
1076
|
with_routing do |set|
|
1002
1077
|
set.draw do |map|
|
@@ -1053,7 +1128,7 @@ class ResourcesTest < Test::Unit::TestCase
|
|
1053
1128
|
|
1054
1129
|
path = "#{options[:as] || controller_name}"
|
1055
1130
|
collection_path = "/#{options[:path_prefix]}#{path}"
|
1056
|
-
shallow_path = "/#{options[:
|
1131
|
+
shallow_path = "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
|
1057
1132
|
member_path = "#{shallow_path}/1"
|
1058
1133
|
new_path = "#{collection_path}/#{new_action}"
|
1059
1134
|
edit_member_path = "#{member_path}/#{edit_action}"
|
@@ -1117,10 +1192,10 @@ class ResourcesTest < Test::Unit::TestCase
|
|
1117
1192
|
options[:options].delete :action
|
1118
1193
|
|
1119
1194
|
path = "#{options[:as] || controller_name}"
|
1120
|
-
shallow_path = "/#{options[:
|
1195
|
+
shallow_path = "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
|
1121
1196
|
full_path = "/#{options[:path_prefix]}#{path}"
|
1122
1197
|
name_prefix = options[:name_prefix]
|
1123
|
-
shallow_prefix =
|
1198
|
+
shallow_prefix = options[:shallow] ? options[:namespace].try(:gsub, /\//, '_') : options[:name_prefix]
|
1124
1199
|
|
1125
1200
|
new_action = "new"
|
1126
1201
|
edit_action = "edit"
|
@@ -1130,14 +1205,14 @@ class ResourcesTest < Test::Unit::TestCase
|
|
1130
1205
|
end
|
1131
1206
|
|
1132
1207
|
assert_named_route "#{full_path}", "#{name_prefix}#{controller_name}_path", options[:options]
|
1133
|
-
assert_named_route "#{full_path}.xml", "
|
1208
|
+
assert_named_route "#{full_path}.xml", "#{name_prefix}#{controller_name}_path", options[:options].merge(:format => 'xml')
|
1134
1209
|
assert_named_route "#{shallow_path}/1", "#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1')
|
1135
|
-
assert_named_route "#{shallow_path}/1.xml", "
|
1210
|
+
assert_named_route "#{shallow_path}/1.xml", "#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => 'xml')
|
1136
1211
|
|
1137
1212
|
assert_named_route "#{full_path}/#{new_action}", "new_#{name_prefix}#{singular_name}_path", options[:options]
|
1138
|
-
assert_named_route "#{full_path}/#{new_action}.xml", "
|
1213
|
+
assert_named_route "#{full_path}/#{new_action}.xml", "new_#{name_prefix}#{singular_name}_path", options[:options].merge(:format => 'xml')
|
1139
1214
|
assert_named_route "#{shallow_path}/1/#{edit_action}", "edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1')
|
1140
|
-
assert_named_route "#{shallow_path}/1/#{edit_action}.xml", "
|
1215
|
+
assert_named_route "#{shallow_path}/1/#{edit_action}.xml", "edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => 'xml')
|
1141
1216
|
|
1142
1217
|
yield options[:options] if block_given?
|
1143
1218
|
end
|
@@ -1189,12 +1264,12 @@ class ResourcesTest < Test::Unit::TestCase
|
|
1189
1264
|
name_prefix = options[:name_prefix]
|
1190
1265
|
|
1191
1266
|
assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options]
|
1192
|
-
assert_named_route "#{full_path}.xml", "
|
1267
|
+
assert_named_route "#{full_path}.xml", "#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
1193
1268
|
|
1194
1269
|
assert_named_route "#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options[:options]
|
1195
|
-
assert_named_route "#{full_path}/new.xml", "
|
1270
|
+
assert_named_route "#{full_path}/new.xml", "new_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
1196
1271
|
assert_named_route "#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", options[:options]
|
1197
|
-
assert_named_route "#{full_path}/edit.xml", "
|
1272
|
+
assert_named_route "#{full_path}/edit.xml", "edit_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
1198
1273
|
end
|
1199
1274
|
|
1200
1275
|
def assert_named_route(expected, route, options)
|
@@ -1250,7 +1325,7 @@ class ResourcesTest < Test::Unit::TestCase
|
|
1250
1325
|
end
|
1251
1326
|
|
1252
1327
|
def assert_not_recognizes(expected_options, path)
|
1253
|
-
assert_raise ActionController::RoutingError, ActionController::MethodNotAllowed,
|
1328
|
+
assert_raise ActionController::RoutingError, ActionController::MethodNotAllowed, Assertion do
|
1254
1329
|
assert_recognizes(expected_options, path)
|
1255
1330
|
end
|
1256
1331
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'abstract_unit'
|
2
2
|
require 'controller/fake_controllers'
|
3
|
-
require 'action_controller/routing'
|
4
3
|
|
5
4
|
class MilestonesController < ActionController::Base
|
6
5
|
def index() head :ok end
|
@@ -220,7 +219,7 @@ class DynamicSegmentTest < Test::Unit::TestCase
|
|
220
219
|
a_value = nil
|
221
220
|
|
222
221
|
# Local jump because of return inside eval.
|
223
|
-
|
222
|
+
assert_raise(LocalJumpError) { eval(segment.extraction_code) }
|
224
223
|
end
|
225
224
|
|
226
225
|
def test_extraction_code_should_return_on_mismatch
|
@@ -230,7 +229,7 @@ class DynamicSegmentTest < Test::Unit::TestCase
|
|
230
229
|
a_value = nil
|
231
230
|
|
232
231
|
# Local jump because of return inside eval.
|
233
|
-
|
232
|
+
assert_raise(LocalJumpError) { eval(segment.extraction_code) }
|
234
233
|
end
|
235
234
|
|
236
235
|
def test_extraction_code_should_accept_value_and_set_local
|
@@ -495,7 +494,7 @@ class RouteBuilderTest < Test::Unit::TestCase
|
|
495
494
|
defaults = {:action => 'buy', :person => nil, :car => nil}
|
496
495
|
requirements = {:person => /\w+/, :car => /^\w+$/}
|
497
496
|
|
498
|
-
|
497
|
+
assert_raise ArgumentError do
|
499
498
|
route_requirements = builder.assign_route_options(segments, defaults, requirements)
|
500
499
|
end
|
501
500
|
|
@@ -716,1650 +715,1656 @@ class RoutingTest < Test::Unit::TestCase
|
|
716
715
|
end
|
717
716
|
end
|
718
717
|
|
719
|
-
|
720
|
-
|
721
|
-
attr_accessor :routes
|
718
|
+
class MockController
|
719
|
+
attr_accessor :routes
|
722
720
|
|
723
|
-
|
724
|
-
|
725
|
-
|
721
|
+
def initialize(routes)
|
722
|
+
self.routes = routes
|
723
|
+
end
|
726
724
|
|
727
|
-
|
728
|
-
|
725
|
+
def url_for(options)
|
726
|
+
only_path = options.delete(:only_path)
|
729
727
|
|
730
|
-
|
731
|
-
|
728
|
+
port = options.delete(:port) || 80
|
729
|
+
port_string = port == 80 ? '' : ":#{port}"
|
732
730
|
|
733
|
-
|
734
|
-
|
735
|
-
|
731
|
+
protocol = options.delete(:protocol) || "http"
|
732
|
+
host = options.delete(:host) || "test.host"
|
733
|
+
anchor = "##{options.delete(:anchor)}" if options.key?(:anchor)
|
736
734
|
|
737
|
-
|
735
|
+
path = routes.generate(options)
|
738
736
|
|
739
|
-
|
740
|
-
|
737
|
+
only_path ? "#{path}#{anchor}" : "#{protocol}://#{host}#{port_string}#{path}#{anchor}"
|
738
|
+
end
|
741
739
|
|
742
|
-
|
743
|
-
|
744
|
-
end
|
740
|
+
def request
|
741
|
+
@request ||= ActionController::TestRequest.new
|
745
742
|
end
|
743
|
+
end
|
746
744
|
|
747
|
-
|
748
|
-
|
745
|
+
class LegacyRouteSetTests < Test::Unit::TestCase
|
746
|
+
attr_reader :rs
|
749
747
|
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
748
|
+
def setup
|
749
|
+
# These tests assume optimisation is on, so re-enable it.
|
750
|
+
ActionController::Base.optimise_named_routes = true
|
751
|
+
|
752
|
+
@rs = ::ActionController::Routing::RouteSet.new
|
753
|
+
|
754
|
+
ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed)
|
755
|
+
end
|
756
|
+
|
757
|
+
def teardown
|
758
|
+
@rs.clear!
|
759
|
+
end
|
760
|
+
|
761
|
+
def test_default_setup
|
762
|
+
@rs.draw {|m| m.connect ':controller/:action/:id' }
|
763
|
+
assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
|
764
|
+
assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
|
765
|
+
assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
|
766
|
+
|
767
|
+
assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10"))
|
768
|
+
|
769
|
+
assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10)
|
770
|
+
|
771
|
+
assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
772
|
+
assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
773
|
+
|
774
|
+
assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
775
|
+
assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
776
|
+
end
|
777
|
+
|
778
|
+
def test_ignores_leading_slash
|
779
|
+
@rs.clear!
|
780
|
+
@rs.draw {|m| m.connect '/:controller/:action/:id'}
|
781
|
+
test_default_setup
|
782
|
+
end
|
783
|
+
|
784
|
+
def test_time_recognition
|
785
|
+
# We create many routes to make situation more realistic
|
786
|
+
@rs = ::ActionController::Routing::RouteSet.new
|
787
|
+
@rs.draw { |map|
|
788
|
+
map.frontpage '', :controller => 'search', :action => 'new'
|
789
|
+
map.resources :videos do |video|
|
790
|
+
video.resources :comments
|
791
|
+
video.resource :file, :controller => 'video_file'
|
792
|
+
video.resource :share, :controller => 'video_shares'
|
793
|
+
video.resource :abuse, :controller => 'video_abuses'
|
794
|
+
end
|
795
|
+
map.resources :abuses, :controller => 'video_abuses'
|
796
|
+
map.resources :video_uploads
|
797
|
+
map.resources :video_visits
|
798
|
+
|
799
|
+
map.resources :users do |user|
|
800
|
+
user.resource :settings
|
801
|
+
user.resources :videos
|
802
|
+
end
|
803
|
+
map.resources :channels do |channel|
|
804
|
+
channel.resources :videos, :controller => 'channel_videos'
|
805
|
+
end
|
806
|
+
map.resource :session
|
807
|
+
map.resource :lost_password
|
808
|
+
map.search 'search', :controller => 'search'
|
809
|
+
map.resources :pages
|
810
|
+
map.connect ':controller/:action/:id'
|
811
|
+
}
|
812
|
+
n = 1000
|
813
|
+
if RunTimeTests
|
814
|
+
GC.start
|
815
|
+
rectime = Benchmark.realtime do
|
816
|
+
n.times do
|
817
|
+
rs.recognize_path("/videos/1234567", {:method => :get})
|
818
|
+
rs.recognize_path("/videos/1234567/abuse", {:method => :get})
|
819
|
+
rs.recognize_path("/users/1234567/settings", {:method => :get})
|
820
|
+
rs.recognize_path("/channels/1234567", {:method => :get})
|
821
|
+
rs.recognize_path("/session/new", {:method => :get})
|
822
|
+
rs.recognize_path("/admin/user/show/10", {:method => :get})
|
823
|
+
end
|
755
824
|
end
|
825
|
+
puts "\n\nRecognition (#{rs.routes.size} routes):"
|
826
|
+
per_url = rectime / (n * 6)
|
827
|
+
puts "#{per_url * 1000} ms/url"
|
828
|
+
puts "#{1 / per_url} url/s\n\n"
|
829
|
+
end
|
830
|
+
end
|
831
|
+
|
832
|
+
def test_time_generation
|
833
|
+
n = 5000
|
834
|
+
if RunTimeTests
|
835
|
+
GC.start
|
836
|
+
pairs = [
|
837
|
+
[{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}],
|
838
|
+
[{:controller => 'content'}, {:controller => 'content', :action => 'index'}],
|
839
|
+
[{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}],
|
840
|
+
[{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}],
|
841
|
+
[{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}],
|
842
|
+
[{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}],
|
843
|
+
[{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}],
|
844
|
+
[{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}],
|
845
|
+
]
|
846
|
+
p = nil
|
847
|
+
gentime = Benchmark.realtime do
|
848
|
+
n.times do
|
849
|
+
pairs.each {|(a, b)| rs.generate(a, b)}
|
850
|
+
end
|
851
|
+
end
|
852
|
+
|
853
|
+
puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)"
|
854
|
+
per_url = gentime / (n * 8)
|
855
|
+
puts "#{per_url * 1000} ms/url"
|
856
|
+
puts "#{1 / per_url} url/s\n\n"
|
756
857
|
end
|
858
|
+
end
|
757
859
|
|
758
|
-
|
759
|
-
|
860
|
+
def test_route_with_colon_first
|
861
|
+
rs.draw do |map|
|
862
|
+
map.connect '/:controller/:action/:id', :action => 'index', :id => nil
|
863
|
+
map.connect ':url', :controller => 'tiny_url', :action => 'translate'
|
760
864
|
end
|
865
|
+
end
|
761
866
|
|
762
|
-
|
763
|
-
|
867
|
+
def test_route_with_regexp_for_controller
|
868
|
+
rs.draw do |map|
|
869
|
+
map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/
|
870
|
+
map.connect ':controller/:action/:id'
|
764
871
|
end
|
872
|
+
assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
|
873
|
+
rs.recognize_path("/admin/user/foo"))
|
874
|
+
assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo"))
|
875
|
+
assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index")
|
876
|
+
assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo")
|
765
877
|
end
|
766
878
|
|
767
|
-
|
768
|
-
|
879
|
+
def test_route_with_regexp_and_captures_for_controller
|
880
|
+
rs.draw do |map|
|
881
|
+
map.connect ':controller/:action/:id', :controller => /admin\/(accounts|users)/
|
882
|
+
end
|
883
|
+
assert_equal({:controller => "admin/accounts", :action => "index"}, rs.recognize_path("/admin/accounts"))
|
884
|
+
assert_equal({:controller => "admin/users", :action => "index"}, rs.recognize_path("/admin/users"))
|
885
|
+
assert_raise(ActionController::RoutingError) { rs.recognize_path("/admin/products") }
|
886
|
+
end
|
769
887
|
|
770
|
-
|
771
|
-
|
772
|
-
|
888
|
+
def test_route_with_regexp_and_dot
|
889
|
+
rs.draw do |map|
|
890
|
+
map.connect ':controller/:action/:file',
|
891
|
+
:controller => /admin|user/,
|
892
|
+
:action => /upload|download/,
|
893
|
+
:defaults => {:file => nil},
|
894
|
+
:requirements => {:file => %r{[^/]+(\.[^/]+)?}}
|
895
|
+
end
|
896
|
+
# Without a file extension
|
897
|
+
assert_equal '/user/download/file',
|
898
|
+
rs.generate(:controller => "user", :action => "download", :file => "file")
|
899
|
+
assert_equal(
|
900
|
+
{:controller => "user", :action => "download", :file => "file"},
|
901
|
+
rs.recognize_path("/user/download/file"))
|
773
902
|
|
774
|
-
|
775
|
-
|
903
|
+
# Now, let's try a file with an extension, really a dot (.)
|
904
|
+
assert_equal '/user/download/file.jpg',
|
905
|
+
rs.generate(
|
906
|
+
:controller => "user", :action => "download", :file => "file.jpg")
|
907
|
+
assert_equal(
|
908
|
+
{:controller => "user", :action => "download", :file => "file.jpg"},
|
909
|
+
rs.recognize_path("/user/download/file.jpg"))
|
910
|
+
end
|
776
911
|
|
777
|
-
|
778
|
-
|
912
|
+
def test_basic_named_route
|
913
|
+
rs.add_named_route :home, '', :controller => 'content', :action => 'list'
|
914
|
+
x = setup_for_named_route
|
915
|
+
assert_equal("http://test.host/",
|
916
|
+
x.send(:home_url))
|
917
|
+
end
|
779
918
|
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
919
|
+
def test_basic_named_route_with_relative_url_root
|
920
|
+
rs.add_named_route :home, '', :controller => 'content', :action => 'list'
|
921
|
+
x = setup_for_named_route
|
922
|
+
ActionController::Base.relative_url_root = "/foo"
|
923
|
+
assert_equal("http://test.host/foo/",
|
924
|
+
x.send(:home_url))
|
925
|
+
assert_equal "/foo/", x.send(:home_path)
|
926
|
+
ActionController::Base.relative_url_root = nil
|
927
|
+
end
|
784
928
|
|
785
|
-
|
929
|
+
def test_named_route_with_option
|
930
|
+
rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page'
|
931
|
+
x = setup_for_named_route
|
932
|
+
assert_equal("http://test.host/page/new%20stuff",
|
933
|
+
x.send(:page_url, :title => 'new stuff'))
|
934
|
+
end
|
786
935
|
|
787
|
-
|
936
|
+
def test_named_route_with_default
|
937
|
+
rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
|
938
|
+
x = setup_for_named_route
|
939
|
+
assert_equal("http://test.host/page/AboutRails",
|
940
|
+
x.send(:page_url, :title => "AboutRails"))
|
788
941
|
|
789
|
-
|
790
|
-
assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
942
|
+
end
|
791
943
|
|
792
|
-
|
793
|
-
|
794
|
-
|
944
|
+
def test_named_route_with_name_prefix
|
945
|
+
rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :name_prefix => 'my_'
|
946
|
+
x = setup_for_named_route
|
947
|
+
assert_equal("http://test.host/page",
|
948
|
+
x.send(:my_page_url))
|
949
|
+
end
|
795
950
|
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
951
|
+
def test_named_route_with_path_prefix
|
952
|
+
rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => 'my'
|
953
|
+
x = setup_for_named_route
|
954
|
+
assert_equal("http://test.host/my/page",
|
955
|
+
x.send(:page_url))
|
956
|
+
end
|
800
957
|
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
video.resources :comments
|
808
|
-
video.resource :file, :controller => 'video_file'
|
809
|
-
video.resource :share, :controller => 'video_shares'
|
810
|
-
video.resource :abuse, :controller => 'video_abuses'
|
811
|
-
end
|
812
|
-
map.resources :abuses, :controller => 'video_abuses'
|
813
|
-
map.resources :video_uploads
|
814
|
-
map.resources :video_visits
|
958
|
+
def test_named_route_with_blank_path_prefix
|
959
|
+
rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => ''
|
960
|
+
x = setup_for_named_route
|
961
|
+
assert_equal("http://test.host/page",
|
962
|
+
x.send(:page_url))
|
963
|
+
end
|
815
964
|
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
end
|
823
|
-
map.resource :session
|
824
|
-
map.resource :lost_password
|
825
|
-
map.search 'search', :controller => 'search'
|
826
|
-
map.resources :pages
|
827
|
-
map.connect ':controller/:action/:id'
|
828
|
-
}
|
829
|
-
n = 1000
|
830
|
-
if RunTimeTests
|
831
|
-
GC.start
|
832
|
-
rectime = Benchmark.realtime do
|
833
|
-
n.times do
|
834
|
-
rs.recognize_path("/videos/1234567", {:method => :get})
|
835
|
-
rs.recognize_path("/videos/1234567/abuse", {:method => :get})
|
836
|
-
rs.recognize_path("/users/1234567/settings", {:method => :get})
|
837
|
-
rs.recognize_path("/channels/1234567", {:method => :get})
|
838
|
-
rs.recognize_path("/session/new", {:method => :get})
|
839
|
-
rs.recognize_path("/admin/user/show/10", {:method => :get})
|
840
|
-
end
|
841
|
-
end
|
842
|
-
puts "\n\nRecognition (#{rs.routes.size} routes):"
|
843
|
-
per_url = rectime / (n * 6)
|
844
|
-
puts "#{per_url * 1000} ms/url"
|
845
|
-
puts "#{1 / per_url} url/s\n\n"
|
846
|
-
end
|
847
|
-
end
|
965
|
+
def test_named_route_with_nested_controller
|
966
|
+
rs.add_named_route :users, 'admin/user', :controller => 'admin/user', :action => 'index'
|
967
|
+
x = setup_for_named_route
|
968
|
+
assert_equal("http://test.host/admin/user",
|
969
|
+
x.send(:users_url))
|
970
|
+
end
|
848
971
|
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
[{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}],
|
860
|
-
[{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}],
|
861
|
-
[{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}],
|
862
|
-
]
|
863
|
-
p = nil
|
864
|
-
gentime = Benchmark.realtime do
|
865
|
-
n.times do
|
866
|
-
pairs.each {|(a, b)| rs.generate(a, b)}
|
867
|
-
end
|
868
|
-
end
|
972
|
+
def test_optimised_named_route_call_never_uses_url_for
|
973
|
+
rs.add_named_route :users, 'admin/user', :controller => '/admin/user', :action => 'index'
|
974
|
+
rs.add_named_route :user, 'admin/user/:id', :controller=>'/admin/user', :action=>'show'
|
975
|
+
x = setup_for_named_route
|
976
|
+
x.expects(:url_for).never
|
977
|
+
x.send(:users_url)
|
978
|
+
x.send(:users_path)
|
979
|
+
x.send(:user_url, 2, :foo=>"bar")
|
980
|
+
x.send(:user_path, 3, :bar=>"foo")
|
981
|
+
end
|
869
982
|
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
983
|
+
def test_optimised_named_route_with_host
|
984
|
+
rs.add_named_route :pages, 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com'
|
985
|
+
x = setup_for_named_route
|
986
|
+
x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once
|
987
|
+
x.send(:pages_url)
|
988
|
+
end
|
876
989
|
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
990
|
+
def setup_for_named_route
|
991
|
+
klass = Class.new(MockController)
|
992
|
+
rs.install_helpers(klass)
|
993
|
+
klass.new(rs)
|
994
|
+
end
|
995
|
+
|
996
|
+
def test_named_route_without_hash
|
997
|
+
rs.draw do |map|
|
998
|
+
map.normal ':controller/:action/:id'
|
882
999
|
end
|
1000
|
+
end
|
883
1001
|
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
map.connect ':controller/:action/:id'
|
888
|
-
end
|
889
|
-
assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
|
890
|
-
rs.recognize_path("/admin/user/foo"))
|
891
|
-
assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo"))
|
892
|
-
assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index")
|
893
|
-
assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo")
|
1002
|
+
def test_named_route_root
|
1003
|
+
rs.draw do |map|
|
1004
|
+
map.root :controller => "hello"
|
894
1005
|
end
|
1006
|
+
x = setup_for_named_route
|
1007
|
+
assert_equal("http://test.host/", x.send(:root_url))
|
1008
|
+
assert_equal("/", x.send(:root_path))
|
1009
|
+
end
|
895
1010
|
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
assert_equal({:controller => "admin/users", :action => "index"}, rs.recognize_path("/admin/users"))
|
902
|
-
assert_raises(ActionController::RoutingError) { rs.recognize_path("/admin/products") }
|
1011
|
+
def test_named_route_with_regexps
|
1012
|
+
rs.draw do |map|
|
1013
|
+
map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
|
1014
|
+
:year => /\d+/, :month => /\d+/, :day => /\d+/
|
1015
|
+
map.connect ':controller/:action/:id'
|
903
1016
|
end
|
1017
|
+
x = setup_for_named_route
|
1018
|
+
# assert_equal(
|
1019
|
+
# {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false},
|
1020
|
+
# x.send(:article_url, :title => 'hi')
|
1021
|
+
# )
|
1022
|
+
assert_equal(
|
1023
|
+
"http://test.host/page/2005/6/10/hi",
|
1024
|
+
x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
|
1025
|
+
)
|
1026
|
+
end
|
904
1027
|
|
1028
|
+
def test_changing_controller
|
1029
|
+
@rs.draw {|m| m.connect ':controller/:action/:id' }
|
905
1030
|
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
:defaults => {:file => nil},
|
912
|
-
:requirements => {:file => %r{[^/]+(\.[^/]+)?}}
|
913
|
-
end
|
914
|
-
# Without a file extension
|
915
|
-
assert_equal '/user/download/file',
|
916
|
-
rs.generate(:controller => "user", :action => "download", :file => "file")
|
917
|
-
assert_equal(
|
918
|
-
{:controller => "user", :action => "download", :file => "file"},
|
919
|
-
rs.recognize_path("/user/download/file"))
|
1031
|
+
assert_equal '/admin/stuff/show/10', rs.generate(
|
1032
|
+
{:controller => 'stuff', :action => 'show', :id => 10},
|
1033
|
+
{:controller => 'admin/user', :action => 'index'}
|
1034
|
+
)
|
1035
|
+
end
|
920
1036
|
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
assert_equal(
|
926
|
-
{:controller => "user", :action => "download", :file => "file.jpg"},
|
927
|
-
rs.recognize_path("/user/download/file.jpg"))
|
1037
|
+
def test_paths_escaped
|
1038
|
+
rs.draw do |map|
|
1039
|
+
map.path 'file/*path', :controller => 'content', :action => 'show_file'
|
1040
|
+
map.connect ':controller/:action/:id'
|
928
1041
|
end
|
929
1042
|
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
x.send(:home_url))
|
935
|
-
end
|
1043
|
+
# No + to space in URI escaping, only for query params.
|
1044
|
+
results = rs.recognize_path "/file/hello+world/how+are+you%3F"
|
1045
|
+
assert results, "Recognition should have succeeded"
|
1046
|
+
assert_equal ['hello+world', 'how+are+you?'], results[:path]
|
936
1047
|
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
assert_equal("http://named.route.test/foo/",
|
942
|
-
x.send(:home_url))
|
943
|
-
assert_equal "/foo/", x.send(:home_path)
|
944
|
-
ActionController::Base.relative_url_root = nil
|
945
|
-
end
|
1048
|
+
# Use %20 for space instead.
|
1049
|
+
results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F"
|
1050
|
+
assert results, "Recognition should have succeeded"
|
1051
|
+
assert_equal ['hello world', 'how are you?'], results[:path]
|
946
1052
|
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
x.send(:page_url, :title => 'new stuff'))
|
952
|
-
end
|
1053
|
+
results = rs.recognize_path "/file"
|
1054
|
+
assert results, "Recognition should have succeeded"
|
1055
|
+
assert_equal [], results[:path]
|
1056
|
+
end
|
953
1057
|
|
954
|
-
|
955
|
-
|
956
|
-
x = setup_for_named_route
|
957
|
-
assert_equal("http://named.route.test/page/AboutRails",
|
958
|
-
x.send(:page_url, :title => "AboutRails"))
|
1058
|
+
def test_paths_slashes_unescaped_with_ordered_parameters
|
1059
|
+
rs.add_named_route :path, '/file/*path', :controller => 'content'
|
959
1060
|
|
960
|
-
|
1061
|
+
# No / to %2F in URI, only for query params.
|
1062
|
+
x = setup_for_named_route
|
1063
|
+
assert_equal("/file/hello/world", x.send(:path_path, 'hello/world'))
|
1064
|
+
end
|
961
1065
|
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
assert_equal("http://named.route.test/page",
|
966
|
-
x.send(:my_page_url))
|
1066
|
+
def test_non_controllers_cannot_be_matched
|
1067
|
+
rs.draw do |map|
|
1068
|
+
map.connect ':controller/:action/:id'
|
967
1069
|
end
|
1070
|
+
assert_raise(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") }
|
1071
|
+
end
|
968
1072
|
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
1073
|
+
def test_paths_do_not_accept_defaults
|
1074
|
+
assert_raise(ActionController::RoutingError) do
|
1075
|
+
rs.draw do |map|
|
1076
|
+
map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
|
1077
|
+
map.connect ':controller/:action/:id'
|
1078
|
+
end
|
974
1079
|
end
|
975
1080
|
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
assert_equal("http://named.route.test/admin/user",
|
980
|
-
x.send(:users_url))
|
1081
|
+
rs.draw do |map|
|
1082
|
+
map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => []
|
1083
|
+
map.connect ':controller/:action/:id'
|
981
1084
|
end
|
1085
|
+
end
|
982
1086
|
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
x = setup_for_named_route
|
987
|
-
x.expects(:url_for).never
|
988
|
-
x.send(:users_url)
|
989
|
-
x.send(:users_path)
|
990
|
-
x.send(:user_url, 2, :foo=>"bar")
|
991
|
-
x.send(:user_path, 3, :bar=>"foo")
|
1087
|
+
def test_should_list_options_diff_when_routing_requirements_dont_match
|
1088
|
+
rs.draw do |map|
|
1089
|
+
map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
|
992
1090
|
end
|
1091
|
+
exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") }
|
1092
|
+
assert_match /^post_url failed to generate/, exception.message
|
1093
|
+
from_match = exception.message.match(/from \{[^\}]+\}/).to_s
|
1094
|
+
assert_match /:bad_param=>"foo"/, from_match
|
1095
|
+
assert_match /:action=>"show"/, from_match
|
1096
|
+
assert_match /:controller=>"post"/, from_match
|
993
1097
|
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
x.send(:pages_url)
|
999
|
-
end
|
1098
|
+
expected_match = exception.message.match(/expected: \{[^\}]+\}/).to_s
|
1099
|
+
assert_no_match /:bad_param=>"foo"/, expected_match
|
1100
|
+
assert_match /:action=>"show"/, expected_match
|
1101
|
+
assert_match /:controller=>"post"/, expected_match
|
1000
1102
|
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1103
|
+
diff_match = exception.message.match(/diff: \{[^\}]+\}/).to_s
|
1104
|
+
assert_match /:bad_param=>"foo"/, diff_match
|
1105
|
+
assert_no_match /:action=>"show"/, diff_match
|
1106
|
+
assert_no_match /:controller=>"post"/, diff_match
|
1107
|
+
end
|
1006
1108
|
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1109
|
+
# this specifies the case where your formerly would get a very confusing error message with an empty diff
|
1110
|
+
def test_should_have_better_error_message_when_options_diff_is_empty
|
1111
|
+
rs.draw do |map|
|
1112
|
+
map.content '/content/:query', :controller => 'content', :action => 'show'
|
1011
1113
|
end
|
1012
1114
|
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
assert_equal("/", x.send(:root_path))
|
1020
|
-
end
|
1115
|
+
exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'content', :action => 'show', :use_route => "content") }
|
1116
|
+
assert_match %r[:action=>"show"], exception.message
|
1117
|
+
assert_match %r[:controller=>"content"], exception.message
|
1118
|
+
assert_match %r[you may have ambiguous routes, or you may need to supply additional parameters for this route], exception.message
|
1119
|
+
assert_match %r[content_url has the following required parameters: \["content", :query\] - are they all satisfied?], exception.message
|
1120
|
+
end
|
1021
1121
|
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
:year => /\d+/, :month => /\d+/, :day => /\d+/
|
1026
|
-
map.connect ':controller/:action/:id'
|
1027
|
-
end
|
1028
|
-
x = setup_for_named_route
|
1029
|
-
# assert_equal(
|
1030
|
-
# {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false},
|
1031
|
-
# x.send(:article_url, :title => 'hi')
|
1032
|
-
# )
|
1033
|
-
assert_equal(
|
1034
|
-
"http://named.route.test/page/2005/6/10/hi",
|
1035
|
-
x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
|
1036
|
-
)
|
1122
|
+
def test_dynamic_path_allowed
|
1123
|
+
rs.draw do |map|
|
1124
|
+
map.connect '*path', :controller => 'content', :action => 'show_file'
|
1037
1125
|
end
|
1038
1126
|
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1127
|
+
assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo))
|
1128
|
+
end
|
1129
|
+
|
1130
|
+
def test_dynamic_recall_paths_allowed
|
1131
|
+
rs.draw do |map|
|
1132
|
+
map.connect '*path', :controller => 'content', :action => 'show_file'
|
1044
1133
|
end
|
1045
1134
|
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
map.connect ':controller/:action/:id'
|
1050
|
-
end
|
1135
|
+
recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo))
|
1136
|
+
assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path)
|
1137
|
+
end
|
1051
1138
|
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1139
|
+
def test_backwards
|
1140
|
+
rs.draw do |map|
|
1141
|
+
map.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
|
1142
|
+
map.connect ':controller/:action/:id'
|
1143
|
+
end
|
1056
1144
|
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1145
|
+
assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'})
|
1146
|
+
assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show')
|
1147
|
+
assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo')
|
1148
|
+
end
|
1061
1149
|
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1150
|
+
def test_route_with_fixnum_default
|
1151
|
+
rs.draw do |map|
|
1152
|
+
map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
|
1153
|
+
map.connect ':controller/:action/:id'
|
1065
1154
|
end
|
1066
1155
|
|
1067
|
-
|
1068
|
-
|
1156
|
+
assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page')
|
1157
|
+
assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1)
|
1158
|
+
assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1')
|
1159
|
+
assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10)
|
1069
1160
|
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1161
|
+
assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page"))
|
1162
|
+
assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1"))
|
1163
|
+
assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10"))
|
1164
|
+
end
|
1074
1165
|
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1166
|
+
# For newer revision
|
1167
|
+
def test_route_with_text_default
|
1168
|
+
rs.draw do |map|
|
1169
|
+
map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
|
1170
|
+
map.connect ':controller/:action/:id'
|
1080
1171
|
end
|
1081
1172
|
|
1082
|
-
|
1083
|
-
|
1084
|
-
rs.draw do |map|
|
1085
|
-
map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
|
1086
|
-
map.connect ':controller/:action/:id'
|
1087
|
-
end
|
1088
|
-
end
|
1173
|
+
assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo')
|
1174
|
+
assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo"))
|
1089
1175
|
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
end
|
1094
|
-
end
|
1176
|
+
token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
|
1177
|
+
token.force_encoding("UTF-8") if token.respond_to?(:force_encoding)
|
1178
|
+
escaped_token = CGI::escape(token)
|
1095
1179
|
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
end
|
1100
|
-
exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") }
|
1101
|
-
assert_match /^post_url failed to generate/, exception.message
|
1102
|
-
from_match = exception.message.match(/from \{[^\}]+\}/).to_s
|
1103
|
-
assert_match /:bad_param=>"foo"/, from_match
|
1104
|
-
assert_match /:action=>"show"/, from_match
|
1105
|
-
assert_match /:controller=>"post"/, from_match
|
1106
|
-
|
1107
|
-
expected_match = exception.message.match(/expected: \{[^\}]+\}/).to_s
|
1108
|
-
assert_no_match /:bad_param=>"foo"/, expected_match
|
1109
|
-
assert_match /:action=>"show"/, expected_match
|
1110
|
-
assert_match /:controller=>"post"/, expected_match
|
1111
|
-
|
1112
|
-
diff_match = exception.message.match(/diff: \{[^\}]+\}/).to_s
|
1113
|
-
assert_match /:bad_param=>"foo"/, diff_match
|
1114
|
-
assert_no_match /:action=>"show"/, diff_match
|
1115
|
-
assert_no_match /:controller=>"post"/, diff_match
|
1116
|
-
end
|
1117
|
-
|
1118
|
-
# this specifies the case where your formerly would get a very confusing error message with an empty diff
|
1119
|
-
def test_should_have_better_error_message_when_options_diff_is_empty
|
1120
|
-
rs.draw do |map|
|
1121
|
-
map.content '/content/:query', :controller => 'content', :action => 'show'
|
1122
|
-
end
|
1180
|
+
assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token)
|
1181
|
+
assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}"))
|
1182
|
+
end
|
1123
1183
|
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
assert_match %r[content_url has the following required parameters: \["content", :query\] - are they all satisfied?], exception.message
|
1129
|
-
end
|
1184
|
+
def test_action_expiry
|
1185
|
+
@rs.draw {|m| m.connect ':controller/:action/:id' }
|
1186
|
+
assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
|
1187
|
+
end
|
1130
1188
|
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1189
|
+
def test_recognition_with_uppercase_controller_name
|
1190
|
+
@rs.draw {|m| m.connect ':controller/:action/:id' }
|
1191
|
+
assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/Content"))
|
1192
|
+
assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/ConTent/list"))
|
1193
|
+
assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/CONTENT/show/10"))
|
1194
|
+
|
1195
|
+
# these used to work, before the routes rewrite, but support for this was pulled in the new version...
|
1196
|
+
#assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/NewsFeed"))
|
1197
|
+
#assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/News_Feed"))
|
1198
|
+
end
|
1135
1199
|
|
1136
|
-
|
1200
|
+
def test_requirement_should_prevent_optional_id
|
1201
|
+
rs.draw do |map|
|
1202
|
+
map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
|
1137
1203
|
end
|
1138
1204
|
|
1139
|
-
|
1140
|
-
rs.draw do |map|
|
1141
|
-
map.connect '*path', :controller => 'content', :action => 'show_file'
|
1142
|
-
end
|
1205
|
+
assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10)
|
1143
1206
|
|
1144
|
-
|
1145
|
-
|
1207
|
+
assert_raise ActionController::RoutingError do
|
1208
|
+
rs.generate(:controller => 'post', :action => 'show')
|
1146
1209
|
end
|
1210
|
+
end
|
1147
1211
|
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
|
1152
|
-
|
1153
|
-
|
1154
|
-
|
1155
|
-
assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show')
|
1156
|
-
assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo')
|
1212
|
+
def test_both_requirement_and_optional
|
1213
|
+
rs.draw do |map|
|
1214
|
+
map.blog('test/:year', :controller => 'post', :action => 'show',
|
1215
|
+
:defaults => { :year => nil },
|
1216
|
+
:requirements => { :year => /\d{4}/ }
|
1217
|
+
)
|
1218
|
+
map.connect ':controller/:action/:id'
|
1157
1219
|
end
|
1158
1220
|
|
1159
|
-
|
1160
|
-
|
1161
|
-
map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
|
1162
|
-
map.connect ':controller/:action/:id'
|
1163
|
-
end
|
1221
|
+
assert_equal '/test', rs.generate(:controller => 'post', :action => 'show')
|
1222
|
+
assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil)
|
1164
1223
|
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1224
|
+
x = setup_for_named_route
|
1225
|
+
assert_equal("http://test.host/test",
|
1226
|
+
x.send(:blog_url))
|
1227
|
+
end
|
1169
1228
|
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1229
|
+
def test_set_to_nil_forgets
|
1230
|
+
rs.draw do |map|
|
1231
|
+
map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil
|
1232
|
+
map.connect ':controller/:action/:id'
|
1173
1233
|
end
|
1174
1234
|
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1235
|
+
assert_equal '/pages/2005',
|
1236
|
+
rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005)
|
1237
|
+
assert_equal '/pages/2005/6',
|
1238
|
+
rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
|
1239
|
+
assert_equal '/pages/2005/6/12',
|
1240
|
+
rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
|
1181
1241
|
|
1182
|
-
|
1183
|
-
|
1242
|
+
assert_equal '/pages/2005/6/4',
|
1243
|
+
rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
|
1184
1244
|
|
1185
|
-
|
1186
|
-
|
1187
|
-
escaped_token = CGI::escape(token)
|
1245
|
+
assert_equal '/pages/2005/6',
|
1246
|
+
rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
|
1188
1247
|
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1248
|
+
assert_equal '/pages/2005',
|
1249
|
+
rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
|
1250
|
+
end
|
1192
1251
|
|
1193
|
-
|
1194
|
-
|
1252
|
+
def test_url_with_no_action_specified
|
1253
|
+
rs.draw do |map|
|
1254
|
+
map.connect '', :controller => 'content'
|
1255
|
+
map.connect ':controller/:action/:id'
|
1195
1256
|
end
|
1196
1257
|
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/CONTENT/show/10"))
|
1258
|
+
assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
|
1259
|
+
assert_equal '/', rs.generate(:controller => 'content')
|
1260
|
+
end
|
1201
1261
|
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1262
|
+
def test_named_url_with_no_action_specified
|
1263
|
+
rs.draw do |map|
|
1264
|
+
map.home '', :controller => 'content'
|
1265
|
+
map.connect ':controller/:action/:id'
|
1205
1266
|
end
|
1206
1267
|
|
1207
|
-
|
1208
|
-
|
1209
|
-
map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
|
1210
|
-
end
|
1211
|
-
|
1212
|
-
assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10)
|
1268
|
+
assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
|
1269
|
+
assert_equal '/', rs.generate(:controller => 'content')
|
1213
1270
|
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1271
|
+
x = setup_for_named_route
|
1272
|
+
assert_equal("http://test.host/",
|
1273
|
+
x.send(:home_url))
|
1274
|
+
end
|
1218
1275
|
|
1219
|
-
|
1276
|
+
def test_url_generated_when_forgetting_action
|
1277
|
+
[{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash|
|
1220
1278
|
rs.draw do |map|
|
1221
|
-
map.
|
1222
|
-
:defaults => { :year => nil },
|
1223
|
-
:requirements => { :year => /\d{4}/ }
|
1224
|
-
)
|
1279
|
+
map.home '', hash
|
1225
1280
|
map.connect ':controller/:action/:id'
|
1226
1281
|
end
|
1282
|
+
assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'})
|
1283
|
+
assert_equal '/', rs.generate({:controller => 'content'})
|
1284
|
+
assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
|
1285
|
+
end
|
1286
|
+
end
|
1227
1287
|
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
assert_equal("http://named.route.test/test",
|
1233
|
-
x.send(:blog_url))
|
1288
|
+
def test_named_route_method
|
1289
|
+
rs.draw do |map|
|
1290
|
+
map.categories 'categories', :controller => 'content', :action => 'categories'
|
1291
|
+
map.connect ':controller/:action/:id'
|
1234
1292
|
end
|
1235
1293
|
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
map.connect ':controller/:action/:id'
|
1240
|
-
end
|
1294
|
+
assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories')
|
1295
|
+
assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
|
1296
|
+
end
|
1241
1297
|
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1298
|
+
def test_named_routes_array
|
1299
|
+
test_named_route_method
|
1300
|
+
assert_equal [:categories], rs.named_routes.names
|
1301
|
+
end
|
1302
|
+
|
1303
|
+
def test_nil_defaults
|
1304
|
+
rs.draw do |map|
|
1305
|
+
map.connect 'journal',
|
1306
|
+
:controller => 'content',
|
1307
|
+
:action => 'list_journal',
|
1308
|
+
:date => nil, :user_id => nil
|
1309
|
+
map.connect ':controller/:action/:id'
|
1310
|
+
end
|
1248
1311
|
|
1249
|
-
|
1250
|
-
|
1312
|
+
assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
|
1313
|
+
end
|
1251
1314
|
|
1252
|
-
|
1253
|
-
|
1315
|
+
def setup_request_method_routes_for(method)
|
1316
|
+
@request = ActionController::TestRequest.new
|
1317
|
+
@request.env["REQUEST_METHOD"] = method
|
1318
|
+
@request.request_uri = "/match"
|
1254
1319
|
|
1255
|
-
|
1256
|
-
|
1320
|
+
rs.draw do |r|
|
1321
|
+
r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get }
|
1322
|
+
r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post }
|
1323
|
+
r.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put }
|
1324
|
+
r.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete }
|
1257
1325
|
end
|
1326
|
+
end
|
1258
1327
|
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1328
|
+
%w(GET POST PUT DELETE).each do |request_method|
|
1329
|
+
define_method("test_request_method_recognized_with_#{request_method}") do
|
1330
|
+
begin
|
1331
|
+
Object.const_set(:BooksController, Class.new(ActionController::Base))
|
1332
|
+
|
1333
|
+
setup_request_method_routes_for(request_method)
|
1334
|
+
|
1335
|
+
assert_nothing_raised { rs.recognize(@request) }
|
1336
|
+
assert_equal request_method.downcase, @request.path_parameters[:action]
|
1337
|
+
ensure
|
1338
|
+
Object.send(:remove_const, :BooksController) rescue nil
|
1263
1339
|
end
|
1340
|
+
end
|
1341
|
+
end
|
1264
1342
|
|
1265
|
-
|
1266
|
-
|
1343
|
+
def test_recognize_array_of_methods
|
1344
|
+
Object.const_set(:BooksController, Class.new(ActionController::Base))
|
1345
|
+
rs.draw do |r|
|
1346
|
+
r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
|
1347
|
+
r.connect '/match', :controller => 'books', :action => 'not_get_or_post'
|
1267
1348
|
end
|
1268
1349
|
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1350
|
+
@request = ActionController::TestRequest.new
|
1351
|
+
@request.env["REQUEST_METHOD"] = 'POST'
|
1352
|
+
@request.request_uri = "/match"
|
1353
|
+
assert_nothing_raised { rs.recognize(@request) }
|
1354
|
+
assert_equal 'get_or_post', @request.path_parameters[:action]
|
1355
|
+
|
1356
|
+
# have to recreate or else the RouteSet uses a cached version:
|
1357
|
+
@request = ActionController::TestRequest.new
|
1358
|
+
@request.env["REQUEST_METHOD"] = 'PUT'
|
1359
|
+
@request.request_uri = "/match"
|
1360
|
+
assert_nothing_raised { rs.recognize(@request) }
|
1361
|
+
assert_equal 'not_get_or_post', @request.path_parameters[:action]
|
1362
|
+
ensure
|
1363
|
+
Object.send(:remove_const, :BooksController) rescue nil
|
1364
|
+
end
|
1274
1365
|
|
1275
|
-
|
1276
|
-
|
1366
|
+
def test_subpath_recognized
|
1367
|
+
Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
|
1277
1368
|
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1369
|
+
rs.draw do |r|
|
1370
|
+
r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
|
1371
|
+
r.connect '/items/:id/:action', :controller => 'subpath_books'
|
1372
|
+
r.connect '/posts/new/:action', :controller => 'subpath_books'
|
1373
|
+
r.connect '/posts/:id', :controller => 'subpath_books', :action => "show"
|
1281
1374
|
end
|
1282
1375
|
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
map.home '', hash
|
1287
|
-
map.connect ':controller/:action/:id'
|
1288
|
-
end
|
1289
|
-
assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'})
|
1290
|
-
assert_equal '/', rs.generate({:controller => 'content'})
|
1291
|
-
assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
|
1292
|
-
end
|
1293
|
-
end
|
1376
|
+
hash = rs.recognize_path "/books/17/edit"
|
1377
|
+
assert_not_nil hash
|
1378
|
+
assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]]
|
1294
1379
|
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
map.connect ':controller/:action/:id'
|
1299
|
-
end
|
1380
|
+
hash = rs.recognize_path "/items/3/complete"
|
1381
|
+
assert_not_nil hash
|
1382
|
+
assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]]
|
1300
1383
|
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1384
|
+
hash = rs.recognize_path "/posts/new/preview"
|
1385
|
+
assert_not_nil hash
|
1386
|
+
assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]]
|
1387
|
+
|
1388
|
+
hash = rs.recognize_path "/posts/7"
|
1389
|
+
assert_not_nil hash
|
1390
|
+
assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]]
|
1391
|
+
ensure
|
1392
|
+
Object.send(:remove_const, :SubpathBooksController) rescue nil
|
1393
|
+
end
|
1394
|
+
|
1395
|
+
def test_subpath_generated
|
1396
|
+
Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
|
1304
1397
|
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1398
|
+
rs.draw do |r|
|
1399
|
+
r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
|
1400
|
+
r.connect '/items/:id/:action', :controller => 'subpath_books'
|
1401
|
+
r.connect '/posts/new/:action', :controller => 'subpath_books'
|
1308
1402
|
end
|
1309
1403
|
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
map.connect ':controller/:action/:id'
|
1317
|
-
end
|
1404
|
+
assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit")
|
1405
|
+
assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete")
|
1406
|
+
assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview")
|
1407
|
+
ensure
|
1408
|
+
Object.send(:remove_const, :SubpathBooksController) rescue nil
|
1409
|
+
end
|
1318
1410
|
|
1319
|
-
|
1411
|
+
def test_failed_requirements_raises_exception_with_violated_requirements
|
1412
|
+
rs.draw do |r|
|
1413
|
+
r.foo_with_requirement 'foos/:id', :controller=>'foos', :requirements=>{:id=>/\d+/}
|
1320
1414
|
end
|
1321
1415
|
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1325
|
-
|
1416
|
+
x = setup_for_named_route
|
1417
|
+
assert_raise(ActionController::RoutingError) do
|
1418
|
+
x.send(:foo_with_requirement_url, "I am Against the requirements")
|
1419
|
+
end
|
1420
|
+
end
|
1326
1421
|
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1422
|
+
def test_routes_changed_correctly_after_clear
|
1423
|
+
ActionController::Base.optimise_named_routes = true
|
1424
|
+
rs = ::ActionController::Routing::RouteSet.new
|
1425
|
+
rs.draw do |r|
|
1426
|
+
r.connect 'ca', :controller => 'ca', :action => "aa"
|
1427
|
+
r.connect 'cb', :controller => 'cb', :action => "ab"
|
1428
|
+
r.connect 'cc', :controller => 'cc', :action => "ac"
|
1429
|
+
r.connect ':controller/:action/:id'
|
1430
|
+
r.connect ':controller/:action/:id.:format'
|
1333
1431
|
end
|
1334
1432
|
|
1335
|
-
|
1336
|
-
define_method("test_request_method_recognized_with_#{request_method}") do
|
1337
|
-
begin
|
1338
|
-
Object.const_set(:BooksController, Class.new(ActionController::Base))
|
1433
|
+
hash = rs.recognize_path "/cc"
|
1339
1434
|
|
1340
|
-
|
1435
|
+
assert_not_nil hash
|
1436
|
+
assert_equal %w(cc ac), [hash[:controller], hash[:action]]
|
1341
1437
|
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
end
|
1438
|
+
rs.draw do |r|
|
1439
|
+
r.connect 'cb', :controller => 'cb', :action => "ab"
|
1440
|
+
r.connect 'cc', :controller => 'cc', :action => "ac"
|
1441
|
+
r.connect ':controller/:action/:id'
|
1442
|
+
r.connect ':controller/:action/:id.:format'
|
1348
1443
|
end
|
1349
1444
|
|
1350
|
-
|
1351
|
-
Object.const_set(:BooksController, Class.new(ActionController::Base))
|
1352
|
-
rs.draw do |r|
|
1353
|
-
r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
|
1354
|
-
r.connect '/match', :controller => 'books', :action => 'not_get_or_post'
|
1355
|
-
end
|
1445
|
+
hash = rs.recognize_path "/cc"
|
1356
1446
|
|
1357
|
-
|
1358
|
-
|
1359
|
-
@request.request_uri = "/match"
|
1360
|
-
assert_nothing_raised { rs.recognize(@request) }
|
1361
|
-
assert_equal 'get_or_post', @request.path_parameters[:action]
|
1362
|
-
|
1363
|
-
# have to recreate or else the RouteSet uses a cached version:
|
1364
|
-
@request = ActionController::TestRequest.new
|
1365
|
-
@request.env["REQUEST_METHOD"] = 'PUT'
|
1366
|
-
@request.request_uri = "/match"
|
1367
|
-
assert_nothing_raised { rs.recognize(@request) }
|
1368
|
-
assert_equal 'not_get_or_post', @request.path_parameters[:action]
|
1369
|
-
ensure
|
1370
|
-
Object.send(:remove_const, :BooksController) rescue nil
|
1371
|
-
end
|
1372
|
-
|
1373
|
-
def test_subpath_recognized
|
1374
|
-
Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
|
1375
|
-
|
1376
|
-
rs.draw do |r|
|
1377
|
-
r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
|
1378
|
-
r.connect '/items/:id/:action', :controller => 'subpath_books'
|
1379
|
-
r.connect '/posts/new/:action', :controller => 'subpath_books'
|
1380
|
-
r.connect '/posts/:id', :controller => 'subpath_books', :action => "show"
|
1381
|
-
end
|
1447
|
+
assert_not_nil hash
|
1448
|
+
assert_equal %w(cc ac), [hash[:controller], hash[:action]]
|
1382
1449
|
|
1383
|
-
|
1384
|
-
|
1385
|
-
assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]]
|
1450
|
+
end
|
1451
|
+
end
|
1386
1452
|
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1453
|
+
class RouteTest < Test::Unit::TestCase
|
1454
|
+
def setup
|
1455
|
+
@route = ROUTING::Route.new
|
1456
|
+
end
|
1390
1457
|
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1458
|
+
def slash_segment(is_optional = false)
|
1459
|
+
ROUTING::DividerSegment.new('/', :optional => is_optional)
|
1460
|
+
end
|
1394
1461
|
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1462
|
+
def default_route
|
1463
|
+
unless defined?(@default_route)
|
1464
|
+
segments = []
|
1465
|
+
segments << ROUTING::StaticSegment.new('/', :raw => true)
|
1466
|
+
segments << ROUTING::DynamicSegment.new(:controller)
|
1467
|
+
segments << slash_segment(:optional)
|
1468
|
+
segments << ROUTING::DynamicSegment.new(:action, :default => 'index', :optional => true)
|
1469
|
+
segments << slash_segment(:optional)
|
1470
|
+
segments << ROUTING::DynamicSegment.new(:id, :optional => true)
|
1471
|
+
segments << slash_segment(:optional)
|
1472
|
+
@default_route = ROUTING::Route.new(segments).freeze
|
1400
1473
|
end
|
1474
|
+
@default_route
|
1475
|
+
end
|
1401
1476
|
|
1402
|
-
|
1403
|
-
|
1477
|
+
def test_default_route_recognition
|
1478
|
+
expected = {:controller => 'accounts', :action => 'show', :id => '10'}
|
1479
|
+
assert_equal expected, default_route.recognize('/accounts/show/10')
|
1480
|
+
assert_equal expected, default_route.recognize('/accounts/show/10/')
|
1404
1481
|
|
1405
|
-
|
1406
|
-
|
1407
|
-
r.connect '/items/:id/:action', :controller => 'subpath_books'
|
1408
|
-
r.connect '/posts/new/:action', :controller => 'subpath_books'
|
1409
|
-
end
|
1482
|
+
expected[:id] = 'jamis'
|
1483
|
+
assert_equal expected, default_route.recognize('/accounts/show/jamis/')
|
1410
1484
|
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
ensure
|
1415
|
-
Object.send(:remove_const, :SubpathBooksController) rescue nil
|
1416
|
-
end
|
1485
|
+
expected.delete :id
|
1486
|
+
assert_equal expected, default_route.recognize('/accounts/show')
|
1487
|
+
assert_equal expected, default_route.recognize('/accounts/show/')
|
1417
1488
|
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
end
|
1489
|
+
expected[:action] = 'index'
|
1490
|
+
assert_equal expected, default_route.recognize('/accounts/')
|
1491
|
+
assert_equal expected, default_route.recognize('/accounts')
|
1422
1492
|
|
1423
|
-
|
1424
|
-
|
1425
|
-
|
1426
|
-
end
|
1427
|
-
end
|
1493
|
+
assert_equal nil, default_route.recognize('/')
|
1494
|
+
assert_equal nil, default_route.recognize('/accounts/how/goood/it/is/to/be/free')
|
1495
|
+
end
|
1428
1496
|
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
r.connect 'ca', :controller => 'ca', :action => "aa"
|
1434
|
-
r.connect 'cb', :controller => 'cb', :action => "ab"
|
1435
|
-
r.connect 'cc', :controller => 'cc', :action => "ac"
|
1436
|
-
r.connect ':controller/:action/:id'
|
1437
|
-
r.connect ':controller/:action/:id.:format'
|
1438
|
-
end
|
1497
|
+
def test_default_route_should_omit_default_action
|
1498
|
+
o = {:controller => 'accounts', :action => 'index'}
|
1499
|
+
assert_equal '/accounts', default_route.generate(o, o, {})
|
1500
|
+
end
|
1439
1501
|
|
1440
|
-
|
1502
|
+
def test_default_route_should_include_default_action_when_id_present
|
1503
|
+
o = {:controller => 'accounts', :action => 'index', :id => '20'}
|
1504
|
+
assert_equal '/accounts/index/20', default_route.generate(o, o, {})
|
1505
|
+
end
|
1441
1506
|
|
1442
|
-
|
1443
|
-
|
1507
|
+
def test_default_route_should_work_with_action_but_no_id
|
1508
|
+
o = {:controller => 'accounts', :action => 'list_all'}
|
1509
|
+
assert_equal '/accounts/list_all', default_route.generate(o, o, {})
|
1510
|
+
end
|
1444
1511
|
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
end
|
1512
|
+
def test_default_route_should_uri_escape_pluses
|
1513
|
+
expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' }
|
1514
|
+
assert_equal expected, default_route.recognize('/accounts/show/hello world')
|
1515
|
+
assert_equal expected, default_route.recognize('/accounts/show/hello%20world')
|
1516
|
+
assert_equal '/accounts/show/hello%20world', default_route.generate(expected, expected, {})
|
1451
1517
|
|
1452
|
-
|
1518
|
+
expected[:id] = 'hello+world'
|
1519
|
+
assert_equal expected, default_route.recognize('/accounts/show/hello+world')
|
1520
|
+
assert_equal expected, default_route.recognize('/accounts/show/hello%2Bworld')
|
1521
|
+
assert_equal '/accounts/show/hello+world', default_route.generate(expected, expected, {})
|
1522
|
+
end
|
1453
1523
|
|
1454
|
-
|
1455
|
-
|
1524
|
+
def test_matches_controller_and_action
|
1525
|
+
# requirement_for should only be called for the action and controller _once_
|
1526
|
+
@route.expects(:requirement_for).with(:controller).times(1).returns('pages')
|
1527
|
+
@route.expects(:requirement_for).with(:action).times(1).returns('show')
|
1456
1528
|
|
1457
|
-
|
1529
|
+
@route.requirements = {:controller => 'pages', :action => 'show'}
|
1530
|
+
assert @route.matches_controller_and_action?('pages', 'show')
|
1531
|
+
assert !@route.matches_controller_and_action?('not_pages', 'show')
|
1532
|
+
assert !@route.matches_controller_and_action?('pages', 'not_show')
|
1458
1533
|
end
|
1459
1534
|
|
1460
|
-
|
1461
|
-
|
1462
|
-
|
1463
|
-
|
1535
|
+
def test_parameter_shell
|
1536
|
+
page_url = ROUTING::Route.new
|
1537
|
+
page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/}
|
1538
|
+
assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell)
|
1539
|
+
end
|
1464
1540
|
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1541
|
+
def test_defaults
|
1542
|
+
route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html"
|
1543
|
+
assert_equal(
|
1544
|
+
{ :controller => "users", :action => "show", :format => "html" },
|
1545
|
+
route.defaults)
|
1546
|
+
end
|
1468
1547
|
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
segments << ROUTING::StaticSegment.new('/', :raw => true)
|
1473
|
-
segments << ROUTING::DynamicSegment.new(:controller)
|
1474
|
-
segments << slash_segment(:optional)
|
1475
|
-
segments << ROUTING::DynamicSegment.new(:action, :default => 'index', :optional => true)
|
1476
|
-
segments << slash_segment(:optional)
|
1477
|
-
segments << ROUTING::DynamicSegment.new(:id, :optional => true)
|
1478
|
-
segments << slash_segment(:optional)
|
1479
|
-
@default_route = ROUTING::Route.new(segments).freeze
|
1480
|
-
end
|
1481
|
-
@default_route
|
1548
|
+
def test_builder_complains_without_controller
|
1549
|
+
assert_raise(ArgumentError) do
|
1550
|
+
ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index"
|
1482
1551
|
end
|
1552
|
+
end
|
1483
1553
|
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
expected[:id] = 'jamis'
|
1490
|
-
assert_equal expected, default_route.recognize('/accounts/show/jamis/')
|
1491
|
-
|
1492
|
-
expected.delete :id
|
1493
|
-
assert_equal expected, default_route.recognize('/accounts/show')
|
1494
|
-
assert_equal expected, default_route.recognize('/accounts/show/')
|
1495
|
-
|
1496
|
-
expected[:action] = 'index'
|
1497
|
-
assert_equal expected, default_route.recognize('/accounts/')
|
1498
|
-
assert_equal expected, default_route.recognize('/accounts')
|
1554
|
+
def test_significant_keys_for_default_route
|
1555
|
+
keys = default_route.significant_keys.sort_by {|k| k.to_s }
|
1556
|
+
assert_equal [:action, :controller, :id], keys
|
1557
|
+
end
|
1499
1558
|
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1559
|
+
def test_significant_keys
|
1560
|
+
segments = []
|
1561
|
+
segments << ROUTING::StaticSegment.new('/', :raw => true)
|
1562
|
+
segments << ROUTING::StaticSegment.new('user')
|
1563
|
+
segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
|
1564
|
+
segments << ROUTING::DynamicSegment.new(:user)
|
1565
|
+
segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
|
1503
1566
|
|
1504
|
-
|
1505
|
-
o = {:controller => 'accounts', :action => 'index'}
|
1506
|
-
assert_equal '/accounts', default_route.generate(o, o, {})
|
1507
|
-
end
|
1567
|
+
requirements = {:controller => 'users', :action => 'show'}
|
1508
1568
|
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
1569
|
+
user_url = ROUTING::Route.new(segments, requirements)
|
1570
|
+
keys = user_url.significant_keys.sort_by { |k| k.to_s }
|
1571
|
+
assert_equal [:action, :controller, :user], keys
|
1572
|
+
end
|
1513
1573
|
|
1514
|
-
|
1515
|
-
|
1516
|
-
|
1517
|
-
end
|
1574
|
+
def test_build_empty_query_string
|
1575
|
+
assert_equal '', @route.build_query_string({})
|
1576
|
+
end
|
1518
1577
|
|
1519
|
-
|
1520
|
-
|
1521
|
-
|
1522
|
-
assert_equal expected, default_route.recognize('/accounts/show/hello%20world')
|
1523
|
-
assert_equal '/accounts/show/hello%20world', default_route.generate(expected, expected, {})
|
1578
|
+
def test_build_query_string_with_nil_value
|
1579
|
+
assert_equal '', @route.build_query_string({:x => nil})
|
1580
|
+
end
|
1524
1581
|
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
assert_equal '/accounts/show/hello+world', default_route.generate(expected, expected, {})
|
1529
|
-
end
|
1582
|
+
def test_simple_build_query_string
|
1583
|
+
assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => '1', :y => '2'))
|
1584
|
+
end
|
1530
1585
|
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
@route.expects(:requirement_for).with(:action).times(1).returns('show')
|
1586
|
+
def test_convert_ints_build_query_string
|
1587
|
+
assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => 1, :y => 2))
|
1588
|
+
end
|
1535
1589
|
|
1536
|
-
|
1537
|
-
|
1538
|
-
|
1539
|
-
assert !@route.matches_controller_and_action?('pages', 'not_show')
|
1540
|
-
end
|
1590
|
+
def test_escape_spaces_build_query_string
|
1591
|
+
assert_equal '?x=hello+world&y=goodbye+world', order_query_string(@route.build_query_string(:x => 'hello world', :y => 'goodbye world'))
|
1592
|
+
end
|
1541
1593
|
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell)
|
1546
|
-
end
|
1594
|
+
def test_expand_array_build_query_string
|
1595
|
+
assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2]))
|
1596
|
+
end
|
1547
1597
|
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
{ :controller => "users", :action => "show", :format => "html" },
|
1552
|
-
route.defaults)
|
1553
|
-
end
|
1598
|
+
def test_escape_spaces_build_query_string_selected_keys
|
1599
|
+
assert_equal '?x=hello+world', order_query_string(@route.build_query_string({:x => 'hello world', :y => 'goodbye world'}, [:x]))
|
1600
|
+
end
|
1554
1601
|
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
end
|
1602
|
+
private
|
1603
|
+
def order_query_string(qs)
|
1604
|
+
'?' + qs[1..-1].split('&').sort.join('&')
|
1559
1605
|
end
|
1606
|
+
end
|
1560
1607
|
|
1561
|
-
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1608
|
+
class RouteSetTest < Test::Unit::TestCase
|
1609
|
+
def set
|
1610
|
+
@set ||= ROUTING::RouteSet.new
|
1611
|
+
end
|
1565
1612
|
|
1566
|
-
|
1567
|
-
|
1568
|
-
|
1569
|
-
segments << ROUTING::StaticSegment.new('user')
|
1570
|
-
segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
|
1571
|
-
segments << ROUTING::DynamicSegment.new(:user)
|
1572
|
-
segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
|
1613
|
+
def request
|
1614
|
+
@request ||= ActionController::TestRequest.new
|
1615
|
+
end
|
1573
1616
|
|
1574
|
-
|
1617
|
+
def test_generate_extras
|
1618
|
+
set.draw { |m| m.connect ':controller/:action/:id' }
|
1619
|
+
path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
|
1620
|
+
assert_equal "/foo/bar/15", path
|
1621
|
+
assert_equal %w(that this), extras.map(&:to_s).sort
|
1622
|
+
end
|
1575
1623
|
|
1576
|
-
|
1577
|
-
|
1578
|
-
|
1579
|
-
|
1624
|
+
def test_extra_keys
|
1625
|
+
set.draw { |m| m.connect ':controller/:action/:id' }
|
1626
|
+
extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
|
1627
|
+
assert_equal %w(that this), extras.map(&:to_s).sort
|
1628
|
+
end
|
1580
1629
|
|
1581
|
-
|
1582
|
-
|
1630
|
+
def test_generate_extras_not_first
|
1631
|
+
set.draw do |map|
|
1632
|
+
map.connect ':controller/:action/:id.:format'
|
1633
|
+
map.connect ':controller/:action/:id'
|
1583
1634
|
end
|
1635
|
+
path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
|
1636
|
+
assert_equal "/foo/bar/15", path
|
1637
|
+
assert_equal %w(that this), extras.map(&:to_s).sort
|
1638
|
+
end
|
1584
1639
|
|
1585
|
-
|
1586
|
-
|
1640
|
+
def test_generate_not_first
|
1641
|
+
set.draw do |map|
|
1642
|
+
map.connect ':controller/:action/:id.:format'
|
1643
|
+
map.connect ':controller/:action/:id'
|
1587
1644
|
end
|
1645
|
+
assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello")
|
1646
|
+
end
|
1588
1647
|
|
1589
|
-
|
1590
|
-
|
1648
|
+
def test_extra_keys_not_first
|
1649
|
+
set.draw do |map|
|
1650
|
+
map.connect ':controller/:action/:id.:format'
|
1651
|
+
map.connect ':controller/:action/:id'
|
1591
1652
|
end
|
1653
|
+
extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
|
1654
|
+
assert_equal %w(that this), extras.map(&:to_s).sort
|
1655
|
+
end
|
1592
1656
|
|
1593
|
-
|
1594
|
-
|
1657
|
+
def test_draw
|
1658
|
+
assert_equal 0, set.routes.size
|
1659
|
+
set.draw do |map|
|
1660
|
+
map.connect '/hello/world', :controller => 'a', :action => 'b'
|
1595
1661
|
end
|
1662
|
+
assert_equal 1, set.routes.size
|
1663
|
+
end
|
1596
1664
|
|
1597
|
-
|
1598
|
-
|
1665
|
+
def test_named_draw
|
1666
|
+
assert_equal 0, set.routes.size
|
1667
|
+
set.draw do |map|
|
1668
|
+
map.hello '/hello/world', :controller => 'a', :action => 'b'
|
1599
1669
|
end
|
1670
|
+
assert_equal 1, set.routes.size
|
1671
|
+
assert_equal set.routes.first, set.named_routes[:hello]
|
1672
|
+
end
|
1600
1673
|
|
1601
|
-
|
1602
|
-
|
1674
|
+
def test_later_named_routes_take_precedence
|
1675
|
+
set.draw do |map|
|
1676
|
+
map.hello '/hello/world', :controller => 'a', :action => 'b'
|
1677
|
+
map.hello '/hello', :controller => 'a', :action => 'b'
|
1603
1678
|
end
|
1679
|
+
assert_equal set.routes.last, set.named_routes[:hello]
|
1680
|
+
end
|
1604
1681
|
|
1605
|
-
|
1606
|
-
|
1682
|
+
def setup_named_route_test
|
1683
|
+
set.draw do |map|
|
1684
|
+
map.show '/people/:id', :controller => 'people', :action => 'show'
|
1685
|
+
map.index '/people', :controller => 'people', :action => 'index'
|
1686
|
+
map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi'
|
1687
|
+
map.users '/admin/users', :controller => 'admin/users', :action => 'index'
|
1607
1688
|
end
|
1608
1689
|
|
1609
|
-
|
1610
|
-
|
1611
|
-
|
1612
|
-
end
|
1690
|
+
klass = Class.new(MockController)
|
1691
|
+
set.install_helpers(klass)
|
1692
|
+
klass.new(set)
|
1613
1693
|
end
|
1614
1694
|
|
1615
|
-
|
1616
|
-
|
1617
|
-
@set ||= ROUTING::RouteSet.new
|
1618
|
-
end
|
1695
|
+
def test_named_route_hash_access_method
|
1696
|
+
controller = setup_named_route_test
|
1619
1697
|
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1698
|
+
assert_equal(
|
1699
|
+
{ :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false },
|
1700
|
+
controller.send(:hash_for_show_url, :id => 5))
|
1623
1701
|
|
1624
|
-
|
1625
|
-
|
1626
|
-
|
1627
|
-
assert_equal "/foo/bar/15", path
|
1628
|
-
assert_equal %w(that this), extras.map(&:to_s).sort
|
1629
|
-
end
|
1702
|
+
assert_equal(
|
1703
|
+
{ :controller => 'people', :action => 'index', :use_route => :index, :only_path => false },
|
1704
|
+
controller.send(:hash_for_index_url))
|
1630
1705
|
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1706
|
+
assert_equal(
|
1707
|
+
{ :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true },
|
1708
|
+
controller.send(:hash_for_show_path, :id => 5)
|
1709
|
+
)
|
1710
|
+
end
|
1636
1711
|
|
1637
|
-
|
1638
|
-
|
1639
|
-
map.connect ':controller/:action/:id.:format'
|
1640
|
-
map.connect ':controller/:action/:id'
|
1641
|
-
end
|
1642
|
-
path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
|
1643
|
-
assert_equal "/foo/bar/15", path
|
1644
|
-
assert_equal %w(that this), extras.map(&:to_s).sort
|
1645
|
-
end
|
1646
|
-
|
1647
|
-
def test_generate_not_first
|
1648
|
-
set.draw do |map|
|
1649
|
-
map.connect ':controller/:action/:id.:format'
|
1650
|
-
map.connect ':controller/:action/:id'
|
1651
|
-
end
|
1652
|
-
assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello")
|
1653
|
-
end
|
1654
|
-
|
1655
|
-
def test_extra_keys_not_first
|
1656
|
-
set.draw do |map|
|
1657
|
-
map.connect ':controller/:action/:id.:format'
|
1658
|
-
map.connect ':controller/:action/:id'
|
1659
|
-
end
|
1660
|
-
extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
|
1661
|
-
assert_equal %w(that this), extras.map(&:to_s).sort
|
1662
|
-
end
|
1663
|
-
|
1664
|
-
def test_draw
|
1665
|
-
assert_equal 0, set.routes.size
|
1666
|
-
set.draw do |map|
|
1667
|
-
map.connect '/hello/world', :controller => 'a', :action => 'b'
|
1668
|
-
end
|
1669
|
-
assert_equal 1, set.routes.size
|
1670
|
-
end
|
1712
|
+
def test_named_route_url_method
|
1713
|
+
controller = setup_named_route_test
|
1671
1714
|
|
1672
|
-
|
1673
|
-
|
1674
|
-
set.draw do |map|
|
1675
|
-
map.hello '/hello/world', :controller => 'a', :action => 'b'
|
1676
|
-
end
|
1677
|
-
assert_equal 1, set.routes.size
|
1678
|
-
assert_equal set.routes.first, set.named_routes[:hello]
|
1679
|
-
end
|
1715
|
+
assert_equal "http://test.host/people/5", controller.send(:show_url, :id => 5)
|
1716
|
+
assert_equal "/people/5", controller.send(:show_path, :id => 5)
|
1680
1717
|
|
1681
|
-
|
1682
|
-
|
1683
|
-
map.hello '/hello/world', :controller => 'a', :action => 'b'
|
1684
|
-
map.hello '/hello', :controller => 'a', :action => 'b'
|
1685
|
-
end
|
1686
|
-
assert_equal set.routes.last, set.named_routes[:hello]
|
1687
|
-
end
|
1718
|
+
assert_equal "http://test.host/people", controller.send(:index_url)
|
1719
|
+
assert_equal "/people", controller.send(:index_path)
|
1688
1720
|
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi'
|
1694
|
-
map.users '/admin/users', :controller => 'admin/users', :action => 'index'
|
1695
|
-
end
|
1721
|
+
assert_equal "http://test.host/admin/users", controller.send(:users_url)
|
1722
|
+
assert_equal '/admin/users', controller.send(:users_path)
|
1723
|
+
assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
|
1724
|
+
end
|
1696
1725
|
|
1697
|
-
|
1698
|
-
|
1699
|
-
klass.new(set)
|
1700
|
-
end
|
1726
|
+
def test_named_route_url_method_with_anchor
|
1727
|
+
controller = setup_named_route_test
|
1701
1728
|
|
1702
|
-
|
1703
|
-
|
1729
|
+
assert_equal "http://test.host/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
|
1730
|
+
assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location')
|
1704
1731
|
|
1705
|
-
|
1706
|
-
|
1707
|
-
controller.send(:hash_for_show_url, :id => 5))
|
1732
|
+
assert_equal "http://test.host/people#location", controller.send(:index_url, :anchor => 'location')
|
1733
|
+
assert_equal "/people#location", controller.send(:index_path, :anchor => 'location')
|
1708
1734
|
|
1709
|
-
|
1710
|
-
|
1711
|
-
controller.send(:hash_for_index_url))
|
1735
|
+
assert_equal "http://test.host/admin/users#location", controller.send(:users_url, :anchor => 'location')
|
1736
|
+
assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location')
|
1712
1737
|
|
1713
|
-
|
1714
|
-
|
1715
|
-
controller.send(:hash_for_show_path, :id => 5)
|
1716
|
-
)
|
1717
|
-
end
|
1738
|
+
assert_equal "http://test.host/people/go/7/hello/joe/5#location",
|
1739
|
+
controller.send(:multi_url, 7, "hello", 5, :anchor => 'location')
|
1718
1740
|
|
1719
|
-
|
1720
|
-
controller
|
1741
|
+
assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar#location",
|
1742
|
+
controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location')
|
1721
1743
|
|
1722
|
-
|
1723
|
-
|
1744
|
+
assert_equal "http://test.host/people?baz=bar#location",
|
1745
|
+
controller.send(:index_url, :baz => "bar", :anchor => 'location')
|
1746
|
+
end
|
1724
1747
|
|
1725
|
-
|
1726
|
-
|
1748
|
+
def test_named_route_url_method_with_port
|
1749
|
+
controller = setup_named_route_test
|
1750
|
+
assert_equal "http://test.host:8080/people/5", controller.send(:show_url, 5, :port=>8080)
|
1751
|
+
end
|
1727
1752
|
|
1728
|
-
|
1729
|
-
|
1730
|
-
|
1731
|
-
|
1753
|
+
def test_named_route_url_method_with_host
|
1754
|
+
controller = setup_named_route_test
|
1755
|
+
assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com")
|
1756
|
+
end
|
1732
1757
|
|
1733
|
-
|
1734
|
-
|
1758
|
+
def test_named_route_url_method_with_protocol
|
1759
|
+
controller = setup_named_route_test
|
1760
|
+
assert_equal "https://test.host/people/5", controller.send(:show_url, 5, :protocol => "https")
|
1761
|
+
end
|
1735
1762
|
|
1736
|
-
|
1737
|
-
|
1763
|
+
def test_named_route_url_method_with_ordered_parameters
|
1764
|
+
controller = setup_named_route_test
|
1765
|
+
assert_equal "http://test.host/people/go/7/hello/joe/5",
|
1766
|
+
controller.send(:multi_url, 7, "hello", 5)
|
1767
|
+
end
|
1738
1768
|
|
1739
|
-
|
1740
|
-
|
1769
|
+
def test_named_route_url_method_with_ordered_parameters_and_hash
|
1770
|
+
controller = setup_named_route_test
|
1771
|
+
assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar",
|
1772
|
+
controller.send(:multi_url, 7, "hello", 5, :baz => "bar")
|
1773
|
+
end
|
1741
1774
|
|
1742
|
-
|
1743
|
-
|
1775
|
+
def test_named_route_url_method_with_ordered_parameters_and_empty_hash
|
1776
|
+
controller = setup_named_route_test
|
1777
|
+
assert_equal "http://test.host/people/go/7/hello/joe/5",
|
1778
|
+
controller.send(:multi_url, 7, "hello", 5, {})
|
1779
|
+
end
|
1744
1780
|
|
1745
|
-
|
1746
|
-
|
1781
|
+
def test_named_route_url_method_with_no_positional_arguments
|
1782
|
+
controller = setup_named_route_test
|
1783
|
+
assert_equal "http://test.host/people?baz=bar",
|
1784
|
+
controller.send(:index_url, :baz => "bar")
|
1785
|
+
end
|
1747
1786
|
|
1748
|
-
|
1749
|
-
|
1787
|
+
def test_draw_default_route
|
1788
|
+
ActionController::Routing.with_controllers(['users']) do
|
1789
|
+
set.draw do |map|
|
1790
|
+
map.connect '/:controller/:action/:id'
|
1791
|
+
end
|
1750
1792
|
|
1751
|
-
assert_equal
|
1752
|
-
|
1753
|
-
end
|
1793
|
+
assert_equal 1, set.routes.size
|
1794
|
+
route = set.routes.first
|
1754
1795
|
|
1755
|
-
|
1756
|
-
controller = setup_named_route_test
|
1757
|
-
assert_equal "http://named.route.test:8080/people/5", controller.send(:show_url, 5, :port=>8080)
|
1758
|
-
end
|
1796
|
+
assert route.segments.last.optional?
|
1759
1797
|
|
1760
|
-
|
1761
|
-
controller
|
1762
|
-
assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com")
|
1763
|
-
end
|
1798
|
+
assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10)
|
1799
|
+
assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10)
|
1764
1800
|
|
1765
|
-
|
1766
|
-
controller
|
1767
|
-
assert_equal "https://named.route.test/people/5", controller.send(:show_url, 5, :protocol => "https")
|
1801
|
+
assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10'))
|
1802
|
+
assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/'))
|
1768
1803
|
end
|
1804
|
+
end
|
1769
1805
|
|
1770
|
-
|
1771
|
-
|
1772
|
-
|
1773
|
-
|
1806
|
+
def test_draw_default_route_with_default_controller
|
1807
|
+
ActionController::Routing.with_controllers(['users']) do
|
1808
|
+
set.draw do |map|
|
1809
|
+
map.connect '/:controller/:action/:id', :controller => 'users'
|
1810
|
+
end
|
1811
|
+
assert_equal({:controller => 'users', :action => 'index'}, set.recognize_path('/'))
|
1774
1812
|
end
|
1813
|
+
end
|
1775
1814
|
|
1776
|
-
|
1777
|
-
|
1778
|
-
|
1779
|
-
|
1780
|
-
|
1815
|
+
def test_route_with_parameter_shell
|
1816
|
+
ActionController::Routing.with_controllers(['users', 'pages']) do
|
1817
|
+
set.draw do |map|
|
1818
|
+
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/
|
1819
|
+
map.connect '/:controller/:action/:id'
|
1820
|
+
end
|
1781
1821
|
|
1782
|
-
|
1783
|
-
controller
|
1784
|
-
assert_equal
|
1785
|
-
controller.send(:multi_url, 7, "hello", 5, {})
|
1786
|
-
end
|
1822
|
+
assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages'))
|
1823
|
+
assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages/index'))
|
1824
|
+
assert_equal({:controller => 'pages', :action => 'list'}, set.recognize_path('/pages/list'))
|
1787
1825
|
|
1788
|
-
|
1789
|
-
controller
|
1790
|
-
assert_equal "http://named.route.test/people?baz=bar",
|
1791
|
-
controller.send(:index_url, :baz => "bar")
|
1826
|
+
assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/pages/show/10'))
|
1827
|
+
assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
|
1792
1828
|
end
|
1829
|
+
end
|
1793
1830
|
|
1794
|
-
|
1795
|
-
|
1796
|
-
|
1797
|
-
|
1798
|
-
end
|
1799
|
-
|
1800
|
-
assert_equal 1, set.routes.size
|
1801
|
-
route = set.routes.first
|
1802
|
-
|
1803
|
-
assert route.segments.last.optional?
|
1804
|
-
|
1805
|
-
assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10)
|
1806
|
-
assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10)
|
1807
|
-
|
1808
|
-
assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10'))
|
1809
|
-
assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/'))
|
1831
|
+
def test_route_requirements_with_anchor_chars_are_invalid
|
1832
|
+
assert_raise ArgumentError do
|
1833
|
+
set.draw do |map|
|
1834
|
+
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /^\d+/
|
1810
1835
|
end
|
1811
1836
|
end
|
1812
|
-
|
1813
|
-
|
1814
|
-
|
1815
|
-
set.draw do |map|
|
1816
|
-
map.connect '/:controller/:action/:id', :controller => 'users'
|
1817
|
-
end
|
1818
|
-
assert_equal({:controller => 'users', :action => 'index'}, set.recognize_path('/'))
|
1837
|
+
assert_raise ArgumentError do
|
1838
|
+
set.draw do |map|
|
1839
|
+
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\A\d+/
|
1819
1840
|
end
|
1820
1841
|
end
|
1821
|
-
|
1822
|
-
|
1823
|
-
|
1824
|
-
set.draw do |map|
|
1825
|
-
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/
|
1826
|
-
map.connect '/:controller/:action/:id'
|
1827
|
-
end
|
1828
|
-
|
1829
|
-
assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages'))
|
1830
|
-
assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages/index'))
|
1831
|
-
assert_equal({:controller => 'pages', :action => 'list'}, set.recognize_path('/pages/list'))
|
1832
|
-
|
1833
|
-
assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/pages/show/10'))
|
1834
|
-
assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
|
1842
|
+
assert_raise ArgumentError do
|
1843
|
+
set.draw do |map|
|
1844
|
+
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+$/
|
1835
1845
|
end
|
1836
1846
|
end
|
1837
|
-
|
1838
|
-
|
1839
|
-
|
1840
|
-
set.draw do |map|
|
1841
|
-
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /^\d+/
|
1842
|
-
end
|
1843
|
-
end
|
1844
|
-
assert_raises ArgumentError do
|
1845
|
-
set.draw do |map|
|
1846
|
-
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\A\d+/
|
1847
|
-
end
|
1848
|
-
end
|
1849
|
-
assert_raises ArgumentError do
|
1850
|
-
set.draw do |map|
|
1851
|
-
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+$/
|
1852
|
-
end
|
1847
|
+
assert_raise ArgumentError do
|
1848
|
+
set.draw do |map|
|
1849
|
+
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\Z/
|
1853
1850
|
end
|
1854
|
-
|
1855
|
-
|
1856
|
-
|
1857
|
-
|
1851
|
+
end
|
1852
|
+
assert_raise ArgumentError do
|
1853
|
+
set.draw do |map|
|
1854
|
+
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\z/
|
1858
1855
|
end
|
1859
|
-
|
1860
|
-
|
1861
|
-
|
1862
|
-
|
1856
|
+
end
|
1857
|
+
assert_nothing_raised do
|
1858
|
+
set.draw do |map|
|
1859
|
+
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/, :name => /^(david|jamis)/
|
1863
1860
|
end
|
1864
|
-
|
1865
|
-
set.
|
1866
|
-
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/, :name => /^(david|jamis)/
|
1867
|
-
end
|
1868
|
-
assert_raises ActionController::RoutingError do
|
1869
|
-
set.generate :controller => 'pages', :action => 'show', :id => 10
|
1870
|
-
end
|
1861
|
+
assert_raise ActionController::RoutingError do
|
1862
|
+
set.generate :controller => 'pages', :action => 'show', :id => 10
|
1871
1863
|
end
|
1872
1864
|
end
|
1865
|
+
end
|
1873
1866
|
|
1874
|
-
|
1875
|
-
|
1876
|
-
|
1877
|
-
|
1878
|
-
end
|
1867
|
+
def test_route_requirements_with_invalid_http_method_is_invalid
|
1868
|
+
assert_raise ArgumentError do
|
1869
|
+
set.draw do |map|
|
1870
|
+
map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :invalid}
|
1879
1871
|
end
|
1880
1872
|
end
|
1873
|
+
end
|
1881
1874
|
|
1882
|
-
|
1883
|
-
|
1884
|
-
|
1885
|
-
|
1886
|
-
end
|
1875
|
+
def test_route_requirements_with_options_method_condition_is_valid
|
1876
|
+
assert_nothing_raised do
|
1877
|
+
set.draw do |map|
|
1878
|
+
map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :options}
|
1887
1879
|
end
|
1888
1880
|
end
|
1881
|
+
end
|
1889
1882
|
|
1890
|
-
|
1883
|
+
def test_route_requirements_with_head_method_condition_is_invalid
|
1884
|
+
assert_raise ArgumentError do
|
1891
1885
|
set.draw do |map|
|
1892
|
-
map.connect '
|
1893
|
-
end
|
1894
|
-
assert_equal '/page/37s', set.generate(:controller => 'pages', :action => 'show', :name => 'jamis')
|
1895
|
-
assert_raises ActionController::RoutingError do
|
1896
|
-
set.generate(:controller => 'pages', :action => 'show', :name => 'not_jamis')
|
1897
|
-
end
|
1898
|
-
assert_raises ActionController::RoutingError do
|
1899
|
-
set.generate(:controller => 'pages', :action => 'show', :name => 'nor_jamis_and_david')
|
1886
|
+
map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :head}
|
1900
1887
|
end
|
1901
1888
|
end
|
1889
|
+
end
|
1902
1890
|
|
1903
|
-
|
1904
|
-
|
1905
|
-
|
1906
|
-
|
1891
|
+
def test_non_path_route_requirements_match_all
|
1892
|
+
set.draw do |map|
|
1893
|
+
map.connect 'page/37s', :controller => 'pages', :action => 'show', :name => /(jamis|david)/
|
1894
|
+
end
|
1895
|
+
assert_equal '/page/37s', set.generate(:controller => 'pages', :action => 'show', :name => 'jamis')
|
1896
|
+
assert_raise ActionController::RoutingError do
|
1897
|
+
set.generate(:controller => 'pages', :action => 'show', :name => 'not_jamis')
|
1898
|
+
end
|
1899
|
+
assert_raise ActionController::RoutingError do
|
1900
|
+
set.generate(:controller => 'pages', :action => 'show', :name => 'nor_jamis_and_david')
|
1901
|
+
end
|
1902
|
+
end
|
1907
1903
|
|
1908
|
-
|
1909
|
-
|
1904
|
+
def test_recognize_with_encoded_id_and_regex
|
1905
|
+
set.draw do |map|
|
1906
|
+
map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /[a-zA-Z0-9\+]+/
|
1910
1907
|
end
|
1911
1908
|
|
1912
|
-
|
1913
|
-
|
1909
|
+
assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
|
1910
|
+
assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, set.recognize_path('/page/hello+world'))
|
1911
|
+
end
|
1914
1912
|
|
1915
|
-
|
1916
|
-
|
1917
|
-
|
1918
|
-
|
1919
|
-
|
1920
|
-
|
1921
|
-
|
1922
|
-
|
1913
|
+
def test_recognize_with_conditions
|
1914
|
+
Object.const_set(:PeopleController, Class.new)
|
1915
|
+
|
1916
|
+
set.draw do |map|
|
1917
|
+
map.with_options(:controller => "people") do |people|
|
1918
|
+
people.people "/people", :action => "index", :conditions => { :method => :get }
|
1919
|
+
people.connect "/people", :action => "create", :conditions => { :method => :post }
|
1920
|
+
people.person "/people/:id", :action => "show", :conditions => { :method => :get }
|
1921
|
+
people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
|
1922
|
+
people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete }
|
1923
1923
|
end
|
1924
|
+
end
|
1924
1925
|
|
1925
|
-
|
1926
|
-
|
1927
|
-
|
1928
|
-
|
1926
|
+
request.path = "/people"
|
1927
|
+
request.env["REQUEST_METHOD"] = "GET"
|
1928
|
+
assert_nothing_raised { set.recognize(request) }
|
1929
|
+
assert_equal("index", request.path_parameters[:action])
|
1930
|
+
request.recycle!
|
1929
1931
|
|
1930
|
-
|
1931
|
-
|
1932
|
-
|
1932
|
+
request.env["REQUEST_METHOD"] = "POST"
|
1933
|
+
assert_nothing_raised { set.recognize(request) }
|
1934
|
+
assert_equal("create", request.path_parameters[:action])
|
1935
|
+
request.recycle!
|
1933
1936
|
|
1934
|
-
|
1935
|
-
|
1936
|
-
|
1937
|
+
request.env["REQUEST_METHOD"] = "PUT"
|
1938
|
+
assert_nothing_raised { set.recognize(request) }
|
1939
|
+
assert_equal("update", request.path_parameters[:action])
|
1940
|
+
request.recycle!
|
1937
1941
|
|
1938
|
-
|
1939
|
-
|
1940
|
-
|
1941
|
-
|
1942
|
-
|
1943
|
-
assert_equal [:get, :post, :put, :delete], e.allowed_methods
|
1944
|
-
end
|
1942
|
+
assert_raise(ActionController::UnknownHttpMethod) {
|
1943
|
+
request.env["REQUEST_METHOD"] = "BACON"
|
1944
|
+
set.recognize(request)
|
1945
|
+
}
|
1946
|
+
request.recycle!
|
1945
1947
|
|
1946
|
-
|
1947
|
-
|
1948
|
-
|
1949
|
-
|
1950
|
-
|
1948
|
+
request.path = "/people/5"
|
1949
|
+
request.env["REQUEST_METHOD"] = "GET"
|
1950
|
+
assert_nothing_raised { set.recognize(request) }
|
1951
|
+
assert_equal("show", request.path_parameters[:action])
|
1952
|
+
assert_equal("5", request.path_parameters[:id])
|
1953
|
+
request.recycle!
|
1951
1954
|
|
1952
|
-
|
1953
|
-
|
1954
|
-
|
1955
|
-
|
1955
|
+
request.env["REQUEST_METHOD"] = "PUT"
|
1956
|
+
assert_nothing_raised { set.recognize(request) }
|
1957
|
+
assert_equal("update", request.path_parameters[:action])
|
1958
|
+
assert_equal("5", request.path_parameters[:id])
|
1959
|
+
request.recycle!
|
1956
1960
|
|
1957
|
-
|
1958
|
-
|
1959
|
-
|
1960
|
-
|
1961
|
+
request.env["REQUEST_METHOD"] = "DELETE"
|
1962
|
+
assert_nothing_raised { set.recognize(request) }
|
1963
|
+
assert_equal("destroy", request.path_parameters[:action])
|
1964
|
+
assert_equal("5", request.path_parameters[:id])
|
1965
|
+
request.recycle!
|
1961
1966
|
|
1962
|
-
|
1963
|
-
|
1964
|
-
|
1965
|
-
|
1966
|
-
|
1967
|
-
|
1968
|
-
end
|
1969
|
-
|
1970
|
-
ensure
|
1971
|
-
Object.send(:remove_const, :PeopleController)
|
1967
|
+
begin
|
1968
|
+
request.env["REQUEST_METHOD"] = "POST"
|
1969
|
+
set.recognize(request)
|
1970
|
+
flunk 'Should have raised MethodNotAllowed'
|
1971
|
+
rescue ActionController::MethodNotAllowed => e
|
1972
|
+
assert_equal [:get, :put, :delete], e.allowed_methods
|
1972
1973
|
end
|
1974
|
+
request.recycle!
|
1973
1975
|
|
1974
|
-
|
1975
|
-
|
1976
|
-
|
1977
|
-
set.draw do |map|
|
1978
|
-
map.people "/people", :controller => 'people', :action => "index",
|
1979
|
-
:conditions => { :method => :get }
|
1980
|
-
map.root :people
|
1981
|
-
end
|
1976
|
+
ensure
|
1977
|
+
Object.send(:remove_const, :PeopleController)
|
1978
|
+
end
|
1982
1979
|
|
1983
|
-
|
1984
|
-
|
1985
|
-
assert_nothing_raised { set.recognize(request) }
|
1986
|
-
assert_equal("people", request.path_parameters[:controller])
|
1987
|
-
assert_equal("index", request.path_parameters[:action])
|
1980
|
+
def test_recognize_with_alias_in_conditions
|
1981
|
+
Object.const_set(:PeopleController, Class.new)
|
1988
1982
|
|
1989
|
-
|
1990
|
-
|
1991
|
-
|
1992
|
-
|
1993
|
-
assert_equal("index", request.path_parameters[:action])
|
1994
|
-
ensure
|
1995
|
-
Object.send(:remove_const, :PeopleController)
|
1983
|
+
set.draw do |map|
|
1984
|
+
map.people "/people", :controller => 'people', :action => "index",
|
1985
|
+
:conditions => { :method => :get }
|
1986
|
+
map.root :people
|
1996
1987
|
end
|
1997
1988
|
|
1998
|
-
|
1999
|
-
|
1989
|
+
request.path = "/people"
|
1990
|
+
request.env["REQUEST_METHOD"] = "GET"
|
1991
|
+
assert_nothing_raised { set.recognize(request) }
|
1992
|
+
assert_equal("people", request.path_parameters[:controller])
|
1993
|
+
assert_equal("index", request.path_parameters[:action])
|
2000
1994
|
|
2001
|
-
|
2002
|
-
|
2003
|
-
|
2004
|
-
|
2005
|
-
|
1995
|
+
request.path = "/"
|
1996
|
+
request.env["REQUEST_METHOD"] = "GET"
|
1997
|
+
assert_nothing_raised { set.recognize(request) }
|
1998
|
+
assert_equal("people", request.path_parameters[:controller])
|
1999
|
+
assert_equal("index", request.path_parameters[:action])
|
2000
|
+
ensure
|
2001
|
+
Object.send(:remove_const, :PeopleController)
|
2002
|
+
end
|
2006
2003
|
|
2007
|
-
|
2008
|
-
|
2009
|
-
assert_nothing_raised { set.recognize(request) }
|
2010
|
-
assert_equal("permalink", request.path_parameters[:action])
|
2011
|
-
assert_equal("2005", request.path_parameters[:year])
|
2012
|
-
assert_equal("11", request.path_parameters[:month])
|
2013
|
-
assert_equal("05", request.path_parameters[:day])
|
2014
|
-
assert_equal("a-very-interesting-article", request.path_parameters[:title])
|
2004
|
+
def test_typo_recognition
|
2005
|
+
Object.const_set(:ArticlesController, Class.new)
|
2015
2006
|
|
2016
|
-
|
2017
|
-
|
2007
|
+
set.draw do |map|
|
2008
|
+
map.connect 'articles/:year/:month/:day/:title',
|
2009
|
+
:controller => 'articles', :action => 'permalink',
|
2010
|
+
:year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
|
2018
2011
|
end
|
2019
2012
|
|
2020
|
-
|
2021
|
-
|
2022
|
-
|
2023
|
-
|
2024
|
-
|
2013
|
+
request.path = "/articles/2005/11/05/a-very-interesting-article"
|
2014
|
+
request.env["REQUEST_METHOD"] = "GET"
|
2015
|
+
assert_nothing_raised { set.recognize(request) }
|
2016
|
+
assert_equal("permalink", request.path_parameters[:action])
|
2017
|
+
assert_equal("2005", request.path_parameters[:year])
|
2018
|
+
assert_equal("11", request.path_parameters[:month])
|
2019
|
+
assert_equal("05", request.path_parameters[:day])
|
2020
|
+
assert_equal("a-very-interesting-article", request.path_parameters[:title])
|
2025
2021
|
|
2026
|
-
|
2027
|
-
|
2028
|
-
|
2022
|
+
ensure
|
2023
|
+
Object.send(:remove_const, :ArticlesController)
|
2024
|
+
end
|
2029
2025
|
|
2030
|
-
|
2026
|
+
def test_routing_traversal_does_not_load_extra_classes
|
2027
|
+
assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
|
2028
|
+
set.draw do |map|
|
2029
|
+
map.connect '/profile', :controller => 'profile'
|
2031
2030
|
end
|
2032
2031
|
|
2033
|
-
|
2034
|
-
Object.const_set(:PeopleController, Class.new)
|
2032
|
+
request.path = '/profile'
|
2035
2033
|
|
2036
|
-
|
2037
|
-
map.with_options(:controller => "people") do |people|
|
2038
|
-
people.person "/people/:id", :action => "show", :conditions => { :method => :get }
|
2039
|
-
people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
|
2040
|
-
people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get }
|
2041
|
-
end
|
2042
|
-
end
|
2034
|
+
set.recognize(request) rescue nil
|
2043
2035
|
|
2044
|
-
|
2045
|
-
|
2046
|
-
assert_nothing_raised { set.recognize(request) }
|
2047
|
-
assert_equal("show", request.path_parameters[:action])
|
2048
|
-
assert_equal("5", request.path_parameters[:id])
|
2036
|
+
assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
|
2037
|
+
end
|
2049
2038
|
|
2050
|
-
|
2051
|
-
|
2052
|
-
assert_equal("update", request.path_parameters[:action])
|
2039
|
+
def test_recognize_with_conditions_and_format
|
2040
|
+
Object.const_set(:PeopleController, Class.new)
|
2053
2041
|
|
2054
|
-
|
2055
|
-
|
2056
|
-
|
2057
|
-
|
2058
|
-
|
2059
|
-
|
2060
|
-
ensure
|
2061
|
-
Object.send(:remove_const, :PeopleController)
|
2042
|
+
set.draw do |map|
|
2043
|
+
map.with_options(:controller => "people") do |people|
|
2044
|
+
people.person "/people/:id", :action => "show", :conditions => { :method => :get }
|
2045
|
+
people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
|
2046
|
+
people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get }
|
2047
|
+
end
|
2062
2048
|
end
|
2063
2049
|
|
2064
|
-
|
2065
|
-
|
2066
|
-
|
2067
|
-
|
2068
|
-
|
2050
|
+
request.path = "/people/5"
|
2051
|
+
request.env["REQUEST_METHOD"] = "GET"
|
2052
|
+
assert_nothing_raised { set.recognize(request) }
|
2053
|
+
assert_equal("show", request.path_parameters[:action])
|
2054
|
+
assert_equal("5", request.path_parameters[:id])
|
2055
|
+
request.recycle!
|
2056
|
+
|
2057
|
+
request.env["REQUEST_METHOD"] = "PUT"
|
2058
|
+
assert_nothing_raised { set.recognize(request) }
|
2059
|
+
assert_equal("update", request.path_parameters[:action])
|
2060
|
+
request.recycle!
|
2061
|
+
|
2062
|
+
request.path = "/people/5.png"
|
2063
|
+
request.env["REQUEST_METHOD"] = "GET"
|
2064
|
+
assert_nothing_raised { set.recognize(request) }
|
2065
|
+
assert_equal("show", request.path_parameters[:action])
|
2066
|
+
assert_equal("5", request.path_parameters[:id])
|
2067
|
+
assert_equal("png", request.path_parameters[:_format])
|
2068
|
+
ensure
|
2069
|
+
Object.send(:remove_const, :PeopleController)
|
2070
|
+
end
|
2069
2071
|
|
2070
|
-
|
2071
|
-
|
2072
|
+
def test_generate_with_default_action
|
2073
|
+
set.draw do |map|
|
2074
|
+
map.connect "/people", :controller => "people"
|
2075
|
+
map.connect "/people/list", :controller => "people", :action => "list"
|
2072
2076
|
end
|
2073
2077
|
|
2074
|
-
|
2075
|
-
|
2078
|
+
url = set.generate(:controller => "people", :action => "list")
|
2079
|
+
assert_equal "/people/list", url
|
2080
|
+
end
|
2076
2081
|
|
2077
|
-
|
2082
|
+
def test_root_map
|
2083
|
+
Object.const_set(:PeopleController, Class.new)
|
2078
2084
|
|
2079
|
-
|
2080
|
-
request.method = :get
|
2081
|
-
assert_nothing_raised { set.recognize(request) }
|
2082
|
-
assert_equal("people", request.path_parameters[:controller])
|
2083
|
-
assert_equal("index", request.path_parameters[:action])
|
2084
|
-
ensure
|
2085
|
-
Object.send(:remove_const, :PeopleController)
|
2086
|
-
end
|
2085
|
+
set.draw { |map| map.root :controller => "people" }
|
2087
2086
|
|
2088
|
-
|
2089
|
-
|
2087
|
+
request.path = ""
|
2088
|
+
request.env["REQUEST_METHOD"] = "GET"
|
2089
|
+
assert_nothing_raised { set.recognize(request) }
|
2090
|
+
assert_equal("people", request.path_parameters[:controller])
|
2091
|
+
assert_equal("index", request.path_parameters[:action])
|
2092
|
+
ensure
|
2093
|
+
Object.send(:remove_const, :PeopleController)
|
2094
|
+
end
|
2090
2095
|
|
2091
|
-
|
2096
|
+
def test_namespace
|
2097
|
+
Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
|
2092
2098
|
|
2093
|
-
|
2094
|
-
api.route 'inventory', :controller => "products", :action => 'inventory'
|
2095
|
-
end
|
2099
|
+
set.draw do |map|
|
2096
2100
|
|
2101
|
+
map.namespace 'api' do |api|
|
2102
|
+
api.route 'inventory', :controller => "products", :action => 'inventory'
|
2097
2103
|
end
|
2098
2104
|
|
2099
|
-
request.path = "/api/inventory"
|
2100
|
-
request.method = :get
|
2101
|
-
assert_nothing_raised { set.recognize(request) }
|
2102
|
-
assert_equal("api/products", request.path_parameters[:controller])
|
2103
|
-
assert_equal("inventory", request.path_parameters[:action])
|
2104
|
-
ensure
|
2105
|
-
Object.send(:remove_const, :Api)
|
2106
2105
|
end
|
2107
2106
|
|
2108
|
-
|
2109
|
-
|
2107
|
+
request.path = "/api/inventory"
|
2108
|
+
request.env["REQUEST_METHOD"] = "GET"
|
2109
|
+
assert_nothing_raised { set.recognize(request) }
|
2110
|
+
assert_equal("api/products", request.path_parameters[:controller])
|
2111
|
+
assert_equal("inventory", request.path_parameters[:action])
|
2112
|
+
ensure
|
2113
|
+
Object.send(:remove_const, :Api)
|
2114
|
+
end
|
2110
2115
|
|
2111
|
-
|
2116
|
+
def test_namespaced_root_map
|
2117
|
+
Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
|
2112
2118
|
|
2113
|
-
|
2114
|
-
api.root :controller => "products"
|
2115
|
-
end
|
2119
|
+
set.draw do |map|
|
2116
2120
|
|
2121
|
+
map.namespace 'api' do |api|
|
2122
|
+
api.root :controller => "products"
|
2117
2123
|
end
|
2118
2124
|
|
2119
|
-
request.path = "/api"
|
2120
|
-
request.method = :get
|
2121
|
-
assert_nothing_raised { set.recognize(request) }
|
2122
|
-
assert_equal("api/products", request.path_parameters[:controller])
|
2123
|
-
assert_equal("index", request.path_parameters[:action])
|
2124
|
-
ensure
|
2125
|
-
Object.send(:remove_const, :Api)
|
2126
2125
|
end
|
2127
2126
|
|
2128
|
-
|
2129
|
-
|
2130
|
-
|
2131
|
-
|
2127
|
+
request.path = "/api"
|
2128
|
+
request.env["REQUEST_METHOD"] = "GET"
|
2129
|
+
assert_nothing_raised { set.recognize(request) }
|
2130
|
+
assert_equal("api/products", request.path_parameters[:controller])
|
2131
|
+
assert_equal("index", request.path_parameters[:action])
|
2132
|
+
ensure
|
2133
|
+
Object.send(:remove_const, :Api)
|
2134
|
+
end
|
2132
2135
|
|
2133
|
-
|
2134
|
-
|
2135
|
-
end
|
2136
|
+
def test_namespace_with_path_prefix
|
2137
|
+
Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
|
2136
2138
|
|
2139
|
+
set.draw do |map|
|
2140
|
+
map.namespace 'api', :path_prefix => 'prefix' do |api|
|
2141
|
+
api.route 'inventory', :controller => "products", :action => 'inventory'
|
2137
2142
|
end
|
2138
|
-
|
2139
|
-
request.path = "/prefix/inventory"
|
2140
|
-
request.method = :get
|
2141
|
-
assert_nothing_raised { set.recognize(request) }
|
2142
|
-
assert_equal("api/products", request.path_parameters[:controller])
|
2143
|
-
assert_equal("inventory", request.path_parameters[:action])
|
2144
|
-
ensure
|
2145
|
-
Object.send(:remove_const, :Api)
|
2146
2143
|
end
|
2147
2144
|
|
2148
|
-
|
2149
|
-
|
2150
|
-
|
2151
|
-
|
2152
|
-
|
2145
|
+
request.path = "/prefix/inventory"
|
2146
|
+
request.env["REQUEST_METHOD"] = "GET"
|
2147
|
+
assert_nothing_raised { set.recognize(request) }
|
2148
|
+
assert_equal("api/products", request.path_parameters[:controller])
|
2149
|
+
assert_equal("inventory", request.path_parameters[:action])
|
2150
|
+
ensure
|
2151
|
+
Object.send(:remove_const, :Api)
|
2152
|
+
end
|
2153
2153
|
|
2154
|
-
|
2155
|
-
|
2156
|
-
end
|
2154
|
+
def test_namespace_with_blank_path_prefix
|
2155
|
+
Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
|
2157
2156
|
|
2158
|
-
|
2159
|
-
|
2160
|
-
|
2161
|
-
|
2162
|
-
assert_equal "/foo/bar/baz/7", url
|
2157
|
+
set.draw do |map|
|
2158
|
+
map.namespace 'api', :path_prefix => '' do |api|
|
2159
|
+
api.route 'inventory', :controller => "products", :action => 'inventory'
|
2160
|
+
end
|
2163
2161
|
end
|
2164
2162
|
|
2165
|
-
|
2166
|
-
|
2167
|
-
|
2168
|
-
|
2169
|
-
|
2163
|
+
request.path = "/inventory"
|
2164
|
+
request.env["REQUEST_METHOD"] = "GET"
|
2165
|
+
assert_nothing_raised { set.recognize(request) }
|
2166
|
+
assert_equal("api/products", request.path_parameters[:controller])
|
2167
|
+
assert_equal("inventory", request.path_parameters[:action])
|
2168
|
+
ensure
|
2169
|
+
Object.send(:remove_const, :Api)
|
2170
|
+
end
|
2170
2171
|
|
2171
|
-
|
2172
|
-
|
2173
|
-
|
2172
|
+
def test_generate_finds_best_fit
|
2173
|
+
set.draw do |map|
|
2174
|
+
map.connect "/people", :controller => "people", :action => "index"
|
2175
|
+
map.connect "/ws/people", :controller => "people", :action => "index", :ws => true
|
2174
2176
|
end
|
2175
2177
|
|
2176
|
-
|
2177
|
-
|
2178
|
-
|
2179
|
-
|
2178
|
+
url = set.generate(:controller => "people", :action => "index", :ws => true)
|
2179
|
+
assert_equal "/ws/people", url
|
2180
|
+
end
|
2181
|
+
|
2182
|
+
def test_generate_changes_controller_module
|
2183
|
+
set.draw { |map| map.connect ':controller/:action/:id' }
|
2184
|
+
current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
|
2185
|
+
url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current)
|
2186
|
+
assert_equal "/foo/bar/baz/7", url
|
2187
|
+
end
|
2180
2188
|
|
2181
|
-
|
2182
|
-
|
2189
|
+
def test_id_is_not_impossibly_sticky
|
2190
|
+
set.draw do |map|
|
2191
|
+
map.connect 'foo/:number', :controller => "people", :action => "index"
|
2192
|
+
map.connect ':controller/:action/:id'
|
2183
2193
|
end
|
2184
2194
|
|
2185
|
-
|
2186
|
-
|
2187
|
-
|
2188
|
-
|
2189
|
-
end
|
2195
|
+
url = set.generate({:controller => "people", :action => "index", :number => 3},
|
2196
|
+
{:controller => "people", :action => "index", :id => "21"})
|
2197
|
+
assert_equal "/foo/3", url
|
2198
|
+
end
|
2190
2199
|
|
2191
|
-
|
2192
|
-
|
2193
|
-
|
2200
|
+
def test_id_is_sticky_when_it_ought_to_be
|
2201
|
+
set.draw do |map|
|
2202
|
+
map.connect ':controller/:id/:action'
|
2194
2203
|
end
|
2195
2204
|
|
2196
|
-
|
2197
|
-
|
2205
|
+
url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
|
2206
|
+
assert_equal "/people/7/destroy", url
|
2207
|
+
end
|
2198
2208
|
|
2199
|
-
|
2200
|
-
|
2201
|
-
|
2202
|
-
|
2209
|
+
def test_use_static_path_when_possible
|
2210
|
+
set.draw do |map|
|
2211
|
+
map.connect 'about', :controller => "welcome", :action => "about"
|
2212
|
+
map.connect ':controller/:action/:id'
|
2203
2213
|
end
|
2204
2214
|
|
2205
|
-
|
2206
|
-
|
2215
|
+
url = set.generate({:controller => "welcome", :action => "about"},
|
2216
|
+
{:controller => "welcome", :action => "get", :id => "7"})
|
2217
|
+
assert_equal "/about", url
|
2218
|
+
end
|
2207
2219
|
|
2208
|
-
|
2209
|
-
|
2210
|
-
end
|
2220
|
+
def test_generate
|
2221
|
+
set.draw { |map| map.connect ':controller/:action/:id' }
|
2211
2222
|
|
2212
|
-
|
2213
|
-
|
2214
|
-
|
2215
|
-
|
2216
|
-
|
2217
|
-
end
|
2223
|
+
args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
|
2224
|
+
assert_equal "/foo/bar/7?x=y", set.generate(args)
|
2225
|
+
assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
|
2226
|
+
assert_equal [:x], set.extra_keys(args)
|
2227
|
+
end
|
2218
2228
|
|
2219
|
-
|
2220
|
-
|
2229
|
+
def test_generate_with_path_prefix
|
2230
|
+
set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => 'my' }
|
2221
2231
|
|
2222
|
-
|
2223
|
-
|
2224
|
-
|
2232
|
+
args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
|
2233
|
+
assert_equal "/my/foo/bar/7?x=y", set.generate(args)
|
2234
|
+
end
|
2225
2235
|
|
2226
|
-
|
2227
|
-
|
2228
|
-
|
2229
|
-
|
2230
|
-
|
2231
|
-
|
2232
|
-
|
2233
|
-
|
2236
|
+
def test_generate_with_blank_path_prefix
|
2237
|
+
set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => '' }
|
2238
|
+
|
2239
|
+
args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
|
2240
|
+
assert_equal "/foo/bar/7?x=y", set.generate(args)
|
2241
|
+
end
|
2242
|
+
|
2243
|
+
def test_named_routes_are_never_relative_to_modules
|
2244
|
+
set.draw do |map|
|
2245
|
+
map.connect "/connection/manage/:action", :controller => 'connection/manage'
|
2246
|
+
map.connect "/connection/connection", :controller => "connection/connection"
|
2247
|
+
map.family_connection "/connection", :controller => "connection"
|
2234
2248
|
end
|
2235
2249
|
|
2236
|
-
|
2237
|
-
|
2238
|
-
|
2239
|
-
|
2240
|
-
|
2241
|
-
|
2242
|
-
|
2243
|
-
|
2244
|
-
|
2250
|
+
url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'})
|
2251
|
+
assert_equal "/connection/connection", url
|
2252
|
+
|
2253
|
+
url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'})
|
2254
|
+
assert_equal "/connection", url
|
2255
|
+
end
|
2256
|
+
|
2257
|
+
def test_action_left_off_when_id_is_recalled
|
2258
|
+
set.draw do |map|
|
2259
|
+
map.connect ':controller/:action/:id'
|
2245
2260
|
end
|
2261
|
+
assert_equal '/post', set.generate(
|
2262
|
+
{:controller => 'post', :action => 'index'},
|
2263
|
+
{:controller => 'post', :action => 'show', :id => '10'}
|
2264
|
+
)
|
2265
|
+
end
|
2246
2266
|
|
2247
|
-
|
2248
|
-
|
2249
|
-
|
2250
|
-
|
2251
|
-
{:controller => 'post', :action => 'show', :project_id => '1'})
|
2267
|
+
def test_query_params_will_be_shown_when_recalled
|
2268
|
+
set.draw do |map|
|
2269
|
+
map.connect 'show_post/:parameter', :controller => 'post', :action => 'show'
|
2270
|
+
map.connect ':controller/:action/:id'
|
2252
2271
|
end
|
2272
|
+
assert_equal '/post/edit?parameter=1', set.generate(
|
2273
|
+
{:action => 'edit', :parameter => 1},
|
2274
|
+
{:controller => 'post', :action => 'show', :parameter => 1}
|
2275
|
+
)
|
2276
|
+
end
|
2253
2277
|
|
2254
|
-
|
2255
|
-
|
2256
|
-
|
2257
|
-
map.connect ':controller/:action/:id'
|
2258
|
-
end
|
2259
|
-
all = set.generate(
|
2260
|
-
{:action => 'show', :id => 10, :generate_all => true},
|
2261
|
-
{:controller => 'post', :action => 'show'}
|
2262
|
-
)
|
2263
|
-
assert_equal 2, all.length
|
2264
|
-
assert_equal '/show_post/10', all.first
|
2265
|
-
assert_equal '/post/show/10', all.last
|
2278
|
+
def test_format_is_not_inherit
|
2279
|
+
set.draw do |map|
|
2280
|
+
map.connect '/posts.:format', :controller => 'posts'
|
2266
2281
|
end
|
2267
2282
|
|
2268
|
-
|
2269
|
-
|
2270
|
-
|
2271
|
-
|
2272
|
-
end
|
2273
|
-
end
|
2283
|
+
assert_equal '/posts', set.generate(
|
2284
|
+
{:controller => 'posts'},
|
2285
|
+
{:controller => 'posts', :action => 'index', :format => 'xml'}
|
2286
|
+
)
|
2274
2287
|
|
2275
|
-
|
2276
|
-
|
2277
|
-
|
2278
|
-
|
2279
|
-
|
2280
|
-
end
|
2288
|
+
assert_equal '/posts.xml', set.generate(
|
2289
|
+
{:controller => 'posts', :format => 'xml'},
|
2290
|
+
{:controller => 'posts', :action => 'index', :format => 'xml'}
|
2291
|
+
)
|
2292
|
+
end
|
2281
2293
|
|
2282
|
-
|
2283
|
-
|
2284
|
-
|
2285
|
-
|
2286
|
-
|
2287
|
-
|
2288
|
-
|
2289
|
-
|
2294
|
+
def test_expiry_determination_should_consider_values_with_to_param
|
2295
|
+
set.draw { |map| map.connect 'projects/:project_id/:controller/:action' }
|
2296
|
+
assert_equal '/projects/1/post/show', set.generate(
|
2297
|
+
{:action => 'show', :project_id => 1},
|
2298
|
+
{:controller => 'post', :action => 'show', :project_id => '1'})
|
2299
|
+
end
|
2300
|
+
|
2301
|
+
def test_generate_all
|
2302
|
+
set.draw do |map|
|
2303
|
+
map.connect 'show_post/:id', :controller => 'post', :action => 'show'
|
2304
|
+
map.connect ':controller/:action/:id'
|
2290
2305
|
end
|
2306
|
+
all = set.generate(
|
2307
|
+
{:action => 'show', :id => 10, :generate_all => true},
|
2308
|
+
{:controller => 'post', :action => 'show'}
|
2309
|
+
)
|
2310
|
+
assert_equal 2, all.length
|
2311
|
+
assert_equal '/show_post/10', all.first
|
2312
|
+
assert_equal '/post/show/10', all.last
|
2313
|
+
end
|
2291
2314
|
|
2292
|
-
|
2293
|
-
|
2294
|
-
|
2295
|
-
|
2296
|
-
admin.root :controller => 'home'
|
2297
|
-
end
|
2298
|
-
end
|
2315
|
+
def test_named_route_in_nested_resource
|
2316
|
+
set.draw do |map|
|
2317
|
+
map.resources :projects do |project|
|
2318
|
+
project.milestones 'milestones', :controller => 'milestones', :action => 'index'
|
2299
2319
|
end
|
2300
2320
|
end
|
2301
2321
|
|
2302
|
-
|
2303
|
-
|
2304
|
-
|
2305
|
-
|
2306
|
-
|
2307
|
-
|
2322
|
+
request.path = "/projects/1/milestones"
|
2323
|
+
request.env["REQUEST_METHOD"] = "GET"
|
2324
|
+
assert_nothing_raised { set.recognize(request) }
|
2325
|
+
assert_equal("milestones", request.path_parameters[:controller])
|
2326
|
+
assert_equal("index", request.path_parameters[:action])
|
2327
|
+
end
|
2328
|
+
|
2329
|
+
def test_setting_root_in_namespace_using_symbol
|
2330
|
+
assert_nothing_raised do
|
2331
|
+
set.draw do |map|
|
2332
|
+
map.namespace :admin do |admin|
|
2333
|
+
admin.root :controller => 'home'
|
2308
2334
|
end
|
2309
2335
|
end
|
2310
2336
|
end
|
2337
|
+
end
|
2311
2338
|
|
2312
|
-
|
2313
|
-
|
2314
|
-
|
2315
|
-
|
2316
|
-
|
2317
|
-
:requirements => {:name => /(david|jamis)/i}
|
2318
|
-
end
|
2319
|
-
end
|
2320
|
-
assert_nothing_raised do
|
2321
|
-
set.draw do |map|
|
2322
|
-
map.connect 'page/:name', :controller => 'pages',
|
2323
|
-
:action => 'show',
|
2324
|
-
:requirements => {:name => / # Desperately overcommented regexp
|
2325
|
-
( #Either
|
2326
|
-
david #The Creator
|
2327
|
-
| #Or
|
2328
|
-
jamis #The Deployer
|
2329
|
-
)/x}
|
2339
|
+
def test_setting_root_in_namespace_using_string
|
2340
|
+
assert_nothing_raised do
|
2341
|
+
set.draw do |map|
|
2342
|
+
map.namespace 'admin' do |admin|
|
2343
|
+
admin.root :controller => 'home'
|
2330
2344
|
end
|
2331
2345
|
end
|
2332
2346
|
end
|
2347
|
+
end
|
2333
2348
|
|
2334
|
-
|
2349
|
+
def test_route_requirements_with_unsupported_regexp_options_must_error
|
2350
|
+
assert_raise ArgumentError do
|
2335
2351
|
set.draw do |map|
|
2336
2352
|
map.connect 'page/:name', :controller => 'pages',
|
2337
2353
|
:action => 'show',
|
2338
|
-
:requirements => {:name => /(david|jamis)/
|
2354
|
+
:requirements => {:name => /(david|jamis)/m}
|
2339
2355
|
end
|
2340
|
-
assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
|
2341
|
-
assert_raises ActionController::RoutingError do
|
2342
|
-
set.recognize_path('/page/davidjamis')
|
2343
|
-
end
|
2344
|
-
assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, set.recognize_path('/page/DAVID'))
|
2345
2356
|
end
|
2357
|
+
end
|
2346
2358
|
|
2347
|
-
|
2359
|
+
def test_route_requirements_with_supported_options_must_not_error
|
2360
|
+
assert_nothing_raised do
|
2348
2361
|
set.draw do |map|
|
2349
2362
|
map.connect 'page/:name', :controller => 'pages',
|
2350
2363
|
:action => 'show',
|
2351
2364
|
:requirements => {:name => /(david|jamis)/i}
|
2352
2365
|
end
|
2353
|
-
url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
|
2354
|
-
assert_equal "/page/david", url
|
2355
|
-
assert_raises ActionController::RoutingError do
|
2356
|
-
url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
|
2357
|
-
end
|
2358
|
-
url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
|
2359
|
-
assert_equal "/page/JAMIS", url
|
2360
2366
|
end
|
2361
|
-
|
2362
|
-
def test_route_requirement_recognize_with_extended_syntax
|
2367
|
+
assert_nothing_raised do
|
2363
2368
|
set.draw do |map|
|
2364
2369
|
map.connect 'page/:name', :controller => 'pages',
|
2365
2370
|
:action => 'show',
|
@@ -2370,130 +2375,183 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
|
|
2370
2375
|
jamis #The Deployer
|
2371
2376
|
)/x}
|
2372
2377
|
end
|
2373
|
-
assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
|
2374
|
-
assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, set.recognize_path('/page/david'))
|
2375
|
-
assert_raises ActionController::RoutingError do
|
2376
|
-
set.recognize_path('/page/david #The Creator')
|
2377
|
-
end
|
2378
|
-
assert_raises ActionController::RoutingError do
|
2379
|
-
set.recognize_path('/page/David')
|
2380
|
-
end
|
2381
2378
|
end
|
2379
|
+
end
|
2382
2380
|
|
2383
|
-
|
2384
|
-
|
2385
|
-
|
2386
|
-
|
2387
|
-
|
2388
|
-
|
2389
|
-
|
2390
|
-
|
2391
|
-
|
2392
|
-
)/x}
|
2393
|
-
end
|
2394
|
-
url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
|
2395
|
-
assert_equal "/page/david", url
|
2396
|
-
assert_raises ActionController::RoutingError do
|
2397
|
-
url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
|
2398
|
-
end
|
2399
|
-
assert_raises ActionController::RoutingError do
|
2400
|
-
url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
|
2401
|
-
end
|
2381
|
+
def test_route_requirement_recognize_with_ignore_case
|
2382
|
+
set.draw do |map|
|
2383
|
+
map.connect 'page/:name', :controller => 'pages',
|
2384
|
+
:action => 'show',
|
2385
|
+
:requirements => {:name => /(david|jamis)/i}
|
2386
|
+
end
|
2387
|
+
assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
|
2388
|
+
assert_raise ActionController::RoutingError do
|
2389
|
+
set.recognize_path('/page/davidjamis')
|
2402
2390
|
end
|
2391
|
+
assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, set.recognize_path('/page/DAVID'))
|
2392
|
+
end
|
2403
2393
|
|
2404
|
-
|
2405
|
-
|
2406
|
-
|
2407
|
-
|
2408
|
-
|
2409
|
-
( #Either
|
2410
|
-
david #The Creator
|
2411
|
-
| #Or
|
2412
|
-
jamis #The Deployer
|
2413
|
-
)/xi}
|
2414
|
-
end
|
2415
|
-
url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
|
2416
|
-
assert_equal "/page/JAMIS", url
|
2394
|
+
def test_route_requirement_generate_with_ignore_case
|
2395
|
+
set.draw do |map|
|
2396
|
+
map.connect 'page/:name', :controller => 'pages',
|
2397
|
+
:action => 'show',
|
2398
|
+
:requirements => {:name => /(david|jamis)/i}
|
2417
2399
|
end
|
2400
|
+
url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
|
2401
|
+
assert_equal "/page/david", url
|
2402
|
+
assert_raise ActionController::RoutingError do
|
2403
|
+
url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
|
2404
|
+
end
|
2405
|
+
url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
|
2406
|
+
assert_equal "/page/JAMIS", url
|
2407
|
+
end
|
2418
2408
|
|
2419
|
-
|
2420
|
-
|
2421
|
-
|
2422
|
-
|
2423
|
-
|
2424
|
-
|
2425
|
-
|
2426
|
-
|
2427
|
-
|
2428
|
-
|
2429
|
-
|
2430
|
-
|
2409
|
+
def test_route_requirement_recognize_with_extended_syntax
|
2410
|
+
set.draw do |map|
|
2411
|
+
map.connect 'page/:name', :controller => 'pages',
|
2412
|
+
:action => 'show',
|
2413
|
+
:requirements => {:name => / # Desperately overcommented regexp
|
2414
|
+
( #Either
|
2415
|
+
david #The Creator
|
2416
|
+
| #Or
|
2417
|
+
jamis #The Deployer
|
2418
|
+
)/x}
|
2419
|
+
end
|
2420
|
+
assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
|
2421
|
+
assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, set.recognize_path('/page/david'))
|
2422
|
+
assert_raise ActionController::RoutingError do
|
2423
|
+
set.recognize_path('/page/david #The Creator')
|
2424
|
+
end
|
2425
|
+
assert_raise ActionController::RoutingError do
|
2426
|
+
set.recognize_path('/page/David')
|
2431
2427
|
end
|
2432
2428
|
end
|
2433
2429
|
|
2434
|
-
|
2435
|
-
|
2436
|
-
|
2437
|
-
|
2438
|
-
|
2430
|
+
def test_route_requirement_generate_with_extended_syntax
|
2431
|
+
set.draw do |map|
|
2432
|
+
map.connect 'page/:name', :controller => 'pages',
|
2433
|
+
:action => 'show',
|
2434
|
+
:requirements => {:name => / # Desperately overcommented regexp
|
2435
|
+
( #Either
|
2436
|
+
david #The Creator
|
2437
|
+
| #Or
|
2438
|
+
jamis #The Deployer
|
2439
|
+
)/x}
|
2440
|
+
end
|
2441
|
+
url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
|
2442
|
+
assert_equal "/page/david", url
|
2443
|
+
assert_raise ActionController::RoutingError do
|
2444
|
+
url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
|
2445
|
+
end
|
2446
|
+
assert_raise ActionController::RoutingError do
|
2447
|
+
url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
|
2448
|
+
end
|
2449
|
+
end
|
2439
2450
|
|
2440
|
-
|
2451
|
+
def test_route_requirement_generate_with_xi_modifiers
|
2452
|
+
set.draw do |map|
|
2453
|
+
map.connect 'page/:name', :controller => 'pages',
|
2454
|
+
:action => 'show',
|
2455
|
+
:requirements => {:name => / # Desperately overcommented regexp
|
2456
|
+
( #Either
|
2457
|
+
david #The Creator
|
2458
|
+
| #Or
|
2459
|
+
jamis #The Deployer
|
2460
|
+
)/xi}
|
2441
2461
|
end
|
2462
|
+
url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
|
2463
|
+
assert_equal "/page/JAMIS", url
|
2464
|
+
end
|
2442
2465
|
|
2443
|
-
|
2444
|
-
|
2445
|
-
|
2466
|
+
def test_route_requirement_recognize_with_xi_modifiers
|
2467
|
+
set.draw do |map|
|
2468
|
+
map.connect 'page/:name', :controller => 'pages',
|
2469
|
+
:action => 'show',
|
2470
|
+
:requirements => {:name => / # Desperately overcommented regexp
|
2471
|
+
( #Either
|
2472
|
+
david #The Creator
|
2473
|
+
| #Or
|
2474
|
+
jamis #The Deployer
|
2475
|
+
)/xi}
|
2446
2476
|
end
|
2477
|
+
assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, set.recognize_path('/page/JAMIS'))
|
2478
|
+
end
|
2479
|
+
end
|
2447
2480
|
|
2448
|
-
|
2449
|
-
|
2450
|
-
|
2481
|
+
class RouteLoadingTest < Test::Unit::TestCase
|
2482
|
+
def setup
|
2483
|
+
routes.instance_variable_set '@routes_last_modified', nil
|
2484
|
+
silence_warnings { Object.const_set :RAILS_ROOT, '.' }
|
2485
|
+
routes.add_configuration_file(File.join(RAILS_ROOT, 'config', 'routes.rb'))
|
2451
2486
|
|
2452
|
-
|
2453
|
-
|
2487
|
+
@stat = stub_everything
|
2488
|
+
end
|
2454
2489
|
|
2455
|
-
|
2456
|
-
|
2457
|
-
|
2458
|
-
|
2490
|
+
def teardown
|
2491
|
+
ActionController::Routing::Routes.configuration_files.clear
|
2492
|
+
Object.send :remove_const, :RAILS_ROOT
|
2493
|
+
end
|
2459
2494
|
|
2460
|
-
|
2461
|
-
|
2495
|
+
def test_load
|
2496
|
+
File.expects(:stat).returns(@stat)
|
2497
|
+
routes.expects(:load).with(regexp_matches(/routes\.rb$/))
|
2462
2498
|
|
2463
|
-
|
2464
|
-
|
2465
|
-
File.expects(:stat).at_least(2).returns(@stat)
|
2466
|
-
routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
|
2499
|
+
routes.reload
|
2500
|
+
end
|
2467
2501
|
|
2468
|
-
|
2469
|
-
|
2502
|
+
def test_no_reload_when_not_modified
|
2503
|
+
@stat.expects(:mtime).times(2).returns(1)
|
2504
|
+
File.expects(:stat).times(2).returns(@stat)
|
2505
|
+
routes.expects(:load).with(regexp_matches(/routes\.rb$/)).at_most_once
|
2470
2506
|
|
2471
|
-
|
2472
|
-
|
2473
|
-
File.expects(:stat).at_least(2).returns(@stat)
|
2474
|
-
routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
|
2507
|
+
2.times { routes.reload }
|
2508
|
+
end
|
2475
2509
|
|
2476
|
-
|
2477
|
-
|
2510
|
+
def test_reload_when_modified
|
2511
|
+
@stat.expects(:mtime).at_least(2).returns(1, 2)
|
2512
|
+
File.expects(:stat).at_least(2).returns(@stat)
|
2513
|
+
routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
|
2478
2514
|
|
2479
|
-
|
2480
|
-
|
2481
|
-
routes.expects(:reload!)
|
2515
|
+
2.times { routes.reload }
|
2516
|
+
end
|
2482
2517
|
|
2483
|
-
|
2484
|
-
|
2518
|
+
def test_bang_forces_reload
|
2519
|
+
@stat.expects(:mtime).at_least(2).returns(1)
|
2520
|
+
File.expects(:stat).at_least(2).returns(@stat)
|
2521
|
+
routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
|
2485
2522
|
|
2486
|
-
|
2487
|
-
|
2488
|
-
File.expects(:stat).returns(@stat)
|
2489
|
-
routes.expects(:load).with("foobarbaz")
|
2523
|
+
2.times { routes.reload! }
|
2524
|
+
end
|
2490
2525
|
|
2491
|
-
|
2492
|
-
|
2526
|
+
def test_adding_inflections_forces_reload
|
2527
|
+
ActiveSupport::Inflector::Inflections.instance.expects(:uncountable).with('equipment')
|
2528
|
+
routes.expects(:reload!)
|
2493
2529
|
|
2494
|
-
|
2495
|
-
def routes
|
2496
|
-
ActionController::Routing::Routes
|
2497
|
-
end
|
2530
|
+
ActiveSupport::Inflector.inflections { |inflect| inflect.uncountable('equipment') }
|
2498
2531
|
end
|
2532
|
+
|
2533
|
+
def test_load_with_configuration
|
2534
|
+
routes.configuration_files.clear
|
2535
|
+
routes.add_configuration_file("foobarbaz")
|
2536
|
+
File.expects(:stat).returns(@stat)
|
2537
|
+
routes.expects(:load).with("foobarbaz")
|
2538
|
+
|
2539
|
+
routes.reload
|
2540
|
+
end
|
2541
|
+
|
2542
|
+
def test_load_multiple_configurations
|
2543
|
+
routes.add_configuration_file("engines.rb")
|
2544
|
+
|
2545
|
+
File.expects(:stat).at_least_once.returns(@stat)
|
2546
|
+
|
2547
|
+
routes.expects(:load).with('./config/routes.rb')
|
2548
|
+
routes.expects(:load).with('engines.rb')
|
2549
|
+
|
2550
|
+
routes.reload
|
2551
|
+
end
|
2552
|
+
|
2553
|
+
private
|
2554
|
+
def routes
|
2555
|
+
ActionController::Routing::Routes
|
2556
|
+
end
|
2499
2557
|
end
|