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.

Files changed (264) hide show
  1. data/CHANGELOG +433 -375
  2. data/MIT-LICENSE +1 -1
  3. data/README +21 -75
  4. data/Rakefile +1 -1
  5. data/lib/action_controller.rb +80 -43
  6. data/lib/action_controller/assertions/model_assertions.rb +1 -0
  7. data/lib/action_controller/assertions/response_assertions.rb +43 -16
  8. data/lib/action_controller/assertions/routing_assertions.rb +1 -1
  9. data/lib/action_controller/assertions/selector_assertions.rb +17 -12
  10. data/lib/action_controller/assertions/tag_assertions.rb +1 -4
  11. data/lib/action_controller/base.rb +153 -82
  12. data/lib/action_controller/benchmarking.rb +9 -9
  13. data/lib/action_controller/caching.rb +9 -11
  14. data/lib/action_controller/caching/actions.rb +11 -18
  15. data/lib/action_controller/caching/fragments.rb +28 -20
  16. data/lib/action_controller/caching/pages.rb +13 -15
  17. data/lib/action_controller/caching/sweeping.rb +2 -2
  18. data/lib/action_controller/cgi_ext.rb +0 -1
  19. data/lib/action_controller/cgi_ext/cookie.rb +2 -0
  20. data/lib/action_controller/cgi_process.rb +54 -162
  21. data/lib/action_controller/cookies.rb +13 -25
  22. data/lib/action_controller/dispatcher.rb +43 -122
  23. data/lib/action_controller/failsafe.rb +52 -0
  24. data/lib/action_controller/flash.rb +38 -47
  25. data/lib/action_controller/helpers.rb +13 -9
  26. data/lib/action_controller/http_authentication.rb +203 -23
  27. data/lib/action_controller/integration.rb +126 -70
  28. data/lib/action_controller/layout.rb +36 -39
  29. data/lib/action_controller/middleware_stack.rb +119 -0
  30. data/lib/action_controller/middlewares.rb +13 -0
  31. data/lib/action_controller/mime_responds.rb +19 -4
  32. data/lib/action_controller/mime_type.rb +8 -0
  33. data/lib/action_controller/params_parser.rb +71 -0
  34. data/lib/action_controller/performance_test.rb +0 -1
  35. data/lib/action_controller/polymorphic_routes.rb +36 -30
  36. data/lib/action_controller/reloader.rb +14 -0
  37. data/lib/action_controller/request.rb +107 -499
  38. data/lib/action_controller/request_forgery_protection.rb +7 -39
  39. data/lib/action_controller/rescue.rb +55 -35
  40. data/lib/action_controller/resources.rb +34 -31
  41. data/lib/action_controller/response.rb +99 -57
  42. data/lib/action_controller/rewindable_input.rb +28 -0
  43. data/lib/action_controller/routing.rb +7 -7
  44. data/lib/action_controller/routing/builder.rb +4 -1
  45. data/lib/action_controller/routing/optimisations.rb +1 -1
  46. data/lib/action_controller/routing/recognition_optimisation.rb +1 -2
  47. data/lib/action_controller/routing/route.rb +15 -5
  48. data/lib/action_controller/routing/route_set.rb +82 -35
  49. data/lib/action_controller/routing/segments.rb +35 -0
  50. data/lib/action_controller/session/abstract_store.rb +181 -0
  51. data/lib/action_controller/session/cookie_store.rb +197 -175
  52. data/lib/action_controller/session/mem_cache_store.rb +36 -83
  53. data/lib/action_controller/session_management.rb +26 -134
  54. data/lib/action_controller/streaming.rb +24 -7
  55. data/lib/action_controller/templates/rescues/diagnostics.erb +2 -2
  56. data/lib/action_controller/templates/rescues/template_error.erb +2 -2
  57. data/lib/action_controller/test_case.rb +87 -30
  58. data/lib/action_controller/test_process.rb +145 -104
  59. data/lib/action_controller/uploaded_file.rb +44 -0
  60. data/lib/action_controller/url_rewriter.rb +3 -6
  61. data/lib/action_controller/vendor/html-scanner.rb +16 -0
  62. data/lib/action_controller/vendor/html-scanner/html/selector.rb +1 -1
  63. data/lib/action_controller/vendor/rack-1.0/rack.rb +89 -0
  64. data/lib/action_controller/vendor/rack-1.0/rack/adapter/camping.rb +22 -0
  65. data/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/handler.rb +37 -0
  66. data/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/request.rb +37 -0
  67. data/lib/action_controller/vendor/rack-1.0/rack/auth/basic.rb +58 -0
  68. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/md5.rb +124 -0
  69. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/nonce.rb +51 -0
  70. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/params.rb +55 -0
  71. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/request.rb +40 -0
  72. data/lib/action_controller/vendor/rack-1.0/rack/auth/openid.rb +480 -0
  73. data/lib/action_controller/vendor/rack-1.0/rack/builder.rb +63 -0
  74. data/lib/action_controller/vendor/rack-1.0/rack/cascade.rb +36 -0
  75. data/lib/action_controller/vendor/rack-1.0/rack/chunked.rb +49 -0
  76. data/lib/action_controller/vendor/rack-1.0/rack/commonlogger.rb +61 -0
  77. data/lib/action_controller/vendor/rack-1.0/rack/conditionalget.rb +45 -0
  78. data/lib/action_controller/vendor/rack-1.0/rack/content_length.rb +29 -0
  79. data/lib/action_controller/vendor/rack-1.0/rack/content_type.rb +23 -0
  80. data/lib/action_controller/vendor/rack-1.0/rack/deflater.rb +85 -0
  81. data/lib/action_controller/vendor/rack-1.0/rack/directory.rb +153 -0
  82. data/lib/action_controller/vendor/rack-1.0/rack/file.rb +88 -0
  83. data/lib/action_controller/vendor/rack-1.0/rack/handler.rb +48 -0
  84. data/lib/action_controller/vendor/rack-1.0/rack/handler/cgi.rb +61 -0
  85. data/lib/action_controller/vendor/rack-1.0/rack/handler/evented_mongrel.rb +8 -0
  86. data/lib/action_controller/vendor/rack-1.0/rack/handler/fastcgi.rb +89 -0
  87. data/lib/action_controller/vendor/rack-1.0/rack/handler/lsws.rb +55 -0
  88. data/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb +84 -0
  89. data/lib/action_controller/vendor/rack-1.0/rack/handler/scgi.rb +59 -0
  90. data/lib/action_controller/vendor/rack-1.0/rack/handler/swiftiplied_mongrel.rb +8 -0
  91. data/lib/action_controller/vendor/rack-1.0/rack/handler/thin.rb +18 -0
  92. data/lib/action_controller/vendor/rack-1.0/rack/handler/webrick.rb +67 -0
  93. data/lib/action_controller/vendor/rack-1.0/rack/head.rb +19 -0
  94. data/lib/action_controller/vendor/rack-1.0/rack/lint.rb +462 -0
  95. data/lib/action_controller/vendor/rack-1.0/rack/lobster.rb +65 -0
  96. data/lib/action_controller/vendor/rack-1.0/rack/lock.rb +16 -0
  97. data/lib/action_controller/vendor/rack-1.0/rack/methodoverride.rb +27 -0
  98. data/lib/action_controller/vendor/rack-1.0/rack/mime.rb +204 -0
  99. data/lib/action_controller/vendor/rack-1.0/rack/mock.rb +160 -0
  100. data/lib/action_controller/vendor/rack-1.0/rack/recursive.rb +57 -0
  101. data/lib/action_controller/vendor/rack-1.0/rack/reloader.rb +64 -0
  102. data/lib/action_controller/vendor/rack-1.0/rack/request.rb +241 -0
  103. data/lib/action_controller/vendor/rack-1.0/rack/response.rb +179 -0
  104. data/lib/action_controller/vendor/rack-1.0/rack/session/abstract/id.rb +142 -0
  105. data/lib/action_controller/vendor/rack-1.0/rack/session/cookie.rb +91 -0
  106. data/lib/action_controller/vendor/rack-1.0/rack/session/memcache.rb +109 -0
  107. data/lib/action_controller/vendor/rack-1.0/rack/session/pool.rb +100 -0
  108. data/lib/action_controller/vendor/rack-1.0/rack/showexceptions.rb +349 -0
  109. data/lib/action_controller/vendor/rack-1.0/rack/showstatus.rb +106 -0
  110. data/lib/action_controller/vendor/rack-1.0/rack/static.rb +38 -0
  111. data/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb +55 -0
  112. data/lib/action_controller/vendor/rack-1.0/rack/utils.rb +392 -0
  113. data/lib/action_controller/verification.rb +1 -1
  114. data/lib/action_pack.rb +1 -1
  115. data/lib/action_pack/version.rb +2 -2
  116. data/lib/action_view.rb +22 -17
  117. data/lib/action_view/base.rb +53 -79
  118. data/lib/action_view/erb/util.rb +38 -0
  119. data/lib/action_view/helpers.rb +24 -5
  120. data/lib/action_view/helpers/active_record_helper.rb +2 -2
  121. data/lib/action_view/helpers/asset_tag_helper.rb +81 -50
  122. data/lib/action_view/helpers/atom_feed_helper.rb +1 -1
  123. data/lib/action_view/helpers/benchmark_helper.rb +26 -5
  124. data/lib/action_view/helpers/date_helper.rb +82 -7
  125. data/lib/action_view/helpers/form_helper.rb +295 -64
  126. data/lib/action_view/helpers/form_options_helper.rb +160 -18
  127. data/lib/action_view/helpers/form_tag_helper.rb +2 -2
  128. data/lib/action_view/helpers/number_helper.rb +31 -18
  129. data/lib/action_view/helpers/prototype_helper.rb +2 -12
  130. data/lib/action_view/helpers/sanitize_helper.rb +0 -10
  131. data/lib/action_view/helpers/scriptaculous_helper.rb +1 -0
  132. data/lib/action_view/helpers/tag_helper.rb +3 -4
  133. data/lib/action_view/helpers/text_helper.rb +99 -122
  134. data/lib/action_view/helpers/translation_helper.rb +19 -1
  135. data/lib/action_view/helpers/url_helper.rb +25 -2
  136. data/lib/action_view/inline_template.rb +1 -1
  137. data/lib/action_view/locale/en.yml +19 -1
  138. data/lib/action_view/partials.rb +46 -9
  139. data/lib/action_view/paths.rb +28 -84
  140. data/lib/action_view/reloadable_template.rb +117 -0
  141. data/lib/action_view/renderable.rb +28 -35
  142. data/lib/action_view/renderable_partial.rb +3 -4
  143. data/lib/action_view/template.rb +172 -31
  144. data/lib/action_view/template_error.rb +8 -9
  145. data/lib/action_view/template_handler.rb +1 -1
  146. data/lib/action_view/template_handlers.rb +9 -6
  147. data/lib/action_view/template_handlers/erb.rb +2 -39
  148. data/lib/action_view/template_handlers/rjs.rb +1 -0
  149. data/lib/action_view/test_case.rb +27 -1
  150. data/test/abstract_unit.rb +23 -17
  151. data/test/active_record_unit.rb +5 -4
  152. data/test/activerecord/active_record_store_test.rb +139 -106
  153. data/test/activerecord/render_partial_with_record_identification_test.rb +5 -21
  154. data/test/controller/action_pack_assertions_test.rb +25 -23
  155. data/test/controller/addresses_render_test.rb +3 -6
  156. data/test/controller/assert_select_test.rb +83 -70
  157. data/test/controller/base_test.rb +11 -13
  158. data/test/controller/benchmark_test.rb +3 -3
  159. data/test/controller/caching_test.rb +34 -24
  160. data/test/controller/capture_test.rb +3 -6
  161. data/test/controller/content_type_test.rb +3 -6
  162. data/test/controller/cookie_test.rb +31 -66
  163. data/test/controller/deprecation/deprecated_base_methods_test.rb +9 -11
  164. data/test/controller/dispatcher_test.rb +23 -28
  165. data/test/controller/fake_models.rb +8 -0
  166. data/test/controller/filters_test.rb +6 -2
  167. data/test/controller/flash_test.rb +2 -6
  168. data/test/controller/helper_test.rb +15 -1
  169. data/test/controller/html-scanner/document_test.rb +1 -1
  170. data/test/controller/html-scanner/sanitizer_test.rb +1 -1
  171. data/test/controller/http_basic_authentication_test.rb +88 -0
  172. data/test/controller/http_digest_authentication_test.rb +178 -0
  173. data/test/controller/integration_test.rb +56 -52
  174. data/test/controller/layout_test.rb +46 -44
  175. data/test/controller/middleware_stack_test.rb +90 -0
  176. data/test/controller/mime_responds_test.rb +7 -11
  177. data/test/controller/mime_type_test.rb +9 -0
  178. data/test/controller/polymorphic_routes_test.rb +235 -151
  179. data/test/controller/rack_test.rb +52 -81
  180. data/test/controller/redirect_test.rb +6 -14
  181. data/test/controller/render_test.rb +273 -60
  182. data/test/controller/request/json_params_parsing_test.rb +45 -0
  183. data/test/controller/request/multipart_params_parsing_test.rb +223 -0
  184. data/test/controller/request/query_string_parsing_test.rb +120 -0
  185. data/test/controller/request/url_encoded_params_parsing_test.rb +184 -0
  186. data/test/controller/request/xml_params_parsing_test.rb +88 -0
  187. data/test/controller/request_forgery_protection_test.rb +17 -98
  188. data/test/controller/request_test.rb +45 -530
  189. data/test/controller/rescue_test.rb +45 -22
  190. data/test/controller/resources_test.rb +112 -37
  191. data/test/controller/routing_test.rb +1442 -1384
  192. data/test/controller/selector_test.rb +3 -3
  193. data/test/controller/send_file_test.rb +30 -3
  194. data/test/controller/session/cookie_store_test.rb +169 -240
  195. data/test/controller/session/mem_cache_store_test.rb +94 -148
  196. data/test/controller/session/test_session_test.rb +58 -0
  197. data/test/controller/test_test.rb +32 -13
  198. data/test/controller/url_rewriter_test.rb +54 -4
  199. data/test/controller/verification_test.rb +1 -1
  200. data/test/controller/view_paths_test.rb +15 -15
  201. data/test/controller/webservice_test.rb +178 -147
  202. data/test/fixtures/alternate_helpers/foo_helper.rb +3 -0
  203. data/test/fixtures/layout_tests/alt/layouts/alt.rhtml +0 -0
  204. data/test/fixtures/layouts/default_html.html.erb +1 -0
  205. data/test/fixtures/layouts/xhr.html.erb +2 -0
  206. data/test/fixtures/multipart/empty +10 -0
  207. data/test/fixtures/multipart/hello.txt +1 -0
  208. data/test/fixtures/multipart/none +9 -0
  209. data/test/fixtures/public/500.da.html +1 -0
  210. data/test/fixtures/quiz/questions/_question.html.erb +1 -0
  211. data/test/fixtures/replies.yml +1 -1
  212. data/test/fixtures/test/_one.html.erb +1 -0
  213. data/test/fixtures/test/_two.html.erb +1 -0
  214. data/test/fixtures/test/dont_pick_me +1 -0
  215. data/test/fixtures/test/hello.builder +1 -1
  216. data/test/fixtures/test/hello_world.da.html.erb +1 -0
  217. data/test/fixtures/test/hello_world.erb~ +1 -0
  218. data/test/fixtures/test/hello_world.pt-BR.html.erb +1 -0
  219. data/test/fixtures/test/malformed/malformed.en.html.erb~ +1 -0
  220. data/test/fixtures/test/malformed/malformed.erb~ +1 -0
  221. data/test/fixtures/test/malformed/malformed.html.erb~ +1 -0
  222. data/test/fixtures/test/render_explicit_html_template.js.rjs +1 -0
  223. data/test/fixtures/test/render_implicit_html_template.js.rjs +1 -0
  224. data/test/fixtures/test/render_implicit_html_template_from_xhr_request.da.html.erb +1 -0
  225. data/test/fixtures/test/render_implicit_html_template_from_xhr_request.html.erb +1 -0
  226. data/test/fixtures/test/render_implicit_js_template_without_layout.js.erb +1 -0
  227. data/test/fixtures/test/utf8.html.erb +2 -0
  228. data/test/template/active_record_helper_i18n_test.rb +31 -33
  229. data/test/template/active_record_helper_test.rb +34 -0
  230. data/test/template/asset_tag_helper_test.rb +52 -14
  231. data/test/template/atom_feed_helper_test.rb +3 -5
  232. data/test/template/benchmark_helper_test.rb +50 -24
  233. data/test/template/compiled_templates_test.rb +177 -33
  234. data/test/template/date_helper_i18n_test.rb +88 -81
  235. data/test/template/date_helper_test.rb +427 -43
  236. data/test/template/form_helper_test.rb +243 -44
  237. data/test/template/form_options_helper_test.rb +631 -565
  238. data/test/template/form_tag_helper_test.rb +9 -2
  239. data/test/template/javascript_helper_test.rb +0 -5
  240. data/test/template/number_helper_i18n_test.rb +60 -48
  241. data/test/template/number_helper_test.rb +1 -0
  242. data/test/template/render_test.rb +117 -35
  243. data/test/template/test_test.rb +4 -6
  244. data/test/template/text_helper_test.rb +129 -50
  245. data/test/template/translation_helper_test.rb +23 -19
  246. data/test/template/url_helper_test.rb +35 -2
  247. data/test/view/test_case_test.rb +8 -0
  248. metadata +197 -23
  249. data/lib/action_controller/assertions.rb +0 -69
  250. data/lib/action_controller/caching/sql_cache.rb +0 -18
  251. data/lib/action_controller/cgi_ext/session.rb +0 -53
  252. data/lib/action_controller/components.rb +0 -169
  253. data/lib/action_controller/rack_process.rb +0 -297
  254. data/lib/action_controller/request_profiler.rb +0 -169
  255. data/lib/action_controller/session/active_record_store.rb +0 -340
  256. data/lib/action_controller/session/drb_server.rb +0 -32
  257. data/lib/action_controller/session/drb_store.rb +0 -35
  258. data/test/controller/cgi_test.rb +0 -269
  259. data/test/controller/components_test.rb +0 -156
  260. data/test/controller/http_authentication_test.rb +0 -54
  261. data/test/controller/integration_upload_test.rb +0 -43
  262. data/test/controller/session_fixation_test.rb +0 -89
  263. data/test/controller/session_management_test.rb +0 -178
  264. 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
