PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/railties/test/application/routing_test.rb

https://github.com/github/rails
Ruby | 400 lines | 322 code | 75 blank | 3 comment | 0 complexity | 858071d30651af2a66bedbac79abc6a1 MD5 | raw file
  1. require 'isolation/abstract_unit'
  2. require 'rack/test'
  3. module ApplicationTests
  4. class RoutingTest < Test::Unit::TestCase
  5. include ActiveSupport::Testing::Isolation
  6. include Rack::Test::Methods
  7. def setup
  8. build_app
  9. boot_rails
  10. end
  11. def teardown
  12. teardown_app
  13. end
  14. test "rails/info/properties in development" do
  15. app("development")
  16. get "/rails/info/properties"
  17. assert_equal 200, last_response.status
  18. end
  19. test "rails/info/properties in production" do
  20. app("production")
  21. get "/rails/info/properties"
  22. assert_equal 404, last_response.status
  23. end
  24. test "simple controller" do
  25. simple_controller
  26. get '/foo'
  27. assert_equal 'foo', last_response.body
  28. end
  29. test "simple controller with helper" do
  30. controller :foo, <<-RUBY
  31. class FooController < ApplicationController
  32. def index
  33. render :inline => "<%= foo_or_bar? %>"
  34. end
  35. end
  36. RUBY
  37. app_file 'app/helpers/bar_helper.rb', <<-RUBY
  38. module BarHelper
  39. def foo_or_bar?
  40. "bar"
  41. end
  42. end
  43. RUBY
  44. app_file 'config/routes.rb', <<-RUBY
  45. AppTemplate::Application.routes.draw do
  46. match ':controller(/:action)'
  47. end
  48. RUBY
  49. get '/foo'
  50. assert_equal 'bar', last_response.body
  51. end
  52. test "mount rack app" do
  53. app_file 'config/routes.rb', <<-RUBY
  54. AppTemplate::Application.routes.draw do
  55. mount lambda { |env| [200, {}, [env["PATH_INFO"]]] }, :at => "/blog"
  56. # The line below is required because mount sometimes
  57. # fails when a resource route is added.
  58. resource :user
  59. end
  60. RUBY
  61. get '/blog/archives'
  62. assert_equal '/archives', last_response.body
  63. end
  64. test "multiple controllers" do
  65. controller :foo, <<-RUBY
  66. class FooController < ApplicationController
  67. def index
  68. render :text => "foo"
  69. end
  70. end
  71. RUBY
  72. controller :bar, <<-RUBY
  73. class BarController < ActionController::Base
  74. def index
  75. render :text => "bar"
  76. end
  77. end
  78. RUBY
  79. app_file 'config/routes.rb', <<-RUBY
  80. AppTemplate::Application.routes.draw do
  81. match ':controller(/:action)'
  82. end
  83. RUBY
  84. get '/foo'
  85. assert_equal 'foo', last_response.body
  86. get '/bar'
  87. assert_equal 'bar', last_response.body
  88. end
  89. test "nested controller" do
  90. controller 'foo', <<-RUBY
  91. class FooController < ApplicationController
  92. def index
  93. render :text => "foo"
  94. end
  95. end
  96. RUBY
  97. controller 'admin/foo', <<-RUBY
  98. module Admin
  99. class FooController < ApplicationController
  100. def index
  101. render :text => "admin::foo"
  102. end
  103. end
  104. end
  105. RUBY
  106. app_file 'config/routes.rb', <<-RUBY
  107. AppTemplate::Application.routes.draw do
  108. match 'admin/foo', :to => 'admin/foo#index'
  109. match 'foo', :to => 'foo#index'
  110. end
  111. RUBY
  112. get '/foo'
  113. assert_equal 'foo', last_response.body
  114. get '/admin/foo'
  115. assert_equal 'admin::foo', last_response.body
  116. end
  117. test "routes appending blocks" do
  118. app_file 'config/routes.rb', <<-RUBY
  119. AppTemplate::Application.routes.draw do
  120. match ':controller/:action'
  121. end
  122. RUBY
  123. add_to_config <<-R
  124. routes.append do
  125. match '/win' => lambda { |e| [200, {'Content-Type'=>'text/plain'}, ['WIN']] }
  126. end
  127. R
  128. app 'development'
  129. get '/win'
  130. assert_equal 'WIN', last_response.body
  131. app_file 'config/routes.rb', <<-R
  132. AppTemplate::Application.routes.draw do
  133. match 'lol' => 'hello#index'
  134. end
  135. R
  136. get '/win'
  137. assert_equal 'WIN', last_response.body
  138. end
  139. {"development" => "baz", "production" => "bar"}.each do |mode, expected|
  140. test "reloads routes when configuration is changed in #{mode}" do
  141. controller :foo, <<-RUBY
  142. class FooController < ApplicationController
  143. def bar
  144. render :text => "bar"
  145. end
  146. def baz
  147. render :text => "baz"
  148. end
  149. end
  150. RUBY
  151. app_file 'config/routes.rb', <<-RUBY
  152. AppTemplate::Application.routes.draw do
  153. match 'foo', :to => 'foo#bar'
  154. end
  155. RUBY
  156. app(mode)
  157. get '/foo'
  158. assert_equal 'bar', last_response.body
  159. app_file 'config/routes.rb', <<-RUBY
  160. AppTemplate::Application.routes.draw do
  161. match 'foo', :to => 'foo#baz'
  162. end
  163. RUBY
  164. sleep 0.1
  165. get '/foo'
  166. assert_equal expected, last_response.body
  167. end
  168. end
  169. test 'routes are loaded just after initialization' do
  170. require "#{app_path}/config/application"
  171. # Create the rack app just inside after initialize callback
  172. ActiveSupport.on_load(:after_initialize) do
  173. ::InitializeRackApp = lambda { |env| [200, {}, ["InitializeRackApp"]] }
  174. end
  175. app_file 'config/routes.rb', <<-RUBY
  176. AppTemplate::Application.routes.draw do
  177. match 'foo', :to => ::InitializeRackApp
  178. end
  179. RUBY
  180. get '/foo'
  181. assert_equal "InitializeRackApp", last_response.body
  182. end
  183. test 'reload_routes! is part of Rails.application API' do
  184. app("development")
  185. assert_nothing_raised do
  186. Rails.application.reload_routes!
  187. end
  188. end
  189. def test_root_path
  190. app('development')
  191. controller :foo, <<-RUBY
  192. class FooController < ApplicationController
  193. def index
  194. render :text => "foo"
  195. end
  196. end
  197. RUBY
  198. app_file 'config/routes.rb', <<-RUBY
  199. AppTemplate::Application.routes.draw do
  200. get 'foo', :to => 'foo#index'
  201. root :to => 'foo#index'
  202. end
  203. RUBY
  204. remove_file 'public/index.html'
  205. get '/'
  206. assert_equal 'foo', last_response.body
  207. end
  208. test 'routes are added and removed when reloading' do
  209. app('development')
  210. controller :foo, <<-RUBY
  211. class FooController < ApplicationController
  212. def index
  213. render :text => "foo"
  214. end
  215. end
  216. RUBY
  217. controller :bar, <<-RUBY
  218. class BarController < ApplicationController
  219. def index
  220. render :text => "bar"
  221. end
  222. end
  223. RUBY
  224. app_file 'config/routes.rb', <<-RUBY
  225. AppTemplate::Application.routes.draw do
  226. get 'foo', :to => 'foo#index'
  227. end
  228. RUBY
  229. get '/foo'
  230. assert_equal 'foo', last_response.body
  231. assert_equal '/foo', Rails.application.routes.url_helpers.foo_path
  232. get '/bar'
  233. assert_equal 404, last_response.status
  234. assert_raises NoMethodError do
  235. assert_equal '/bar', Rails.application.routes.url_helpers.bar_path
  236. end
  237. app_file 'config/routes.rb', <<-RUBY
  238. AppTemplate::Application.routes.draw do
  239. get 'foo', :to => 'foo#index'
  240. get 'bar', :to => 'bar#index'
  241. end
  242. RUBY
  243. Rails.application.reload_routes!
  244. get '/foo'
  245. assert_equal 'foo', last_response.body
  246. assert_equal '/foo', Rails.application.routes.url_helpers.foo_path
  247. get '/bar'
  248. assert_equal 'bar', last_response.body
  249. assert_equal '/bar', Rails.application.routes.url_helpers.bar_path
  250. app_file 'config/routes.rb', <<-RUBY
  251. AppTemplate::Application.routes.draw do
  252. get 'foo', :to => 'foo#index'
  253. end
  254. RUBY
  255. Rails.application.reload_routes!
  256. get '/foo'
  257. assert_equal 'foo', last_response.body
  258. assert_equal '/foo', Rails.application.routes.url_helpers.foo_path
  259. get '/bar'
  260. assert_equal 404, last_response.status
  261. assert_raises NoMethodError do
  262. assert_equal '/bar', Rails.application.routes.url_helpers.bar_path
  263. end
  264. end
  265. test 'named routes are cleared when reloading' do
  266. app('development')
  267. controller :foo, <<-RUBY
  268. class FooController < ApplicationController
  269. def index
  270. render :text => "foo"
  271. end
  272. end
  273. RUBY
  274. controller :bar, <<-RUBY
  275. class BarController < ApplicationController
  276. def index
  277. render :text => "bar"
  278. end
  279. end
  280. RUBY
  281. app_file 'config/routes.rb', <<-RUBY
  282. Rails.application.routes.draw do
  283. get ':locale/foo', :to => 'foo#index', :as => 'foo'
  284. end
  285. RUBY
  286. get '/en/foo'
  287. assert_equal 'foo', last_response.body
  288. assert_equal '/en/foo', Rails.application.routes.url_helpers.foo_path(:locale => 'en')
  289. app_file 'config/routes.rb', <<-RUBY
  290. Rails.application.routes.draw do
  291. get ':locale/bar', :to => 'bar#index', :as => 'foo'
  292. end
  293. RUBY
  294. Rails.application.reload_routes!
  295. get '/en/foo'
  296. assert_equal 404, last_response.status
  297. get '/en/bar'
  298. assert_equal 'bar', last_response.body
  299. assert_equal '/en/bar', Rails.application.routes.url_helpers.foo_path(:locale => 'en')
  300. end
  301. test 'resource routing with irregular inflection' do
  302. app_file 'config/initializers/inflection.rb', <<-RUBY
  303. ActiveSupport::Inflector.inflections do |inflect|
  304. inflect.irregular 'yazi', 'yazilar'
  305. end
  306. RUBY
  307. app_file 'config/routes.rb', <<-RUBY
  308. AppTemplate::Application.routes.draw do
  309. resources :yazilar
  310. end
  311. RUBY
  312. controller 'yazilar', <<-RUBY
  313. class YazilarController < ApplicationController
  314. def index
  315. render :text => 'yazilar#index'
  316. end
  317. end
  318. RUBY
  319. get '/yazilars'
  320. assert_equal 404, last_response.status
  321. get '/yazilar'
  322. assert_equal 200, last_response.status
  323. end
  324. end
  325. end