PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/actionpack/test/controller/action_pack_assertions_test.rb

http://github.com/benburkert/cruisecontrolrb
Ruby | 610 lines | 445 code | 98 blank | 67 comment | 2 complexity | 3d532d859443961b4c3622f8fce0d7c9 MD5 | raw file
Possible License(s): Apache-2.0
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. # a controller class to facilitate the tests
  3. class ActionPackAssertionsController < ActionController::Base
  4. # this does absolutely nothing
  5. def nothing() head :ok end
  6. # a standard template
  7. def hello_world() render "test/hello_world"; end
  8. # a standard template
  9. def hello_xml_world() render "test/hello_xml_world"; end
  10. # a redirect to an internal location
  11. def redirect_internal() redirect_to "/nothing"; end
  12. def redirect_to_action() redirect_to :action => "flash_me", :id => 1, :params => { "panda" => "fun" }; end
  13. def redirect_to_controller() redirect_to :controller => "elsewhere", :action => "flash_me"; end
  14. def redirect_to_path() redirect_to '/some/path' end
  15. def redirect_to_named_route() redirect_to route_one_url end
  16. # a redirect to an external location
  17. def redirect_external() redirect_to_url "http://www.rubyonrails.org"; end
  18. # a 404
  19. def response404() head '404 AWOL' end
  20. # a 500
  21. def response500() head '500 Sorry' end
  22. # a fictional 599
  23. def response599() head '599 Whoah!' end
  24. # putting stuff in the flash
  25. def flash_me
  26. flash['hello'] = 'my name is inigo montoya...'
  27. render_text "Inconceivable!"
  28. end
  29. # we have a flash, but nothing is in it
  30. def flash_me_naked
  31. flash.clear
  32. render_text "wow!"
  33. end
  34. # assign some template instance variables
  35. def assign_this
  36. @howdy = "ho"
  37. render :inline => "Mr. Henke"
  38. end
  39. def render_based_on_parameters
  40. render_text "Mr. #{params[:name]}"
  41. end
  42. def render_url
  43. render_text "<div>#{url_for(:action => 'flash_me', :only_path => true)}</div>"
  44. end
  45. def render_text_with_custom_content_type
  46. render :text => "Hello!", :content_type => Mime::RSS
  47. end
  48. # puts something in the session
  49. def session_stuffing
  50. session['xmas'] = 'turkey'
  51. render_text "ho ho ho"
  52. end
  53. # raises exception on get requests
  54. def raise_on_get
  55. raise "get" if request.get?
  56. render_text "request method: #{request.env['REQUEST_METHOD']}"
  57. end
  58. # raises exception on post requests
  59. def raise_on_post
  60. raise "post" if request.post?
  61. render_text "request method: #{request.env['REQUEST_METHOD']}"
  62. end
  63. def get_valid_record
  64. @record = Class.new do
  65. def valid?
  66. true
  67. end
  68. def errors
  69. Class.new do
  70. def full_messages; []; end
  71. end.new
  72. end
  73. end.new
  74. render :nothing => true
  75. end
  76. def get_invalid_record
  77. @record = Class.new do
  78. def valid?
  79. false
  80. end
  81. def errors
  82. Class.new do
  83. def full_messages; ['...stuff...']; end
  84. end.new
  85. end
  86. end.new
  87. render :nothing => true
  88. end
  89. # 911
  90. def rescue_action(e) raise; end
  91. end
  92. module Admin
  93. class InnerModuleController < ActionController::Base
  94. def index
  95. render :nothing => true
  96. end
  97. def redirect_to_index
  98. redirect_to admin_inner_module_path
  99. end
  100. def redirect_to_absolute_controller
  101. redirect_to :controller => '/content'
  102. end
  103. def redirect_to_fellow_controller
  104. redirect_to :controller => 'user'
  105. end
  106. def redirect_to_top_level_named_route
  107. redirect_to top_level_url(:id => "foo")
  108. end
  109. end
  110. end
  111. # ---------------------------------------------------------------------------
  112. # tell the controller where to find its templates but start from parent
  113. # directory of test_request_response to simulate the behaviour of a
  114. # production environment
  115. ActionPackAssertionsController.template_root = File.dirname(__FILE__) + "/../fixtures/"
  116. # a test case to exercise the new capabilities TestRequest & TestResponse
  117. class ActionPackAssertionsControllerTest < Test::Unit::TestCase
  118. # let's get this party started
  119. def setup
  120. ActionController::Routing::Routes.reload
  121. ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module content admin/user))
  122. @controller = ActionPackAssertionsController.new
  123. @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
  124. end
  125. def teardown
  126. ActionController::Routing::Routes.reload
  127. end
  128. # -- assertion-based testing ------------------------------------------------
  129. def test_assert_tag_and_url_for
  130. get :render_url
  131. assert_tag :content => "/action_pack_assertions/flash_me"
  132. end
  133. # test the session assertion to make sure something is there.
  134. def test_assert_session_has
  135. process :session_stuffing
  136. assert_deprecated_assertion { assert_session_has 'xmas' }
  137. assert_deprecated_assertion { assert_session_has_no 'halloween' }
  138. end
  139. # test the get method, make sure the request really was a get
  140. def test_get
  141. assert_raise(RuntimeError) { get :raise_on_get }
  142. get :raise_on_post
  143. assert_equal @response.body, 'request method: GET'
  144. end
  145. # test the get method, make sure the request really was a get
  146. def test_post
  147. assert_raise(RuntimeError) { post :raise_on_post }
  148. post :raise_on_get
  149. assert_equal @response.body, 'request method: POST'
  150. end
  151. # the following test fails because the request_method is now cached on the request instance
  152. # test the get/post switch within one test action
  153. # def test_get_post_switch
  154. # post :raise_on_get
  155. # assert_equal @response.body, 'request method: POST'
  156. # get :raise_on_post
  157. # assert_equal @response.body, 'request method: GET'
  158. # post :raise_on_get
  159. # assert_equal @response.body, 'request method: POST'
  160. # get :raise_on_post
  161. # assert_equal @response.body, 'request method: GET'
  162. # end
  163. # test the assertion of goodies in the template
  164. def test_assert_template_has
  165. process :assign_this
  166. assert_deprecated_assertion { assert_template_has 'howdy' }
  167. end
  168. # test the assertion for goodies that shouldn't exist in the template
  169. def test_assert_template_has_no
  170. process :nothing
  171. assert_deprecated_assertion { assert_template_has_no 'maple syrup' }
  172. assert_deprecated_assertion { assert_template_has_no 'howdy' }
  173. end
  174. # test the redirection assertions
  175. def test_assert_redirect
  176. process :redirect_internal
  177. assert_deprecated_assertion { assert_redirect }
  178. end
  179. # test the redirect url string
  180. def test_assert_redirect_url
  181. process :redirect_external
  182. assert_deprecated_assertion do
  183. assert_redirect_url 'http://www.rubyonrails.org'
  184. end
  185. end
  186. # test the redirection pattern matching on a string
  187. def test_assert_redirect_url_match_string
  188. process :redirect_external
  189. assert_deprecated_assertion do
  190. assert_redirect_url_match 'rails.org'
  191. end
  192. end
  193. # test the redirection pattern matching on a pattern
  194. def test_assert_redirect_url_match_pattern
  195. process :redirect_external
  196. assert_deprecated_assertion do
  197. assert_redirect_url_match /ruby/
  198. end
  199. end
  200. # test the redirection to a named route
  201. def test_assert_redirect_to_named_route
  202. with_routing do |set|
  203. set.draw do |map|
  204. map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing'
  205. map.connect ':controller/:action/:id'
  206. end
  207. set.named_routes.install
  208. process :redirect_to_named_route
  209. assert_redirected_to 'http://test.host/route_one'
  210. assert_redirected_to route_one_url
  211. assert_redirected_to :route_one_url
  212. end
  213. end
  214. def test_assert_redirect_to_named_route_failure
  215. with_routing do |set|
  216. set.draw do |map|
  217. map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'one'
  218. map.route_two 'route_two', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
  219. map.connect ':controller/:action/:id'
  220. end
  221. process :redirect_to_named_route
  222. assert_raise(Test::Unit::AssertionFailedError) do
  223. assert_redirected_to 'http://test.host/route_two'
  224. end
  225. assert_raise(Test::Unit::AssertionFailedError) do
  226. assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
  227. end
  228. assert_raise(Test::Unit::AssertionFailedError) do
  229. assert_redirected_to route_two_url
  230. end
  231. assert_raise(Test::Unit::AssertionFailedError) do
  232. assert_redirected_to :route_two_url
  233. end
  234. end
  235. end
  236. def test_assert_redirect_to_nested_named_route
  237. with_routing do |set|
  238. set.draw do |map|
  239. map.admin_inner_module 'admin/inner_module', :controller => 'admin/inner_module', :action => 'index'
  240. map.connect ':controller/:action/:id'
  241. end
  242. @controller = Admin::InnerModuleController.new
  243. process :redirect_to_index
  244. # redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
  245. assert_redirected_to admin_inner_module_path
  246. end
  247. end
  248. def test_assert_redirected_to_top_level_named_route_from_nested_controller
  249. with_routing do |set|
  250. set.draw do |map|
  251. map.top_level '/action_pack_assertions/:id', :controller => 'action_pack_assertions', :action => 'index'
  252. map.connect ':controller/:action/:id'
  253. end
  254. @controller = Admin::InnerModuleController.new
  255. process :redirect_to_top_level_named_route
  256. # passes -> assert_redirected_to "http://test.host/action_pack_assertions/foo"
  257. assert_redirected_to "/action_pack_assertions/foo"
  258. end
  259. end
  260. # test the flash-based assertions with something is in the flash
  261. def test_flash_assertions_full
  262. process :flash_me
  263. assert @response.has_flash_with_contents?
  264. assert_deprecated_assertion { assert_flash_exists }
  265. assert_deprecated_assertion { assert_flash_not_empty }
  266. assert_deprecated_assertion { assert_flash_has 'hello' }
  267. assert_deprecated_assertion { assert_flash_has_no 'stds' }
  268. end
  269. # test the flash-based assertions with no flash at all
  270. def test_flash_assertions_negative
  271. process :nothing
  272. assert_deprecated_assertion { assert_flash_empty }
  273. assert_deprecated_assertion { assert_flash_has_no 'hello' }
  274. assert_deprecated_assertion { assert_flash_has_no 'qwerty' }
  275. end
  276. # test the assert_rendered_file
  277. def test_assert_rendered_file
  278. assert_deprecated(/render/) { process :hello_world }
  279. assert_deprecated_assertion { assert_rendered_file 'test/hello_world' }
  280. assert_deprecated_assertion { assert_rendered_file 'hello_world' }
  281. end
  282. # test the assert_success assertion
  283. def test_assert_success
  284. process :nothing
  285. assert_deprecated_assertion { assert_success }
  286. end
  287. # -- standard request/response object testing --------------------------------
  288. # ensure our session is working properly
  289. def test_session_objects
  290. process :session_stuffing
  291. assert @response.has_session_object?('xmas')
  292. assert_deprecated_assertion { assert_session_equal 'turkey', 'xmas' }
  293. assert !@response.has_session_object?('easter')
  294. end
  295. # make sure that the template objects exist
  296. def test_template_objects_alive
  297. process :assign_this
  298. assert !@response.has_template_object?('hi')
  299. assert @response.has_template_object?('howdy')
  300. end
  301. # make sure we don't have template objects when we shouldn't
  302. def test_template_object_missing
  303. process :nothing
  304. assert_nil @response.template_objects['howdy']
  305. end
  306. def test_assigned_equal
  307. process :assign_this
  308. assert_deprecated_assertion { assert_assigned_equal "ho", :howdy }
  309. end
  310. # check the empty flashing
  311. def test_flash_me_naked
  312. process :flash_me_naked
  313. assert !@response.has_flash?
  314. assert !@response.has_flash_with_contents?
  315. end
  316. # check if we have flash objects
  317. def test_flash_haves
  318. process :flash_me
  319. assert @response.has_flash?
  320. assert @response.has_flash_with_contents?
  321. assert @response.has_flash_object?('hello')
  322. end
  323. # ensure we don't have flash objects
  324. def test_flash_have_nots
  325. process :nothing
  326. assert !@response.has_flash?
  327. assert !@response.has_flash_with_contents?
  328. assert_nil @response.flash['hello']
  329. end
  330. # examine that the flash objects are what we expect
  331. def test_flash_equals
  332. process :flash_me
  333. assert_deprecated_assertion do
  334. assert_flash_equal 'my name is inigo montoya...', 'hello'
  335. end
  336. end
  337. # check if we were rendered by a file-based template?
  338. def test_rendered_action
  339. process :nothing
  340. assert !@response.rendered_with_file?
  341. assert_deprecated(/render/) { process :hello_world }
  342. assert @response.rendered_with_file?
  343. assert 'hello_world', @response.rendered_file
  344. end
  345. # check the redirection location
  346. def test_redirection_location
  347. process :redirect_internal
  348. assert_equal 'http://test.host/nothing', @response.redirect_url
  349. process :redirect_external
  350. assert_equal 'http://www.rubyonrails.org', @response.redirect_url
  351. end
  352. def test_no_redirect_url
  353. process :nothing
  354. assert_nil @response.redirect_url
  355. end
  356. # check server errors
  357. def test_server_error_response_code
  358. process :response500
  359. assert @response.server_error?
  360. process :response599
  361. assert @response.server_error?
  362. process :response404
  363. assert !@response.server_error?
  364. end
  365. # check a 404 response code
  366. def test_missing_response_code
  367. process :response404
  368. assert @response.missing?
  369. end
  370. # check to see if our redirection matches a pattern
  371. def test_redirect_url_match
  372. process :redirect_external
  373. assert @response.redirect?
  374. assert @response.redirect_url_match?("rubyonrails")
  375. assert @response.redirect_url_match?(/rubyonrails/)
  376. assert !@response.redirect_url_match?("phpoffrails")
  377. assert !@response.redirect_url_match?(/perloffrails/)
  378. end
  379. # check for a redirection
  380. def test_redirection
  381. process :redirect_internal
  382. assert @response.redirect?
  383. process :redirect_external
  384. assert @response.redirect?
  385. process :nothing
  386. assert !@response.redirect?
  387. end
  388. # check a successful response code
  389. def test_successful_response_code
  390. process :nothing
  391. assert @response.success?
  392. end
  393. # a basic check to make sure we have a TestResponse object
  394. def test_has_response
  395. process :nothing
  396. assert_kind_of ActionController::TestResponse, @response
  397. end
  398. def test_render_based_on_parameters
  399. process :render_based_on_parameters, "name" => "David"
  400. assert_equal "Mr. David", @response.body
  401. end
  402. def test_assert_template_xpath_match_no_matches
  403. assert_deprecated(/render/) { process :hello_xml_world }
  404. assert_raises Test::Unit::AssertionFailedError do
  405. assert_deprecated_assertion do
  406. assert_template_xpath_match('/no/such/node/in/document')
  407. end
  408. end
  409. end
  410. def test_simple_one_element_xpath_match
  411. assert_deprecated(/render/) { process :hello_xml_world }
  412. assert_deprecated_assertion do
  413. assert_template_xpath_match('//title', "Hello World")
  414. end
  415. end
  416. def test_array_of_elements_in_xpath_match
  417. assert_deprecated(/render/) { process :hello_xml_world }
  418. assert_deprecated_assertion do
  419. assert_template_xpath_match('//p', %w( abes monks wiseguys ))
  420. end
  421. end
  422. def test_follow_redirect
  423. process :redirect_to_action
  424. assert_redirected_to :action => "flash_me"
  425. follow_redirect
  426. assert_equal 1, @request.parameters["id"].to_i
  427. assert "Inconceivable!", @response.body
  428. end
  429. def test_follow_redirect_outside_current_action
  430. process :redirect_to_controller
  431. assert_redirected_to :controller => "elsewhere", :action => "flash_me"
  432. assert_raises(RuntimeError, "Can't follow redirects outside of current controller (elsewhere)") { follow_redirect }
  433. end
  434. def test_assert_redirection_fails_with_incorrect_controller
  435. process :redirect_to_controller
  436. assert_raise(Test::Unit::AssertionFailedError) do
  437. assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me"
  438. end
  439. end
  440. def test_assert_redirection_with_extra_controller_option
  441. get :redirect_to_action
  442. assert_redirected_to :controller => 'action_pack_assertions', :action => "flash_me", :id => 1, :params => { :panda => 'fun' }
  443. end
  444. def test_redirected_to_url_leadling_slash
  445. process :redirect_to_path
  446. assert_redirected_to '/some/path'
  447. end
  448. def test_redirected_to_url_no_leadling_slash
  449. process :redirect_to_path
  450. assert_redirected_to 'some/path'
  451. end
  452. def test_redirected_to_url_full_url
  453. process :redirect_to_path
  454. assert_redirected_to 'http://test.host/some/path'
  455. end
  456. def test_redirected_to_with_nested_controller
  457. @controller = Admin::InnerModuleController.new
  458. get :redirect_to_absolute_controller
  459. assert_redirected_to :controller => 'content'
  460. get :redirect_to_fellow_controller
  461. assert_redirected_to :controller => 'admin/user'
  462. end
  463. def test_assert_valid
  464. get :get_valid_record
  465. assert_valid assigns('record')
  466. end
  467. def test_assert_valid_failing
  468. get :get_invalid_record
  469. begin
  470. assert_valid assigns('record')
  471. assert false
  472. rescue Test::Unit::AssertionFailedError => e
  473. end
  474. end
  475. protected
  476. def assert_deprecated_assertion(&block)
  477. assert_deprecated(/assert/, &block)
  478. end
  479. end
  480. class ActionPackHeaderTest < Test::Unit::TestCase
  481. def setup
  482. @controller = ActionPackAssertionsController.new
  483. @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
  484. end
  485. def test_rendering_xml_sets_content_type
  486. assert_deprecated(/render/) { process :hello_xml_world }
  487. assert_equal('application/xml; charset=utf-8', @controller.headers['Content-Type'])
  488. end
  489. def test_rendering_xml_respects_content_type
  490. @response.headers['Content-Type'] = 'application/pdf'
  491. assert_deprecated(/render/) { process :hello_xml_world }
  492. assert_equal('application/pdf; charset=utf-8', @controller.headers['Content-Type'])
  493. end
  494. def test_render_text_with_custom_content_type
  495. get :render_text_with_custom_content_type
  496. assert_equal 'application/rss+xml; charset=utf-8', @response.headers['Content-Type']
  497. end
  498. end