- RescueController.process_with_exception(@request, @response, ActionController::RoutingError.new("Route not found"))
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 < Test::Unit::TestCase
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
- assert_raises(ActionController::RoutingError) do
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", "formatted_#{action}_thread_messages_path", :action => action, :format => '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, :formatted_preview_new_thread_message_path, preview_options
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
- assert_raises(ActionController::MethodNotAllowed) do
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
- assert_raises(ActionController::MethodNotAllowed) do
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
- assert_raises(ActionController::MethodNotAllowed) do
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
- assert_raises(ArgumentError) do
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
- assert_raises(ArgumentError) do
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, :as => 'reviews' do
754
- assert_simply_restful_for :messages, :as => 'reviews'
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 test_nested_resource_ignores_only_option
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 test_nested_resource_ignores_except_option
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[:path_prefix] unless options[:shallow]}#{path}"
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[:path_prefix] unless options[:shallow]}#{path}"
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 = "#{options[:name_prefix] unless options[:shallow]}"
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", "formatted_#{name_prefix}#{controller_name}_path", options[:options].merge(:format => '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", "formatted_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => '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", "formatted_new_#{name_prefix}#{singular_name}_path", options[:options].merge(:format => '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", "formatted_edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => '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", "formatted_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => '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", "formatted_new_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => '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", "formatted_edit_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => '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, Test::Unit::AssertionFailedError do
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
- assert_raises(LocalJumpError) { eval(segment.extraction_code) }
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
- assert_raises(LocalJumpError) { eval(segment.extraction_code) }
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
- assert_raises ArgumentError do
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
- uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
720
- class MockController
721
- attr_accessor :routes
718
+ class MockController
719
+ attr_accessor :routes
722
720
 
