PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://monkeycharger.googlecode.com/
Ruby | 428 lines | 373 code | 50 blank | 5 comment | 0 complexity | 39506c60cbab355dae1044fd7bb5a0c0 MD5 | raw file
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. unless defined?(Customer)
  3. Customer = Struct.new("Customer", :name)
  4. end
  5. module Fun
  6. class GamesController < ActionController::Base
  7. def hello_world
  8. end
  9. end
  10. end
  11. class TestController < ActionController::Base
  12. layout :determine_layout
  13. def hello_world
  14. end
  15. def render_hello_world
  16. render "test/hello_world"
  17. end
  18. def render_hello_world_from_variable
  19. @person = "david"
  20. render_text "hello #{@person}"
  21. end
  22. def render_action_hello_world
  23. render_action "hello_world"
  24. end
  25. def render_action_hello_world_with_symbol
  26. render_action :hello_world
  27. end
  28. def render_text_hello_world
  29. render_text "hello world"
  30. end
  31. def render_json_hello_world
  32. render_json({:hello => 'world'}.to_json)
  33. end
  34. def render_json_hello_world_with_callback
  35. render_json({:hello => 'world'}.to_json, 'alert')
  36. end
  37. def render_symbol_json
  38. render :json => {:hello => 'world'}.to_json
  39. end
  40. def render_custom_code
  41. render_text "hello world", "404 Moved"
  42. end
  43. def render_text_appendix
  44. render_text "hello world"
  45. render_text ", goodbye!", "404 Not Found", true
  46. end
  47. def render_nothing_with_appendix
  48. render_text "appended", nil, true
  49. end
  50. def render_xml_hello
  51. @name = "David"
  52. render "test/hello"
  53. end
  54. def heading
  55. head :ok
  56. end
  57. def greeting
  58. # let's just rely on the template
  59. end
  60. def layout_test
  61. render_action "hello_world"
  62. end
  63. def builder_layout_test
  64. render_action "hello"
  65. end
  66. def builder_partial_test
  67. render_action "hello_world_container"
  68. end
  69. def partials_list
  70. @test_unchanged = 'hello'
  71. @customers = [ Customer.new("david"), Customer.new("mary") ]
  72. render_action "list"
  73. end
  74. def partial_only
  75. render_partial
  76. end
  77. def hello_in_a_string
  78. @customers = [ Customer.new("david"), Customer.new("mary") ]
  79. render_text "How's there? #{render_to_string("test/list")}"
  80. end
  81. def accessing_params_in_template
  82. render_template "Hello: <%= params[:name] %>"
  83. end
  84. def accessing_local_assigns_in_inline_template
  85. name = params[:local_name]
  86. render :inline => "<%= 'Goodbye, ' + local_name %>",
  87. :locals => { :local_name => name }
  88. end
  89. def accessing_local_assigns_in_inline_template_with_string_keys
  90. name = params[:local_name]
  91. ActionView::Base.local_assigns_support_string_keys = true
  92. render :inline => "<%= 'Goodbye, ' + local_name %>",
  93. :locals => { "local_name" => name }
  94. ActionView::Base.local_assigns_support_string_keys = false
  95. end
  96. def formatted_html_erb
  97. end
  98. def formatted_xml_erb
  99. end
  100. def render_to_string_test
  101. @foo = render_to_string :inline => "this is a test"
  102. end
  103. def partial
  104. render :partial => 'partial'
  105. end
  106. def partial_dot_html
  107. render :partial => 'partial.html.erb'
  108. end
  109. def partial_as_rjs
  110. render :update do |page|
  111. page.replace :foo, :partial => 'partial'
  112. end
  113. end
  114. def respond_to_partial_as_rjs
  115. respond_to do |format|
  116. format.js do
  117. render :update do |page|
  118. page.replace :foo, :partial => 'partial'
  119. end
  120. end
  121. end
  122. end
  123. def rescue_action(e) raise end
  124. private
  125. def determine_layout
  126. case action_name
  127. when "layout_test": "layouts/standard"
  128. when "builder_layout_test": "layouts/builder"
  129. when "render_symbol_json": "layouts/standard" # to make sure layouts don't interfere
  130. end
  131. end
  132. end
  133. TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
  134. Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
  135. class RenderTest < Test::Unit::TestCase
  136. def setup
  137. @request = ActionController::TestRequest.new
  138. @response = ActionController::TestResponse.new
  139. @controller = TestController.new
  140. @request.host = "www.nextangle.com"
  141. end
  142. def test_simple_show
  143. get :hello_world
  144. assert_response 200
  145. assert_template "test/hello_world"
  146. end
  147. def test_do_with_render
  148. assert_deprecated_render { get :render_hello_world }
  149. assert_template "test/hello_world"
  150. end
  151. def test_do_with_render_from_variable
  152. get :render_hello_world_from_variable
  153. assert_equal "hello david", @response.body
  154. end
  155. def test_do_with_render_action
  156. get :render_action_hello_world
  157. assert_template "test/hello_world"
  158. end
  159. def test_do_with_render_action_with_symbol
  160. get :render_action_hello_world_with_symbol
  161. assert_template "test/hello_world"
  162. end
  163. def test_do_with_render_text
  164. get :render_text_hello_world
  165. assert_equal "hello world", @response.body
  166. end
  167. def test_do_with_render_json
  168. get :render_json_hello_world
  169. assert_equal '{hello: "world"}', @response.body
  170. assert_equal 'application/json', @response.content_type
  171. end
  172. def test_do_with_render_json_with_callback
  173. get :render_json_hello_world_with_callback
  174. assert_equal 'alert({hello: "world"})', @response.body
  175. assert_equal 'application/json', @response.content_type
  176. end
  177. def test_do_with_render_symbol_json
  178. get :render_symbol_json
  179. assert_equal '{hello: "world"}', @response.body
  180. assert_equal 'application/json', @response.content_type
  181. end
  182. def test_do_with_render_custom_code
  183. get :render_custom_code
  184. assert_response 404
  185. end
  186. def test_do_with_render_text_appendix
  187. get :render_text_appendix
  188. assert_response 404
  189. assert_equal 'hello world, goodbye!', @response.body
  190. end
  191. def test_do_with_render_nothing_with_appendix
  192. get :render_nothing_with_appendix
  193. assert_response 200
  194. assert_equal 'appended', @response.body
  195. end
  196. def test_attempt_to_access_object_method
  197. assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
  198. end
  199. def test_private_methods
  200. assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
  201. end
  202. def test_render_xml
  203. assert_deprecated_render { get :render_xml_hello }
  204. assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
  205. end
  206. def test_render_xml_with_default
  207. get :greeting
  208. assert_equal "<p>This is grand!</p>\n", @response.body
  209. end
  210. def test_render_xml_with_partial
  211. get :builder_partial_test
  212. assert_equal "<test>\n <hello/>\n</test>\n", @response.body
  213. end
  214. def test_layout_rendering
  215. get :layout_test
  216. assert_equal "<html>Hello world!</html>", @response.body
  217. end
  218. def test_render_xml_with_layouts
  219. get :builder_layout_test
  220. assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
  221. end
  222. # def test_partials_list
  223. # get :partials_list
  224. # assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
  225. # end
  226. def test_partial_only
  227. get :partial_only
  228. assert_equal "only partial", @response.body
  229. end
  230. def test_render_to_string
  231. get :hello_in_a_string
  232. assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
  233. end
  234. def test_render_to_string_resets_assigns
  235. get :render_to_string_test
  236. assert_equal "The value of foo is: ::this is a test::\n", @response.body
  237. end
  238. def test_nested_rendering
  239. @controller = Fun::GamesController.new
  240. get :hello_world
  241. assert_equal "Living in a nested world", @response.body
  242. end
  243. def test_accessing_params_in_template
  244. get :accessing_params_in_template, :name => "David"
  245. assert_equal "Hello: David", @response.body
  246. end
  247. def test_accessing_local_assigns_in_inline_template
  248. get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
  249. assert_equal "Goodbye, Local David", @response.body
  250. end
  251. def test_accessing_local_assigns_in_inline_template_with_string_keys
  252. get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David"
  253. assert_equal "Goodbye, Local David", @response.body
  254. end
  255. def test_render_200_should_set_etag
  256. get :render_hello_world_from_variable
  257. assert_equal etag_for("hello david"), @response.headers['ETag']
  258. end
  259. def test_render_against_etag_request_should_304_when_match
  260. @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello david")
  261. get :render_hello_world_from_variable
  262. assert_equal "304 Not Modified", @response.headers['Status']
  263. assert @response.body.empty?
  264. end
  265. def test_render_against_etag_request_should_200_when_no_match
  266. @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello somewhere else")
  267. get :render_hello_world_from_variable
  268. assert_equal "200 OK", @response.headers['Status']
  269. assert !@response.body.empty?
  270. end
  271. def test_render_with_etag
  272. get :render_hello_world_from_variable
  273. expected_etag = etag_for('hello david')
  274. assert_equal expected_etag, @response.headers['ETag']
  275. @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
  276. get :render_hello_world_from_variable
  277. assert_equal "304 Not Modified", @response.headers['Status']
  278. @request.headers["HTTP_IF_NONE_MATCH"] = "\"diftag\""
  279. get :render_hello_world_from_variable
  280. assert_equal "200 OK", @response.headers['Status']
  281. end
  282. def render_with_404_shouldnt_have_etag
  283. get :render_custom_code
  284. assert_nil @response.headers['ETag']
  285. end
  286. def test_etag_should_not_be_changed_when_already_set
  287. expected_etag = etag_for("hello somewhere else")
  288. @response.headers["ETag"] = expected_etag
  289. get :render_hello_world_from_variable
  290. assert_equal expected_etag, @response.headers['ETag']
  291. end
  292. def test_etag_should_govern_renders_with_layouts_too
  293. get :builder_layout_test
  294. assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
  295. assert_equal etag_for("<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n"), @response.headers['ETag']
  296. end
  297. def test_should_render_formatted_template
  298. get :formatted_html_erb
  299. assert_equal 'formatted html erb', @response.body
  300. end
  301. def test_should_render_formatted_xml_erb_template
  302. get :formatted_xml_erb, :format => :xml
  303. assert_equal '<test>passed formatted xml erb</test>', @response.body
  304. end
  305. def test_should_render_formatted_html_erb_template
  306. get :formatted_xml_erb
  307. assert_equal '<test>passed formatted html erb</test>', @response.body
  308. end
  309. def test_should_render_formatted_html_erb_template_with_faulty_accepts_header
  310. @request.env["HTTP_ACCEPT"] = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*"
  311. get :formatted_xml_erb
  312. assert_equal '<test>passed formatted html erb</test>', @response.body
  313. end
  314. def test_should_render_html_formatted_partial
  315. get :partial
  316. assert_equal 'partial html', @response.body
  317. end
  318. def test_should_render_html_partial_with_dot
  319. get :partial_dot_html
  320. assert_equal 'partial html', @response.body
  321. end
  322. def test_should_render_html_formatted_partial_with_rjs
  323. xhr :get, :partial_as_rjs
  324. assert_equal %(Element.replace("foo", "partial html");), @response.body
  325. end
  326. def test_should_render_html_formatted_partial_with_rjs_and_js_format
  327. xhr :get, :respond_to_partial_as_rjs
  328. assert_equal %(Element.replace("foo", "partial html");), @response.body
  329. end
  330. def test_should_render_js_partial
  331. xhr :get, :partial, :format => 'js'
  332. assert_equal 'partial js', @response.body
  333. end
  334. protected
  335. def assert_deprecated_render(&block)
  336. assert_deprecated(/render/, &block)
  337. end
  338. def etag_for(text)
  339. %("#{Digest::MD5.hexdigest(text)}")
  340. end
  341. end