actionpack-2.3.17-rack-upgrade 2.3.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (441) hide show
  1. data/CHANGELOG +5250 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +409 -0
  4. data/RUNNING_UNIT_TESTS +24 -0
  5. data/Rakefile +158 -0
  6. data/install.rb +30 -0
  7. data/lib/action_controller.rb +113 -0
  8. data/lib/action_controller/assertions/dom_assertions.rb +55 -0
  9. data/lib/action_controller/assertions/model_assertions.rb +21 -0
  10. data/lib/action_controller/assertions/response_assertions.rb +169 -0
  11. data/lib/action_controller/assertions/routing_assertions.rb +146 -0
  12. data/lib/action_controller/assertions/selector_assertions.rb +638 -0
  13. data/lib/action_controller/assertions/tag_assertions.rb +127 -0
  14. data/lib/action_controller/base.rb +1425 -0
  15. data/lib/action_controller/benchmarking.rb +107 -0
  16. data/lib/action_controller/caching.rb +71 -0
  17. data/lib/action_controller/caching/actions.rb +177 -0
  18. data/lib/action_controller/caching/fragments.rb +120 -0
  19. data/lib/action_controller/caching/pages.rb +152 -0
  20. data/lib/action_controller/caching/sweeper.rb +45 -0
  21. data/lib/action_controller/caching/sweeping.rb +55 -0
  22. data/lib/action_controller/cgi_ext.rb +15 -0
  23. data/lib/action_controller/cgi_ext/cookie.rb +112 -0
  24. data/lib/action_controller/cgi_ext/query_extension.rb +22 -0
  25. data/lib/action_controller/cgi_ext/stdinput.rb +24 -0
  26. data/lib/action_controller/cgi_process.rb +77 -0
  27. data/lib/action_controller/cookies.rb +197 -0
  28. data/lib/action_controller/dispatcher.rb +133 -0
  29. data/lib/action_controller/failsafe.rb +87 -0
  30. data/lib/action_controller/filters.rb +680 -0
  31. data/lib/action_controller/flash.rb +213 -0
  32. data/lib/action_controller/headers.rb +33 -0
  33. data/lib/action_controller/helpers.rb +225 -0
  34. data/lib/action_controller/http_authentication.rb +309 -0
  35. data/lib/action_controller/integration.rb +708 -0
  36. data/lib/action_controller/layout.rb +286 -0
  37. data/lib/action_controller/middleware_stack.rb +119 -0
  38. data/lib/action_controller/middlewares.rb +14 -0
  39. data/lib/action_controller/mime_responds.rb +193 -0
  40. data/lib/action_controller/mime_type.rb +212 -0
  41. data/lib/action_controller/mime_types.rb +21 -0
  42. data/lib/action_controller/params_parser.rb +77 -0
  43. data/lib/action_controller/performance_test.rb +15 -0
  44. data/lib/action_controller/polymorphic_routes.rb +189 -0
  45. data/lib/action_controller/rack_lint_patch.rb +36 -0
  46. data/lib/action_controller/record_identifier.rb +104 -0
  47. data/lib/action_controller/reloader.rb +54 -0
  48. data/lib/action_controller/request.rb +518 -0
  49. data/lib/action_controller/request_forgery_protection.rb +116 -0
  50. data/lib/action_controller/rescue.rb +183 -0
  51. data/lib/action_controller/resources.rb +682 -0
  52. data/lib/action_controller/response.rb +237 -0
  53. data/lib/action_controller/routing.rb +388 -0
  54. data/lib/action_controller/routing/builder.rb +197 -0
  55. data/lib/action_controller/routing/optimisations.rb +130 -0
  56. data/lib/action_controller/routing/recognition_optimisation.rb +167 -0
  57. data/lib/action_controller/routing/route.rb +265 -0
  58. data/lib/action_controller/routing/route_set.rb +503 -0
  59. data/lib/action_controller/routing/routing_ext.rb +49 -0
  60. data/lib/action_controller/routing/segments.rb +343 -0
  61. data/lib/action_controller/session/abstract_store.rb +276 -0
  62. data/lib/action_controller/session/cookie_store.rb +240 -0
  63. data/lib/action_controller/session/mem_cache_store.rb +60 -0
  64. data/lib/action_controller/session_management.rb +54 -0
  65. data/lib/action_controller/status_codes.rb +88 -0
  66. data/lib/action_controller/streaming.rb +181 -0
  67. data/lib/action_controller/string_coercion.rb +29 -0
  68. data/lib/action_controller/templates/rescues/_request_and_response.erb +24 -0
  69. data/lib/action_controller/templates/rescues/_trace.erb +26 -0
  70. data/lib/action_controller/templates/rescues/diagnostics.erb +11 -0
  71. data/lib/action_controller/templates/rescues/layout.erb +29 -0
  72. data/lib/action_controller/templates/rescues/missing_template.erb +2 -0
  73. data/lib/action_controller/templates/rescues/routing_error.erb +10 -0
  74. data/lib/action_controller/templates/rescues/template_error.erb +21 -0
  75. data/lib/action_controller/templates/rescues/unknown_action.erb +2 -0
  76. data/lib/action_controller/test_case.rb +209 -0
  77. data/lib/action_controller/test_process.rb +580 -0
  78. data/lib/action_controller/translation.rb +13 -0
  79. data/lib/action_controller/uploaded_file.rb +44 -0
  80. data/lib/action_controller/url_rewriter.rb +229 -0
  81. data/lib/action_controller/vendor/html-scanner.rb +16 -0
  82. data/lib/action_controller/vendor/html-scanner/html/document.rb +68 -0
  83. data/lib/action_controller/vendor/html-scanner/html/node.rb +537 -0
  84. data/lib/action_controller/vendor/html-scanner/html/sanitizer.rb +173 -0
  85. data/lib/action_controller/vendor/html-scanner/html/selector.rb +828 -0
  86. data/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +105 -0
  87. data/lib/action_controller/vendor/html-scanner/html/version.rb +11 -0
  88. data/lib/action_controller/verification.rb +130 -0
  89. data/lib/action_pack.rb +24 -0
  90. data/lib/action_pack/version.rb +9 -0
  91. data/lib/action_view.rb +58 -0
  92. data/lib/action_view/base.rb +362 -0
  93. data/lib/action_view/helpers.rb +61 -0
  94. data/lib/action_view/helpers/active_record_helper.rb +305 -0
  95. data/lib/action_view/helpers/asset_tag_helper.rb +695 -0
  96. data/lib/action_view/helpers/atom_feed_helper.rb +198 -0
  97. data/lib/action_view/helpers/benchmark_helper.rb +54 -0
  98. data/lib/action_view/helpers/cache_helper.rb +39 -0
  99. data/lib/action_view/helpers/capture_helper.rb +136 -0
  100. data/lib/action_view/helpers/csrf_helper.rb +14 -0
  101. data/lib/action_view/helpers/date_helper.rb +989 -0
  102. data/lib/action_view/helpers/debug_helper.rb +38 -0
  103. data/lib/action_view/helpers/form_helper.rb +1118 -0
  104. data/lib/action_view/helpers/form_options_helper.rb +599 -0
  105. data/lib/action_view/helpers/form_tag_helper.rb +490 -0
  106. data/lib/action_view/helpers/javascript_helper.rb +208 -0
  107. data/lib/action_view/helpers/number_helper.rb +308 -0
  108. data/lib/action_view/helpers/prototype_helper.rb +1305 -0
  109. data/lib/action_view/helpers/raw_output_helper.rb +9 -0
  110. data/lib/action_view/helpers/record_identification_helper.rb +20 -0
  111. data/lib/action_view/helpers/record_tag_helper.rb +58 -0
  112. data/lib/action_view/helpers/sanitize_helper.rb +251 -0
  113. data/lib/action_view/helpers/scriptaculous_helper.rb +226 -0
  114. data/lib/action_view/helpers/tag_helper.rb +151 -0
  115. data/lib/action_view/helpers/text_helper.rb +597 -0
  116. data/lib/action_view/helpers/translation_helper.rb +67 -0
  117. data/lib/action_view/helpers/url_helper.rb +637 -0
  118. data/lib/action_view/inline_template.rb +19 -0
  119. data/lib/action_view/locale/en.yml +117 -0
  120. data/lib/action_view/partials.rb +241 -0
  121. data/lib/action_view/paths.rb +77 -0
  122. data/lib/action_view/reloadable_template.rb +117 -0
  123. data/lib/action_view/renderable.rb +109 -0
  124. data/lib/action_view/renderable_partial.rb +53 -0
  125. data/lib/action_view/template.rb +252 -0
  126. data/lib/action_view/template_error.rb +99 -0
  127. data/lib/action_view/template_handler.rb +34 -0
  128. data/lib/action_view/template_handlers.rb +48 -0
  129. data/lib/action_view/template_handlers/builder.rb +17 -0
  130. data/lib/action_view/template_handlers/erb.rb +25 -0
  131. data/lib/action_view/template_handlers/rjs.rb +13 -0
  132. data/lib/action_view/test_case.rb +162 -0
  133. data/lib/actionpack.rb +2 -0
  134. data/test/abstract_unit.rb +78 -0
  135. data/test/active_record_unit.rb +104 -0
  136. data/test/activerecord/active_record_store_test.rb +221 -0
  137. data/test/activerecord/render_partial_with_record_identification_test.rb +188 -0
  138. data/test/adv_attr_test.rb +20 -0
  139. data/test/controller/action_pack_assertions_test.rb +545 -0
  140. data/test/controller/addresses_render_test.rb +37 -0
  141. data/test/controller/assert_select_test.rb +735 -0
  142. data/test/controller/base_test.rb +217 -0
  143. data/test/controller/benchmark_test.rb +32 -0
  144. data/test/controller/caching_test.rb +743 -0
  145. data/test/controller/capture_test.rb +66 -0
  146. data/test/controller/content_type_test.rb +178 -0
  147. data/test/controller/controller_fixtures/app/controllers/admin/user_controller.rb +0 -0
  148. data/test/controller/controller_fixtures/app/controllers/user_controller.rb +0 -0
  149. data/test/controller/controller_fixtures/vendor/plugins/bad_plugin/lib/plugin_controller.rb +0 -0
  150. data/test/controller/cookie_test.rb +208 -0
  151. data/test/controller/deprecation/deprecated_base_methods_test.rb +32 -0
  152. data/test/controller/dispatcher_test.rb +144 -0
  153. data/test/controller/dom_assertions_test.rb +53 -0
  154. data/test/controller/failsafe_test.rb +60 -0
  155. data/test/controller/fake_controllers.rb +33 -0
  156. data/test/controller/fake_models.rb +19 -0
  157. data/test/controller/filter_params_test.rb +52 -0
  158. data/test/controller/filters_test.rb +885 -0
  159. data/test/controller/flash_test.rb +174 -0
  160. data/test/controller/header_test.rb +14 -0
  161. data/test/controller/helper_test.rb +224 -0
  162. data/test/controller/html-scanner/cdata_node_test.rb +15 -0
  163. data/test/controller/html-scanner/document_test.rb +148 -0
  164. data/test/controller/html-scanner/node_test.rb +89 -0
  165. data/test/controller/html-scanner/sanitizer_test.rb +281 -0
  166. data/test/controller/html-scanner/tag_node_test.rb +238 -0
  167. data/test/controller/html-scanner/text_node_test.rb +50 -0
  168. data/test/controller/html-scanner/tokenizer_test.rb +131 -0
  169. data/test/controller/http_basic_authentication_test.rb +113 -0
  170. data/test/controller/http_digest_authentication_test.rb +254 -0
  171. data/test/controller/integration_test.rb +526 -0
  172. data/test/controller/layout_test.rb +215 -0
  173. data/test/controller/localized_templates_test.rb +24 -0
  174. data/test/controller/logging_test.rb +46 -0
  175. data/test/controller/middleware_stack_test.rb +90 -0
  176. data/test/controller/mime_responds_test.rb +536 -0
  177. data/test/controller/mime_type_test.rb +93 -0
  178. data/test/controller/output_escaping_test.rb +19 -0
  179. data/test/controller/polymorphic_routes_test.rb +297 -0
  180. data/test/controller/rack_test.rb +308 -0
  181. data/test/controller/record_identifier_test.rb +139 -0
  182. data/test/controller/redirect_test.rb +285 -0
  183. data/test/controller/reloader_test.rb +125 -0
  184. data/test/controller/render_test.rb +1783 -0
  185. data/test/controller/request/json_params_parsing_test.rb +65 -0
  186. data/test/controller/request/multipart_params_parsing_test.rb +177 -0
  187. data/test/controller/request/query_string_parsing_test.rb +129 -0
  188. data/test/controller/request/test_request_test.rb +35 -0
  189. data/test/controller/request/url_encoded_params_parsing_test.rb +146 -0
  190. data/test/controller/request/xml_params_parsing_test.rb +103 -0
  191. data/test/controller/request_forgery_protection_test.rb +233 -0
  192. data/test/controller/request_test.rb +398 -0
  193. data/test/controller/rescue_test.rb +541 -0
  194. data/test/controller/resources_test.rb +1393 -0
  195. data/test/controller/routing_test.rb +2592 -0
  196. data/test/controller/selector_test.rb +628 -0
  197. data/test/controller/send_file_test.rb +171 -0
  198. data/test/controller/session/abstract_store_test.rb +64 -0
  199. data/test/controller/session/cookie_store_test.rb +354 -0
  200. data/test/controller/session/mem_cache_store_test.rb +187 -0
  201. data/test/controller/session/test_session_test.rb +58 -0
  202. data/test/controller/test_test.rb +700 -0
  203. data/test/controller/translation_test.rb +26 -0
  204. data/test/controller/url_rewriter_test.rb +395 -0
  205. data/test/controller/verification_test.rb +270 -0
  206. data/test/controller/view_paths_test.rb +141 -0
  207. data/test/controller/webservice_test.rb +273 -0
  208. data/test/fixtures/_top_level_partial.html.erb +1 -0
  209. data/test/fixtures/_top_level_partial_only.erb +1 -0
  210. data/test/fixtures/addresses/list.erb +1 -0
  211. data/test/fixtures/alternate_helpers/foo_helper.rb +3 -0
  212. data/test/fixtures/bad_customers/_bad_customer.html.erb +1 -0
  213. data/test/fixtures/companies.yml +24 -0
  214. data/test/fixtures/company.rb +10 -0
  215. data/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml +1 -0
  216. data/test/fixtures/content_type/render_default_for_rhtml.rhtml +1 -0
  217. data/test/fixtures/content_type/render_default_for_rjs.rjs +1 -0
  218. data/test/fixtures/content_type/render_default_for_rxml.rxml +1 -0
  219. data/test/fixtures/customers/_customer.html.erb +1 -0
  220. data/test/fixtures/db_definitions/sqlite.sql +49 -0
  221. data/test/fixtures/developer.rb +9 -0
  222. data/test/fixtures/developers.yml +21 -0
  223. data/test/fixtures/developers/_developer.erb +1 -0
  224. data/test/fixtures/developers_projects.yml +13 -0
  225. data/test/fixtures/failsafe/500.html +1 -0
  226. data/test/fixtures/fun/games/_game.erb +1 -0
  227. data/test/fixtures/fun/games/hello_world.erb +1 -0
  228. data/test/fixtures/fun/serious/games/_game.erb +1 -0
  229. data/test/fixtures/functional_caching/_partial.erb +3 -0
  230. data/test/fixtures/functional_caching/formatted_fragment_cached.html.erb +3 -0
  231. data/test/fixtures/functional_caching/formatted_fragment_cached.js.rjs +6 -0
  232. data/test/fixtures/functional_caching/formatted_fragment_cached.xml.builder +5 -0
  233. data/test/fixtures/functional_caching/fragment_cached.html.erb +2 -0
  234. data/test/fixtures/functional_caching/html_fragment_cached_with_partial.html.erb +1 -0
  235. data/test/fixtures/functional_caching/inline_fragment_cached.html.erb +2 -0
  236. data/test/fixtures/functional_caching/js_fragment_cached_with_partial.js.rjs +1 -0
  237. data/test/fixtures/good_customers/_good_customer.html.erb +1 -0
  238. data/test/fixtures/helpers/abc_helper.rb +5 -0
  239. data/test/fixtures/helpers/fun/games_helper.rb +3 -0
  240. data/test/fixtures/helpers/fun/pdf_helper.rb +3 -0
  241. data/test/fixtures/layout_tests/abs_path_layout.rhtml +1 -0
  242. data/test/fixtures/layout_tests/alt/hello.rhtml +1 -0
  243. data/test/fixtures/layout_tests/alt/layouts/alt.rhtml +0 -0
  244. data/test/fixtures/layout_tests/layouts/controller_name_space/nested.rhtml +1 -0
  245. data/test/fixtures/layout_tests/layouts/item.rhtml +1 -0
  246. data/test/fixtures/layout_tests/layouts/layout_test.rhtml +1 -0
  247. data/test/fixtures/layout_tests/layouts/multiple_extensions.html.erb +1 -0
  248. data/test/fixtures/layout_tests/layouts/third_party_template_library.mab +1 -0
  249. data/test/fixtures/layout_tests/views/hello.rhtml +1 -0
  250. data/test/fixtures/layouts/_column.html.erb +2 -0
  251. data/test/fixtures/layouts/block_with_layout.erb +3 -0
  252. data/test/fixtures/layouts/builder.builder +3 -0
  253. data/test/fixtures/layouts/default_html.html.erb +1 -0
  254. data/test/fixtures/layouts/partial_with_layout.erb +3 -0
  255. data/test/fixtures/layouts/standard.erb +1 -0
  256. data/test/fixtures/layouts/talk_from_action.erb +2 -0
  257. data/test/fixtures/layouts/xhr.html.erb +2 -0
  258. data/test/fixtures/layouts/yield.erb +2 -0
  259. data/test/fixtures/localized/hello_world.de.html +1 -0
  260. data/test/fixtures/localized/hello_world.en.html +1 -0
  261. data/test/fixtures/mascot.rb +3 -0
  262. data/test/fixtures/mascots.yml +4 -0
  263. data/test/fixtures/mascots/_mascot.html.erb +1 -0
  264. data/test/fixtures/multipart/binary_file +0 -0
  265. data/test/fixtures/multipart/boundary_problem_file +10 -0
  266. data/test/fixtures/multipart/bracketed_param +5 -0
  267. data/test/fixtures/multipart/empty +10 -0
  268. data/test/fixtures/multipart/hello.txt +1 -0
  269. data/test/fixtures/multipart/large_text_file +10 -0
  270. data/test/fixtures/multipart/mixed_files +0 -0
  271. data/test/fixtures/multipart/mona_lisa.jpg +0 -0
  272. data/test/fixtures/multipart/none +9 -0
  273. data/test/fixtures/multipart/single_parameter +5 -0
  274. data/test/fixtures/multipart/text_file +10 -0
  275. data/test/fixtures/override/test/hello_world.erb +1 -0
  276. data/test/fixtures/override2/layouts/test/sub.erb +1 -0
  277. data/test/fixtures/post_test/layouts/post.html.erb +1 -0
  278. data/test/fixtures/post_test/layouts/super_post.iphone.erb +1 -0
  279. data/test/fixtures/post_test/post/index.html.erb +1 -0
  280. data/test/fixtures/post_test/post/index.iphone.erb +1 -0
  281. data/test/fixtures/post_test/super_post/index.html.erb +1 -0
  282. data/test/fixtures/post_test/super_post/index.iphone.erb +1 -0
  283. data/test/fixtures/project.rb +3 -0
  284. data/test/fixtures/projects.yml +7 -0
  285. data/test/fixtures/projects/_project.erb +1 -0
  286. data/test/fixtures/public/404.html +1 -0
  287. data/test/fixtures/public/500.da.html +1 -0
  288. data/test/fixtures/public/500.html +1 -0
  289. data/test/fixtures/public/absolute/test.css +23 -0
  290. data/test/fixtures/public/absolute/test.js +63 -0
  291. data/test/fixtures/public/images/rails.png +0 -0
  292. data/test/fixtures/public/javascripts/application.js +1 -0
  293. data/test/fixtures/public/javascripts/bank.js +1 -0
  294. data/test/fixtures/public/javascripts/controls.js +1 -0
  295. data/test/fixtures/public/javascripts/dragdrop.js +1 -0
  296. data/test/fixtures/public/javascripts/effects.js +1 -0
  297. data/test/fixtures/public/javascripts/prototype.js +1 -0
  298. data/test/fixtures/public/javascripts/robber.js +1 -0
  299. data/test/fixtures/public/javascripts/subdir/subdir.js +1 -0
  300. data/test/fixtures/public/javascripts/version.1.0.js +1 -0
  301. data/test/fixtures/public/stylesheets/bank.css +1 -0
  302. data/test/fixtures/public/stylesheets/robber.css +1 -0
  303. data/test/fixtures/public/stylesheets/subdir/subdir.css +1 -0
  304. data/test/fixtures/public/stylesheets/version.1.0.css +1 -0
  305. data/test/fixtures/quiz/questions/_question.html.erb +1 -0
  306. data/test/fixtures/replies.yml +15 -0
  307. data/test/fixtures/replies/_reply.erb +1 -0
  308. data/test/fixtures/reply.rb +7 -0
  309. data/test/fixtures/respond_to/all_types_with_layout.html.erb +1 -0
  310. data/test/fixtures/respond_to/all_types_with_layout.js.rjs +1 -0
  311. data/test/fixtures/respond_to/custom_constant_handling_without_block.mobile.erb +1 -0
  312. data/test/fixtures/respond_to/iphone_with_html_response_type.html.erb +1 -0
  313. data/test/fixtures/respond_to/iphone_with_html_response_type.iphone.erb +1 -0
  314. data/test/fixtures/respond_to/layouts/missing.html.erb +1 -0
  315. data/test/fixtures/respond_to/layouts/standard.html.erb +1 -0
  316. data/test/fixtures/respond_to/layouts/standard.iphone.erb +1 -0
  317. data/test/fixtures/respond_to/using_defaults.html.erb +1 -0
  318. data/test/fixtures/respond_to/using_defaults.js.rjs +1 -0
  319. data/test/fixtures/respond_to/using_defaults.xml.builder +1 -0
  320. data/test/fixtures/respond_to/using_defaults_with_type_list.html.erb +1 -0
  321. data/test/fixtures/respond_to/using_defaults_with_type_list.js.rjs +1 -0
  322. data/test/fixtures/respond_to/using_defaults_with_type_list.xml.builder +1 -0
  323. data/test/fixtures/scope/test/modgreet.erb +1 -0
  324. data/test/fixtures/session_autoload_test/session_autoload_test/foo.rb +10 -0
  325. data/test/fixtures/shared.html.erb +1 -0
  326. data/test/fixtures/symlink_parent/symlinked_layout.erb +5 -0
  327. data/test/fixtures/test/_counter.html.erb +1 -0
  328. data/test/fixtures/test/_customer.erb +1 -0
  329. data/test/fixtures/test/_customer_counter.erb +1 -0
  330. data/test/fixtures/test/_customer_counter_with_as.erb +1 -0
  331. data/test/fixtures/test/_customer_greeting.erb +1 -0
  332. data/test/fixtures/test/_customer_with_var.erb +1 -0
  333. data/test/fixtures/test/_form.erb +1 -0
  334. data/test/fixtures/test/_from_helper.erb +1 -0
  335. data/test/fixtures/test/_hash_greeting.erb +1 -0
  336. data/test/fixtures/test/_hash_object.erb +2 -0
  337. data/test/fixtures/test/_hello.builder +1 -0
  338. data/test/fixtures/test/_labelling_form.erb +1 -0
  339. data/test/fixtures/test/_layout_for_block_with_args.html.erb +3 -0
  340. data/test/fixtures/test/_layout_for_partial.html.erb +3 -0
  341. data/test/fixtures/test/_local_inspector.html.erb +1 -0
  342. data/test/fixtures/test/_one.html.erb +1 -0
  343. data/test/fixtures/test/_partial.erb +1 -0
  344. data/test/fixtures/test/_partial.html.erb +1 -0
  345. data/test/fixtures/test/_partial.js.erb +1 -0
  346. data/test/fixtures/test/_partial_for_use_in_layout.html.erb +1 -0
  347. data/test/fixtures/test/_partial_only.erb +1 -0
  348. data/test/fixtures/test/_partial_with_only_html_version.html.erb +1 -0
  349. data/test/fixtures/test/_person.erb +2 -0
  350. data/test/fixtures/test/_raise.html.erb +1 -0
  351. data/test/fixtures/test/_two.html.erb +1 -0
  352. data/test/fixtures/test/_utf8_partial.html.erb +1 -0
  353. data/test/fixtures/test/_utf8_partial_magic.html.erb +2 -0
  354. data/test/fixtures/test/action_talk_to_layout.erb +2 -0
  355. data/test/fixtures/test/array_translation.erb +1 -0
  356. data/test/fixtures/test/calling_partial_with_layout.html.erb +1 -0
  357. data/test/fixtures/test/capturing.erb +4 -0
  358. data/test/fixtures/test/content_for.erb +2 -0
  359. data/test/fixtures/test/content_for_concatenated.erb +3 -0
  360. data/test/fixtures/test/content_for_with_parameter.erb +2 -0
  361. data/test/fixtures/test/delete_with_js.rjs +2 -0
  362. data/test/fixtures/test/dont_pick_me +1 -0
  363. data/test/fixtures/test/dot.directory/render_file_with_ivar.erb +1 -0
  364. data/test/fixtures/test/enum_rjs_test.rjs +6 -0
  365. data/test/fixtures/test/formatted_html_erb.html.erb +1 -0
  366. data/test/fixtures/test/formatted_xml_erb.builder +1 -0
  367. data/test/fixtures/test/formatted_xml_erb.html.erb +1 -0
  368. data/test/fixtures/test/formatted_xml_erb.xml.erb +1 -0
  369. data/test/fixtures/test/greeting.erb +1 -0
  370. data/test/fixtures/test/greeting.js.rjs +1 -0
  371. data/test/fixtures/test/hello.builder +4 -0
  372. data/test/fixtures/test/hello_world.da.html.erb +1 -0
  373. data/test/fixtures/test/hello_world.erb +1 -0
  374. data/test/fixtures/test/hello_world.erb~ +1 -0
  375. data/test/fixtures/test/hello_world.pt-BR.html.erb +1 -0
  376. data/test/fixtures/test/hello_world_container.builder +3 -0
  377. data/test/fixtures/test/hello_world_from_rxml.builder +4 -0
  378. data/test/fixtures/test/hello_world_with_layout_false.erb +1 -0
  379. data/test/fixtures/test/hello_xml_world.builder +11 -0
  380. data/test/fixtures/test/hyphen-ated.erb +1 -0
  381. data/test/fixtures/test/implicit_content_type.atom.builder +2 -0
  382. data/test/fixtures/test/list.erb +1 -0
  383. data/test/fixtures/test/malformed/malformed.en.html.erb~ +1 -0
  384. data/test/fixtures/test/malformed/malformed.erb~ +1 -0
  385. data/test/fixtures/test/malformed/malformed.html.erb~ +1 -0
  386. data/test/fixtures/test/nested_layout.erb +3 -0
  387. data/test/fixtures/test/non_erb_block_content_for.builder +4 -0
  388. data/test/fixtures/test/potential_conflicts.erb +4 -0
  389. data/test/fixtures/test/render_explicit_html_template.js.rjs +1 -0
  390. data/test/fixtures/test/render_file_from_template.html.erb +1 -0
  391. data/test/fixtures/test/render_file_with_ivar.erb +1 -0
  392. data/test/fixtures/test/render_file_with_locals.erb +1 -0
  393. data/test/fixtures/test/render_implicit_html_template.js.rjs +1 -0
  394. data/test/fixtures/test/render_implicit_html_template_from_xhr_request.da.html.erb +1 -0
  395. data/test/fixtures/test/render_implicit_html_template_from_xhr_request.html.erb +1 -0
  396. data/test/fixtures/test/render_implicit_js_template_without_layout.js.erb +1 -0
  397. data/test/fixtures/test/render_to_string_test.erb +1 -0
  398. data/test/fixtures/test/scoped_array_translation.erb +1 -0
  399. data/test/fixtures/test/sub_template_raise.html.erb +1 -0
  400. data/test/fixtures/test/template.erb +1 -0
  401. data/test/fixtures/test/translation.erb +1 -0
  402. data/test/fixtures/test/update_element_with_capture.erb +9 -0
  403. data/test/fixtures/test/using_layout_around_block.html.erb +1 -0
  404. data/test/fixtures/test/using_layout_around_block_with_args.html.erb +1 -0
  405. data/test/fixtures/test/utf8.html.erb +4 -0
  406. data/test/fixtures/test/utf8_magic.html.erb +5 -0
  407. data/test/fixtures/test/utf8_magic_with_bare_partial.html.erb +5 -0
  408. data/test/fixtures/topic.rb +3 -0
  409. data/test/fixtures/topics.yml +22 -0
  410. data/test/fixtures/topics/_topic.html.erb +1 -0
  411. data/test/template/active_record_helper_i18n_test.rb +51 -0
  412. data/test/template/active_record_helper_test.rb +302 -0
  413. data/test/template/asset_tag_helper_test.rb +770 -0
  414. data/test/template/atom_feed_helper_test.rb +315 -0
  415. data/test/template/benchmark_helper_test.rb +86 -0
  416. data/test/template/compiled_templates_test.rb +204 -0
  417. data/test/template/date_helper_i18n_test.rb +121 -0
  418. data/test/template/date_helper_test.rb +2603 -0
  419. data/test/template/erb_util_test.rb +36 -0
  420. data/test/template/form_helper_test.rb +1447 -0
  421. data/test/template/form_options_helper_i18n_test.rb +27 -0
  422. data/test/template/form_options_helper_test.rb +811 -0
  423. data/test/template/form_tag_helper_test.rb +356 -0
  424. data/test/template/javascript_helper_test.rb +106 -0
  425. data/test/template/number_helper_i18n_test.rb +69 -0
  426. data/test/template/number_helper_test.rb +132 -0
  427. data/test/template/prototype_helper_test.rb +639 -0
  428. data/test/template/raw_output_helper_test.rb +21 -0
  429. data/test/template/record_tag_helper_test.rb +58 -0
  430. data/test/template/render_test.rb +329 -0
  431. data/test/template/sanitize_helper_test.rb +57 -0
  432. data/test/template/scriptaculous_helper_test.rb +90 -0
  433. data/test/template/tag_helper_test.rb +98 -0
  434. data/test/template/template_test.rb +32 -0
  435. data/test/template/test_test.rb +54 -0
  436. data/test/template/text_helper_test.rb +601 -0
  437. data/test/template/translation_helper_test.rb +95 -0
  438. data/test/template/url_helper_test.rb +641 -0
  439. data/test/testing_sandbox.rb +15 -0
  440. data/test/view/test_case_test.rb +176 -0
  441. metadata +519 -0