723
- def initialize(routes)
724
- self.routes = routes
725
- end
721
+ def initialize(routes)
722
+ self.routes = routes
723
+ end
726
724
 
727
- def url_for(options)
728
- only_path = options.delete(:only_path)
725
+ def url_for(options)
726
+ only_path = options.delete(:only_path)
729
727
 
730
- port = options.delete(:port) || 80
731
- port_string = port == 80 ? '' : ":#{port}"
728
+ port = options.delete(:port) || 80
729
+ port_string = port == 80 ? '' : ":#{port}"
732
730
 
733
- protocol = options.delete(:protocol) || "http"
734
- host = options.delete(:host) || "named.route.test"
735
- anchor = "##{options.delete(:anchor)}" if options.key?(:anchor)
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
- path = routes.generate(options)
735
+ path = routes.generate(options)
738
736
 
739
- only_path ? "#{path}#{anchor}" : "#{protocol}://#{host}#{port_string}#{path}#{anchor}"
740
- end
737
+ only_path ? "#{path}#{anchor}" : "#{protocol}://#{host}#{port_string}#{path}#{anchor}"
738
+ end
741
739
 
742
- def request
743
- @request ||= MockRequest.new(:host => "named.route.test", :method => :get)
744
- end
740
+ def request
741
+ @request ||= ActionController::TestRequest.new
745
742
  end
743
+ end
746
744
 
747
- class MockRequest
748
- attr_accessor :path, :path_parameters, :host, :subdomains, :domain, :method
745
+ class LegacyRouteSetTests < Test::Unit::TestCase
746
+ attr_reader :rs
749
747
 
750
- def initialize(values={})
751
- values.each { |key, value| send("#{key}=", value) }
752
- if values[:host]
753
- subdomain, self.domain = values[:host].split(/\./, 2)
754
- self.subdomains = [subdomain]
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
- def protocol
759
- "http://"
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
- def host_with_port
763
- (subdomains * '.') + '.' + domain
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
- class LegacyRouteSetTests < Test::Unit::TestCase
768
- attr_reader :rs
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
- def setup
771
- # These tests assume optimisation is on, so re-enable it.
772
- ActionController::Base.optimise_named_routes = true
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
- @rs = ::ActionController::Routing::RouteSet.new
775
- @rs.draw {|m| m.connect ':controller/:action/:id' }
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
- ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed)
778
- end
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
- def test_default_setup
781
- assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
782
- assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
783
- assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
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
- assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10"))
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
- assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10)
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
- assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
790
- assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
942
+ end
791
943
 
