Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / test / controller / mime_responds_test.rb
blob4c48d4cdece29bef3911e582c588c373e7568036
1 require File.dirname(__FILE__) + '/../abstract_unit'
3 class RespondToController < ActionController::Base
4   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
15   def js_or_html
16     respond_to do |type|
17       type.html { render :text => "HTML"    }
18       type.js   { render :text => "JS"      }
19       type.all  { render :text => "Nothing" }
20     end
21   end
23   def json_or_yaml
24     respond_to do |type|
25       type.json { render :text => "JSON" }
26       type.yaml { render :text => "YAML" }
27     end
28   end
30   def html_or_xml
31     respond_to do |type|
32       type.html { render :text => "HTML"    }
33       type.xml  { render :text => "XML"     }
34       type.all  { render :text => "Nothing" }
35     end
36   end
38   def forced_xml
39     request.format = :xml
41     respond_to do |type|
42       type.html { render :text => "HTML"    }
43       type.xml  { render :text => "XML"     }
44     end
45   end
47   def just_xml
48     respond_to do |type|
49       type.xml  { render :text => "XML" }
50     end
51   end
53   def using_defaults
54     respond_to do |type|
55       type.html
56       type.js
57       type.xml
58     end
59   end
61   def using_defaults_with_type_list
62     respond_to(:html, :js, :xml)
63   end
65   def made_for_content_type
66     respond_to do |type|
67       type.rss  { render :text => "RSS"  }
68       type.atom { render :text => "ATOM" }
69       type.all  { render :text => "Nothing" }
70     end
71   end
73   def custom_type_handling
74     respond_to do |type|
75       type.html { render :text => "HTML"    }
76       type.custom("application/crazy-xml")  { render :text => "Crazy XML"  }
77       type.all  { render :text => "Nothing" }
78     end
79   end
81   def custom_constant_handling
82     Mime::Type.register("text/x-mobile", :mobile)
84     respond_to do |type|
85       type.html   { render :text => "HTML"   }
86       type.mobile { render :text => "Mobile" }
87     end
88   ensure
89     Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
90   end
92   def custom_constant_handling_without_block
93     Mime::Type.register("text/x-mobile", :mobile)
95     respond_to do |type|
96       type.html   { render :text => "HTML"   }
97       type.mobile
98     end
100   ensure
101     Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
102   end
104   def handle_any
105     respond_to do |type|
106       type.html { render :text => "HTML" }
107       type.any(:js, :xml) { render :text => "Either JS or XML" }
108     end
109   end
111   def all_types_with_layout
112     respond_to do |type|
113       type.html
114       type.js
115     end
116   end 
117   
118   def iphone_with_html_response_type 
119     Mime::Type.register_alias("text/html", :iphone)
120     request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
121     
122     respond_to do |type|
123       type.html   { @type = "Firefox" }
124       type.iphone { @type = "iPhone"  }
125     end
127   ensure
128     Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
129   end
131   def iphone_with_html_response_type_without_layout
132     Mime::Type.register_alias("text/html", :iphone)
133     request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
134     
135     respond_to do |type|
136       type.html   { @type = "Firefox"; render :action => "iphone_with_html_response_type" }
137       type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" }
138     end
140   ensure
141     Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
142   end
144   def rescue_action(e)
145     raise
146   end
148   protected
149     def set_layout
150       if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name)
151         "respond_to/layouts/standard"
152       elsif action_name == "iphone_with_html_response_type_without_layout"
153         "respond_to/layouts/missing"
154       end
155     end
158 RespondToController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
160 class MimeControllerTest < Test::Unit::TestCase
161   def setup
162     @request    = ActionController::TestRequest.new
163     @response   = ActionController::TestResponse.new
165     @controller = RespondToController.new
166     @request.host = "www.example.com"
167   end
169   def test_html
170     @request.env["HTTP_ACCEPT"] = "text/html"
171     get :js_or_html
172     assert_equal 'HTML', @response.body
174     get :html_or_xml
175     assert_equal 'HTML', @response.body
177     get :just_xml
178     assert_response 406
179   end
181   def test_all
182     @request.env["HTTP_ACCEPT"] = "*/*"
183     get :js_or_html
184     assert_equal 'HTML', @response.body # js is not part of all
186     get :html_or_xml
187     assert_equal 'HTML', @response.body
189     get :just_xml
190     assert_equal 'XML', @response.body
191   end
193   def test_xml
194     @request.env["HTTP_ACCEPT"] = "application/xml"
195     get :html_xml_or_rss
196     assert_equal 'XML', @response.body
197   end
199   def test_js_or_html
200     @request.env["HTTP_ACCEPT"] = "text/javascript, text/html"
201     get :js_or_html
202     assert_equal 'JS', @response.body
204     get :html_or_xml
205     assert_equal 'HTML', @response.body
207     get :just_xml
208     assert_response 406
209   end
211   def test_json_or_yaml
212     get :json_or_yaml
213     assert_equal 'JSON', @response.body
215     get :json_or_yaml, :format => 'json'
216     assert_equal 'JSON', @response.body
218     get :json_or_yaml, :format => 'yaml'
219     assert_equal 'YAML', @response.body
221     { 'YAML' => %w(text/yaml),
222       'JSON' => %w(application/json text/x-json)
223     }.each do |body, content_types|
224       content_types.each do |content_type|
225         @request.env['HTTP_ACCEPT'] = content_type
226         get :json_or_yaml
227         assert_equal body, @response.body
228       end
229     end
230   end
232   def test_js_or_anything
233     @request.env["HTTP_ACCEPT"] = "text/javascript, */*"
234     get :js_or_html
235     assert_equal 'JS', @response.body
237     get :html_or_xml
238     assert_equal 'HTML', @response.body
240     get :just_xml
241     assert_equal 'XML', @response.body
242   end
243   
244   def test_using_defaults
245     @request.env["HTTP_ACCEPT"] = "*/*"
246     get :using_defaults
247     assert_equal "text/html", @response.content_type
248     assert_equal 'Hello world!', @response.body
250     @request.env["HTTP_ACCEPT"] = "text/javascript"
251     get :using_defaults
252     assert_equal "text/javascript", @response.content_type
253     assert_equal '$("body").visualEffect("highlight");', @response.body
255     @request.env["HTTP_ACCEPT"] = "application/xml"
256     get :using_defaults
257     assert_equal "application/xml", @response.content_type
258     assert_equal "<p>Hello world!</p>\n", @response.body
259   end
261   def test_using_defaults_with_type_list
262     @request.env["HTTP_ACCEPT"] = "*/*"
263     get :using_defaults_with_type_list
264     assert_equal "text/html", @response.content_type
265     assert_equal 'Hello world!', @response.body
267     @request.env["HTTP_ACCEPT"] = "text/javascript"
268     get :using_defaults_with_type_list
269     assert_equal "text/javascript", @response.content_type
270     assert_equal '$("body").visualEffect("highlight");', @response.body
272     @request.env["HTTP_ACCEPT"] = "application/xml"
273     get :using_defaults_with_type_list
274     assert_equal "application/xml", @response.content_type
275     assert_equal "<p>Hello world!</p>\n", @response.body
276   end
278   def test_with_atom_content_type
279     @request.env["CONTENT_TYPE"] = "application/atom+xml"
280     get :made_for_content_type
281     assert_equal "ATOM", @response.body
282   end
284   def test_with_rss_content_type
285     @request.env["CONTENT_TYPE"] = "application/rss+xml"
286     get :made_for_content_type
287     assert_equal "RSS", @response.body
288   end
290   def test_synonyms
291     @request.env["HTTP_ACCEPT"] = "application/javascript"
292     get :js_or_html
293     assert_equal 'JS', @response.body
295     @request.env["HTTP_ACCEPT"] = "application/x-xml"
296     get :html_xml_or_rss
297     assert_equal "XML", @response.body
298   end
300   def test_custom_types
301     @request.env["HTTP_ACCEPT"] = "application/crazy-xml"
302     get :custom_type_handling
303     assert_equal "application/crazy-xml", @response.content_type
304     assert_equal 'Crazy XML', @response.body
306     @request.env["HTTP_ACCEPT"] = "text/html"
307     get :custom_type_handling
308     assert_equal "text/html", @response.content_type
309     assert_equal 'HTML', @response.body
310   end
312   def test_xhtml_alias
313     @request.env["HTTP_ACCEPT"] = "application/xhtml+xml,application/xml"
314     get :html_or_xml
315     assert_equal 'HTML', @response.body
316   end
318   def test_firefox_simulation
319     @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"
320     get :html_or_xml
321     assert_equal 'HTML', @response.body
322   end
324   def test_handle_any
325     @request.env["HTTP_ACCEPT"] = "*/*"
326     get :handle_any
327     assert_equal 'HTML', @response.body
329     @request.env["HTTP_ACCEPT"] = "text/javascript"
330     get :handle_any
331     assert_equal 'Either JS or XML', @response.body
333     @request.env["HTTP_ACCEPT"] = "text/xml"
334     get :handle_any
335     assert_equal 'Either JS or XML', @response.body
336   end
338   def test_rjs_type_skips_layout
339     @request.env["HTTP_ACCEPT"] = "text/javascript"
340     get :all_types_with_layout
341     assert_equal 'RJS for all_types_with_layout', @response.body
342   end
344   def test_html_type_with_layout
345     @request.env["HTTP_ACCEPT"] = "text/html"
346     get :all_types_with_layout
347     assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
348   end
350   def test_xhr
351     xhr :get, :js_or_html
352     assert_equal 'JS', @response.body
354     xhr :get, :using_defaults
355     assert_equal '$("body").visualEffect("highlight");', @response.body
356   end
358   def test_custom_constant
359     get :custom_constant_handling, :format => "mobile"
360     assert_equal "text/x-mobile", @response.content_type
361     assert_equal "Mobile", @response.body
362   end
364   def test_custom_constant_handling_without_block
365     get :custom_constant_handling_without_block, :format => "mobile"
366     assert_equal "text/x-mobile", @response.content_type
367     assert_equal "Mobile", @response.body
368   end
370   def test_forced_format
371     get :html_xml_or_rss
372     assert_equal "HTML", @response.body
374     get :html_xml_or_rss, :format => "html"
375     assert_equal "HTML", @response.body
377     get :html_xml_or_rss, :format => "xml"
378     assert_equal "XML", @response.body
380     get :html_xml_or_rss, :format => "rss"
381     assert_equal "RSS", @response.body
382   end
384   def test_internally_forced_format
385     get :forced_xml
386     assert_equal "XML", @response.body
388     get :forced_xml, :format => "html"
389     assert_equal "XML", @response.body
390   end
392   def test_extension_synonyms
393     get :html_xml_or_rss, :format => "xhtml"
394     assert_equal "HTML", @response.body
395   end
397   def test_render_action_for_html
398     @controller.instance_eval do
399       def render(*args)
400         unless args.empty?
401           @action = args.first[:action]
402         end
403         response.body = "#{@action} - #{@template.template_format}"
404       end
405     end
407     get :using_defaults
408     assert_equal "using_defaults - html", @response.body
410     get :using_defaults, :format => "xml"
411     assert_equal "using_defaults - xml", @response.body
412   end 
413   
414   def test_format_with_custom_response_type
415     get :iphone_with_html_response_type
416     assert_equal '<html><div id="html">Hello future from Firefox!</div></html>', @response.body 
417     
418     get :iphone_with_html_response_type, :format => "iphone"
419     assert_equal "text/html", @response.content_type
420     assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
421   end 
422   
423   def test_format_with_custom_response_type_and_request_headers
424     @request.env["HTTP_ACCEPT"] = "text/iphone"
425     get :iphone_with_html_response_type
426     assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
427     assert_equal "text/html", @response.content_type
428   end 
430   def test_format_with_custom_response_type_and_request_headers_with_only_one_layout_present
431     get :iphone_with_html_response_type_without_layout
432     assert_equal '<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body 
434     @request.env["HTTP_ACCEPT"] = "text/iphone"
435     assert_raises(ActionController::MissingTemplate) { get :iphone_with_html_response_type_without_layout }
436   end 
439 class AbstractPostController < ActionController::Base
440   class << self
441     def view_paths
442       [ File.dirname(__FILE__) + "/../fixtures/post_test/" ]
443     end
444   end
447 # For testing layouts which are set automatically
448 class PostController < AbstractPostController
449   around_filter :with_iphone
451   def index
452     respond_to do |type|
453       type.html
454       type.iphone
455     end
456   end
458   protected
459     def with_iphone
460       Mime::Type.register_alias("text/html", :iphone)
461       request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
462       yield
463     ensure
464       Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
465     end
468 class SuperPostController < PostController  
469   def index
470     respond_to do |type|
471       type.html
472       type.iphone
473     end
474   end
477 class MimeControllerLayoutsTest < Test::Unit::TestCase
478   def setup
479     @request    = ActionController::TestRequest.new
480     @response   = ActionController::TestResponse.new
482     @controller   = PostController.new
483     @request.host = "www.example.com"
484   end
485   
486   def test_missing_layout_renders_properly
487     get :index
488     assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body 
490     @request.env["HTTP_ACCEPT"] = "text/iphone"
491     get :index
492     assert_equal 'Hello iPhone', @response.body
493   end
494   
495   def test_format_with_inherited_layouts
496     @controller = SuperPostController.new
497     
498     get :index
499     assert_equal 'Super Firefox', @response.body
500     
501     @request.env["HTTP_ACCEPT"] = "text/iphone"
502     get :index
503     assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
504   end
506