@@ -0,0 +1,2592 @@
1
+ # encoding: us-ascii
2
+ require 'abstract_unit'
3
+ require 'controller/fake_controllers'
4
+ require 'action_controller/routing/route_set'
5
+
6
+ class MilestonesController < ActionController::Base
7
+ def index() head :ok end
8
+ alias_method :show, :index
9
+ def rescue_action(e) raise e end
10
+ end
11
+
12
+ RunTimeTests = ARGV.include? 'time'
13
+ ROUTING = ActionController::Routing
14
+
15
+ class ROUTING::RouteBuilder
16
+ attr_reader :warn_output
17
+
18
+ def warn(msg)
19
+ (@warn_output ||= []) << msg
20
+ end
21
+ end
22
+
23
+ # See RFC 3986, section 3.3 for allowed path characters.
24
+ class UriReservedCharactersRoutingTest < Test::Unit::TestCase
25
+ def setup
26
+ ActionController::Routing.use_controllers! ['controller']
27
+ @set = ActionController::Routing::RouteSet.new
28
+ @set.draw do |map|
29
+ map.connect ':controller/:action/:variable/*additional'
30
+ end
31
+
32
+ safe, unsafe = %w(: @ & = + $ , ;), %w(^ / ? # [ ])
33
+ hex = unsafe.map { |char| '%' + char.unpack('H2').first.upcase }
34
+
35
+ @segment = "#{safe.join}#{unsafe.join}".freeze
36
+ @escaped = "#{safe.join}#{hex.join}".freeze
37
+ end
38
+
39
+ def test_route_generation_escapes_unsafe_path_characters
40
+ assert_equal "/contr#{@segment}oller/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2",
41
+ @set.generate(:controller => "contr#{@segment}oller",
42
+ :action => "act#{@segment}ion",
43
+ :variable => "var#{@segment}iable",
44
+ :additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"])
45
+ end
46
+
47
+ def test_route_recognition_unescapes_path_components
48
+ options = { :controller => "controller",
49
+ :action => "act#{@segment}ion",
50
+ :variable => "var#{@segment}iable",
51
+ :additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"] }
52
+ assert_equal options, @set.recognize_path("/controller/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2")
53
+ end
54
+
55
+ def test_route_generation_allows_passing_non_string_values_to_generated_helper
56
+ assert_equal "/controller/action/variable/1/2", @set.generate(:controller => "controller",
57
+ :action => "action",
58
+ :variable => "variable",
59
+ :additional => [1, 2])
60
+ end
61
+ end
62
+
63
+ class SegmentTest < Test::Unit::TestCase
64
+ def test_first_segment_should_interpolate_for_structure
65
+ s = ROUTING::Segment.new
66
+ def s.interpolation_statement(array) 'hello' end
67
+ assert_equal 'hello', s.continue_string_structure([])
68
+ end
69
+
70
+ def test_interpolation_statement
71
+ s = ROUTING::StaticSegment.new("Hello")
72
+ assert_equal "Hello", eval(s.interpolation_statement([]))
73
+ assert_equal "HelloHello", eval(s.interpolation_statement([s]))
74
+
75
+ s2 = ROUTING::StaticSegment.new("-")
76
+ assert_equal "Hello-Hello", eval(s.interpolation_statement([s, s2]))
77
+
78
+ s3 = ROUTING::StaticSegment.new("World")
79
+ assert_equal "Hello-World", eval(s3.interpolation_statement([s, s2]))
80
+ end
81
+ end
82
+
83
+ class StaticSegmentTest < Test::Unit::TestCase
84
+ def test_interpolation_chunk_should_respect_raw
85
+ s = ROUTING::StaticSegment.new('Hello World')
86
+ assert !s.raw?
87
+ assert_equal 'Hello%20World', s.interpolation_chunk
88
+
89
+ s = ROUTING::StaticSegment.new('Hello World', :raw => true)
90
+ assert s.raw?
91
+ assert_equal 'Hello World', s.interpolation_chunk
92
+ end
93
+
94
+ def test_regexp_chunk_should_escape_specials
95
+ s = ROUTING::StaticSegment.new('Hello*World')
96
+ assert_equal 'Hello\*World', s.regexp_chunk
97
+
98
+ s = ROUTING::StaticSegment.new('HelloWorld')
99
+ assert_equal 'HelloWorld', s.regexp_chunk
100
+ end
101
+
102
+ def test_regexp_chunk_should_add_question_mark_for_optionals
103
+ s = ROUTING::StaticSegment.new("/", :optional => true)
104
+ assert_equal "/?", s.regexp_chunk
105
+
106
+ s = ROUTING::StaticSegment.new("hello", :optional => true)
107
+ assert_equal "(?:hello)?", s.regexp_chunk
108
+ end
109
+ end
110
+
111
+ class DynamicSegmentTest < ActiveSupport::TestCase
112
+ def setup
113
+ @segment = nil
114
+ end
115
+
116
+ def segment(options = {})
117
+ unless @segment
118
+ @segment = ROUTING::DynamicSegment.new(:a, options)
119
+ end
120
+ @segment
121
+ end
122
+
123
+ def test_extract_value
124
+ s = ROUTING::DynamicSegment.new(:a)
125
+
126
+ hash = {:a => '10', :b => '20'}
127
+ assert_equal '10', eval(s.extract_value)
128
+
129
+ hash = {:b => '20'}
130
+ assert_equal nil, eval(s.extract_value)
131
+
132
+ s.default = '20'
133
+ assert_equal '20', eval(s.extract_value)
134
+ end
135
+
136
+ def test_default_local_name
137
+ assert_equal 'a_value', segment.local_name,
138
+ "Unexpected name -- all value_check tests will fail!"
139
+ end
140
+
141
+ def test_presence_value_check
142
+ a_value = 10
143
+ assert eval(segment.value_check)
144
+ end
145
+
146
+ def test_regexp_value_check_rejects_nil
147
+ segment = segment(:regexp => /\d+/)
148
+
149
+ a_value = nil
150
+ assert !eval(segment.value_check)
151
+ end
152
+
153
+ def test_optional_regexp_value_check_should_accept_nil
154
+ segment = segment(:regexp => /\d+/, :optional => true)
155
+
156
+ a_value = nil
157
+ assert eval(segment.value_check)
158
+ end
159
+
160
+ def test_regexp_value_check_rejects_no_match
161
+ segment = segment(:regexp => /\d+/)
162
+
163
+ a_value = "Hello20World"
164
+ assert !eval(segment.value_check)
165
+
166
+ a_value = "20Hi"
167
+ assert !eval(segment.value_check)
168
+ end
169
+
170
+ def test_regexp_value_check_accepts_match
171
+ segment = segment(:regexp => /\d+/)
172
+ a_value = "30"
173
+ assert eval(segment.value_check)
174
+ end
175
+
176
+ def test_value_check_fails_on_nil
177
+ a_value = nil
178
+ assert ! eval(segment.value_check)
179
+ end
180
+
181
+ def test_optional_value_needs_no_check
182
+ segment = segment(:optional => true)
183
+
184
+ a_value = nil
185
+ assert_equal nil, segment.value_check
186
+ end
187
+
188
+ def test_regexp_value_check_should_accept_match_with_default
189
+ segment = segment(:regexp => /\d+/, :default => '200')
190
+
191
+ a_value = '100'
192
+ assert eval(segment.value_check)
193
+ end
194
+
195
+ def test_expiry_should_not_trigger_once_expired
196
+ expired = true
197
+ hash = merged = {:a => 2, :b => 3}
198
+ options = {:b => 3}
199
+ expire_on = Hash.new { raise 'No!!!' }
200
+
201
+ eval(segment.expiry_statement)
202
+ rescue RuntimeError
203
+ flunk "Expiry check should not have occurred!"
204
+ end
205
+
206
+ def test_expiry_should_occur_according_to_expire_on
207
+ expired = false
208
+ hash = merged = {:a => 2, :b => 3}
209
+ options = {:b => 3}
210
+
211
+ expire_on = {:b => true, :a => false}
212
+ eval(segment.expiry_statement)
213
+ assert !expired
214
+ assert_equal({:a => 2, :b => 3}, hash)
215
+
216
+ expire_on = {:b => true, :a => true}
217
+ eval(segment.expiry_statement)
218
+ assert expired
219
+ assert_equal({:b => 3}, hash)
220
+ end
221
+
222
+ def test_extraction_code_should_return_on_nil
223
+ hash = merged = {:b => 3}
224
+ options = {:b => 3}
225
+ a_value = nil
226
+
227
+ # Local jump because of return inside eval.
228
+ assert_raise(LocalJumpError) { eval(segment.extraction_code) }
229
+ end
230
+
231
+ def test_extraction_code_should_return_on_mismatch
232
+ segment = segment(:regexp => /\d+/)
233
+ hash = merged = {:a => 'Hi', :b => '3'}
234
+ options = {:b => '3'}
235
+ a_value = nil
236
+
237
+ # Local jump because of return inside eval.
238
+ assert_raise(LocalJumpError) { eval(segment.extraction_code) }
239
+ end
240
+
241
+ def test_extraction_code_should_accept_value_and_set_local
242
+ hash = merged = {:a => 'Hi', :b => '3'}
243
+ options = {:b => '3'}
244
+ a_value = nil
245
+ expired = true
246
+
247
+ eval(segment.extraction_code)
248
+ assert_equal 'Hi', a_value
249
+ end
250
+
251
+ def test_extraction_should_work_without_value_check
252
+ segment.default = 'hi'
253
+ hash = merged = {:b => '3'}
254
+ options = {:b => '3'}
255
+ a_value = nil
256
+ expired = true
257
+
258
+ eval(segment.extraction_code)
259
+ assert_equal 'hi', a_value
260
+ end
261
+
262
+ def test_extraction_code_should_perform_expiry
263
+ expired = false
264
+ hash = merged = {:a => 'Hi', :b => '3'}
265
+ options = {:b => '3'}
266
+ expire_on = {:a => true}
267
+ a_value = nil
268
+
269
+ eval(segment.extraction_code)
270
+ assert_equal 'Hi', a_value
271
+ assert expired
272
+ assert_equal options, hash
273
+ end
274
+
275
+ def test_interpolation_chunk_should_replace_value
276
+ a_value = 'Hi'
277
+ assert_equal a_value, eval(%("#{segment.interpolation_chunk}"))
278
+ end
279
+
280
+ def test_interpolation_chunk_should_accept_nil
281
+ a_value = nil
282
+ assert_equal '', eval(%("#{segment.interpolation_chunk('a_value')}"))
283
+ end
284
+
285
+ def test_value_regexp_should_be_nil_without_regexp
286
+ assert_equal nil, segment.value_regexp
287
+ end
288
+
289
+ def test_value_regexp_should_match_exacly
290
+ segment = segment(:regexp => /\d+/)
291
+ assert_no_match segment.value_regexp, "Hello 10 World"
292
+ assert_no_match segment.value_regexp, "Hello 10"
293
+ assert_no_match segment.value_regexp, "10 World"
294
+ assert_match segment.value_regexp, "10"
295
+ end
296
+
297
+ def test_regexp_chunk_should_return_string
298
+ segment = segment(:regexp => /\d+/)
299
+ assert_kind_of String, segment.regexp_chunk
300
+ end
301
+
302
+ def test_build_pattern_non_optional_with_no_captures
303
+ # Non optional
304
+ a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /\d+/)
305
+ assert_equal "(\\d+)stuff", a_segment.build_pattern('stuff')
306
+ end
307
+
308
+ def test_build_pattern_non_optional_with_captures
309
+ # Non optional
310
+ a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /(\d+)(.*?)/)
311
+ assert_equal "((\\d+)(.*?))stuff", a_segment.build_pattern('stuff')
312
+ end
313
+
314
+ def test_optionality_implied
315
+ a_segment = ROUTING::DynamicSegment.new(:id)
316
+ assert a_segment.optionality_implied?
317
+
318
+ a_segment = ROUTING::DynamicSegment.new(:action)
319
+ assert a_segment.optionality_implied?
320
+ end
321
+
322
+ def test_modifiers_must_be_handled_sensibly
323
+ a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/i)
324
+ assert_equal "((?i-mx:david|jamis))stuff", a_segment.build_pattern('stuff')
325
+ a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/x)
326
+ assert_equal "((?x-mi:david|jamis))stuff", a_segment.build_pattern('stuff')
327
+ a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/)
328
+ assert_equal "(david|jamis)stuff", a_segment.build_pattern('stuff')
329
+ end
330
+ end
331
+
332
+ class ControllerSegmentTest < Test::Unit::TestCase
333
+ def test_regexp_should_only_match_possible_controllers
334
+ ActionController::Routing.with_controllers %w(admin/accounts admin/users account pages) do
335
+ cs = ROUTING::ControllerSegment.new :controller
336
+ regexp = %r{\A#{cs.regexp_chunk}\Z}
337
+
338
+ ActionController::Routing.possible_controllers.each do |name|
339
+ assert_match regexp, name
340
+ assert_no_match regexp, "#{name}_fake"
341
+
342
+ match = regexp.match name
343
+ assert_equal name, match[1]
344
+ end
345
+ end
346
+ end
347
+ end
348
+
349
+ class PathSegmentTest < ActiveSupport::TestCase
350
+ def setup
351
+ @segment = nil
352
+ end
353
+
354
+ def segment(options = {})
355
+ unless @segment
356
+ @segment = ROUTING::PathSegment.new(:path, options)
357
+ end
358
+ @segment
359
+ end
360
+
361
+ def test_regexp_chunk_should_return_string
362
+ segment = segment(:regexp => /[a-z]+/)
363
+ assert_kind_of String, segment.regexp_chunk
364
+ end
365
+
366
+ def test_regexp_chunk_should_be_wrapped_with_parenthesis
367
+ segment = segment(:regexp => /[a-z]+/)
368
+ assert_equal "([a-z]+)", segment.regexp_chunk
369
+ end
370
+
371
+ def test_regexp_chunk_should_respect_options
372
+ segment = segment(:regexp => /[a-z]+/i)
373
+ assert_equal "((?i-mx:[a-z]+))", segment.regexp_chunk
374
+ end
375
+ end
376
+
377
+ class RouteBuilderTest < Test::Unit::TestCase
378
+ def builder
379
+ @builder ||= ROUTING::RouteBuilder.new
380
+ end
381
+
382
+ def build(path, options)
383
+ builder.build(path, options)
384
+ end
385
+
386
+ def test_options_should_not_be_modified
387
+ requirements1 = { :id => /\w+/, :controller => /(?:[a-z](?:-?[a-z]+)*)/ }
388
+ requirements2 = requirements1.dup
389
+
390
+ assert_equal requirements1, requirements2
391
+
392
+ with_options(:controller => 'folder',
393
+ :requirements => requirements2) do |m|
394
+ m.build 'folders/new', :action => 'new'
395
+ end
396
+
397
+ assert_equal requirements1, requirements2
398
+ end
399
+
400
+ def test_segment_for_static
401
+ segment, rest = builder.segment_for 'ulysses'
402
+ assert_equal '', rest
403
+ assert_kind_of ROUTING::StaticSegment, segment
404
+ assert_equal 'ulysses', segment.value
405
+ end
406
+
407
+ def test_segment_for_action
408
+ segment, rest = builder.segment_for ':action'
409
+ assert_equal '', rest
410
+ assert_kind_of ROUTING::DynamicSegment, segment
411
+ assert_equal :action, segment.key
412
+ assert_equal 'index', segment.default
413
+ end
414
+
415
+ def test_segment_for_dynamic
416
+ segment, rest = builder.segment_for ':login'
417
+ assert_equal '', rest
418
+ assert_kind_of ROUTING::DynamicSegment, segment
419
+ assert_equal :login, segment.key
420
+ assert_equal nil, segment.default
421
+ assert ! segment.optional?
422
+ end
423
+
424
+ def test_segment_for_with_rest
425
+ segment, rest = builder.segment_for ':login/:action'
426
+ assert_equal :login, segment.key
427
+ assert_equal '/:action', rest
428
+ segment, rest = builder.segment_for rest
429
+ assert_equal '/', segment.value
430
+ assert_equal ':action', rest
431
+ segment, rest = builder.segment_for rest
432
+ assert_equal :action, segment.key
433
+ assert_equal '', rest
434
+ end
435
+
436
+ def test_segments_for
437
+ segments = builder.segments_for_route_path '/:controller/:action/:id'
438
+
439
+ assert_kind_of ROUTING::DividerSegment, segments[0]
440
+ assert_equal '/', segments[2].value
441
+
442
+ assert_kind_of ROUTING::DynamicSegment, segments[1]
443
+ assert_equal :controller, segments[1].key
444
+
445
+ assert_kind_of ROUTING::DividerSegment, segments[2]
446
+ assert_equal '/', segments[2].value
447
+
448
+ assert_kind_of ROUTING::DynamicSegment, segments[3]
449
+ assert_equal :action, segments[3].key
450
+
451
+ assert_kind_of ROUTING::DividerSegment, segments[4]
452
+ assert_equal '/', segments[4].value
453
+
454
+ assert_kind_of ROUTING::DynamicSegment, segments[5]
455
+ assert_equal :id, segments[5].key
456
+ end
457
+
458
+ def test_segment_for_action
459
+ s, r = builder.segment_for(':action/something/else')
460
+ assert_equal '/something/else', r
461
+ assert_equal :action, s.key
462
+ end
463
+
464
+ def test_action_default_should_not_trigger_on_prefix
465
+ s, r = builder.segment_for ':action_name/something/else'
466
+ assert_equal '/something/else', r
467
+ assert_equal :action_name, s.key
468
+ assert_equal nil, s.default
469
+ end
470
+
471
+ def test_divide_route_options
472
+ segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
473
+ defaults, requirements = builder.divide_route_options(segments,
474
+ :action => 'buy', :person => /\w+/, :car => /\w+/,
475
+ :defaults => {:person => nil, :car => nil}
476
+ )
477
+
478
+ assert_equal({:action => 'buy', :person => nil, :car => nil}, defaults)
479
+ assert_equal({:person => /\w+/, :car => /\w+/}, requirements)
480
+ end
481
+
482
+ def test_assign_route_options
483
+ segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
484
+ defaults = {:action => 'buy', :person => nil, :car => nil}
485
+ requirements = {:person => /\w+/, :car => /\w+/}
486
+
487
+ route_requirements = builder.assign_route_options(segments, defaults, requirements)
488
+ assert_equal({}, route_requirements)
489
+
490
+ assert_equal :action, segments[3].key
491
+ assert_equal 'buy', segments[3].default
492
+
493
+ assert_equal :person, segments[5].key
494
+ assert_equal %r/\w+/, segments[5].regexp
495
+ assert segments[5].optional?
496
+
497
+ assert_equal :car, segments[7].key
498
+ assert_equal %r/\w+/, segments[7].regexp
499
+ assert segments[7].optional?
500
+ end
501
+
502
+ def test_assign_route_options_with_anchor_chars
503
+ segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
504
+ defaults = {:action => 'buy', :person => nil, :car => nil}
505
+ requirements = {:person => /\w+/, :car => /^\w+$/}
506
+
507
+ assert_raise ArgumentError do
508
+ route_requirements = builder.assign_route_options(segments, defaults, requirements)
509
+ end
510
+
511
+ requirements[:car] = /[^\/]+/
512
+ route_requirements = builder.assign_route_options(segments, defaults, requirements)
513
+ end
514
+
515
+ def test_optional_segments_preceding_required_segments
516
+ segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
517
+ defaults = {:action => 'buy', :person => nil, :car => "model-t"}
518
+ assert builder.assign_route_options(segments, defaults, {}).empty?
519
+
520
+ 0.upto(1) { |i| assert !segments[i].optional?, "segment #{i} is optional and it shouldn't be" }
521
+ assert segments[2].optional?
522
+
523
+ assert_equal nil, builder.warn_output # should only warn on the :person segment
524
+ end
525
+
526
+ def test_segmentation_of_dot_path
527
+ segments = builder.segments_for_route_path '/books/:action.rss'
528
+ assert builder.assign_route_options(segments, {}, {}).empty?
529
+ assert_equal 6, segments.length # "/", "books", "/", ":action", ".", "rss"
530
+ assert !segments.any? { |seg| seg.optional? }
531
+ end
532
+
533
+ def test_segmentation_of_dynamic_dot_path
534
+ segments = builder.segments_for_route_path '/books/:action.:format'
535
+ assert builder.assign_route_options(segments, {}, {}).empty?
536
+ assert_equal 6, segments.length # "/", "books", "/", ":action", ".", ":format"
537
+ assert !segments.any? { |seg| seg.optional? }
538
+ assert_kind_of ROUTING::DynamicSegment, segments.last
539
+ end
540
+
541
+ def test_assignment_of_default_options
542
+ segments = builder.segments_for_route_path '/:controller/:action/:id/'
543
+ action, id = segments[-4], segments[-2]
544
+
545
+ assert_equal :action, action.key
546
+ assert_equal :id, id.key
547
+ assert ! action.optional?
548
+ assert ! id.optional?
549
+
550
+ builder.assign_default_route_options(segments)
551
+
552
+ assert_equal 'index', action.default
553
+ assert action.optional?
554
+ assert id.optional?
555
+ end
556
+
557
+ def test_assignment_of_default_options_respects_existing_defaults
558
+ segments = builder.segments_for_route_path '/:controller/:action/:id/'
559
+ action, id = segments[-4], segments[-2]
560
+
561
+ assert_equal :action, action.key
562
+ assert_equal :id, id.key
563
+ action.default = 'show'
564
+ action.is_optional = true
565
+
566
+ id.default = 'Welcome'
567
+ id.is_optional = true
568
+
569
+ builder.assign_default_route_options(segments)
570
+
571
+ assert_equal 'show', action.default
572
+ assert action.optional?
573
+ assert_equal 'Welcome', id.default
574
+ assert id.optional?
575
+ end
576
+
577
+ def test_assignment_of_default_options_respects_regexps
578
+ segments = builder.segments_for_route_path '/:controller/:action/:id/'
579
+ action = segments[-4]
580
+
581
+ assert_equal :action, action.key
582
+ segments[-4] = ROUTING::DynamicSegment.new(:action, :regexp => /show|in/)
583
+
584
+ builder.assign_default_route_options(segments)
585
+
586
+ assert_equal nil, action.default
587
+ assert ! action.optional?
588
+ end
589
+
590
+ def test_assignment_of_is_optional_when_default
591
+ segments = builder.segments_for_route_path '/books/:action.rss'
592
+ assert_equal segments[3].key, :action
593
+ segments[3].default = 'changes'
594
+ builder.ensure_required_segments(segments)
595
+ assert ! segments[3].optional?
596
+ end
597
+
598
+ def test_is_optional_is_assigned_to_default_segments
599
+ segments = builder.segments_for_route_path '/books/:action'
600
+ builder.assign_route_options(segments, {:action => 'index'}, {})
601
+
602
+ assert_equal segments[3].key, :action
603
+ assert segments[3].optional?
604
+ assert_kind_of ROUTING::DividerSegment, segments[2]
605
+ assert segments[2].optional?
606
+ end
607
+
608
+ # XXX is optional not being set right?
609
+ # /blah/:defaulted_segment <-- is the second slash optional? it should be.
610
+
611
+ def test_route_build
612
+ ActionController::Routing.with_controllers %w(users pages) do
613
+ r = builder.build '/:controller/:action/:id/', :action => nil
614
+
615
+ [0, 2, 4].each do |i|
616
+ assert_kind_of ROUTING::DividerSegment, r.segments[i]
617
+ assert_equal '/', r.segments[i].value
618
+ assert r.segments[i].optional? if i > 1
619
+ end
620
+
621
+ assert_kind_of ROUTING::DynamicSegment, r.segments[1]
622
+ assert_equal :controller, r.segments[1].key
623
+ assert_equal nil, r.segments[1].default
624
+
625
+ assert_kind_of ROUTING::DynamicSegment, r.segments[3]
626
+ assert_equal :action, r.segments[3].key
627
+ assert_equal 'index', r.segments[3].default
628
+
629
+ assert_kind_of ROUTING::DynamicSegment, r.segments[5]
630
+ assert_equal :id, r.segments[5].key
631
+ assert r.segments[5].optional?
632
+ end
633
+ end
634
+
635
+ def test_slashes_are_implied
636
+ routes = [
637
+ builder.build('/:controller/:action/:id/', :action => nil),
638
+ builder.build('/:controller/:action/:id', :action => nil),
639
+ builder.build(':controller/:action/:id', :action => nil),
640
+ builder.build('/:controller/:action/:id/', :action => nil)
641
+ ]
642
+ expected = routes.first.segments.length
643
+ routes.each_with_index do |route, i|
644
+ found = route.segments.length
645
+ assert_equal expected, found, "Route #{i + 1} has #{found} segments, expected #{expected}"
646
+ end
647
+ end
648
+ end
649
+
650
+ class RoutingTest < Test::Unit::TestCase
651
+ def test_possible_controllers
652
+ true_controller_paths = ActionController::Routing.controller_paths
653
+
654
+ ActionController::Routing.use_controllers! nil
655
+
656
+ silence_warnings do
657
+ Object.send(:const_set, :RAILS_ROOT, File.dirname(__FILE__) + '/controller_fixtures')
658
+ end
659
+
660
+ ActionController::Routing.controller_paths = [
661
+ RAILS_ROOT, RAILS_ROOT + '/app/controllers', RAILS_ROOT + '/vendor/plugins/bad_plugin/lib'
662
+ ]
663
+
664
+ assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort
665
+ ensure
666
+ if true_controller_paths
667
+ ActionController::Routing.controller_paths = true_controller_paths
668
+ end
669
+ ActionController::Routing.use_controllers! nil
670
+ Object.send(:remove_const, :RAILS_ROOT) rescue nil
671
+ end
672
+
673
+ def test_possible_controllers_are_reset_on_each_load
674
+ true_possible_controllers = ActionController::Routing.possible_controllers
675
+ true_controller_paths = ActionController::Routing.controller_paths
676
+
677
+ ActionController::Routing.use_controllers! nil
678
+ root = File.dirname(__FILE__) + '/controller_fixtures'
679
+
680
+ ActionController::Routing.controller_paths = []
681
+ assert_equal [], ActionController::Routing.possible_controllers
682
+
683
+ ActionController::Routing.controller_paths = [
684
+ root, root + '/app/controllers', root + '/vendor/plugins/bad_plugin/lib'
685
+ ]
686
+ ActionController::Routing::Routes.load!
687
+
688
+ assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort
689
+ ensure
690
+ ActionController::Routing.controller_paths = true_controller_paths
691
+ ActionController::Routing.use_controllers! true_possible_controllers
692
+ Object.send(:remove_const, :RAILS_ROOT) rescue nil
693
+
694
+ ActionController::Routing::Routes.clear!
695
+ ActionController::Routing::Routes.load_routes!
696
+ end
697
+
698
+ def test_with_controllers
699
+ c = %w(admin/accounts admin/users account pages)
700
+ ActionController::Routing.with_controllers c do
701
+ assert_equal c, ActionController::Routing.possible_controllers
702
+ end
703
+ end
704
+
705
+ def test_normalize_unix_paths
706
+ load_paths = %w(. config/../app/controllers config/../app//helpers script/../config/../vendor/rails/actionpack/lib vendor/rails/railties/builtin/rails_info app/models lib script/../config/../foo/bar/../../app/models .foo/../.bar foo.bar/../config)
707
+ paths = ActionController::Routing.normalize_paths(load_paths)
708
+ assert_equal %w(vendor/rails/railties/builtin/rails_info vendor/rails/actionpack/lib app/controllers app/helpers app/models config .bar lib .), paths
709
+ end
710
+
711
+ def test_normalize_windows_paths
712
+ load_paths = %w(. config\\..\\app\\controllers config\\..\\app\\\\helpers script\\..\\config\\..\\vendor\\rails\\actionpack\\lib vendor\\rails\\railties\\builtin\\rails_info app\\models lib script\\..\\config\\..\\foo\\bar\\..\\..\\app\\models .foo\\..\\.bar foo.bar\\..\\config)
713
+ paths = ActionController::Routing.normalize_paths(load_paths)
714
+ assert_equal %w(vendor\\rails\\railties\\builtin\\rails_info vendor\\rails\\actionpack\\lib app\\controllers app\\helpers app\\models config .bar lib .), paths
715
+ end
716
+
717
+ def test_routing_helper_module
718
+ assert_kind_of Module, ActionController::Routing::Helpers
719
+
720
+ h = ActionController::Routing::Helpers
721
+ c = Class.new
722
+ assert ! c.ancestors.include?(h)
723
+ ActionController::Routing::Routes.install_helpers c
724
+ assert c.ancestors.include?(h)
725
+ end
726
+ end
727
+
728
+ class MockController
729
+ attr_accessor :routes
730
+
731
+ def initialize(routes)
732
+ self.routes = routes
733
+ end
734
+
735
+ def url_for(options)
736
+ only_path = options.delete(:only_path)
737
+
738
+ port = options.delete(:port) || 80
739
+ port_string = port == 80 ? '' : ":#{port}"
740
+
741
+ protocol = options.delete(:protocol) || "http"
742
+ host = options.delete(:host) || "test.host"
743
+ anchor = "##{options.delete(:anchor)}" if options.key?(:anchor)
744
+
745
+ path = routes.generate(options)
746
+
747
+ only_path ? "#{path}#{anchor}" : "#{protocol}://#{host}#{port_string}#{path}#{anchor}"
748
+ end
749
+
750
+ def request
751
+ @request ||= ActionController::TestRequest.new
752
+ end
753
+ end
754
+
755
+ class LegacyRouteSetTests < ActiveSupport::TestCase
756
+ attr_reader :rs
757
+
758
+ def setup
759
+ # These tests assume optimisation is on, so re-enable it.
760
+ ActionController::Base.optimise_named_routes = true
761
+
762
+ @rs = ::ActionController::Routing::RouteSet.new
763
+
764
+ ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed)
765
+ end
766
+
767
+ def teardown
768
+ @rs.clear!
769
+ end
770
+
771
+ def test_routes_for_controller_and_action_deprecated
772
+ assert_deprecated { @rs.routes_for_controller_and_action("controller", "action") }
773
+ end
774
+
775
+ def test_default_setup
776
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
777
+ assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
778
+ assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
779
+ assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
780
+
781
+ assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10"))
782
+
783
+ assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10)
784
+
785
+ assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
786
+ assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
787
+
788
+ assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
789
+ assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
790
+ end
791
+
792
+ def test_ignores_leading_slash
793
+ @rs.clear!
794
+ @rs.draw {|m| m.connect '/:controller/:action/:id'}
795
+ test_default_setup
796
+ end
797
+
798
+ def test_time_recognition
799
+ # We create many routes to make situation more realistic
800
+ @rs = ::ActionController::Routing::RouteSet.new
801
+ @rs.draw { |map|
802
+ map.frontpage '', :controller => 'search', :action => 'new'
803
+ map.resources :videos do |video|
804
+ video.resources :comments
805
+ video.resource :file, :controller => 'video_file'
806
+ video.resource :share, :controller => 'video_shares'
807
+ video.resource :abuse, :controller => 'video_abuses'
808
+ end
809
+ map.resources :abuses, :controller => 'video_abuses'
810
+ map.resources :video_uploads
811
+ map.resources :video_visits
812
+
813
+ map.resources :users do |user|
814
+ user.resource :settings
815
+ user.resources :videos
816
+ end
817
+ map.resources :channels do |channel|
818
+ channel.resources :videos, :controller => 'channel_videos'
819
+ end
820
+ map.resource :session
821
+ map.resource :lost_password
822
+ map.search 'search', :controller => 'search'
823
+ map.resources :pages
824
+ map.connect ':controller/:action/:id'
825
+ }
826
+ n = 1000
827
+ if RunTimeTests
828
+ GC.start
829
+ rectime = Benchmark.realtime do
830
+ n.times do
831
+ rs.recognize_path("/videos/1234567", {:method => :get})
832
+ rs.recognize_path("/videos/1234567/abuse", {:method => :get})
833
+ rs.recognize_path("/users/1234567/settings", {:method => :get})
834
+ rs.recognize_path("/channels/1234567", {:method => :get})
835
+ rs.recognize_path("/session/new", {:method => :get})
836
+ rs.recognize_path("/admin/user/show/10", {:method => :get})
837
+ end
838
+ end
839
+ puts "\n\nRecognition (#{rs.routes.size} routes):"
840
+ per_url = rectime / (n * 6)
841
+ puts "#{per_url * 1000} ms/url"
842
+ puts "#{1 / per_url} url/s\n\n"
843
+ end
844
+ end
845
+
846
+ def test_time_generation
847
+ n = 5000
848
+ if RunTimeTests
849
+ GC.start
850
+ pairs = [
851
+ [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}],
852
+ [{:controller => 'content'}, {:controller => 'content', :action => 'index'}],
853
+ [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}],
854
+ [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}],
855
+ [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}],
856
+ [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}],
857
+ [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}],
858
+ [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}],
859
+ ]
860
+ p = nil
861
+ gentime = Benchmark.realtime do
862
+ n.times do
863
+ pairs.each {|(a, b)| rs.generate(a, b)}
864
+ end
865
+ end
866
+
867
+ puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)"
868
+ per_url = gentime / (n * 8)
869
+ puts "#{per_url * 1000} ms/url"
870
+ puts "#{1 / per_url} url/s\n\n"
871
+ end
872
+ end
873
+
874
+ def test_route_with_colon_first
875
+ rs.draw do |map|
876
+ map.connect '/:controller/:action/:id', :action => 'index', :id => nil
877
+ map.connect ':url', :controller => 'tiny_url', :action => 'translate'
878
+ end
879
+ end
880
+
881
+ def test_route_with_regexp_for_controller
882
+ rs.draw do |map|
883
+ map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/
884
+ map.connect ':controller/:action/:id'
885
+ end
886
+ assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
887
+ rs.recognize_path("/admin/user/foo"))
888
+ assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo"))
889
+ assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index")
890
+ assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo")
891
+ end
892
+
893
+ def test_route_with_regexp_and_captures_for_controller
894
+ rs.draw do |map|
895
+ map.connect ':controller/:action/:id', :controller => /admin\/(accounts|users)/
896
+ end
897
+ assert_equal({:controller => "admin/accounts", :action => "index"}, rs.recognize_path("/admin/accounts"))
898
+ assert_equal({:controller => "admin/users", :action => "index"}, rs.recognize_path("/admin/users"))
899
+ assert_raise(ActionController::RoutingError) { rs.recognize_path("/admin/products") }
900
+ end
901
+
902
+ def test_route_with_regexp_and_dot
903
+ rs.draw do |map|
904
+ map.connect ':controller/:action/:file',
905
+ :controller => /admin|user/,
906
+ :action => /upload|download/,
907
+ :defaults => {:file => nil},
908
+ :requirements => {:file => %r{[^/]+(\.[^/]+)?}}
909
+ end
910
+ # Without a file extension
911
+ assert_equal '/user/download/file',
912
+ rs.generate(:controller => "user", :action => "download", :file => "file")
913
+ assert_equal(
914
+ {:controller => "user", :action => "download", :file => "file"},
915
+ rs.recognize_path("/user/download/file"))
916
+
917
+ # Now, let's try a file with an extension, really a dot (.)
918
+ assert_equal '/user/download/file.jpg',
919
+ rs.generate(
920
+ :controller => "user", :action => "download", :file => "file.jpg")
921
+ assert_equal(
922
+ {:controller => "user", :action => "download", :file => "file.jpg"},
923
+ rs.recognize_path("/user/download/file.jpg"))
924
+ end
925
+
926
+ def test_basic_named_route
927
+ rs.add_named_route :home, '', :controller => 'content', :action => 'list'
928
+ x = setup_for_named_route
929
+ assert_equal("http://test.host/",
930
+ x.send(:home_url))
931
+ end
932
+
933
+ def test_basic_named_route_with_relative_url_root
934
+ rs.add_named_route :home, '', :controller => 'content', :action => 'list'
935
+ x = setup_for_named_route
936
+ ActionController::Base.relative_url_root = "/foo"
937
+ assert_equal("http://test.host/foo/",
938
+ x.send(:home_url))
939
+ assert_equal "/foo/", x.send(:home_path)
940
+ ActionController::Base.relative_url_root = nil
941
+ end
942
+
943
+ def test_named_route_with_option
944
+ rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page'
945
+ x = setup_for_named_route
946
+ assert_equal("http://test.host/page/new%20stuff",
947
+ x.send(:page_url, :title => 'new stuff'))
948
+ end
949
+
950
+ def test_named_route_with_default
951
+ rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
952
+ x = setup_for_named_route
953
+ assert_equal("http://test.host/page/AboutRails",
954
+ x.send(:page_url, :title => "AboutRails"))
955
+
956
+ end
957
+
958
+ def test_named_route_with_name_prefix
959
+ rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :name_prefix => 'my_'
960
+ x = setup_for_named_route
961
+ assert_equal("http://test.host/page",
962
+ x.send(:my_page_url))
963
+ end
964
+
965
+ def test_named_route_with_path_prefix
966
+ rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => 'my'
967
+ x = setup_for_named_route
968
+ assert_equal("http://test.host/my/page",
969
+ x.send(:page_url))
970
+ end
971
+
972
+ def test_named_route_with_blank_path_prefix
973
+ rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => ''
974
+ x = setup_for_named_route
975
+ assert_equal("http://test.host/page",
976
+ x.send(:page_url))
977
+ end
978
+
979
+ def test_named_route_with_nested_controller
980
+ rs.add_named_route :users, 'admin/user', :controller => 'admin/user', :action => 'index'
981
+ x = setup_for_named_route
982
+ assert_equal("http://test.host/admin/user",
983
+ x.send(:users_url))
984
+ end
985
+
986
+ def test_optimised_named_route_call_never_uses_url_for
987
+ rs.add_named_route :users, 'admin/user', :controller => '/admin/user', :action => 'index'
988
+ rs.add_named_route :user, 'admin/user/:id', :controller=>'/admin/user', :action=>'show'
989
+ x = setup_for_named_route
990
+ x.expects(:url_for).never
991
+ x.send(:users_url)
992
+ x.send(:users_path)
993
+ x.send(:user_url, 2, :foo=>"bar")
994
+ x.send(:user_path, 3, :bar=>"foo")
995
+ end
996
+
997
+ def test_optimised_named_route_with_host
998
+ rs.add_named_route :pages, 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com'
999
+ x = setup_for_named_route
1000
+ x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once
1001
+ x.send(:pages_url)
1002
+ end
1003
+
1004
+ def setup_for_named_route
1005
+ klass = Class.new(MockController)
1006
+ rs.install_helpers(klass)
1007
+ klass.new(rs)
1008
+ end
1009
+
1010
+ def test_named_route_without_hash
1011
+ rs.draw do |map|
1012
+ map.normal ':controller/:action/:id'
1013
+ end
1014
+ end
1015
+
1016
+ def test_named_route_root
1017
+ rs.draw do |map|
1018
+ map.root :controller => "hello"
1019
+ end
1020
+ x = setup_for_named_route
1021
+ assert_equal("http://test.host/", x.send(:root_url))
1022
+ assert_equal("/", x.send(:root_path))
1023
+ end
1024
+
1025
+ def test_named_route_with_regexps
1026
+ rs.draw do |map|
1027
+ map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
1028
+ :year => /\d+/, :month => /\d+/, :day => /\d+/
1029
+ map.connect ':controller/:action/:id'
1030
+ end
1031
+ x = setup_for_named_route
1032
+ # assert_equal(
1033
+ # {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false},
1034
+ # x.send(:article_url, :title => 'hi')
1035
+ # )
1036
+ assert_equal(
1037
+ "http://test.host/page/2005/6/10/hi",
1038
+ x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
1039
+ )
1040
+ end
1041
+
1042
+ def test_changing_controller
1043
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
1044
+
1045
+ assert_equal '/admin/stuff/show/10', rs.generate(
1046
+ {:controller => 'stuff', :action => 'show', :id => 10},
1047
+ {:controller => 'admin/user', :action => 'index'}
1048
+ )
1049
+ end
1050
+
1051
+ def test_paths_escaped
1052
+ rs.draw do |map|
1053
+ map.path 'file/*path', :controller => 'content', :action => 'show_file'
1054
+ map.connect ':controller/:action/:id'
1055
+ end
1056
+
1057
+ # No + to space in URI escaping, only for query params.
1058
+ results = rs.recognize_path "/file/hello+world/how+are+you%3F"
1059
+ assert results, "Recognition should have succeeded"
1060
+ assert_equal ['hello+world', 'how+are+you?'], results[:path]
1061
+
1062
+ # Use %20 for space instead.
1063
+ results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F"
1064
+ assert results, "Recognition should have succeeded"
1065
+ assert_equal ['hello world', 'how are you?'], results[:path]
1066
+
1067
+ results = rs.recognize_path "/file"
1068
+ assert results, "Recognition should have succeeded"
1069
+ assert_equal [], results[:path]
1070
+ end
1071
+
1072
+ def test_paths_slashes_unescaped_with_ordered_parameters
1073
+ rs.add_named_route :path, '/file/*path', :controller => 'content'
1074
+
1075
+ # No / to %2F in URI, only for query params.
1076
+ x = setup_for_named_route
1077
+ assert_equal("/file/hello/world", x.send(:path_path, 'hello/world'))
1078
+ end
1079
+
1080
+ def test_non_controllers_cannot_be_matched
1081
+ rs.draw do |map|
1082
+ map.connect ':controller/:action/:id'
1083
+ end
1084
+ assert_raise(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") }
1085
+ end
1086
+
1087
+ def test_paths_do_not_accept_defaults
1088
+ assert_raise(ActionController::RoutingError) do
1089
+ rs.draw do |map|
1090
+ map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
1091
+ map.connect ':controller/:action/:id'
1092
+ end
1093
+ end
1094
+
1095
+ rs.draw do |map|
1096
+ map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => []
1097
+ map.connect ':controller/:action/:id'
1098
+ end
1099
+ end
1100
+
1101
+ def test_should_list_options_diff_when_routing_requirements_dont_match
1102
+ rs.draw do |map|
1103
+ map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
1104
+ end
1105
+ exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") }
1106
+ assert_match(/^post_url failed to generate/, exception.message)
1107
+ from_match = exception.message.match(/from \{[^\}]+\}/).to_s
1108
+ assert_match(/:bad_param=>"foo"/, from_match)
1109
+ assert_match(/:action=>"show"/, from_match)
1110
+ assert_match(/:controller=>"post"/, from_match)
1111
+
1112
+ expected_match = exception.message.match(/expected: \{[^\}]+\}/).to_s
1113
+ assert_no_match(/:bad_param=>"foo"/, expected_match)
1114
+ assert_match( /:action=>"show"/, expected_match)
1115
+ assert_match( /:controller=>"post"/, expected_match)
1116
+
1117
+ diff_match = exception.message.match(/diff: \{[^\}]+\}/).to_s
1118
+ assert_match( /:bad_param=>"foo"/, diff_match)
1119
+ assert_no_match(/:action=>"show"/, diff_match)
1120
+ assert_no_match(/:controller=>"post"/, diff_match)
1121
+ end
1122
+
1123
+ # this specifies the case where your formerly would get a very confusing error message with an empty diff
1124
+ def test_should_have_better_error_message_when_options_diff_is_empty
1125
+ rs.draw do |map|
1126
+ map.content '/content/:query', :controller => 'content', :action => 'show'
1127
+ end
1128
+
1129
+ exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'content', :action => 'show', :use_route => "content") }
1130
+ assert_match %r[:action=>"show"], exception.message
1131
+ assert_match %r[:controller=>"content"], exception.message
1132
+ assert_match %r[you may have ambiguous routes, or you may need to supply additional parameters for this route], exception.message
1133
+ assert_match %r[content_url has the following required parameters: \["content", :query\] - are they all satisfied?], exception.message
1134
+ end
1135
+
1136
+ def test_dynamic_path_allowed
1137
+ rs.draw do |map|
1138
+ map.connect '*path', :controller => 'content', :action => 'show_file'
1139
+ end
1140
+
1141
+ assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo))
1142
+ end
1143
+
1144
+ def test_dynamic_recall_paths_allowed
1145
+ rs.draw do |map|
1146
+ map.connect '*path', :controller => 'content', :action => 'show_file'
1147
+ end
1148
+
1149
+ recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo))
1150
+ assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path)
1151
+ end
1152
+
1153
+ def test_backwards
1154
+ rs.draw do |map|
1155
+ map.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
1156
+ map.connect ':controller/:action/:id'
1157
+ end
1158
+
1159
+ assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'})
1160
+ assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show')
1161
+ assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo')
1162
+ end
1163
+
1164
+ def test_route_with_fixnum_default
1165
+ rs.draw do |map|
1166
+ map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
1167
+ map.connect ':controller/:action/:id'
1168
+ end
1169
+
1170
+ assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page')
1171
+ assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1)
1172
+ assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1')
1173
+ assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10)
1174
+
1175
+ assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page"))
1176
+ assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1"))
1177
+ assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10"))
1178
+ end
1179
+
1180
+ # For newer revision
1181
+ def test_route_with_text_default
1182
+ rs.draw do |map|
1183
+ map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
1184
+ map.connect ':controller/:action/:id'
1185
+ end
1186
+
1187
+ assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo')
1188
+ assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo"))
1189
+
1190
+ token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
1191
+ token.force_encoding("UTF-8") if token.respond_to?(:force_encoding)
1192
+ escaped_token = CGI::escape(token)
1193
+
1194
+ assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token)
1195
+ assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}"))
1196
+ end
1197
+
1198
+ def test_action_expiry
1199
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
1200
+ assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
1201
+ end
1202
+
1203
+ def test_recognition_with_uppercase_controller_name
1204
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
1205
+ assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/Content"))
1206
+ assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/ConTent/list"))
1207
+ assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/CONTENT/show/10"))
1208
+
1209
+ # these used to work, before the routes rewrite, but support for this was pulled in the new version...
1210
+ #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/NewsFeed"))
1211
+ #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/News_Feed"))
1212
+ end
1213
+
1214
+ def test_requirement_should_prevent_optional_id
1215
+ rs.draw do |map|
1216
+ map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
1217
+ end
1218
+
1219
+ assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10)
1220
+
1221
+ assert_raise ActionController::RoutingError do
1222
+ rs.generate(:controller => 'post', :action => 'show')
1223
+ end
1224
+ end
1225
+
1226
+ def test_both_requirement_and_optional
1227
+ rs.draw do |map|
1228
+ map.blog('test/:year', :controller => 'post', :action => 'show',
1229
+ :defaults => { :year => nil },
1230
+ :requirements => { :year => /\d{4}/ }
1231
+ )
1232
+ map.connect ':controller/:action/:id'
1233
+ end
1234
+
1235
+ assert_equal '/test', rs.generate(:controller => 'post', :action => 'show')
1236
+ assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil)
1237
+
1238
+ x = setup_for_named_route
1239
+ assert_equal("http://test.host/test",
1240
+ x.send(:blog_url))
1241
+ end
1242
+
1243
+ def test_set_to_nil_forgets
1244
+ rs.draw do |map|
1245
+ map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil
1246
+ map.connect ':controller/:action/:id'
1247
+ end
1248
+
1249
+ assert_equal '/pages/2005',
1250
+ rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005)
1251
+ assert_equal '/pages/2005/6',
1252
+ rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
1253
+ assert_equal '/pages/2005/6/12',
1254
+ rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
1255
+
1256
+ assert_equal '/pages/2005/6/4',
1257
+ rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
1258
+
1259
+ assert_equal '/pages/2005/6',
1260
+ rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
1261
+
1262
+ assert_equal '/pages/2005',
1263
+ rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
1264
+ end
1265
+
1266
+ def test_url_with_no_action_specified
1267
+ rs.draw do |map|
1268
+ map.connect '', :controller => 'content'
1269
+ map.connect ':controller/:action/:id'
1270
+ end
1271
+
1272
+ assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
1273
+ assert_equal '/', rs.generate(:controller => 'content')
1274
+ end
1275
+
1276
+ def test_named_url_with_no_action_specified
1277
+ rs.draw do |map|
1278
+ map.home '', :controller => 'content'
1279
+ map.connect ':controller/:action/:id'
1280
+ end
1281
+
1282
+ assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
1283
+ assert_equal '/', rs.generate(:controller => 'content')
1284
+
1285
+ x = setup_for_named_route
1286
+ assert_equal("http://test.host/",
1287
+ x.send(:home_url))
1288
+ end
1289
+
1290
+ def test_url_generated_when_forgetting_action
1291
+ [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash|
1292
+ rs.draw do |map|
1293
+ map.home '', hash
1294
+ map.connect ':controller/:action/:id'
1295
+ end
1296
+ assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'})
1297
+ assert_equal '/', rs.generate({:controller => 'content'})
1298
+ assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
1299
+ end
1300
+ end
1301
+
1302
+ def test_named_route_method
1303
+ rs.draw do |map|
1304
+ map.categories 'categories', :controller => 'content', :action => 'categories'
1305
+ map.connect ':controller/:action/:id'
1306
+ end
1307
+
1308
+ assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories')
1309
+ assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
1310
+ end
1311
+
1312
+ def test_named_routes_array
1313
+ test_named_route_method
1314
+ assert_equal [:categories], rs.named_routes.names
1315
+ end
1316
+
1317
+ def test_nil_defaults
1318
+ rs.draw do |map|
1319
+ map.connect 'journal',
1320
+ :controller => 'content',
1321
+ :action => 'list_journal',
1322
+ :date => nil, :user_id => nil
1323
+ map.connect ':controller/:action/:id'
1324
+ end
1325
+
1326
+ assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
1327
+ end
1328
+
1329
+ def setup_request_method_routes_for(method)
1330
+ @request = ActionController::TestRequest.new
1331
+ @request.env["REQUEST_METHOD"] = method
1332
+ @request.request_uri = "/match"
1333
+
1334
+ rs.draw do |r|
1335
+ r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get }
1336
+ r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post }
1337
+ r.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put }
1338
+ r.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete }
1339
+ end
1340
+ end
1341
+
1342
+ %w(GET POST PUT DELETE).each do |request_method|
1343
+ define_method("test_request_method_recognized_with_#{request_method}") do
1344
+ begin
1345
+ Object.const_set(:BooksController, Class.new(ActionController::Base))
1346
+
1347
+ setup_request_method_routes_for(request_method)
1348
+
1349
+ assert_nothing_raised { rs.recognize(@request) }
1350
+ assert_equal request_method.downcase, @request.path_parameters[:action]
1351
+ ensure
1352
+ Object.send(:remove_const, :BooksController) rescue nil
1353
+ end
1354
+ end
1355
+ end
1356
+
1357
+ def test_recognize_array_of_methods
1358
+ Object.const_set(:BooksController, Class.new(ActionController::Base))
1359
+ rs.draw do |r|
1360
+ r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
1361
+ r.connect '/match', :controller => 'books', :action => 'not_get_or_post'
1362
+ end
1363
+
1364
+ @request = ActionController::TestRequest.new
1365
+ @request.env["REQUEST_METHOD"] = 'POST'
1366
+ @request.request_uri = "/match"
1367
+ assert_nothing_raised { rs.recognize(@request) }
1368
+ assert_equal 'get_or_post', @request.path_parameters[:action]
1369
+
1370
+ # have to recreate or else the RouteSet uses a cached version:
1371
+ @request = ActionController::TestRequest.new
1372
+ @request.env["REQUEST_METHOD"] = 'PUT'
1373
+ @request.request_uri = "/match"
1374
+ assert_nothing_raised { rs.recognize(@request) }
1375
+ assert_equal 'not_get_or_post', @request.path_parameters[:action]
1376
+ ensure
1377
+ Object.send(:remove_const, :BooksController) rescue nil
1378
+ end
1379
+
1380
+ def test_subpath_recognized
1381
+ Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
1382
+
1383
+ rs.draw do |r|
1384
+ r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
1385
+ r.connect '/items/:id/:action', :controller => 'subpath_books'
1386
+ r.connect '/posts/new/:action', :controller => 'subpath_books'
1387
+ r.connect '/posts/:id', :controller => 'subpath_books', :action => "show"
1388
+ end
1389
+
1390
+ hash = rs.recognize_path "/books/17/edit"
1391
+ assert_not_nil hash
1392
+ assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]]
1393
+
1394
+ hash = rs.recognize_path "/items/3/complete"
1395
+ assert_not_nil hash
1396
+ assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]]
1397
+
1398
+ hash = rs.recognize_path "/posts/new/preview"
1399
+ assert_not_nil hash
1400
+ assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]]
1401
+
1402
+ hash = rs.recognize_path "/posts/7"
1403
+ assert_not_nil hash
1404
+ assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]]
1405
+ ensure
1406
+ Object.send(:remove_const, :SubpathBooksController) rescue nil
1407
+ end
1408
+
1409
+ def test_subpath_generated
1410
+ Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
1411
+
1412
+ rs.draw do |r|
1413
+ r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
1414
+ r.connect '/items/:id/:action', :controller => 'subpath_books'
1415
+ r.connect '/posts/new/:action', :controller => 'subpath_books'
1416
+ end
1417
+
1418
+ assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit")
1419
+ assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete")
1420
+ assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview")
1421
+ ensure
1422
+ Object.send(:remove_const, :SubpathBooksController) rescue nil
1423
+ end
1424
+
1425
+ def test_failed_requirements_raises_exception_with_violated_requirements
1426
+ rs.draw do |r|
1427
+ r.foo_with_requirement 'foos/:id', :controller=>'foos', :requirements=>{:id=>/\d+/}
1428
+ end
1429
+
1430
+ x = setup_for_named_route
1431
+ assert_raise(ActionController::RoutingError) do
1432
+ x.send(:foo_with_requirement_url, "I am Against the requirements")
1433
+ end
1434
+ end
1435
+
1436
+ def test_routes_changed_correctly_after_clear
1437
+ ActionController::Base.optimise_named_routes = true
1438
+ rs = ::ActionController::Routing::RouteSet.new
1439
+ rs.draw do |r|
1440
+ r.connect 'ca', :controller => 'ca', :action => "aa"
1441
+ r.connect 'cb', :controller => 'cb', :action => "ab"
1442
+ r.connect 'cc', :controller => 'cc', :action => "ac"
1443
+ r.connect ':controller/:action/:id'
1444
+ r.connect ':controller/:action/:id.:format'
1445
+ end
1446
+
1447
+ hash = rs.recognize_path "/cc"
1448
+
1449
+ assert_not_nil hash
1450
+ assert_equal %w(cc ac), [hash[:controller], hash[:action]]
1451
+
1452
+ rs.draw do |r|
1453
+ r.connect 'cb', :controller => 'cb', :action => "ab"
1454
+ r.connect 'cc', :controller => 'cc', :action => "ac"
1455
+ r.connect ':controller/:action/:id'
1456
+ r.connect ':controller/:action/:id.:format'
1457
+ end
1458
+
1459
+ hash = rs.recognize_path "/cc"
1460
+
1461
+ assert_not_nil hash
1462
+ assert_equal %w(cc ac), [hash[:controller], hash[:action]]
1463
+
1464
+ end
1465
+ end
1466
+
1467
+ class RouteTest < Test::Unit::TestCase
1468
+ def setup
1469
+ @route = ROUTING::Route.new
1470
+ end
1471
+
1472
+ def slash_segment(is_optional = false)
1473
+ ROUTING::DividerSegment.new('/', :optional => is_optional)
1474
+ end
1475
+
1476
+ def default_route
1477
+ unless defined?(@default_route)
1478
+ segments = []
1479
+ segments << ROUTING::StaticSegment.new('/', :raw => true)
1480
+ segments << ROUTING::DynamicSegment.new(:controller)
1481
+ segments << slash_segment(:optional)
1482
+ segments << ROUTING::DynamicSegment.new(:action, :default => 'index', :optional => true)
1483
+ segments << slash_segment(:optional)
1484
+ segments << ROUTING::DynamicSegment.new(:id, :optional => true)
1485
+ segments << slash_segment(:optional)
1486
+ @default_route = ROUTING::Route.new(segments).freeze
1487
+ end
1488
+ @default_route
1489
+ end
1490
+
1491
+ def test_default_route_recognition
1492
+ expected = {:controller => 'accounts', :action => 'show', :id => '10'}
1493
+ assert_equal expected, default_route.recognize('/accounts/show/10')
1494
+ assert_equal expected, default_route.recognize('/accounts/show/10/')
1495
+
1496
+ expected[:id] = 'jamis'
1497
+ assert_equal expected, default_route.recognize('/accounts/show/jamis/')
1498
+
1499
+ expected.delete :id
1500
+ assert_equal expected, default_route.recognize('/accounts/show')
1501
+ assert_equal expected, default_route.recognize('/accounts/show/')
1502
+
1503
+ expected[:action] = 'index'
1504
+ assert_equal expected, default_route.recognize('/accounts/')
1505
+ assert_equal expected, default_route.recognize('/accounts')
1506
+
1507
+ assert_equal nil, default_route.recognize('/')
1508
+ assert_equal nil, default_route.recognize('/accounts/how/goood/it/is/to/be/free')
1509
+ end
1510
+
1511
+ def test_default_route_should_omit_default_action
1512
+ o = {:controller => 'accounts', :action => 'index'}
1513
+ assert_equal '/accounts', default_route.generate(o, o, {})
1514
+ end
1515
+
1516
+ def test_default_route_should_include_default_action_when_id_present
1517
+ o = {:controller => 'accounts', :action => 'index', :id => '20'}
1518
+ assert_equal '/accounts/index/20', default_route.generate(o, o, {})
1519
+ end
1520
+
1521
+ def test_default_route_should_work_with_action_but_no_id
1522
+ o = {:controller => 'accounts', :action => 'list_all'}
1523
+ assert_equal '/accounts/list_all', default_route.generate(o, o, {})
1524
+ end
1525
+
1526
+ def test_default_route_should_uri_escape_pluses
1527
+ expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' }
1528
+ assert_equal expected, default_route.recognize('/accounts/show/hello world')
1529
+ assert_equal expected, default_route.recognize('/accounts/show/hello%20world')
1530
+ assert_equal '/accounts/show/hello%20world', default_route.generate(expected, expected, {})
1531
+
1532
+ expected[:id] = 'hello+world'
1533
+ assert_equal expected, default_route.recognize('/accounts/show/hello+world')
1534
+ assert_equal expected, default_route.recognize('/accounts/show/hello%2Bworld')
1535
+ assert_equal '/accounts/show/hello+world', default_route.generate(expected, expected, {})
1536
+ end
1537
+
1538
+ def test_matches_controller_and_action
1539
+ # requirement_for should only be called for the action and controller _once_
1540
+ @route.expects(:requirement_for).with(:controller).times(1).returns('pages')
1541
+ @route.expects(:requirement_for).with(:action).times(1).returns('show')
1542
+
1543
+ @route.requirements = {:controller => 'pages', :action => 'show'}
1544
+ assert @route.matches_controller_and_action?('pages', 'show')
1545
+ assert !@route.matches_controller_and_action?('not_pages', 'show')
1546
+ assert !@route.matches_controller_and_action?('pages', 'not_show')
1547
+ end
1548
+
1549
+ def test_parameter_shell
1550
+ page_url = ROUTING::Route.new
1551
+ page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/}
1552
+ assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell)
1553
+ end
1554
+
1555
+ def test_defaults
1556
+ route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html"
1557
+ assert_equal(
1558
+ { :controller => "users", :action => "show", :format => "html" },
1559
+ route.defaults)
1560
+ end
1561
+
1562
+ def test_builder_complains_without_controller
1563
+ assert_raise(ArgumentError) do
1564
+ ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index"
1565
+ end
1566
+ end
1567
+
1568
+ def test_significant_keys_for_default_route
1569
+ keys = default_route.significant_keys.sort_by {|k| k.to_s }
1570
+ assert_equal [:action, :controller, :id], keys
1571
+ end
1572
+
1573
+ def test_significant_keys
1574
+ segments = []
1575
+ segments << ROUTING::StaticSegment.new('/', :raw => true)
1576
+ segments << ROUTING::StaticSegment.new('user')
1577
+ segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
1578
+ segments << ROUTING::DynamicSegment.new(:user)
1579
+ segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
1580
+
1581
+ requirements = {:controller => 'users', :action => 'show'}
1582
+
1583
+ user_url = ROUTING::Route.new(segments, requirements)
1584
+ keys = user_url.significant_keys.sort_by { |k| k.to_s }
1585
+ assert_equal [:action, :controller, :user], keys
1586
+ end
1587
+
1588
+ def test_build_empty_query_string
1589
+ assert_equal '', @route.build_query_string({})
1590
+ end
1591
+
1592
+ def test_build_query_string_with_nil_value
1593
+ assert_equal '', @route.build_query_string({:x => nil})
1594
+ end
1595
+
1596
+ def test_simple_build_query_string
1597
+ assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => '1', :y => '2'))
1598
+ end
1599
+
1600
+ def test_convert_ints_build_query_string
1601
+ assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => 1, :y => 2))
1602
+ end
1603
+
1604
+ def test_escape_spaces_build_query_string
1605
+ assert_equal '?x=hello+world&y=goodbye+world', order_query_string(@route.build_query_string(:x => 'hello world', :y => 'goodbye world'))
1606
+ end
1607
+
1608
+ def test_expand_array_build_query_string
1609
+ assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2]))
1610
+ end
1611
+
1612
+ def test_escape_spaces_build_query_string_selected_keys
1613
+ assert_equal '?x=hello+world', order_query_string(@route.build_query_string({:x => 'hello world', :y => 'goodbye world'}, [:x]))
1614
+ end
1615
+
1616
+ private
1617
+ def order_query_string(qs)
1618
+ '?' + qs[1..-1].split('&').sort.join('&')
1619
+ end
1620
+ end
1621
+
1622
+ class RouteSetTest < ActiveSupport::TestCase
1623
+ def set
1624
+ @set ||= ROUTING::RouteSet.new
1625
+ end
1626
+
1627
+ def request
1628
+ @request ||= ActionController::TestRequest.new
1629
+ end
1630
+
1631
+ def test_generate_extras
1632
+ set.draw { |m| m.connect ':controller/:action/:id' }
1633
+ path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
1634
+ assert_equal "/foo/bar/15", path
1635
+ assert_equal %w(that this), extras.map(&:to_s).sort
1636
+ end
1637
+
1638
+ def test_extra_keys
1639
+ set.draw { |m| m.connect ':controller/:action/:id' }
1640
+ extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
1641
+ assert_equal %w(that this), extras.map(&:to_s).sort
1642
+ end
1643
+
1644
+ def test_generate_extras_not_first
1645
+ set.draw do |map|
1646
+ map.connect ':controller/:action/:id.:format'
1647
+ map.connect ':controller/:action/:id'
1648
+ end
1649
+ path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
1650
+ assert_equal "/foo/bar/15", path
1651
+ assert_equal %w(that this), extras.map(&:to_s).sort
1652
+ end
1653
+
1654
+ def test_generate_not_first
1655
+ set.draw do |map|
1656
+ map.connect ':controller/:action/:id.:format'
1657
+ map.connect ':controller/:action/:id'
1658
+ end
1659
+ assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello")
1660
+ end
1661
+
1662
+ def test_extra_keys_not_first
1663
+ set.draw do |map|
1664
+ map.connect ':controller/:action/:id.:format'
1665
+ map.connect ':controller/:action/:id'
1666
+ end
1667
+ extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
1668
+ assert_equal %w(that this), extras.map(&:to_s).sort
1669
+ end
1670
+
1671
+ def test_draw
1672
+ assert_equal 0, set.routes.size
1673
+ set.draw do |map|
1674
+ map.connect '/hello/world', :controller => 'a', :action => 'b'
1675
+ end
1676
+ assert_equal 1, set.routes.size
1677
+ end
1678
+
1679
+ def test_draw_symbol_controller_name
1680
+ assert_equal 0, set.routes.size
1681
+ set.draw do |map|
1682
+ map.connect '/users/index', :controller => :users, :action => :index
1683
+ end
1684
+ @request = ActionController::TestRequest.new
1685
+ @request.request_uri = '/users/index'
1686
+ assert_nothing_raised { set.recognize(@request) }
1687
+ assert_equal 1, set.routes.size
1688
+ end
1689
+
1690
+ def test_named_draw
1691
+ assert_equal 0, set.routes.size
1692
+ set.draw do |map|
1693
+ map.hello '/hello/world', :controller => 'a', :action => 'b'
1694
+ end
1695
+ assert_equal 1, set.routes.size
1696
+ assert_equal set.routes.first, set.named_routes[:hello]
1697
+ end
1698
+
1699
+ def test_later_named_routes_take_precedence
1700
+ set.draw do |map|
1701
+ map.hello '/hello/world', :controller => 'a', :action => 'b'
1702
+ map.hello '/hello', :controller => 'a', :action => 'b'
1703
+ end
1704
+ assert_equal set.routes.last, set.named_routes[:hello]
1705
+ end
1706
+
1707
+ def setup_named_route_test
1708
+ set.draw do |map|
1709
+ map.show '/people/:id', :controller => 'people', :action => 'show'
1710
+ map.index '/people', :controller => 'people', :action => 'index'
1711
+ map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi'
1712
+ map.users '/admin/users', :controller => 'admin/users', :action => 'index'
1713
+ end
1714
+
1715
+ klass = Class.new(MockController)
1716
+ set.install_helpers(klass)
1717
+ klass.new(set)
1718
+ end
1719
+
1720
+ def test_named_route_hash_access_method
1721
+ controller = setup_named_route_test
1722
+
1723
+ assert_equal(
1724
+ { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false },
1725
+ controller.send(:hash_for_show_url, :id => 5))
1726
+
1727
+ assert_equal(
1728
+ { :controller => 'people', :action => 'index', :use_route => :index, :only_path => false },
1729
+ controller.send(:hash_for_index_url))
1730
+
1731
+ assert_equal(
1732
+ { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true },
1733
+ controller.send(:hash_for_show_path, :id => 5)
1734
+ )
1735
+ end
1736
+
1737
+ def test_named_route_url_method
1738
+ controller = setup_named_route_test
1739
+
1740
+ assert_equal "http://test.host/people/5", controller.send(:show_url, :id => 5)
1741
+ assert_equal "/people/5", controller.send(:show_path, :id => 5)
1742
+
1743
+ assert_equal "http://test.host/people", controller.send(:index_url)
1744
+ assert_equal "/people", controller.send(:index_path)
1745
+
1746
+ assert_equal "http://test.host/admin/users", controller.send(:users_url)
1747
+ assert_equal '/admin/users', controller.send(:users_path)
1748
+ assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
1749
+ end
1750
+
1751
+ def test_named_route_url_method_with_anchor
1752
+ controller = setup_named_route_test
1753
+
1754
+ assert_equal "http://test.host/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
1755
+ assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location')
1756
+
1757
+ assert_equal "http://test.host/people#location", controller.send(:index_url, :anchor => 'location')
1758
+ assert_equal "/people#location", controller.send(:index_path, :anchor => 'location')
1759
+
1760
+ assert_equal "http://test.host/admin/users#location", controller.send(:users_url, :anchor => 'location')
1761
+ assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location')
1762
+
1763
+ assert_equal "http://test.host/people/go/7/hello/joe/5#location",
1764
+ controller.send(:multi_url, 7, "hello", 5, :anchor => 'location')
1765
+
1766
+ assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar#location",
1767
+ controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location')
1768
+
1769
+ assert_equal "http://test.host/people?baz=bar#location",
1770
+ controller.send(:index_url, :baz => "bar", :anchor => 'location')
1771
+ end
1772
+
1773
+ def test_named_route_url_method_with_port
1774
+ controller = setup_named_route_test
1775
+ assert_equal "http://test.host:8080/people/5", controller.send(:show_url, 5, :port=>8080)
1776
+ end
1777
+
1778
+ def test_named_route_url_method_with_host
1779
+ controller = setup_named_route_test
1780
+ assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com")
1781
+ end
1782
+
1783
+ def test_named_route_url_method_with_protocol
1784
+ controller = setup_named_route_test
1785
+ assert_equal "https://test.host/people/5", controller.send(:show_url, 5, :protocol => "https")
1786
+ end
1787
+
1788
+ def test_named_route_url_method_with_ordered_parameters
1789
+ controller = setup_named_route_test
1790
+ assert_equal "http://test.host/people/go/7/hello/joe/5",
1791
+ controller.send(:multi_url, 7, "hello", 5)
1792
+ end
1793
+
1794
+ def test_named_route_url_method_with_ordered_parameters_and_hash
1795
+ controller = setup_named_route_test
1796
+ assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar",
1797
+ controller.send(:multi_url, 7, "hello", 5, :baz => "bar")
1798
+ end
1799
+
1800
+ def test_named_route_url_method_with_ordered_parameters_and_empty_hash
1801
+ controller = setup_named_route_test
1802
+ assert_equal "http://test.host/people/go/7/hello/joe/5",
1803
+ controller.send(:multi_url, 7, "hello", 5, {})
1804
+ end
1805
+
1806
+ def test_named_route_url_method_with_no_positional_arguments
1807
+ controller = setup_named_route_test
1808
+ assert_equal "http://test.host/people?baz=bar",
1809
+ controller.send(:index_url, :baz => "bar")
1810
+ end
1811
+
1812
+ def test_draw_default_route
1813
+ ActionController::Routing.with_controllers(['users']) do
1814
+ set.draw do |map|
1815
+ map.connect '/:controller/:action/:id'
1816
+ end
1817
+
1818
+ assert_equal 1, set.routes.size
1819
+ route = set.routes.first
1820
+
1821
+ assert route.segments.last.optional?
1822
+
1823
+ assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10)
1824
+ assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10)
1825
+
1826
+ assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10'))
1827
+ assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/'))
1828
+ end
1829
+ end
1830
+
1831
+ def test_draw_default_route_with_default_controller
1832
+ ActionController::Routing.with_controllers(['users']) do
1833
+ set.draw do |map|
1834
+ map.connect '/:controller/:action/:id', :controller => 'users'
1835
+ end
1836
+ assert_equal({:controller => 'users', :action => 'index'}, set.recognize_path('/'))
1837
+ end
1838
+ end
1839
+
1840
+ def test_route_with_parameter_shell
1841
+ ActionController::Routing.with_controllers(['users', 'pages']) do
1842
+ set.draw do |map|
1843
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/
1844
+ map.connect '/:controller/:action/:id'
1845
+ end
1846
+
1847
+ assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages'))
1848
+ assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages/index'))
1849
+ assert_equal({:controller => 'pages', :action => 'list'}, set.recognize_path('/pages/list'))
1850
+
1851
+ assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/pages/show/10'))
1852
+ assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
1853
+ end
1854
+ end
1855
+
1856
+ def test_route_requirements_with_anchor_chars_are_invalid
1857
+ assert_raise ArgumentError do
1858
+ set.draw do |map|
1859
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /^\d+/
1860
+ end
1861
+ end
1862
+ assert_raise ArgumentError do
1863
+ set.draw do |map|
1864
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\A\d+/
1865
+ end
1866
+ end
1867
+ assert_raise ArgumentError do
1868
+ set.draw do |map|
1869
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+$/
1870
+ end
1871
+ end
1872
+ assert_raise ArgumentError do
1873
+ set.draw do |map|
1874
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\Z/
1875
+ end
1876
+ end
1877
+ assert_raise ArgumentError do
1878
+ set.draw do |map|
1879
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\z/
1880
+ end
1881
+ end
1882
+ assert_nothing_raised do
1883
+ set.draw do |map|
1884
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/, :name => /^(david|jamis)/
1885
+ end
1886
+ assert_raise ActionController::RoutingError do
1887
+ set.generate :controller => 'pages', :action => 'show', :id => 10
1888
+ end
1889
+ end
1890
+ end
1891
+
1892
+ def test_route_requirements_with_invalid_http_method_is_invalid
1893
+ assert_raise ArgumentError do
1894
+ set.draw do |map|
1895
+ map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :invalid}
1896
+ end
1897
+ end
1898
+ end
1899
+
1900
+ def test_route_requirements_with_options_method_condition_is_valid
1901
+ assert_nothing_raised do
1902
+ set.draw do |map|
1903
+ map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :options}
1904
+ end
1905
+ end
1906
+ end
1907
+
1908
+ def test_route_requirements_with_head_method_condition_is_invalid
1909
+ assert_raise ArgumentError do
1910
+ set.draw do |map|
1911
+ map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :head}
1912
+ end
1913
+ end
1914
+ end
1915
+
1916
+ def test_non_path_route_requirements_match_all
1917
+ set.draw do |map|
1918
+ map.connect 'page/37s', :controller => 'pages', :action => 'show', :name => /(jamis|david)/
1919
+ end
1920
+ assert_equal '/page/37s', set.generate(:controller => 'pages', :action => 'show', :name => 'jamis')
1921
+ assert_raise ActionController::RoutingError do
1922
+ set.generate(:controller => 'pages', :action => 'show', :name => 'not_jamis')
1923
+ end
1924
+ assert_raise ActionController::RoutingError do
1925
+ set.generate(:controller => 'pages', :action => 'show', :name => 'nor_jamis_and_david')
1926
+ end
1927
+ end
1928
+
1929
+ def test_recognize_with_encoded_id_and_regex
1930
+ set.draw do |map|
1931
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /[a-zA-Z0-9\+]+/
1932
+ end
1933
+
1934
+ assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
1935
+ assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, set.recognize_path('/page/hello+world'))
1936
+ end
1937
+
1938
+ def test_recognize_with_conditions
1939
+ Object.const_set(:PeopleController, Class.new)
1940
+
1941
+ set.draw do |map|
1942
+ map.with_options(:controller => "people") do |people|
1943
+ people.people "/people", :action => "index", :conditions => { :method => :get }
1944
+ people.connect "/people", :action => "create", :conditions => { :method => :post }
1945
+ people.person "/people/:id", :action => "show", :conditions => { :method => :get }
1946
+ people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
1947
+ people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete }
1948
+ end
1949
+ end
1950
+
1951
+ request.path = "/people"
1952
+ request.env["REQUEST_METHOD"] = "GET"
1953
+ assert_nothing_raised { set.recognize(request) }
1954
+ assert_equal("index", request.path_parameters[:action])
1955
+ request.recycle!
1956
+
1957
+ request.env["REQUEST_METHOD"] = "POST"
1958
+ assert_nothing_raised { set.recognize(request) }
1959
+ assert_equal("create", request.path_parameters[:action])
1960
+ request.recycle!
1961
+
1962
+ request.env["REQUEST_METHOD"] = "PUT"
1963
+ assert_nothing_raised { set.recognize(request) }
1964
+ assert_equal("update", request.path_parameters[:action])
1965
+ request.recycle!
1966
+
1967
+ assert_raise(ActionController::UnknownHttpMethod) {
1968
+ request.env["REQUEST_METHOD"] = "BACON"
1969
+ set.recognize(request)
1970
+ }
1971
+ request.recycle!
1972
+
1973
+ request.path = "/people/5"
1974
+ request.env["REQUEST_METHOD"] = "GET"
1975
+ assert_nothing_raised { set.recognize(request) }
1976
+ assert_equal("show", request.path_parameters[:action])
1977
+ assert_equal("5", request.path_parameters[:id])
1978
+ request.recycle!
1979
+
1980
+ request.env["REQUEST_METHOD"] = "PUT"
1981
+ assert_nothing_raised { set.recognize(request) }
1982
+ assert_equal("update", request.path_parameters[:action])
1983
+ assert_equal("5", request.path_parameters[:id])
1984
+ request.recycle!
1985
+
1986
+ request.env["REQUEST_METHOD"] = "DELETE"
1987
+ assert_nothing_raised { set.recognize(request) }
1988
+ assert_equal("destroy", request.path_parameters[:action])
1989
+ assert_equal("5", request.path_parameters[:id])
1990
+ request.recycle!
1991
+
1992
+ begin
1993
+ request.env["REQUEST_METHOD"] = "POST"
1994
+ set.recognize(request)
1995
+ flunk 'Should have raised MethodNotAllowed'
1996
+ rescue ActionController::MethodNotAllowed => e
1997
+ assert_equal [:get, :put, :delete], e.allowed_methods
1998
+ end
1999
+ request.recycle!
2000
+
2001
+ ensure
2002
+ Object.send(:remove_const, :PeopleController)
2003
+ end
2004
+
2005
+ def test_recognize_with_alias_in_conditions
2006
+ Object.const_set(:PeopleController, Class.new)
2007
+
2008
+ set.draw do |map|
2009
+ map.people "/people", :controller => 'people', :action => "index",
2010
+ :conditions => { :method => :get }
2011
+ map.root :people
2012
+ end
2013
+
2014
+ request.path = "/people"
2015
+ request.env["REQUEST_METHOD"] = "GET"
2016
+ assert_nothing_raised { set.recognize(request) }
2017
+ assert_equal("people", request.path_parameters[:controller])
2018
+ assert_equal("index", request.path_parameters[:action])
2019
+
2020
+ request.path = "/"
2021
+ request.env["REQUEST_METHOD"] = "GET"
2022
+ assert_nothing_raised { set.recognize(request) }
2023
+ assert_equal("people", request.path_parameters[:controller])
2024
+ assert_equal("index", request.path_parameters[:action])
2025
+ ensure
2026
+ Object.send(:remove_const, :PeopleController)
2027
+ end
2028
+
2029
+ def test_typo_recognition
2030
+ Object.const_set(:ArticlesController, Class.new)
2031
+
2032
+ set.draw do |map|
2033
+ map.connect 'articles/:year/:month/:day/:title',
2034
+ :controller => 'articles', :action => 'permalink',
2035
+ :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
2036
+ end
2037
+
2038
+ request.path = "/articles/2005/11/05/a-very-interesting-article"
2039
+ request.env["REQUEST_METHOD"] = "GET"
2040
+ assert_nothing_raised { set.recognize(request) }
2041
+ assert_equal("permalink", request.path_parameters[:action])
2042
+ assert_equal("2005", request.path_parameters[:year])
2043
+ assert_equal("11", request.path_parameters[:month])
2044
+ assert_equal("05", request.path_parameters[:day])
2045
+ assert_equal("a-very-interesting-article", request.path_parameters[:title])
2046
+
2047
+ ensure
2048
+ Object.send(:remove_const, :ArticlesController)
2049
+ end
2050
+
2051
+ def test_routing_traversal_does_not_load_extra_classes
2052
+ assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
2053
+ set.draw do |map|
2054
+ map.connect '/profile', :controller => 'profile'
2055
+ end
2056
+
2057
+ request.path = '/profile'
2058
+
2059
+ set.recognize(request) rescue nil
2060
+
2061
+ assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
2062
+ end
2063
+
2064
+ def test_recognize_with_conditions_and_format
2065
+ Object.const_set(:PeopleController, Class.new)
2066
+
2067
+ set.draw do |map|
2068
+ map.with_options(:controller => "people") do |people|
2069
+ people.person "/people/:id", :action => "show", :conditions => { :method => :get }
2070
+ people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
2071
+ people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get }
2072
+ end
2073
+ end
2074
+
2075
+ request.path = "/people/5"
2076
+ request.env["REQUEST_METHOD"] = "GET"
2077
+ assert_nothing_raised { set.recognize(request) }
2078
+ assert_equal("show", request.path_parameters[:action])
2079
+ assert_equal("5", request.path_parameters[:id])
2080
+ request.recycle!
2081
+
2082
+ request.env["REQUEST_METHOD"] = "PUT"
2083
+ assert_nothing_raised { set.recognize(request) }
2084
+ assert_equal("update", request.path_parameters[:action])
2085
+ request.recycle!
2086
+
2087
+ request.path = "/people/5.png"
2088
+ request.env["REQUEST_METHOD"] = "GET"
2089
+ assert_nothing_raised { set.recognize(request) }
2090
+ assert_equal("show", request.path_parameters[:action])
2091
+ assert_equal("5", request.path_parameters[:id])
2092
+ assert_equal("png", request.path_parameters[:_format])
2093
+ ensure
2094
+ Object.send(:remove_const, :PeopleController)
2095
+ end
2096
+
2097
+ def test_generate_with_default_action
2098
+ set.draw do |map|
2099
+ map.connect "/people", :controller => "people"
2100
+ map.connect "/people/list", :controller => "people", :action => "list"
2101
+ end
2102
+
2103
+ url = set.generate(:controller => "people", :action => "list")
2104
+ assert_equal "/people/list", url
2105
+ end
2106
+
2107
+ def test_root_map
2108
+ Object.const_set(:PeopleController, Class.new)
2109
+
2110
+ set.draw { |map| map.root :controller => "people" }
2111
+
2112
+ request.path = ""
2113
+ request.env["REQUEST_METHOD"] = "GET"
2114
+ assert_nothing_raised { set.recognize(request) }
2115
+ assert_equal("people", request.path_parameters[:controller])
2116
+ assert_equal("index", request.path_parameters[:action])
2117
+ ensure
2118
+ Object.send(:remove_const, :PeopleController)
2119
+ end
2120
+
2121
+ def test_namespace
2122
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
2123
+
2124
+ set.draw do |map|
2125
+
2126
+ map.namespace 'api' do |api|
2127
+ api.route 'inventory', :controller => "products", :action => 'inventory'
2128
+ end
2129
+
2130
+ end
2131
+
2132
+ request.path = "/api/inventory"
2133
+ request.env["REQUEST_METHOD"] = "GET"
2134
+ assert_nothing_raised { set.recognize(request) }
2135
+ assert_equal("api/products", request.path_parameters[:controller])
2136
+ assert_equal("inventory", request.path_parameters[:action])
2137
+ ensure
2138
+ Object.send(:remove_const, :Api)
2139
+ end
2140
+
2141
+ def test_namespaced_root_map
2142
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
2143
+
2144
+ set.draw do |map|
2145
+
2146
+ map.namespace 'api' do |api|
2147
+ api.root :controller => "products"
2148
+ end
2149
+
2150
+ end
2151
+
2152
+ request.path = "/api"
2153
+ request.env["REQUEST_METHOD"] = "GET"
2154
+ assert_nothing_raised { set.recognize(request) }
2155
+ assert_equal("api/products", request.path_parameters[:controller])
2156
+ assert_equal("index", request.path_parameters[:action])
2157
+ ensure
2158
+ Object.send(:remove_const, :Api)
2159
+ end
2160
+
2161
+ def test_namespace_with_path_prefix
2162
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
2163
+
2164
+ set.draw do |map|
2165
+ map.namespace 'api', :path_prefix => 'prefix' do |api|
2166
+ api.route 'inventory', :controller => "products", :action => 'inventory'
2167
+ end
2168
+ end
2169
+
2170
+ request.path = "/prefix/inventory"
2171
+ request.env["REQUEST_METHOD"] = "GET"
2172
+ assert_nothing_raised { set.recognize(request) }
2173
+ assert_equal("api/products", request.path_parameters[:controller])
2174
+ assert_equal("inventory", request.path_parameters[:action])
2175
+ ensure
2176
+ Object.send(:remove_const, :Api)
2177
+ end
2178
+
2179
+ def test_namespace_with_blank_path_prefix
2180
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
2181
+
2182
+ set.draw do |map|
2183
+ map.namespace 'api', :path_prefix => '' do |api|
2184
+ api.route 'inventory', :controller => "products", :action => 'inventory'
2185
+ end
2186
+ end
2187
+
2188
+ request.path = "/inventory"
2189
+ request.env["REQUEST_METHOD"] = "GET"
2190
+ assert_nothing_raised { set.recognize(request) }
2191
+ assert_equal("api/products", request.path_parameters[:controller])
2192
+ assert_equal("inventory", request.path_parameters[:action])
2193
+ ensure
2194
+ Object.send(:remove_const, :Api)
2195
+ end
2196
+
2197
+ def test_generate_finds_best_fit
2198
+ set.draw do |map|
2199
+ map.connect "/people", :controller => "people", :action => "index"
2200
+ map.connect "/ws/people", :controller => "people", :action => "index", :ws => true
2201
+ end
2202
+
2203
+ url = set.generate(:controller => "people", :action => "index", :ws => true)
2204
+ assert_equal "/ws/people", url
2205
+ end
2206
+
2207
+ def test_generate_changes_controller_module
2208
+ set.draw { |map| map.connect ':controller/:action/:id' }
2209
+ current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
2210
+ url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current)
2211
+ assert_equal "/foo/bar/baz/7", url
2212
+ end
2213
+
2214
+ def test_id_is_not_impossibly_sticky
2215
+ set.draw do |map|
2216
+ map.connect 'foo/:number', :controller => "people", :action => "index"
2217
+ map.connect ':controller/:action/:id'
2218
+ end
2219
+
2220
+ url = set.generate({:controller => "people", :action => "index", :number => 3},
2221
+ {:controller => "people", :action => "index", :id => "21"})
2222
+ assert_equal "/foo/3", url
2223
+ end
2224
+
2225
+ def test_id_is_sticky_when_it_ought_to_be
2226
+ set.draw do |map|
2227
+ map.connect ':controller/:id/:action'
2228
+ end
2229
+
2230
+ url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
2231
+ assert_equal "/people/7/destroy", url
2232
+ end
2233
+
2234
+ def test_use_static_path_when_possible
2235
+ set.draw do |map|
2236
+ map.connect 'about', :controller => "welcome", :action => "about"
2237
+ map.connect ':controller/:action/:id'
2238
+ end
2239
+
2240
+ url = set.generate({:controller => "welcome", :action => "about"},
2241
+ {:controller => "welcome", :action => "get", :id => "7"})
2242
+ assert_equal "/about", url
2243
+ end
2244
+
2245
+ def test_generate
2246
+ set.draw { |map| map.connect ':controller/:action/:id' }
2247
+
2248
+ args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
2249
+ assert_equal "/foo/bar/7?x=y", set.generate(args)
2250
+ assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
2251
+ assert_equal [:x], set.extra_keys(args)
2252
+ end
2253
+
2254
+ def test_generate_with_path_prefix
2255
+ set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => 'my' }
2256
+
2257
+ args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
2258
+ assert_equal "/my/foo/bar/7?x=y", set.generate(args)
2259
+ end
2260
+
2261
+ def test_generate_with_blank_path_prefix
2262
+ set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => '' }
2263
+
2264
+ args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
2265
+ assert_equal "/foo/bar/7?x=y", set.generate(args)
2266
+ end
2267
+
2268
+ def test_named_routes_are_never_relative_to_modules
2269
+ set.draw do |map|
2270
+ map.connect "/connection/manage/:action", :controller => 'connection/manage'
2271
+ map.connect "/connection/connection", :controller => "connection/connection"
2272
+ map.family_connection "/connection", :controller => "connection"
2273
+ end
2274
+
2275
+ url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'})
2276
+ assert_equal "/connection/connection", url
2277
+
2278
+ url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'})
2279
+ assert_equal "/connection", url
2280
+ end
2281
+
2282
+ def test_action_left_off_when_id_is_recalled
2283
+ set.draw do |map|
2284
+ map.connect ':controller/:action/:id'
2285
+ end
2286
+ assert_equal '/post', set.generate(
2287
+ {:controller => 'post', :action => 'index'},
2288
+ {:controller => 'post', :action => 'show', :id => '10'}
2289
+ )
2290
+ end
2291
+
2292
+ def test_query_params_will_be_shown_when_recalled
2293
+ set.draw do |map|
2294
+ map.connect 'show_post/:parameter', :controller => 'post', :action => 'show'
2295
+ map.connect ':controller/:action/:id'
2296
+ end
2297
+ assert_equal '/post/edit?parameter=1', set.generate(
2298
+ {:action => 'edit', :parameter => 1},
2299
+ {:controller => 'post', :action => 'show', :parameter => 1}
2300
+ )
2301
+ end
2302
+
2303
+ def test_format_is_not_inherit
2304
+ set.draw do |map|
2305
+ map.connect '/posts.:format', :controller => 'posts'
2306
+ end
2307
+
2308
+ assert_equal '/posts', set.generate(
2309
+ {:controller => 'posts'},
2310
+ {:controller => 'posts', :action => 'index', :format => 'xml'}
2311
+ )
2312
+
2313
+ assert_equal '/posts.xml', set.generate(
2314
+ {:controller => 'posts', :format => 'xml'},
2315
+ {:controller => 'posts', :action => 'index', :format => 'xml'}
2316
+ )
2317
+ end
2318
+
2319
+ def test_expiry_determination_should_consider_values_with_to_param
2320
+ set.draw { |map| map.connect 'projects/:project_id/:controller/:action' }
2321
+ assert_equal '/projects/1/post/show', set.generate(
2322
+ {:action => 'show', :project_id => 1},
2323
+ {:controller => 'post', :action => 'show', :project_id => '1'})
2324
+ end
2325
+
2326
+ def test_generate_all
2327
+ set.draw do |map|
2328
+ map.connect 'show_post/:id', :controller => 'post', :action => 'show'
2329
+ map.connect ':controller/:action/:id'
2330
+ end
2331
+ all = set.generate(
2332
+ {:action => 'show', :id => 10, :generate_all => true},
2333
+ {:controller => 'post', :action => 'show'}
2334
+ )
2335
+ assert_equal 2, all.length
2336
+ assert_equal '/show_post/10', all.first
2337
+ assert_equal '/post/show/10', all.last
2338
+ end
2339
+
2340
+ def test_named_route_in_nested_resource
2341
+ set.draw do |map|
2342
+ map.resources :projects do |project|
2343
+ project.milestones 'milestones', :controller => 'milestones', :action => 'index'
2344
+ end
2345
+ end
2346
+
2347
+ request.path = "/projects/1/milestones"
2348
+ request.env["REQUEST_METHOD"] = "GET"
2349
+ assert_nothing_raised { set.recognize(request) }
2350
+ assert_equal("milestones", request.path_parameters[:controller])
2351
+ assert_equal("index", request.path_parameters[:action])
2352
+ end
2353
+
2354
+ def test_setting_root_in_namespace_using_symbol
2355
+ assert_nothing_raised do
2356
+ set.draw do |map|
2357
+ map.namespace :admin do |admin|
2358
+ admin.root :controller => 'home'
2359
+ end
2360
+ end
2361
+ end
2362
+ end
2363
+
2364
+ def test_setting_root_in_namespace_using_string
2365
+ assert_nothing_raised do
2366
+ set.draw do |map|
2367
+ map.namespace 'admin' do |admin|
2368
+ admin.root :controller => 'home'
2369
+ end
2370
+ end
2371
+ end
2372
+ end
2373
+
2374
+ def test_route_requirements_with_unsupported_regexp_options_must_error
2375
+ assert_raise ArgumentError do
2376
+ set.draw do |map|
2377
+ map.connect 'page/:name', :controller => 'pages',
2378
+ :action => 'show',
2379
+ :requirements => {:name => /(david|jamis)/m}
2380
+ end
2381
+ end
2382
+ end
2383
+
2384
+ def test_route_requirements_with_supported_options_must_not_error
2385
+ assert_nothing_raised do
2386
+ set.draw do |map|
2387
+ map.connect 'page/:name', :controller => 'pages',
2388
+ :action => 'show',
2389
+ :requirements => {:name => /(david|jamis)/i}
2390
+ end
2391
+ end
2392
+ assert_nothing_raised do
2393
+ set.draw do |map|
2394
+ map.connect 'page/:name', :controller => 'pages',
2395
+ :action => 'show',
2396
+ :requirements => {:name => / # Desperately overcommented regexp
2397
+ ( #Either
2398
+ david #The Creator
2399
+ | #Or
2400
+ jamis #The Deployer
2401
+ )/x}
2402
+ end
2403
+ end
2404
+ end
2405
+
2406
+ def test_route_requirement_recognize_with_ignore_case
2407
+ set.draw do |map|
2408
+ map.connect 'page/:name', :controller => 'pages',
2409
+ :action => 'show',
2410
+ :requirements => {:name => /(david|jamis)/i}
2411
+ end
2412
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
2413
+ assert_raise ActionController::RoutingError do
2414
+ set.recognize_path('/page/davidjamis')
2415
+ end
2416
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, set.recognize_path('/page/DAVID'))
2417
+ end
2418
+
2419
+ def test_route_requirement_generate_with_ignore_case
2420
+ set.draw do |map|
2421
+ map.connect 'page/:name', :controller => 'pages',
2422
+ :action => 'show',
2423
+ :requirements => {:name => /(david|jamis)/i}
2424
+ end
2425
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
2426
+ assert_equal "/page/david", url
2427
+ assert_raise ActionController::RoutingError do
2428
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
2429
+ end
2430
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
2431
+ assert_equal "/page/JAMIS", url
2432
+ end
2433
+
2434
+ def test_route_requirement_recognize_with_extended_syntax
2435
+ set.draw do |map|
2436
+ map.connect 'page/:name', :controller => 'pages',
2437
+ :action => 'show',
2438
+ :requirements => {:name => / # Desperately overcommented regexp
2439
+ ( #Either
2440
+ david #The Creator
2441
+ | #Or
2442
+ jamis #The Deployer
2443
+ )/x}
2444
+ end
2445
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
2446
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, set.recognize_path('/page/david'))
2447
+ assert_raise ActionController::RoutingError do
2448
+ set.recognize_path('/page/david #The Creator')
2449
+ end
2450
+ assert_raise ActionController::RoutingError do
2451
+ set.recognize_path('/page/David')
2452
+ end
2453
+ end
2454
+
2455
+ def test_route_requirement_generate_with_extended_syntax
2456
+ set.draw do |map|
2457
+ map.connect 'page/:name', :controller => 'pages',
2458
+ :action => 'show',
2459
+ :requirements => {:name => / # Desperately overcommented regexp
2460
+ ( #Either
2461
+ david #The Creator
2462
+ | #Or
2463
+ jamis #The Deployer
2464
+ )/x}
2465
+ end
2466
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
2467
+ assert_equal "/page/david", url
2468
+ assert_raise ActionController::RoutingError do
2469
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
2470
+ end
2471
+ assert_raise ActionController::RoutingError do
2472
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
2473
+ end
2474
+ end
2475
+
2476
+ def test_route_requirement_generate_with_xi_modifiers
2477
+ set.draw do |map|
2478
+ map.connect 'page/:name', :controller => 'pages',
2479
+ :action => 'show',
2480
+ :requirements => {:name => / # Desperately overcommented regexp
2481
+ ( #Either
2482
+ david #The Creator
2483
+ | #Or
2484
+ jamis #The Deployer
2485
+ )/xi}
2486
+ end
2487
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
2488
+ assert_equal "/page/JAMIS", url
2489
+ end
2490
+
2491
+ def test_route_requirement_recognize_with_xi_modifiers
2492
+ set.draw do |map|
2493
+ map.connect 'page/:name', :controller => 'pages',
2494
+ :action => 'show',
2495
+ :requirements => {:name => / # Desperately overcommented regexp
2496
+ ( #Either
2497
+ david #The Creator
2498
+ | #Or
2499
+ jamis #The Deployer
2500
+ )/xi}
2501
+ end
2502
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, set.recognize_path('/page/JAMIS'))
2503
+ end
2504
+
2505
+ def test_routes_with_symbols
2506
+ set.draw do |map|
2507
+ map.connect 'unnamed', :controller => :pages, :action => :show, :name => :as_symbol
2508
+ map.named 'named', :controller => :pages, :action => :show, :name => :as_symbol
2509
+ end
2510
+ assert_equal({:controller => 'pages', :action => 'show', :name => :as_symbol}, set.recognize_path('/unnamed'))
2511
+ assert_equal({:controller => 'pages', :action => 'show', :name => :as_symbol}, set.recognize_path('/named'))
2512
+ end
2513
+
2514
+ end
2515
+
2516
+ class RouteLoadingTest < Test::Unit::TestCase
2517
+ def setup
2518
+ routes.instance_variable_set '@routes_last_modified', nil
2519
+ silence_warnings { Object.const_set :RAILS_ROOT, '.' }
2520
+ routes.add_configuration_file(File.join(RAILS_ROOT, 'config', 'routes.rb'))
2521
+
2522
+ @stat = stub_everything
2523
+ end
2524
+
2525
+ def teardown
2526
+ ActionController::Routing::Routes.configuration_files.clear
2527
+ Object.send :remove_const, :RAILS_ROOT
2528
+ end
2529
+
2530
+ def test_load
2531
+ File.expects(:stat).returns(@stat)
2532
+ routes.expects(:load).with(regexp_matches(/routes\.rb$/))
2533
+
2534
+ routes.reload
2535
+ end
2536
+
2537
+ def test_no_reload_when_not_modified
2538
+ @stat.expects(:mtime).times(2).returns(1)
2539
+ File.expects(:stat).times(2).returns(@stat)
2540
+ routes.expects(:load).with(regexp_matches(/routes\.rb$/)).at_most_once
2541
+
2542
+ 2.times { routes.reload }
2543
+ end
2544
+
2545
+ def test_reload_when_modified
2546
+ @stat.expects(:mtime).at_least(2).returns(1, 2)
2547
+ File.expects(:stat).at_least(2).returns(@stat)
2548
+ routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
2549
+
2550
+ 2.times { routes.reload }
2551
+ end
2552
+
2553
+ def test_bang_forces_reload
2554
+ @stat.expects(:mtime).at_least(2).returns(1)
2555
+ File.expects(:stat).at_least(2).returns(@stat)
2556
+ routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
2557
+
2558
+ 2.times { routes.reload! }
2559
+ end
2560
+
2561
+ def test_adding_inflections_forces_reload
2562
+ ActiveSupport::Inflector::Inflections.instance.expects(:uncountable).with('equipment')
2563
+ routes.expects(:reload!)
2564
+
2565
+ ActiveSupport::Inflector.inflections { |inflect| inflect.uncountable('equipment') }
2566
+ end
2567
+
2568
+ def test_load_with_configuration
2569
+ routes.configuration_files.clear
2570
+ routes.add_configuration_file("foobarbaz")
2571
+ File.expects(:stat).returns(@stat)
2572
+ routes.expects(:load).with("foobarbaz")
2573
+
2574
+ routes.reload
2575
+ end
2576
+
2577
+ def test_load_multiple_configurations
2578
+ routes.add_configuration_file("engines.rb")
2579
+
2580
+ File.expects(:stat).at_least_once.returns(@stat)
2581
+
2582
+ routes.expects(:load).with('./config/routes.rb')
2583
+ routes.expects(:load).with('engines.rb')
2584
+
2585
+ routes.reload
2586
+ end
2587
+
2588
+ private
2589
+ def routes
2590
+ ActionController::Routing::Routes
2591
+ end
2592
+ end