792
- assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
793
- assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
794
- end
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
- def test_ignores_leading_slash
797
- @rs.draw {|m| m.connect '/:controller/:action/:id'}
798
- test_default_setup
799
- end
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
- def test_time_recognition
802
- # We create many routes to make situation more realistic
803
- @rs = ::ActionController::Routing::RouteSet.new
804
- @rs.draw { |map|
805
- map.frontpage '', :controller => 'search', :action => 'new'
806
- map.resources :videos do |video|
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
- map.resources :users do |user|
817
- user.resource :settings
818
- user.resources :videos
819
- end
820
- map.resources :channels do |channel|
821
- channel.resources :videos, :controller => 'channel_videos'
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
- def test_time_generation
850
- n = 5000
851
- if RunTimeTests
852
- GC.start
853
- pairs = [
854
- [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}],
855
- [{:controller => 'content'}, {:controller => 'content', :action => 'index'}],
856
- [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}],
857
- [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}],
858
- [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}],
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
- puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)"
871
- per_url = gentime / (n * 8)
872
- puts "#{per_url * 1000} ms/url"
873
- puts "#{1 / per_url} url/s\n\n"
874
- end
875
- end
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
- def test_route_with_colon_first
878
- rs.draw do |map|
879
- map.connect '/:controller/:action/:id', :action => 'index', :id => nil
880
- map.connect ':url', :controller => 'tiny_url', :action => 'translate'
881
- end
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
- def test_route_with_regexp_for_controller
885
- rs.draw do |map|
886
- map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/
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
- def test_route_with_regexp_and_captures_for_controller
897
- rs.draw do |map|
898
- map.connect ':controller/:action/:id', :controller => /admin\/(accounts|users)/
899
- end
900
- assert_equal({:controller => "admin/accounts", :action => "index"}, rs.recognize_path("/admin/accounts"))
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
- def test_route_with_regexp_and_dot
907
- rs.draw do |map|
908
- map.connect ':controller/:action/:file',
909
- :controller => /admin|user/,
910
- :action => /upload|download/,
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
- # Now, let's try a file with an extension, really a dot (.)
922
- assert_equal '/user/download/file.jpg',
923
- rs.generate(
924
- :controller => "user", :action => "download", :file => "file.jpg")
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
- def test_basic_named_route
931
- rs.add_named_route :home, '', :controller => 'content', :action => 'list'
932
- x = setup_for_named_route
933
- assert_equal("http://named.route.test/",
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
- def test_basic_named_route_with_relative_url_root
938
- rs.add_named_route :home, '', :controller => 'content', :action => 'list'
939
- x = setup_for_named_route
940
- ActionController::Base.relative_url_root = "/foo"
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
- def test_named_route_with_option
948
- rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page'
949
- x = setup_for_named_route
950
- assert_equal("http://named.route.test/page/new%20stuff",
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
- def test_named_route_with_default
955
- rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
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
- end
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
- def test_named_route_with_name_prefix
963
- rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :name_prefix => 'my_'
964
- x = setup_for_named_route
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
- def test_named_route_with_path_prefix
970
- rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => 'my'
971
- x = setup_for_named_route
972
- assert_equal("http://named.route.test/my/page",
973
- x.send(:page_url))
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
- def test_named_route_with_nested_controller
977
- rs.add_named_route :users, 'admin/user', :controller => 'admin/user', :action => 'index'
978
- x = setup_for_named_route
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
- def test_optimised_named_route_call_never_uses_url_for
984
- rs.add_named_route :users, 'admin/user', :controller => '/admin/user', :action => 'index'
985
- rs.add_named_route :user, 'admin/user/:id', :controller=>'/admin/user', :action=>'show'
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
- def test_optimised_named_route_with_host
995
- rs.add_named_route :pages, 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com'
996
- x = setup_for_named_route
997
- x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once
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
- def setup_for_named_route
1002
- klass = Class.new(MockController)
1003
- rs.install_helpers(klass)
1004
- klass.new(rs)
1005
- end
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
- def test_named_route_without_hash
1008
- rs.draw do |map|
1009
- map.normal ':controller/:action/:id'
1010
- end
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
- def test_named_route_root
1014
- rs.draw do |map|
1015
- map.root :controller => "hello"
1016
- end
1017
- x = setup_for_named_route
1018
- assert_equal("http://named.route.test/", x.send(:root_url))
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
- def test_named_route_with_regexps
1023
- rs.draw do |map|
1024
- map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
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
- def test_changing_controller
1040
- assert_equal '/admin/stuff/show/10', rs.generate(
1041
- {:controller => 'stuff', :action => 'show', :id => 10},
1042
- {:controller => 'admin/user', :action => 'index'}
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
- def test_paths_escaped
1047
- rs.draw do |map|
1048
- map.path 'file/*path', :controller => 'content', :action => 'show_file'
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
- # No + to space in URI escaping, only for query params.
1053
- results = rs.recognize_path "/file/hello+world/how+are+you%3F"
1054
- assert results, "Recognition should have succeeded"
1055
- assert_equal ['hello+world', 'how+are+you?'], results[:path]
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
- # Use %20 for space instead.
1058
- results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F"
1059
- assert results, "Recognition should have succeeded"
1060
- assert_equal ['hello world', 'how are you?'], results[:path]
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
- results = rs.recognize_path "/file"
1063
- assert results, "Recognition should have succeeded"
1064
- assert_equal [], results[:path]
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
- def test_paths_slashes_unescaped_with_ordered_parameters
1068
- rs.add_named_route :path, '/file/*path', :controller => 'content'
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
- # No / to %2F in URI, only for query params.
1071
- x = setup_for_named_route
1072
- assert_equal("/file/hello/world", x.send(:path_path, 'hello/world'))
1073
- end
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
- def test_non_controllers_cannot_be_matched
1076
- rs.draw do |map|
1077
- map.connect ':controller/:action/:id'
1078
- end
1079
- assert_raises(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") }
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
- def test_paths_do_not_accept_defaults
1083
- assert_raises(ActionController::RoutingError) do
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
- rs.draw do |map|
1091
- map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => []
1092
- map.connect ':controller/:action/:id'
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
- def test_should_list_options_diff_when_routing_requirements_dont_match
1097
- rs.draw do |map|
1098
- map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
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
- exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'content', :action => 'show', :use_route => "content") }
1125
- assert_match %r[:action=>"show"], exception.message
1126
- assert_match %r[:controller=>"content"], exception.message
1127
- assert_match %r[you may have ambiguous routes, or you may need to supply additional parameters for this route], exception.message
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
- def test_dynamic_path_allowed
1132
- rs.draw do |map|
1133
- map.connect '*path', :controller => 'content', :action => 'show_file'
1134
- end
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
- assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo))
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
- def test_dynamic_recall_paths_allowed
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
- recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo))
1145
- assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path)
1207
+ assert_raise ActionController::RoutingError do
1208
+ rs.generate(:controller => 'post', :action => 'show')
1146
1209
  end
1210
+ end
1147
1211
 
1148
- def test_backwards
1149
- rs.draw do |map|
1150
- map.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
1151
- map.connect ':controller/:action/:id'
1152
- end
1153
-
1154
- assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'})
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
- def test_route_with_fixnum_default
1160
- rs.draw do |map|
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
- assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page')
1166
- assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1)
1167
- assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1')
1168
- assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10)
1224
+ x = setup_for_named_route
1225
+ assert_equal("http://test.host/test",
1226
+ x.send(:blog_url))
1227
+ end
1169
1228
 
1170
- assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page"))
1171
- assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1"))
1172
- assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10"))
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
- # For newer revision
1176
- def test_route_with_text_default
1177
- rs.draw do |map|
1178
- map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
1179
- map.connect ':controller/:action/:id'
1180
- end
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
- assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo')
1183
- assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo"))
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
- token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
1186
- token.force_encoding("UTF-8") if token.respond_to?(:force_encoding)
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
- assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token)
1190
- assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}"))
1191
- end
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
- def test_action_expiry
1194
- assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
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
- def test_recognition_with_uppercase_controller_name
1198
- assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/Content"))
1199
- assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/ConTent/list"))
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
- # these used to work, before the routes rewrite, but support for this was pulled in the new version...
1203
- #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/NewsFeed"))
1204
- #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/News_Feed"))
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
- def test_requirement_should_prevent_optional_id
1208
- rs.draw do |map|
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
- assert_raises ActionController::RoutingError do
1215
- rs.generate(:controller => 'post', :action => 'show')
1216
- end
1217
- end
1271
+ x = setup_for_named_route
1272
+ assert_equal("http://test.host/",
1273
+ x.send(:home_url))
1274
+ end
1218
1275
 
