PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/benburkert/cruisecontrolrb
Ruby | 495 lines | 368 code | 89 blank | 38 comment | 2 complexity | 161af34500b5e62a9fbec921f5edbfec MD5 | raw file
Possible License(s): Apache-2.0
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. require File.dirname(__FILE__) + '/fake_controllers'
  3. class TestTest < Test::Unit::TestCase
  4. class TestController < ActionController::Base
  5. def set_flash
  6. flash["test"] = ">#{flash["test"]}<"
  7. render :text => 'ignore me'
  8. end
  9. def render_raw_post
  10. raise Test::Unit::AssertionFailedError, "#raw_post is blank" if request.raw_post.blank?
  11. render :text => request.raw_post
  12. end
  13. def test_params
  14. render :text => params.inspect
  15. end
  16. def test_uri
  17. render :text => request.request_uri
  18. end
  19. def test_html_output
  20. render :text => <<HTML
  21. <html>
  22. <body>
  23. <a href="/"><img src="/images/button.png" /></a>
  24. <div id="foo">
  25. <ul>
  26. <li class="item">hello</li>
  27. <li class="item">goodbye</li>
  28. </ul>
  29. </div>
  30. <div id="bar">
  31. <form action="/somewhere">
  32. Name: <input type="text" name="person[name]" id="person_name" />
  33. </form>
  34. </div>
  35. </body>
  36. </html>
  37. HTML
  38. end
  39. def test_only_one_param
  40. render :text => (params[:left] && params[:right]) ? "EEP, Both here!" : "OK"
  41. end
  42. def test_remote_addr
  43. render :text => (request.remote_addr || "not specified")
  44. end
  45. def test_file_upload
  46. render :text => params[:file].size
  47. end
  48. def redirect_to_symbol
  49. redirect_to :generate_url, :id => 5
  50. end
  51. def redirect_to_same_controller
  52. redirect_to :controller => 'test', :action => 'test_uri', :id => 5
  53. end
  54. def redirect_to_different_controller
  55. redirect_to :controller => 'fail', :id => 5
  56. end
  57. def create
  58. headers['Location'] = 'created resource'
  59. head :created
  60. end
  61. private
  62. def rescue_action(e)
  63. raise e
  64. end
  65. def generate_url(opts)
  66. url_for(opts.merge(:action => "test_uri"))
  67. end
  68. end
  69. def setup
  70. @controller = TestController.new
  71. @request = ActionController::TestRequest.new
  72. @response = ActionController::TestResponse.new
  73. ActionController::Routing::Routes.reload
  74. ActionController::Routing.use_controllers! %w(content admin/user test_test/test)
  75. end
  76. def teardown
  77. ActionController::Routing::Routes.reload
  78. end
  79. def test_raw_post_handling
  80. params = {:page => {:name => 'page name'}, 'some key' => 123}
  81. get :render_raw_post, params.dup
  82. raw_post = params.map {|k,v| [CGI::escape(k.to_s), CGI::escape(v.to_s)].join('=')}.sort.join('&')
  83. assert_equal raw_post, @response.body
  84. end
  85. def test_process_without_flash
  86. process :set_flash
  87. assert_equal '><', flash['test']
  88. end
  89. def test_process_with_flash
  90. process :set_flash, nil, nil, { "test" => "value" }
  91. assert_equal '>value<', flash['test']
  92. end
  93. def test_process_with_request_uri_with_no_params
  94. process :test_uri
  95. assert_equal "/test_test/test/test_uri", @response.body
  96. end
  97. def test_process_with_request_uri_with_params
  98. process :test_uri, :id => 7
  99. assert_equal "/test_test/test/test_uri/7", @response.body
  100. end
  101. def test_process_with_request_uri_with_params_with_explicit_uri
  102. @request.set_REQUEST_URI "/explicit/uri"
  103. process :test_uri, :id => 7
  104. assert_equal "/explicit/uri", @response.body
  105. end
  106. def test_multiple_calls
  107. process :test_only_one_param, :left => true
  108. assert_equal "OK", @response.body
  109. process :test_only_one_param, :right => true
  110. assert_equal "OK", @response.body
  111. end
  112. def test_assert_tag_tag
  113. process :test_html_output
  114. # there is a 'form' tag
  115. assert_tag :tag => 'form'
  116. # there is not an 'hr' tag
  117. assert_no_tag :tag => 'hr'
  118. end
  119. def test_assert_tag_attributes
  120. process :test_html_output
  121. # there is a tag with an 'id' of 'bar'
  122. assert_tag :attributes => { :id => "bar" }
  123. # there is no tag with a 'name' of 'baz'
  124. assert_no_tag :attributes => { :name => "baz" }
  125. end
  126. def test_assert_tag_parent
  127. process :test_html_output
  128. # there is a tag with a parent 'form' tag
  129. assert_tag :parent => { :tag => "form" }
  130. # there is no tag with a parent of 'input'
  131. assert_no_tag :parent => { :tag => "input" }
  132. end
  133. def test_assert_tag_child
  134. process :test_html_output
  135. # there is a tag with a child 'input' tag
  136. assert_tag :child => { :tag => "input" }
  137. # there is no tag with a child 'strong' tag
  138. assert_no_tag :child => { :tag => "strong" }
  139. end
  140. def test_assert_tag_ancestor
  141. process :test_html_output
  142. # there is a 'li' tag with an ancestor having an id of 'foo'
  143. assert_tag :ancestor => { :attributes => { :id => "foo" } }, :tag => "li"
  144. # there is no tag of any kind with an ancestor having an href matching 'foo'
  145. assert_no_tag :ancestor => { :attributes => { :href => /foo/ } }
  146. end
  147. def test_assert_tag_descendant
  148. process :test_html_output
  149. # there is a tag with a decendant 'li' tag
  150. assert_tag :descendant => { :tag => "li" }
  151. # there is no tag with a descendant 'html' tag
  152. assert_no_tag :descendant => { :tag => "html" }
  153. end
  154. def test_assert_tag_sibling
  155. process :test_html_output
  156. # there is a tag with a sibling of class 'item'
  157. assert_tag :sibling => { :attributes => { :class => "item" } }
  158. # there is no tag with a sibling 'ul' tag
  159. assert_no_tag :sibling => { :tag => "ul" }
  160. end
  161. def test_assert_tag_after
  162. process :test_html_output
  163. # there is a tag following a sibling 'div' tag
  164. assert_tag :after => { :tag => "div" }
  165. # there is no tag following a sibling tag with id 'bar'
  166. assert_no_tag :after => { :attributes => { :id => "bar" } }
  167. end
  168. def test_assert_tag_before
  169. process :test_html_output
  170. # there is a tag preceeding a tag with id 'bar'
  171. assert_tag :before => { :attributes => { :id => "bar" } }
  172. # there is no tag preceeding a 'form' tag
  173. assert_no_tag :before => { :tag => "form" }
  174. end
  175. def test_assert_tag_children_count
  176. process :test_html_output
  177. # there is a tag with 2 children
  178. assert_tag :children => { :count => 2 }
  179. # in particular, there is a <ul> tag with two children (a nameless pair of <li>s)
  180. assert_tag :tag => 'ul', :children => { :count => 2 }
  181. # there is no tag with 4 children
  182. assert_no_tag :children => { :count => 4 }
  183. end
  184. def test_assert_tag_children_less_than
  185. process :test_html_output
  186. # there is a tag with less than 5 children
  187. assert_tag :children => { :less_than => 5 }
  188. # there is no 'ul' tag with less than 2 children
  189. assert_no_tag :children => { :less_than => 2 }, :tag => "ul"
  190. end
  191. def test_assert_tag_children_greater_than
  192. process :test_html_output
  193. # there is a 'body' tag with more than 1 children
  194. assert_tag :children => { :greater_than => 1 }, :tag => "body"
  195. # there is no tag with more than 10 children
  196. assert_no_tag :children => { :greater_than => 10 }
  197. end
  198. def test_assert_tag_children_only
  199. process :test_html_output
  200. # there is a tag containing only one child with an id of 'foo'
  201. assert_tag :children => { :count => 1,
  202. :only => { :attributes => { :id => "foo" } } }
  203. # there is no tag containing only one 'li' child
  204. assert_no_tag :children => { :count => 1, :only => { :tag => "li" } }
  205. end
  206. def test_assert_tag_content
  207. process :test_html_output
  208. # the output contains the string "Name"
  209. assert_tag :content => /Name/
  210. # the output does not contain the string "test"
  211. assert_no_tag :content => /test/
  212. end
  213. def test_assert_tag_multiple
  214. process :test_html_output
  215. # there is a 'div', id='bar', with an immediate child whose 'action'
  216. # attribute matches the regexp /somewhere/.
  217. assert_tag :tag => "div", :attributes => { :id => "bar" },
  218. :child => { :attributes => { :action => /somewhere/ } }
  219. # there is no 'div', id='foo', with a 'ul' child with more than
  220. # 2 "li" children.
  221. assert_no_tag :tag => "div", :attributes => { :id => "foo" },
  222. :child => {
  223. :tag => "ul",
  224. :children => { :greater_than => 2,
  225. :only => { :tag => "li" } } }
  226. end
  227. def test_assert_tag_children_without_content
  228. process :test_html_output
  229. # there is a form tag with an 'input' child which is a self closing tag
  230. assert_tag :tag => "form",
  231. :children => { :count => 1,
  232. :only => { :tag => "input" } }
  233. # the body tag has an 'a' child which in turn has an 'img' child
  234. assert_tag :tag => "body",
  235. :children => { :count => 1,
  236. :only => { :tag => "a",
  237. :children => { :count => 1,
  238. :only => { :tag => "img" } } } }
  239. end
  240. def test_assert_tag_attribute_matching
  241. @response.body = '<input type="text" name="my_name">'
  242. assert_tag :tag => 'input',
  243. :attributes => { :name => /my/, :type => 'text' }
  244. assert_no_tag :tag => 'input',
  245. :attributes => { :name => 'my', :type => 'text' }
  246. assert_no_tag :tag => 'input',
  247. :attributes => { :name => /^my$/, :type => 'text' }
  248. end
  249. def test_assert_tag_content_matching
  250. @response.body = "<p>hello world</p>"
  251. assert_tag :tag => "p", :content => "hello world"
  252. assert_tag :tag => "p", :content => /hello/
  253. assert_no_tag :tag => "p", :content => "hello"
  254. end
  255. def test_assert_generates
  256. assert_generates 'controller/action/5', :controller => 'controller', :action => 'action', :id => '5'
  257. assert_generates 'controller/action/7', {:id => "7"}, {:controller => "controller", :action => "action"}
  258. assert_generates 'controller/action/5', {:controller => "controller", :action => "action", :id => "5", :name => "bob"}, {}, {:name => "bob"}
  259. assert_generates 'controller/action/7', {:id => "7", :name => "bob"}, {:controller => "controller", :action => "action"}, {:name => "bob"}
  260. assert_generates 'controller/action/7', {:id => "7"}, {:controller => "controller", :action => "action", :name => "bob"}, {}
  261. end
  262. def test_assert_routing
  263. assert_routing 'content', :controller => 'content', :action => 'index'
  264. end
  265. def test_assert_routing_in_module
  266. assert_routing 'admin/user', :controller => 'admin/user', :action => 'index'
  267. end
  268. def test_params_passing
  269. get :test_params, :page => {:name => "Page name", :month => '4', :year => '2004', :day => '6'}
  270. parsed_params = eval(@response.body)
  271. assert_equal(
  272. {'controller' => 'test_test/test', 'action' => 'test_params',
  273. 'page' => {'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6'}},
  274. parsed_params
  275. )
  276. end
  277. def test_id_converted_to_string
  278. get :test_params, :id => 20, :foo => Object.new
  279. assert_kind_of String, @request.path_parameters['id']
  280. end
  281. def test_array_path_parameter_handled_properly
  282. with_routing do |set|
  283. set.draw do |map|
  284. map.connect 'file/*path', :controller => 'test_test/test', :action => 'test_params'
  285. map.connect ':controller/:action/:id'
  286. end
  287. get :test_params, :path => ['hello', 'world']
  288. assert_equal ['hello', 'world'], @request.path_parameters['path']
  289. assert_equal 'hello/world', @request.path_parameters['path'].to_s
  290. end
  291. end
  292. def test_assert_realistic_path_parameters
  293. get :test_params, :id => 20, :foo => Object.new
  294. # All elements of path_parameters should use string keys
  295. @request.path_parameters.keys.each do |key|
  296. assert_kind_of String, key
  297. end
  298. end
  299. def test_with_routing_places_routes_back
  300. assert ActionController::Routing::Routes
  301. routes_id = ActionController::Routing::Routes.object_id
  302. begin
  303. with_routing { raise 'fail' }
  304. fail 'Should not be here.'
  305. rescue RuntimeError
  306. end
  307. assert ActionController::Routing::Routes
  308. assert_equal routes_id, ActionController::Routing::Routes.object_id
  309. end
  310. def test_remote_addr
  311. get :test_remote_addr
  312. assert_equal "0.0.0.0", @response.body
  313. @request.remote_addr = "192.0.0.1"
  314. get :test_remote_addr
  315. assert_equal "192.0.0.1", @response.body
  316. end
  317. def test_header_properly_reset_after_remote_http_request
  318. xhr :get, :test_params
  319. assert_nil @request.env['HTTP_X_REQUESTED_WITH']
  320. end
  321. def test_header_properly_reset_after_get_request
  322. get :test_params
  323. @request.recycle!
  324. assert_nil @request.instance_variable_get("@request_method")
  325. end
  326. %w(controller response request).each do |variable|
  327. %w(get post put delete head process).each do |method|
  328. define_method("test_#{variable}_missing_for_#{method}_raises_error") do
  329. remove_instance_variable "@#{variable}"
  330. begin
  331. send(method, :test_remote_addr)
  332. assert false, "expected RuntimeError, got nothing"
  333. rescue RuntimeError => error
  334. assert true
  335. assert_match %r{@#{variable} is nil}, error.message
  336. rescue => error
  337. assert false, "expected RuntimeError, got #{error.class}"
  338. end
  339. end
  340. end
  341. end
  342. FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart'
  343. def test_test_uploaded_file
  344. filename = 'mona_lisa.jpg'
  345. path = "#{FILES_DIR}/#{filename}"
  346. content_type = 'image/png'
  347. file = ActionController::TestUploadedFile.new(path, content_type)
  348. assert_equal filename, file.original_filename
  349. assert_equal content_type, file.content_type
  350. assert_equal file.path, file.local_path
  351. assert_equal File.read(path), file.read
  352. end
  353. def test_fixture_file_upload
  354. post :test_file_upload, :file => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")
  355. assert_equal 159528, @response.body
  356. end
  357. def test_test_uploaded_file_exception_when_file_doesnt_exist
  358. assert_raise(RuntimeError) { ActionController::TestUploadedFile.new('non_existent_file') }
  359. end
  360. def test_assert_redirected_to_symbol
  361. with_foo_routing do |set|
  362. assert_deprecated(/generate_url.*redirect_to/) do
  363. get :redirect_to_symbol
  364. end
  365. assert_response :redirect
  366. assert_redirected_to :generate_url
  367. end
  368. end
  369. def test_assert_follow_redirect_to_same_controller
  370. with_foo_routing do |set|
  371. get :redirect_to_same_controller
  372. assert_response :redirect
  373. assert_redirected_to :controller => 'test_test/test', :action => 'test_uri', :id => 5
  374. assert_nothing_raised { follow_redirect }
  375. end
  376. end
  377. def test_assert_follow_redirect_to_different_controller
  378. with_foo_routing do |set|
  379. get :redirect_to_different_controller
  380. assert_response :redirect
  381. assert_redirected_to :controller => 'fail', :id => 5
  382. assert_raise(RuntimeError) { follow_redirect }
  383. end
  384. end
  385. def test_redirect_url_only_cares_about_location_header
  386. get :create
  387. assert_response :created
  388. # Redirect url doesn't care that it wasn't a :redirect response.
  389. assert_equal 'created resource', @response.redirect_url
  390. assert_equal @response.redirect_url, redirect_to_url
  391. # Must be a :redirect response.
  392. assert_raise(Test::Unit::AssertionFailedError) do
  393. assert_redirected_to 'created resource'
  394. end
  395. end
  396. protected
  397. def with_foo_routing
  398. with_routing do |set|
  399. set.draw do |map|
  400. map.generate_url 'foo', :controller => 'test'
  401. map.connect ':controller/:action/:id'
  402. end
  403. yield set
  404. end
  405. end
  406. end