PageRenderTime 100ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://github.com/skdaksh/authlogic_example
Ruby | 536 lines | 432 code | 103 blank | 1 comment | 13 complexity | cc967f5f587906956b8573fe03063ce9 MD5 | raw file
  1. require 'abstract_unit'
  2. class RespondToController < ActionController::Base
  3. layout :set_layout
  4. def html_xml_or_rss
  5. respond_to do |type|
  6. type.html { render :text => "HTML" }
  7. type.xml { render :text => "XML" }
  8. type.rss { render :text => "RSS" }
  9. type.all { render :text => "Nothing" }
  10. end
  11. end
  12. def js_or_html
  13. respond_to do |type|
  14. type.html { render :text => "HTML" }
  15. type.js { render :text => "JS" }
  16. type.all { render :text => "Nothing" }
  17. end
  18. end
  19. def json_or_yaml
  20. respond_to do |type|
  21. type.json { render :text => "JSON" }
  22. type.yaml { render :text => "YAML" }
  23. end
  24. end
  25. def html_or_xml
  26. respond_to do |type|
  27. type.html { render :text => "HTML" }
  28. type.xml { render :text => "XML" }
  29. type.all { render :text => "Nothing" }
  30. end
  31. end
  32. def forced_xml
  33. request.format = :xml
  34. respond_to do |type|
  35. type.html { render :text => "HTML" }
  36. type.xml { render :text => "XML" }
  37. end
  38. end
  39. def just_xml
  40. respond_to do |type|
  41. type.xml { render :text => "XML" }
  42. end
  43. end
  44. def using_defaults
  45. respond_to do |type|
  46. type.html
  47. type.js
  48. type.xml
  49. end
  50. end
  51. def using_defaults_with_type_list
  52. respond_to(:html, :js, :xml)
  53. end
  54. def made_for_content_type
  55. respond_to do |type|
  56. type.rss { render :text => "RSS" }
  57. type.atom { render :text => "ATOM" }
  58. type.all { render :text => "Nothing" }
  59. end
  60. end
  61. def custom_type_handling
  62. respond_to do |type|
  63. type.html { render :text => "HTML" }
  64. type.custom("application/crazy-xml") { render :text => "Crazy XML" }
  65. type.all { render :text => "Nothing" }
  66. end
  67. end
  68. def custom_constant_handling
  69. Mime::Type.register("text/x-mobile", :mobile)
  70. respond_to do |type|
  71. type.html { render :text => "HTML" }
  72. type.mobile { render :text => "Mobile" }
  73. end
  74. ensure
  75. Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
  76. end
  77. def custom_constant_handling_without_block
  78. Mime::Type.register("text/x-mobile", :mobile)
  79. respond_to do |type|
  80. type.html { render :text => "HTML" }
  81. type.mobile
  82. end
  83. ensure
  84. Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
  85. end
  86. def handle_any
  87. respond_to do |type|
  88. type.html { render :text => "HTML" }
  89. type.any(:js, :xml) { render :text => "Either JS or XML" }
  90. end
  91. end
  92. def handle_any_any
  93. respond_to do |type|
  94. type.html { render :text => 'HTML' }
  95. type.any { render :text => 'Whatever you ask for, I got it' }
  96. end
  97. end
  98. def all_types_with_layout
  99. respond_to do |type|
  100. type.html
  101. type.js
  102. end
  103. end
  104. def iphone_with_html_response_type
  105. Mime::Type.register_alias("text/html", :iphone)
  106. request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
  107. respond_to do |type|
  108. type.html { @type = "Firefox" }
  109. type.iphone { @type = "iPhone" }
  110. end
  111. ensure
  112. Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
  113. end
  114. def iphone_with_html_response_type_without_layout
  115. Mime::Type.register_alias("text/html", :iphone)
  116. request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
  117. respond_to do |type|
  118. type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" }
  119. type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" }
  120. end
  121. ensure
  122. Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
  123. end
  124. def rescue_action(e)
  125. raise
  126. end
  127. protected
  128. def set_layout
  129. if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name)
  130. "respond_to/layouts/standard"
  131. elsif action_name == "iphone_with_html_response_type_without_layout"
  132. "respond_to/layouts/missing"
  133. end
  134. end
  135. end
  136. class MimeControllerTest < ActionController::TestCase
  137. tests RespondToController
  138. def setup
  139. ActionController::Base.use_accept_header = true
  140. @request.host = "www.example.com"
  141. end
  142. def teardown
  143. ActionController::Base.use_accept_header = false
  144. end
  145. def test_html
  146. @request.accept = "text/html"
  147. get :js_or_html
  148. assert_equal 'HTML', @response.body
  149. get :html_or_xml
  150. assert_equal 'HTML', @response.body
  151. get :just_xml
  152. assert_response 406
  153. end
  154. def test_all
  155. @request.accept = "*/*"
  156. get :js_or_html
  157. assert_equal 'HTML', @response.body # js is not part of all
  158. get :html_or_xml
  159. assert_equal 'HTML', @response.body
  160. get :just_xml
  161. assert_equal 'XML', @response.body
  162. end
  163. def test_xml
  164. @request.accept = "application/xml"
  165. get :html_xml_or_rss
  166. assert_equal 'XML', @response.body
  167. end
  168. def test_js_or_html
  169. @request.accept = "text/javascript, text/html"
  170. get :js_or_html
  171. assert_equal 'JS', @response.body
  172. get :html_or_xml
  173. assert_equal 'HTML', @response.body
  174. get :just_xml
  175. assert_response 406
  176. end
  177. def test_json_or_yaml
  178. get :json_or_yaml
  179. assert_equal 'JSON', @response.body
  180. get :json_or_yaml, :format => 'json'
  181. assert_equal 'JSON', @response.body
  182. get :json_or_yaml, :format => 'yaml'
  183. assert_equal 'YAML', @response.body
  184. { 'YAML' => %w(text/yaml),
  185. 'JSON' => %w(application/json text/x-json)
  186. }.each do |body, content_types|
  187. content_types.each do |content_type|
  188. @request.accept = content_type
  189. get :json_or_yaml
  190. assert_equal body, @response.body
  191. end
  192. end
  193. end
  194. def test_js_or_anything
  195. @request.accept = "text/javascript, */*"
  196. get :js_or_html
  197. assert_equal 'JS', @response.body
  198. get :html_or_xml
  199. assert_equal 'HTML', @response.body
  200. get :just_xml
  201. assert_equal 'XML', @response.body
  202. end
  203. def test_using_defaults
  204. @request.accept = "*/*"
  205. get :using_defaults
  206. assert_equal "text/html", @response.content_type
  207. assert_equal 'Hello world!', @response.body
  208. @request.accept = "text/javascript"
  209. get :using_defaults
  210. assert_equal "text/javascript", @response.content_type
  211. assert_equal '$("body").visualEffect("highlight");', @response.body
  212. @request.accept = "application/xml"
  213. get :using_defaults
  214. assert_equal "application/xml", @response.content_type
  215. assert_equal "<p>Hello world!</p>\n", @response.body
  216. end
  217. def test_using_defaults_with_type_list
  218. @request.accept = "*/*"
  219. get :using_defaults_with_type_list
  220. assert_equal "text/html", @response.content_type
  221. assert_equal 'Hello world!', @response.body
  222. @request.accept = "text/javascript"
  223. get :using_defaults_with_type_list
  224. assert_equal "text/javascript", @response.content_type
  225. assert_equal '$("body").visualEffect("highlight");', @response.body
  226. @request.accept = "application/xml"
  227. get :using_defaults_with_type_list
  228. assert_equal "application/xml", @response.content_type
  229. assert_equal "<p>Hello world!</p>\n", @response.body
  230. end
  231. def test_with_atom_content_type
  232. @request.env["CONTENT_TYPE"] = "application/atom+xml"
  233. get :made_for_content_type
  234. assert_equal "ATOM", @response.body
  235. end
  236. def test_with_rss_content_type
  237. @request.env["CONTENT_TYPE"] = "application/rss+xml"
  238. get :made_for_content_type
  239. assert_equal "RSS", @response.body
  240. end
  241. def test_synonyms
  242. @request.accept = "application/javascript"
  243. get :js_or_html
  244. assert_equal 'JS', @response.body
  245. @request.accept = "application/x-xml"
  246. get :html_xml_or_rss
  247. assert_equal "XML", @response.body
  248. end
  249. def test_custom_types
  250. @request.accept = "application/crazy-xml"
  251. get :custom_type_handling
  252. assert_equal "application/crazy-xml", @response.content_type
  253. assert_equal 'Crazy XML', @response.body
  254. @request.accept = "text/html"
  255. get :custom_type_handling
  256. assert_equal "text/html", @response.content_type
  257. assert_equal 'HTML', @response.body
  258. end
  259. def test_xhtml_alias
  260. @request.accept = "application/xhtml+xml,application/xml"
  261. get :html_or_xml
  262. assert_equal 'HTML', @response.body
  263. end
  264. def test_firefox_simulation
  265. @request.accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
  266. get :html_or_xml
  267. assert_equal 'HTML', @response.body
  268. end
  269. def test_handle_any
  270. @request.accept = "*/*"
  271. get :handle_any
  272. assert_equal 'HTML', @response.body
  273. @request.accept = "text/javascript"
  274. get :handle_any
  275. assert_equal 'Either JS or XML', @response.body
  276. @request.accept = "text/xml"
  277. get :handle_any
  278. assert_equal 'Either JS or XML', @response.body
  279. end
  280. def test_handle_any_any
  281. @request.accept = "*/*"
  282. get :handle_any_any
  283. assert_equal 'HTML', @response.body
  284. end
  285. def test_handle_any_any_parameter_format
  286. get :handle_any_any, {:format=>'html'}
  287. assert_equal 'HTML', @response.body
  288. end
  289. def test_handle_any_any_explicit_html
  290. @request.accept = "text/html"
  291. get :handle_any_any
  292. assert_equal 'HTML', @response.body
  293. end
  294. def test_handle_any_any_javascript
  295. @request.accept = "text/javascript"
  296. get :handle_any_any
  297. assert_equal 'Whatever you ask for, I got it', @response.body
  298. end
  299. def test_handle_any_any_xml
  300. @request.accept = "text/xml"
  301. get :handle_any_any
  302. assert_equal 'Whatever you ask for, I got it', @response.body
  303. end
  304. def test_rjs_type_skips_layout
  305. @request.accept = "text/javascript"
  306. get :all_types_with_layout
  307. assert_equal 'RJS for all_types_with_layout', @response.body
  308. end
  309. def test_html_type_with_layout
  310. @request.accept = "text/html"
  311. get :all_types_with_layout
  312. assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
  313. end
  314. def test_xhr
  315. xhr :get, :js_or_html
  316. assert_equal 'JS', @response.body
  317. xhr :get, :using_defaults
  318. assert_equal '$("body").visualEffect("highlight");', @response.body
  319. end
  320. def test_custom_constant
  321. get :custom_constant_handling, :format => "mobile"
  322. assert_equal "text/x-mobile", @response.content_type
  323. assert_equal "Mobile", @response.body
  324. end
  325. def test_custom_constant_handling_without_block
  326. get :custom_constant_handling_without_block, :format => "mobile"
  327. assert_equal "text/x-mobile", @response.content_type
  328. assert_equal "Mobile", @response.body
  329. end
  330. def test_forced_format
  331. get :html_xml_or_rss
  332. assert_equal "HTML", @response.body
  333. get :html_xml_or_rss, :format => "html"
  334. assert_equal "HTML", @response.body
  335. get :html_xml_or_rss, :format => "xml"
  336. assert_equal "XML", @response.body
  337. get :html_xml_or_rss, :format => "rss"
  338. assert_equal "RSS", @response.body
  339. end
  340. def test_internally_forced_format
  341. get :forced_xml
  342. assert_equal "XML", @response.body
  343. get :forced_xml, :format => "html"
  344. assert_equal "XML", @response.body
  345. end
  346. def test_extension_synonyms
  347. get :html_xml_or_rss, :format => "xhtml"
  348. assert_equal "HTML", @response.body
  349. end
  350. def test_render_action_for_html
  351. @controller.instance_eval do
  352. def render(*args)
  353. unless args.empty?
  354. @action = args.first[:action]
  355. end
  356. response.body = "#{@action} - #{@template.template_format}"
  357. end
  358. end
  359. get :using_defaults
  360. assert_equal "using_defaults - html", @response.body
  361. get :using_defaults, :format => "xml"
  362. assert_equal "using_defaults - xml", @response.body
  363. end
  364. def test_format_with_custom_response_type
  365. get :iphone_with_html_response_type
  366. assert_equal '<html><div id="html">Hello future from Firefox!</div></html>', @response.body
  367. get :iphone_with_html_response_type, :format => "iphone"
  368. assert_equal "text/html", @response.content_type
  369. assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
  370. end
  371. def test_format_with_custom_response_type_and_request_headers
  372. @request.accept = "text/iphone"
  373. get :iphone_with_html_response_type
  374. assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
  375. assert_equal "text/html", @response.content_type
  376. end
  377. def test_format_with_custom_response_type_and_request_headers_with_only_one_layout_present
  378. get :iphone_with_html_response_type_without_layout
  379. assert_equal '<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body
  380. @request.accept = "text/iphone"
  381. assert_raise(ActionView::MissingTemplate) { get :iphone_with_html_response_type_without_layout }
  382. end
  383. end
  384. class AbstractPostController < ActionController::Base
  385. self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/"
  386. end
  387. # For testing layouts which are set automatically
  388. class PostController < AbstractPostController
  389. around_filter :with_iphone
  390. def index
  391. respond_to do |type|
  392. type.html
  393. type.iphone
  394. end
  395. end
  396. protected
  397. def with_iphone
  398. Mime::Type.register_alias("text/html", :iphone)
  399. request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
  400. yield
  401. ensure
  402. Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
  403. end
  404. end
  405. class SuperPostController < PostController
  406. def index
  407. respond_to do |type|
  408. type.html
  409. type.iphone
  410. end
  411. end
  412. end
  413. class MimeControllerLayoutsTest < ActionController::TestCase
  414. tests PostController
  415. def setup
  416. @request.host = "www.example.com"
  417. end
  418. def test_missing_layout_renders_properly
  419. get :index
  420. assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body
  421. @request.accept = "text/iphone"
  422. get :index
  423. assert_equal 'Hello iPhone', @response.body
  424. end
  425. def test_format_with_inherited_layouts
  426. @controller = SuperPostController.new
  427. get :index
  428. assert_equal 'Super Firefox', @response.body
  429. @request.accept = "text/iphone"
  430. get :index
  431. assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
  432. end
  433. end