1219
- def test_both_requirement_and_optional
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.blog('test/:year', :controller => 'post', :action => 'show',
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
- assert_equal '/test', rs.generate(:controller => 'post', :action => 'show')
1229
- assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil)
1230
-
1231
- x = setup_for_named_route
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
- def test_set_to_nil_forgets
1237
- rs.draw do |map|
1238
- map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil
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
- assert_equal '/pages/2005',
1243
- rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005)
1244
- assert_equal '/pages/2005/6',
1245
- rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
1246
- assert_equal '/pages/2005/6/12',
1247
- rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
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
- assert_equal '/pages/2005/6/4',
1250
- rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
1312
+ assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
1313
+ end
1251
1314
 
1252
- assert_equal '/pages/2005/6',
1253
- rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
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
- assert_equal '/pages/2005',
1256
- rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
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
- def test_url_with_no_action_specified
1260
- rs.draw do |map|
1261
- map.connect '', :controller => 'content'
1262
- map.connect ':controller/:action/:id'
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
- assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
1266
- assert_equal '/', rs.generate(:controller => 'content')
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
- def test_named_url_with_no_action_specified
1270
- rs.draw do |map|
1271
- map.home '', :controller => 'content'
1272
- map.connect ':controller/:action/:id'
1273
- end
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
- assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
1276
- assert_equal '/', rs.generate(:controller => 'content')
1366
+ def test_subpath_recognized
1367
+ Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
1277
1368
 
1278
- x = setup_for_named_route
1279
- assert_equal("http://named.route.test/",
1280
- x.send(:home_url))
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
- def test_url_generated_when_forgetting_action
1284
- [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash|
1285
- rs.draw do |map|
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
- def test_named_route_method
1296
- rs.draw do |map|
1297
- map.categories 'categories', :controller => 'content', :action => 'categories'
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
- assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories')
1302
- assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
1303
- end
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
- def test_named_routes_array
1306
- test_named_route_method
1307
- assert_equal [:categories], rs.named_routes.names
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
- def test_nil_defaults
1311
- rs.draw do |map|
1312
- map.connect 'journal',
1313
- :controller => 'content',
1314
- :action => 'list_journal',
1315
- :date => nil, :user_id => nil
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
- assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
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
- def setup_request_method_routes_for(method)
1323
- @request = ActionController::TestRequest.new
1324
- @request.env["REQUEST_METHOD"] = method
1325
- @request.request_uri = "/match"
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
- rs.draw do |r|
1328
- r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get }
1329
- r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post }
1330
- r.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put }
1331
- r.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete }
1332
- end
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
- %w(GET POST PUT DELETE).each do |request_method|
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
- setup_request_method_routes_for(request_method)
1435
+ assert_not_nil hash
1436
+ assert_equal %w(cc ac), [hash[:controller], hash[:action]]
1341
1437
 
1342
- assert_nothing_raised { rs.recognize(@request) }
1343
- assert_equal request_method.downcase, @request.path_parameters[:action]
1344
- ensure
1345
- Object.send(:remove_const, :BooksController) rescue nil
1346
- end
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
- def test_recognize_array_of_methods
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
- @request = ActionController::TestRequest.new
1358
- @request.env["REQUEST_METHOD"] = 'POST'
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
- hash = rs.recognize_path "/books/17/edit"
1384
- assert_not_nil hash
1385
- assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]]
1450
+ end
1451
+ end
1386
1452
 
1387
- hash = rs.recognize_path "/items/3/complete"
1388
- assert_not_nil hash
1389
- assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]]
1453
+ class RouteTest < Test::Unit::TestCase
1454
+ def setup
1455
+ @route = ROUTING::Route.new
1456
+ end
1390
1457
 
1391
- hash = rs.recognize_path "/posts/new/preview"
1392
- assert_not_nil hash
1393
- assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]]
1458
+ def slash_segment(is_optional = false)
1459
+ ROUTING::DividerSegment.new('/', :optional => is_optional)
1460
+ end
1394
1461
 
1395
- hash = rs.recognize_path "/posts/7"
1396
- assert_not_nil hash
1397
- assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]]
1398
- ensure
1399
- Object.send(:remove_const, :SubpathBooksController) rescue nil
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
- def test_subpath_generated
1403
- Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
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
- rs.draw do |r|
1406
- r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
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
- assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit")
1412
- assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete")
1413
- assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview")
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
- def test_failed_requirements_raises_exception_with_violated_requirements
1419
- rs.draw do |r|
1420
- r.foo_with_requirement 'foos/:id', :controller=>'foos', :requirements=>{:id=>/\d+/}
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
- x = setup_for_named_route
1424
- assert_raises(ActionController::RoutingError) do
1425
- x.send(:foo_with_requirement_url, "I am Against the requirements")
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
- def test_routes_changed_correctly_after_clear
1430
- ActionController::Base.optimise_named_routes = true
1431
- rs = ::ActionController::Routing::RouteSet.new
1432
- rs.draw do |r|
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
- hash = rs.recognize_path "/cc"
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
- assert_not_nil hash
1443
- assert_equal %w(cc ac), [hash[:controller], hash[:action]]
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
- rs.draw do |r|
1446
- r.connect 'cb', :controller => 'cb', :action => "ab"
1447
- r.connect 'cc', :controller => 'cc', :action => "ac"
1448
- r.connect ':controller/:action/:id'
1449
- r.connect ':controller/:action/:id.:format'
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
- hash = rs.recognize_path "/cc"
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
- assert_not_nil hash
1455
- assert_equal %w(cc ac), [hash[:controller], hash[:action]]
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
- end
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
- class RouteTest < Test::Unit::TestCase
1461
- def setup
1462
- @route = ROUTING::Route.new
1463
- end
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
- def slash_segment(is_optional = false)
1466
- ROUTING::DividerSegment.new('/', :optional => is_optional)
1467
- end
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
- def default_route
1470
- unless defined?(@default_route)
1471
- segments = []
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
- def test_default_route_recognition
1485
- expected = {:controller => 'accounts', :action => 'show', :id => '10'}
1486
- assert_equal expected, default_route.recognize('/accounts/show/10')
1487
- assert_equal expected, default_route.recognize('/accounts/show/10/')
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
- assert_equal nil, default_route.recognize('/')
1501
- assert_equal nil, default_route.recognize('/accounts/how/goood/it/is/to/be/free')
1502
- end
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
- def test_default_route_should_omit_default_action
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
- def test_default_route_should_include_default_action_when_id_present
1510
- o = {:controller => 'accounts', :action => 'index', :id => '20'}
1511
- assert_equal '/accounts/index/20', default_route.generate(o, o, {})
1512
- end
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
- def test_default_route_should_work_with_action_but_no_id
1515
- o = {:controller => 'accounts', :action => 'list_all'}
1516
- assert_equal '/accounts/list_all', default_route.generate(o, o, {})
1517
- end
1574
+ def test_build_empty_query_string
1575
+ assert_equal '', @route.build_query_string({})
1576
+ end
1518
1577
 
