PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/globalize/test/mime_responds_test.rb

https://github.com/ischroedi/petracommunity
Ruby | 358 lines | 285 code | 71 blank | 2 comment | 2 complexity | 39d397a5a098b693f3f0c01824dcc9ae MD5 | raw file
  1. # Contain same tests as action_pack/test/mime_responds_test.rb and some more
  2. # to check multilingual rendering of templates with respond_to.
  3. require File.dirname(__FILE__) + '/test_helper'
  4. class RespondToController < ActionController::Base
  5. layout :set_layout
  6. def html_xml_or_rss
  7. respond_to do |type|
  8. type.html { render :text => "HTML" }
  9. type.xml { render :text => "XML" }
  10. type.rss { render :text => "RSS" }
  11. type.all { render :text => "Nothing" }
  12. end
  13. end
  14. def js_or_html
  15. respond_to do |type|
  16. type.html { render :text => "HTML" }
  17. type.js { render :text => "JS" }
  18. type.all { render :text => "Nothing" }
  19. end
  20. end
  21. def html_or_xml
  22. respond_to do |type|
  23. type.html { render :text => "HTML" }
  24. type.xml { render :text => "XML" }
  25. type.all { render :text => "Nothing" }
  26. end
  27. end
  28. def just_xml
  29. respond_to do |type|
  30. type.xml { render :text => "XML" }
  31. end
  32. end
  33. def using_defaults
  34. respond_to do |type|
  35. type.html
  36. type.js
  37. type.xml
  38. end
  39. end
  40. def using_defaults_with_type_list
  41. respond_to(:html, :js, :xml)
  42. end
  43. def made_for_content_type
  44. respond_to do |type|
  45. type.rss { render :text => "RSS" }
  46. type.atom { render :text => "ATOM" }
  47. type.all { render :text => "Nothing" }
  48. end
  49. end
  50. def custom_type_handling
  51. respond_to do |type|
  52. type.html { render :text => "HTML" }
  53. type.custom("application/crazy-xml") { render :text => "Crazy XML" }
  54. type.all { render :text => "Nothing" }
  55. end
  56. end
  57. def handle_any
  58. respond_to do |type|
  59. type.html { render :text => "HTML" }
  60. type.any(:js, :xml) { render :text => "Either JS or XML" }
  61. end
  62. end
  63. def all_types_with_layout
  64. respond_to do |type|
  65. type.html
  66. type.js
  67. end
  68. end
  69. def rescue_action(e)
  70. raise
  71. end
  72. protected
  73. def set_layout
  74. if action_name == "all_types_with_layout"
  75. "standard"
  76. end
  77. end
  78. end
  79. RespondToController.prepend_view_path(File.dirname(__FILE__) + "/views")
  80. class MimeControllerTest < Test::Unit::TestCase
  81. include Globalize
  82. def setup
  83. Locale.set(nil)
  84. @request = ActionController::TestRequest.new
  85. @response = ActionController::TestResponse.new
  86. @controller = RespondToController.new
  87. @request.host = "www.example.com"
  88. end
  89. def test_html
  90. @request.env["HTTP_ACCEPT"] = "text/html"
  91. get :js_or_html
  92. assert_equal 'HTML', @response.body
  93. get :html_or_xml
  94. assert_equal 'HTML', @response.body
  95. get :just_xml
  96. assert_response 406
  97. end
  98. def test_fr_html
  99. Locale.set('fr')
  100. test_html
  101. end
  102. def test_all
  103. @request.env["HTTP_ACCEPT"] = "*/*"
  104. get :js_or_html
  105. assert_equal 'HTML', @response.body # js is not part of all
  106. get :html_or_xml
  107. assert_equal 'HTML', @response.body
  108. get :just_xml
  109. assert_equal 'XML', @response.body
  110. end
  111. def test_fr_all
  112. Locale.set('fr')
  113. test_all
  114. end
  115. def test_xml
  116. @request.env["HTTP_ACCEPT"] = "application/xml"
  117. get :html_xml_or_rss
  118. assert_equal 'XML', @response.body
  119. end
  120. def test_js_or_html
  121. @request.env["HTTP_ACCEPT"] = "text/javascript, text/html"
  122. get :js_or_html
  123. assert_equal 'JS', @response.body
  124. get :html_or_xml
  125. assert_equal 'HTML', @response.body
  126. get :just_xml
  127. assert_response 406
  128. end
  129. def test_js_or_anything
  130. @request.env["HTTP_ACCEPT"] = "text/javascript, */*"
  131. get :js_or_html
  132. assert_equal 'JS', @response.body
  133. get :html_or_xml
  134. assert_equal 'HTML', @response.body
  135. get :just_xml
  136. assert_equal 'XML', @response.body
  137. end
  138. def test_fr_js_or_anything
  139. Locale.set('fr')
  140. test_js_or_anything
  141. end
  142. def test_using_defaults
  143. @request.env["HTTP_ACCEPT"] = "*/*"
  144. get :using_defaults
  145. assert_equal 'Hello world!', @response.body
  146. @request.env["HTTP_ACCEPT"] = "text/javascript"
  147. get :using_defaults
  148. assert_equal '$("body").visualEffect("highlight");', @response.body
  149. @request.env["HTTP_ACCEPT"] = "application/xml"
  150. get :using_defaults
  151. assert_equal "<p>Hello world!</p>\n", @response.body
  152. end
  153. def test_fr_using_defaults
  154. Locale.set('fr')
  155. @request.env["HTTP_ACCEPT"] = "*/*"
  156. get :using_defaults
  157. assert_equal 'Bonjour le monde !', @response.body
  158. @request.env["HTTP_ACCEPT"] = "text/javascript"
  159. get :using_defaults
  160. assert_equal '$("body").visualEffect("highlight");', @response.body
  161. @request.env["HTTP_ACCEPT"] = "application/xml"
  162. get :using_defaults
  163. assert_equal "<p>Bonjour le monde !</p>\n", @response.body
  164. end
  165. def test_fr_CH_fallback_to_fr_using_defaults
  166. Locale.set('fr-CH')
  167. test_fr_using_defaults
  168. end
  169. def test_de_fallback_to_default
  170. Locale.set('de')
  171. test_using_defaults
  172. end
  173. def test_using_defaults_with_type_list
  174. @request.env["HTTP_ACCEPT"] = "*/*"
  175. get :using_defaults_with_type_list
  176. assert_equal 'Hello world!', @response.body
  177. @request.env["HTTP_ACCEPT"] = "text/javascript"
  178. get :using_defaults_with_type_list
  179. assert_equal '$("body").visualEffect("highlight");', @response.body
  180. @request.env["HTTP_ACCEPT"] = "application/xml"
  181. get :using_defaults_with_type_list
  182. assert_equal "<p>Hello world!</p>\n", @response.body
  183. end
  184. def test_fr_using_defaults_with_type_list
  185. Locale.set('fr')
  186. @request.env["HTTP_ACCEPT"] = "*/*"
  187. get :using_defaults_with_type_list
  188. assert_equal 'Bonjour le monde !', @response.body
  189. @request.env["HTTP_ACCEPT"] = "text/javascript"
  190. get :using_defaults_with_type_list
  191. assert_equal '$("body").visualEffect("highlight");', @response.body
  192. @request.env["HTTP_ACCEPT"] = "application/xml"
  193. get :using_defaults_with_type_list
  194. assert_equal "<p>Bonjour le monde !</p>\n", @response.body
  195. end
  196. def test_with_content_type
  197. @request.env["CONTENT_TYPE"] = "application/atom+xml"
  198. get :made_for_content_type
  199. assert_equal "ATOM", @response.body
  200. @request.env["CONTENT_TYPE"] = "application/rss+xml"
  201. get :made_for_content_type
  202. assert_equal "RSS", @response.body
  203. end
  204. def test_fr_with_content_type
  205. Locale.set('fr')
  206. test_with_content_type
  207. end
  208. def test_synonyms
  209. @request.env["HTTP_ACCEPT"] = "application/javascript"
  210. get :js_or_html
  211. assert_equal 'JS', @response.body
  212. @request.env["HTTP_ACCEPT"] = "application/x-xml"
  213. get :html_xml_or_rss
  214. assert_equal "XML", @response.body
  215. end
  216. def test_fr_synonyms
  217. Locale.set('fr')
  218. test_synonyms
  219. end
  220. def test_custom_types
  221. @request.env["HTTP_ACCEPT"] = "application/crazy-xml"
  222. get :custom_type_handling
  223. assert_equal 'Crazy XML', @response.body
  224. @request.env["HTTP_ACCEPT"] = "text/html"
  225. get :custom_type_handling
  226. assert_equal 'HTML', @response.body
  227. end
  228. def test_fr_custom_types
  229. Locale.set('fr')
  230. test_custom_types
  231. end
  232. def test_xhtml_alias
  233. @request.env["HTTP_ACCEPT"] = "application/xhtml+xml,application/xml"
  234. get :html_or_xml
  235. assert_equal 'HTML', @response.body
  236. end
  237. def test_fr_xhtml_alias
  238. Locale.set('fr')
  239. test_custom_types
  240. end
  241. def test_firefox_simulation
  242. @request.env["HTTP_ACCEPT"] = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
  243. get :html_or_xml
  244. assert_equal 'HTML', @response.body
  245. end
  246. def test_fr_firefox_simulation
  247. Locale.set('fr')
  248. test_firefox_simulation
  249. end
  250. def test_handle_any
  251. @request.env["HTTP_ACCEPT"] = "*/*"
  252. get :handle_any
  253. assert_equal 'HTML', @response.body
  254. @request.env["HTTP_ACCEPT"] = "text/javascript"
  255. get :handle_any
  256. assert_equal 'Either JS or XML', @response.body
  257. @request.env["HTTP_ACCEPT"] = "text/xml"
  258. get :handle_any
  259. assert_equal 'Either JS or XML', @response.body
  260. end
  261. def test_fr_handle_any
  262. Locale.set('fr')
  263. test_handle_any
  264. end
  265. def test_all_types_with_layout
  266. @request.env["HTTP_ACCEPT"] = "text/javascript"
  267. get :all_types_with_layout
  268. assert_equal 'RJS for all_types_with_layout', @response.body
  269. @request.env["HTTP_ACCEPT"] = "text/html"
  270. get :all_types_with_layout
  271. assert_equal '<html>HTML for all_types_with_layout</html>', @response.body
  272. end
  273. def test_fr_all_types_with_layout
  274. Locale.set('fr')
  275. test_all_types_with_layout
  276. end
  277. def test_xhr
  278. xhr :get, :js_or_html
  279. assert_equal 'JS', @response.body
  280. xhr :get, :using_defaults
  281. assert_equal '$("body").visualEffect("highlight");', @response.body
  282. end
  283. def test_fr_xhr
  284. Locale.set('fr')
  285. test_xhr
  286. end
  287. end