1519
- def test_default_route_should_uri_escape_pluses
1520
- expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' }
1521
- assert_equal expected, default_route.recognize('/accounts/show/hello world')
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
- expected[:id] = 'hello+world'
1526
- assert_equal expected, default_route.recognize('/accounts/show/hello+world')
1527
- assert_equal expected, default_route.recognize('/accounts/show/hello%2Bworld')
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
- def test_matches_controller_and_action
1532
- # requirement_for should only be called for the action and controller _once_
1533
- @route.expects(:requirement_for).with(:controller).times(1).returns('pages')
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
- @route.requirements = {:controller => 'pages', :action => 'show'}
1537
- assert @route.matches_controller_and_action?('pages', 'show')
1538
- assert !@route.matches_controller_and_action?('not_pages', 'show')
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
- def test_parameter_shell
1543
- page_url = ROUTING::Route.new
1544
- page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/}
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
- def test_defaults
1549
- route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html"
1550
- assert_equal(
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
- def test_builder_complains_without_controller
1556
- assert_raises(ArgumentError) do
1557
- ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index"
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
- def test_significant_keys_for_default_route
1562
- keys = default_route.significant_keys.sort_by {|k| k.to_s }
1563
- assert_equal [:action, :controller, :id], keys
1564
- end
1608
+ class RouteSetTest < Test::Unit::TestCase
1609
+ def set
1610
+ @set ||= ROUTING::RouteSet.new
1611
+ end
1565
1612
 
1566
- def test_significant_keys
1567
- segments = []
1568
- segments << ROUTING::StaticSegment.new('/', :raw => true)
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
- requirements = {:controller => 'users', :action => 'show'}
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
- user_url = ROUTING::Route.new(segments, requirements)
1577
- keys = user_url.significant_keys.sort_by { |k| k.to_s }
1578
- assert_equal [:action, :controller, :user], keys
1579
- end
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
- def test_build_empty_query_string
1582
- assert_equal '', @route.build_query_string({})
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
- def test_build_query_string_with_nil_value
1586
- assert_equal '', @route.build_query_string({:x => nil})
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
- def test_simple_build_query_string
1590
- assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => '1', :y => '2'))
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
- def test_convert_ints_build_query_string
1594
- assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => 1, :y => 2))
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
- def test_escape_spaces_build_query_string
1598
- assert_equal '?x=hello+world&y=goodbye+world', order_query_string(@route.build_query_string(:x => 'hello world', :y => 'goodbye world'))
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
- def test_expand_array_build_query_string
1602
- assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2]))
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
- def test_escape_spaces_build_query_string_selected_keys
1606
- assert_equal '?x=hello+world', order_query_string(@route.build_query_string({:x => 'hello world', :y => 'goodbye world'}, [:x]))
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
- private
1610
- def order_query_string(qs)
1611
- '?' + qs[1..-1].split('&').sort.join('&')
1612
- end
1690
+ klass = Class.new(MockController)
1691
+ set.install_helpers(klass)
1692
+ klass.new(set)
1613
1693
  end
1614
1694
 
1615
- class RouteSetTest < Test::Unit::TestCase
1616
- def set
1617
- @set ||= ROUTING::RouteSet.new
1618
- end
1695
+ def test_named_route_hash_access_method
1696
+ controller = setup_named_route_test
1619
1697
 
1620
- def request
1621
- @request ||= MockRequest.new(:host => "named.routes.test", :method => :get)
1622
- end
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
- def test_generate_extras
1625
- set.draw { |m| m.connect ':controller/:action/:id' }
1626
- path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
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
- def test_extra_keys
1632
- set.draw { |m| m.connect ':controller/:action/:id' }
1633
- extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
1634
- assert_equal %w(that this), extras.map(&:to_s).sort
1635
- end
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
- def test_generate_extras_not_first
1638
- set.draw do |map|
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
- def test_named_draw
1673
- assert_equal 0, set.routes.size
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
- def test_later_named_routes_take_precedence
1682
- set.draw do |map|
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
- def setup_named_route_test
1690
- set.draw do |map|
1691
- map.show '/people/:id', :controller => 'people', :action => 'show'
1692
- map.index '/people', :controller => 'people', :action => 'index'
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
- klass = Class.new(MockController)
1698
- set.install_helpers(klass)
1699
- klass.new(set)
1700
- end
1726
+ def test_named_route_url_method_with_anchor
1727
+ controller = setup_named_route_test
1701
1728
 
1702
- def test_named_route_hash_access_method
1703
- controller = setup_named_route_test
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
- assert_equal(
1706
- { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false },
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
- assert_equal(
1710
- { :controller => 'people', :action => 'index', :use_route => :index, :only_path => false },
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
- assert_equal(
1714
- { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true },
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
- def test_named_route_url_method
1720
- controller = setup_named_route_test
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
- assert_equal "http://named.route.test/people/5", controller.send(:show_url, :id => 5)
1723
- assert_equal "/people/5", controller.send(:show_path, :id => 5)
1744
+ assert_equal "http://test.host/people?baz=bar#location",
1745
+ controller.send(:index_url, :baz => "bar", :anchor => 'location')
1746
+ end
1724
1747
 
1725
- assert_equal "http://named.route.test/people", controller.send(:index_url)
1726
- assert_equal "/people", controller.send(:index_path)
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
- assert_equal "http://named.route.test/admin/users", controller.send(:users_url)
1729
- assert_equal '/admin/users', controller.send(:users_path)
1730
- assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
1731
- end
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
- def test_named_route_url_method_with_anchor
1734
- controller = setup_named_route_test
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
- assert_equal "http://named.route.test/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
1737
- assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location')
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
- assert_equal "http://named.route.test/people#location", controller.send(:index_url, :anchor => 'location')
1740
- assert_equal "/people#location", controller.send(:index_path, :anchor => 'location')
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
- assert_equal "http://named.route.test/admin/users#location", controller.send(:users_url, :anchor => 'location')
1743
- assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location')
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
- assert_equal "http://named.route.test/people/go/7/hello/joe/5#location",
1746
- controller.send(:multi_url, 7, "hello", 5, :anchor => 'location')
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
- assert_equal "http://named.route.test/people/go/7/hello/joe/5?baz=bar#location",
1749
- controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location')
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 "http://named.route.test/people?baz=bar#location",
1752
- controller.send(:index_url, :baz => "bar", :anchor => 'location')
1753
- end
1793
+ assert_equal 1, set.routes.size
1794
+ route = set.routes.first
1754
1795
 
1755
- def test_named_route_url_method_with_port
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
- def test_named_route_url_method_with_host
1761
- controller = setup_named_route_test
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
- def test_named_route_url_method_with_protocol
1766
- controller = setup_named_route_test
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
- def test_named_route_url_method_with_ordered_parameters
1771
- controller = setup_named_route_test
1772
- assert_equal "http://named.route.test/people/go/7/hello/joe/5",
1773
- controller.send(:multi_url, 7, "hello", 5)
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
- def test_named_route_url_method_with_ordered_parameters_and_hash
1777
- controller = setup_named_route_test
1778
- assert_equal "http://named.route.test/people/go/7/hello/joe/5?baz=bar",
1779
- controller.send(:multi_url, 7, "hello", 5, :baz => "bar")
1780
- end
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
- def test_named_route_url_method_with_ordered_parameters_and_empty_hash
1783
- controller = setup_named_route_test
1784
- assert_equal "http://named.route.test/people/go/7/hello/joe/5",
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
- def test_named_route_url_method_with_no_positional_arguments
1789
- controller = setup_named_route_test
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
- def test_draw_default_route
1795
- ActionController::Routing.with_controllers(['users']) do
1796
- set.draw do |map|
1797
- map.connect '/:controller/:action/:id'
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
- def test_draw_default_route_with_default_controller
1814
- ActionController::Routing.with_controllers(['users']) do
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
- def test_route_with_parameter_shell
1823
- ActionController::Routing.with_controllers(['users', 'pages']) do
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
- def test_route_requirements_with_anchor_chars_are_invalid
1839
- assert_raises ArgumentError do
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
- assert_raises ArgumentError do
1855
- set.draw do |map|
1856
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\Z/
1857
- end
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
- assert_raises ArgumentError do
1860
- set.draw do |map|
1861
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\z/
1862
- end
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
- assert_nothing_raised do
1865
- set.draw do |map|
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
- def test_route_requirements_with_invalid_http_method_is_invalid
1875
- assert_raises ArgumentError do
1876
- set.draw do |map|
1877
- map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :invalid}
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
- def test_route_requirements_with_head_method_condition_is_invalid
1883
- assert_raises ArgumentError do
1884
- set.draw do |map|
1885
- map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :head}
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
- def test_non_path_route_requirements_match_all
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 'page/37s', :controller => 'pages', :action => 'show', :name => /(jamis|david)/
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
- def test_recognize_with_encoded_id_and_regex
1904
- set.draw do |map|
1905
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /[a-zA-Z0-9\+]+/
1906
- end
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
- assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
1909
- assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, set.recognize_path('/page/hello+world'))
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
- def test_recognize_with_conditions
1913
- Object.const_set(:PeopleController, Class.new)
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
- set.draw do |map|
1916
- map.with_options(:controller => "people") do |people|
1917
- people.people "/people", :action => "index", :conditions => { :method => :get }
1918
- people.connect "/people", :action => "create", :conditions => { :method => :post }
1919
- people.person "/people/:id", :action => "show", :conditions => { :method => :get }
1920
- people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
1921
- people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete }
1922
- end
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
- request.path = "/people"
1926
- request.method = :get
1927
- assert_nothing_raised { set.recognize(request) }
1928
- assert_equal("index", request.path_parameters[:action])
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
- request.method = :post
1931
- assert_nothing_raised { set.recognize(request) }
1932
- assert_equal("create", request.path_parameters[:action])
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
- request.method = :put
1935
- assert_nothing_raised { set.recognize(request) }
1936
- assert_equal("update", request.path_parameters[:action])
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
- begin
1939
- request.method = :bacon
1940
- set.recognize(request)
1941
- flunk 'Should have raised NotImplemented'
1942
- rescue ActionController::NotImplemented => e
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
- request.path = "/people/5"
1947
- request.method = :get
1948
- assert_nothing_raised { set.recognize(request) }
1949
- assert_equal("show", request.path_parameters[:action])
1950
- assert_equal("5", request.path_parameters[:id])
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
- request.method = :put
1953
- assert_nothing_raised { set.recognize(request) }
1954
- assert_equal("update", request.path_parameters[:action])
1955
- assert_equal("5", request.path_parameters[:id])
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
- request.method = :delete
1958
- assert_nothing_raised { set.recognize(request) }
1959
- assert_equal("destroy", request.path_parameters[:action])
1960
- assert_equal("5", request.path_parameters[:id])
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
- begin
1963
- request.method = :post
1964
- set.recognize(request)
1965
- flunk 'Should have raised MethodNotAllowed'
1966
- rescue ActionController::MethodNotAllowed => e
1967
- assert_equal [:get, :put, :delete], e.allowed_methods
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
- def test_recognize_with_alias_in_conditions
1975
- Object.const_set(:PeopleController, Class.new)
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
- request.path = "/people"
1984
- request.method = :get
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
- request.path = "/"
1990
- 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])
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
- def test_typo_recognition
1999
- Object.const_set(:ArticlesController, Class.new)
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
- set.draw do |map|
2002
- map.connect 'articles/:year/:month/:day/:title',
2003
- :controller => 'articles', :action => 'permalink',
2004
- :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
2005
- end
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
- request.path = "/articles/2005/11/05/a-very-interesting-article"
2008
- request.method = :get
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
- ensure
2017
- Object.send(:remove_const, :ArticlesController)
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
- def test_routing_traversal_does_not_load_extra_classes
2021
- assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
2022
- set.draw do |map|
2023
- map.connect '/profile', :controller => 'profile'
2024
- end
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
- request.path = '/profile'
2027
-
2028
- set.recognize(request) rescue nil
2022
+ ensure
2023
+ Object.send(:remove_const, :ArticlesController)
2024
+ end
2029
2025
 
2030
- assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
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
- def test_recognize_with_conditions_and_format
2034
- Object.const_set(:PeopleController, Class.new)
2032
+ request.path = '/profile'
2035
2033
 
2036
- set.draw do |map|
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
- request.path = "/people/5"
2045
- request.method = :get
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
- request.method = :put
2051
- assert_nothing_raised { set.recognize(request) }
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
- request.path = "/people/5.png"
2055
- request.method = :get
2056
- assert_nothing_raised { set.recognize(request) }
2057
- assert_equal("show", request.path_parameters[:action])
2058
- assert_equal("5", request.path_parameters[:id])
2059
- assert_equal("png", request.path_parameters[:_format])
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
- def test_generate_with_default_action
2065
- set.draw do |map|
2066
- map.connect "/people", :controller => "people"
2067
- map.connect "/people/list", :controller => "people", :action => "list"
2068
- end
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
- url = set.generate(:controller => "people", :action => "list")
2071
- assert_equal "/people/list", url
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
- def test_root_map
2075
- Object.const_set(:PeopleController, Class.new)
2078
+ url = set.generate(:controller => "people", :action => "list")
2079
+ assert_equal "/people/list", url
2080
+ end
2076
2081
 
2077
- set.draw { |map| map.root :controller => "people" }
2082
+ def test_root_map
2083
+ Object.const_set(:PeopleController, Class.new)
2078
2084
 
2079
- request.path = ""
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
- def test_namespace
2089
- Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
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
- set.draw do |map|
2096
+ def test_namespace
2097
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
2092
2098
 
2093
- map.namespace 'api' do |api|
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
- def test_namespaced_root_map
2109
- Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
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
- set.draw do |map|
2116
+ def test_namespaced_root_map
2117
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
2112
2118
 
2113
- map.namespace 'api' do |api|
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
- def test_namespace_with_path_prefix
2129
- Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
2130
-
2131
- set.draw do |map|
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
- map.namespace 'api', :path_prefix => 'prefix' do |api|
2134
- api.route 'inventory', :controller => "products", :action => 'inventory'
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
- def test_generate_finds_best_fit
2149
- set.draw do |map|
2150
- map.connect "/people", :controller => "people", :action => "index"
2151
- map.connect "/ws/people", :controller => "people", :action => "index", :ws => true
2152
- end
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
- url = set.generate(:controller => "people", :action => "index", :ws => true)
2155
- assert_equal "/ws/people", url
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
- def test_generate_changes_controller_module
2159
- set.draw { |map| map.connect ':controller/:action/:id' }
2160
- current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
2161
- url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current)
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
- def test_id_is_not_impossibly_sticky
2166
- set.draw do |map|
2167
- map.connect 'foo/:number', :controller => "people", :action => "index"
2168
- map.connect ':controller/:action/:id'
2169
- end
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
- url = set.generate({:controller => "people", :action => "index", :number => 3},
2172
- {:controller => "people", :action => "index", :id => "21"})
2173
- assert_equal "/foo/3", url
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
- def test_id_is_sticky_when_it_ought_to_be
2177
- set.draw do |map|
2178
- map.connect ':controller/:id/:action'
2179
- end
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
- url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
2182
- assert_equal "/people/7/destroy", url
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
- def test_use_static_path_when_possible
2186
- set.draw do |map|
2187
- map.connect 'about', :controller => "welcome", :action => "about"
2188
- map.connect ':controller/:action/:id'
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
- url = set.generate({:controller => "welcome", :action => "about"},
2192
- {:controller => "welcome", :action => "get", :id => "7"})
2193
- assert_equal "/about", url
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
- def test_generate
2197
- set.draw { |map| map.connect ':controller/:action/:id' }
2205
+ url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
2206
+ assert_equal "/people/7/destroy", url
2207
+ end
2198
2208
 
2199
- args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
2200
- assert_equal "/foo/bar/7?x=y", set.generate(args)
2201
- assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
2202
- assert_equal [:x], set.extra_keys(args)
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
- def test_generate_with_path_prefix
2206
- set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => 'my' }
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
- args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
2209
- assert_equal "/my/foo/bar/7?x=y", set.generate(args)
2210
- end
2220
+ def test_generate
2221
+ set.draw { |map| map.connect ':controller/:action/:id' }
2211
2222
 
2212
- def test_named_routes_are_never_relative_to_modules
2213
- set.draw do |map|
2214
- map.connect "/connection/manage/:action", :controller => 'connection/manage'
2215
- map.connect "/connection/connection", :controller => "connection/connection"
2216
- map.family_connection "/connection", :controller => "connection"
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
- url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'})
2220
- assert_equal "/connection/connection", url
2229
+ def test_generate_with_path_prefix
2230
+ set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => 'my' }
2221
2231
 
2222
- url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'})
2223
- assert_equal "/connection", url
2224
- end
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
- def test_action_left_off_when_id_is_recalled
2227
- set.draw do |map|
2228
- map.connect ':controller/:action/:id'
2229
- end
2230
- assert_equal '/post', set.generate(
2231
- {:controller => 'post', :action => 'index'},
2232
- {:controller => 'post', :action => 'show', :id => '10'}
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
- def test_query_params_will_be_shown_when_recalled
2237
- set.draw do |map|
2238
- map.connect 'show_post/:parameter', :controller => 'post', :action => 'show'
2239
- map.connect ':controller/:action/:id'
2240
- end
2241
- assert_equal '/post/edit?parameter=1', set.generate(
2242
- {:action => 'edit', :parameter => 1},
2243
- {:controller => 'post', :action => 'show', :parameter => 1}
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
- def test_expiry_determination_should_consider_values_with_to_param
2248
- set.draw { |map| map.connect 'projects/:project_id/:controller/:action' }
2249
- assert_equal '/projects/1/post/show', set.generate(
2250
- {:action => 'show', :project_id => 1},
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
- def test_generate_all
2255
- set.draw do |map|
2256
- map.connect 'show_post/:id', :controller => 'post', :action => 'show'
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
- def test_named_route_in_nested_resource
2269
- set.draw do |map|
2270
- map.resources :projects do |project|
2271
- project.milestones 'milestones', :controller => 'milestones', :action => 'index'
2272
- end
2273
- end
2283
+ assert_equal '/posts', set.generate(
2284
+ {:controller => 'posts'},
2285
+ {:controller => 'posts', :action => 'index', :format => 'xml'}
2286
+ )
2274
2287
 
2275
- request.path = "/projects/1/milestones"
2276
- request.method = :get
2277
- assert_nothing_raised { set.recognize(request) }
2278
- assert_equal("milestones", request.path_parameters[:controller])
2279
- assert_equal("index", request.path_parameters[:action])
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
- def test_setting_root_in_namespace_using_symbol
2283
- assert_nothing_raised do
2284
- set.draw do |map|
2285
- map.namespace :admin do |admin|
2286
- admin.root :controller => 'home'
2287
- end
2288
- end
2289
- end
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
- def test_setting_root_in_namespace_using_string
2293
- assert_nothing_raised do
2294
- set.draw do |map|
2295
- map.namespace 'admin' do |admin|
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
- def test_route_requirements_with_unsupported_regexp_options_must_error
2303
- assert_raises ArgumentError do
2304
- set.draw do |map|
2305
- map.connect 'page/:name', :controller => 'pages',
2306
- :action => 'show',
2307
- :requirements => {:name => /(david|jamis)/m}
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
- def test_route_requirements_with_supported_options_must_not_error
2313
- assert_nothing_raised do
2314
- set.draw do |map|
2315
- map.connect 'page/:name', :controller => 'pages',
2316
- :action => 'show',
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
- def test_route_requirement_recognize_with_ignore_case
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)/i}
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
- def test_route_requirement_generate_with_ignore_case
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
- def test_route_requirement_generate_with_extended_syntax
2384
- set.draw do |map|
2385
- map.connect 'page/:name', :controller => 'pages',
2386
- :action => 'show',
2387
- :requirements => {:name => / # Desperately overcommented regexp
2388
- ( #Either
2389
- david #The Creator
2390
- | #Or
2391
- jamis #The Deployer
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
- def test_route_requirement_generate_with_xi_modifiers
2405
- set.draw do |map|
2406
- map.connect 'page/:name', :controller => 'pages',
2407
- :action => 'show',
2408
- :requirements => {:name => / # Desperately overcommented regexp
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
- def test_route_requirement_recognize_with_xi_modifiers
2420
- set.draw do |map|
2421
- map.connect 'page/:name', :controller => 'pages',
2422
- :action => 'show',
2423
- :requirements => {:name => / # Desperately overcommented regexp
2424
- ( #Either
2425
- david #The Creator
2426
- | #Or
2427
- jamis #The Deployer
2428
- )/xi}
2429
- end
2430
- assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, set.recognize_path('/page/JAMIS'))
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
- class RouteLoadingTest < Test::Unit::TestCase
2435
- def setup
2436
- routes.instance_variable_set '@routes_last_modified', nil
2437
- silence_warnings { Object.const_set :RAILS_ROOT, '.' }
2438
- ActionController::Routing::Routes.configuration_file = File.join(RAILS_ROOT, 'config', 'routes.rb')
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
- @stat = stub_everything
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
- def teardown
2444
- ActionController::Routing::Routes.configuration_file = nil
2445
- Object.send :remove_const, :RAILS_ROOT
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
- def test_load
2449
- File.expects(:stat).returns(@stat)
2450
- routes.expects(:load).with(regexp_matches(/routes\.rb$/))
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
- routes.reload
2453
- end
2487
+ @stat = stub_everything
2488
+ end
2454
2489
 
2455
- def test_no_reload_when_not_modified
2456
- @stat.expects(:mtime).times(2).returns(1)
2457
- File.expects(:stat).times(2).returns(@stat)
2458
- routes.expects(:load).with(regexp_matches(/routes\.rb$/)).at_most_once
2490
+ def teardown
2491
+ ActionController::Routing::Routes.configuration_files.clear
2492
+ Object.send :remove_const, :RAILS_ROOT
2493
+ end
2459
2494
 
2460
- 2.times { routes.reload }
2461
- end
2495
+ def test_load
2496
+ File.expects(:stat).returns(@stat)
2497
+ routes.expects(:load).with(regexp_matches(/routes\.rb$/))
2462
2498
 
2463
- def test_reload_when_modified
2464
- @stat.expects(:mtime).at_least(2).returns(1, 2)
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
- 2.times { routes.reload }
2469
- end
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
- def test_bang_forces_reload
2472
- @stat.expects(:mtime).at_least(2).returns(1)
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
- 2.times { routes.reload! }
2477
- end
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
- def test_adding_inflections_forces_reload
2480
- ActiveSupport::Inflector::Inflections.instance.expects(:uncountable).with('equipment')
2481
- routes.expects(:reload!)
2515
+ 2.times { routes.reload }
2516
+ end
2482
2517
 
2483
- ActiveSupport::Inflector.inflections { |inflect| inflect.uncountable('equipment') }
2484
- end
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
- def test_load_with_configuration
2487
- routes.configuration_file = "foobarbaz"
2488
- File.expects(:stat).returns(@stat)
2489
- routes.expects(:load).with("foobarbaz")
2523
+ 2.times { routes.reload! }
2524
+ end
2490
2525
 
2491
- routes.reload
2492
- end
2526
+ def test_adding_inflections_forces_reload
2527
+ ActiveSupport::Inflector::Inflections.instance.expects(:uncountable).with('equipment')
2528
+ routes.expects(:reload!)
2493
2529
 
2494
- private
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