Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / content_type_test.rb
1 require 'abstract_unit'
2
3 class ContentTypeController < ActionController::Base
4 def render_content_type_from_body
5 response.content_type = Mime::RSS
6 render :text => "hello world!"
7 end
8
9 def render_defaults
10 render :text => "hello world!"
11 end
12
13 def render_content_type_from_render
14 render :text => "hello world!", :content_type => Mime::RSS
15 end
16
17 def render_charset_from_body
18 response.charset = "utf-16"
19 render :text => "hello world!"
20 end
21
22 def render_nil_charset_from_body
23 response.charset = nil
24 render :text => "hello world!"
25 end
26
27 def render_default_for_rhtml
28 end
29
30 def render_default_for_rxml
31 end
32
33 def render_default_for_rjs
34 end
35
36 def render_change_for_rxml
37 response.content_type = Mime::HTML
38 render :action => "render_default_for_rxml"
39 end
40
41 def render_default_content_types_for_respond_to
42 respond_to do |format|
43 format.html { render :text => "hello world!" }
44 format.xml { render :action => "render_default_content_types_for_respond_to.rhtml" }
45 format.js { render :text => "hello world!" }
46 format.rss { render :text => "hello world!", :content_type => Mime::XML }
47 end
48 end
49
50 def rescue_action(e) raise end
51 end
52
53 class ContentTypeTest < Test::Unit::TestCase
54 def setup
55 @controller = ContentTypeController.new
56
57 # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
58 # a more accurate simulation of what happens in "real life".
59 @controller.logger = Logger.new(nil)
60
61 @request = ActionController::TestRequest.new
62 @response = ActionController::TestResponse.new
63 end
64
65 def test_render_defaults
66 get :render_defaults
67 assert_equal "utf-8", @response.charset
68 assert_equal Mime::HTML, @response.content_type
69 end
70
71 def test_render_changed_charset_default
72 ContentTypeController.default_charset = "utf-16"
73 get :render_defaults
74 assert_equal "utf-16", @response.charset
75 assert_equal Mime::HTML, @response.content_type
76 ContentTypeController.default_charset = "utf-8"
77 end
78
79 def test_content_type_from_body
80 get :render_content_type_from_body
81 assert_equal "application/rss+xml", @response.content_type
82 assert_equal "utf-8", @response.charset
83 end
84
85 def test_content_type_from_render
86 get :render_content_type_from_render
87 assert_equal "application/rss+xml", @response.content_type
88 assert_equal "utf-8", @response.charset
89 end
90
91 def test_charset_from_body
92 get :render_charset_from_body
93 assert_equal Mime::HTML, @response.content_type
94 assert_equal "utf-16", @response.charset
95 end
96
97 def test_nil_charset_from_body
98 get :render_nil_charset_from_body
99 assert_equal Mime::HTML, @response.content_type
100 assert_equal "utf-8", @response.charset, @response.headers.inspect
101 end
102
103 def test_nil_default_for_rhtml
104 ContentTypeController.default_charset = nil
105 get :render_default_for_rhtml
106 assert_equal Mime::HTML, @response.content_type
107 assert_nil @response.charset, @response.headers.inspect
108 ensure
109 ContentTypeController.default_charset = "utf-8"
110 end
111
112 def test_default_for_rhtml
113 get :render_default_for_rhtml
114 assert_equal Mime::HTML, @response.content_type
115 assert_equal "utf-8", @response.charset
116 end
117
118 def test_default_for_rxml
119 get :render_default_for_rxml
120 assert_equal Mime::XML, @response.content_type
121 assert_equal "utf-8", @response.charset
122 end
123
124 def test_default_for_rjs
125 xhr :post, :render_default_for_rjs
126 assert_equal Mime::JS, @response.content_type
127 assert_equal "utf-8", @response.charset
128 end
129
130 def test_change_for_rxml
131 get :render_change_for_rxml
132 assert_equal Mime::HTML, @response.content_type
133 assert_equal "utf-8", @response.charset
134 end
135 end
136
137 class AcceptBasedContentTypeTest < ActionController::TestCase
138
139 tests ContentTypeController
140
141 def setup
142 ActionController::Base.use_accept_header = true
143 end
144
145 def teardown
146 ActionController::Base.use_accept_header = false
147 end
148
149
150 def test_render_default_content_types_for_respond_to
151 @request.accept = Mime::HTML.to_s
152 get :render_default_content_types_for_respond_to
153 assert_equal Mime::HTML, @response.content_type
154
155 @request.accept = Mime::JS.to_s
156 get :render_default_content_types_for_respond_to
157 assert_equal Mime::JS, @response.content_type
158 end
159
160 def test_render_default_content_types_for_respond_to_with_template
161 @request.accept = Mime::XML.to_s
162 get :render_default_content_types_for_respond_to
163 assert_equal Mime::XML, @response.content_type
164 end
165
166 def test_render_default_content_types_for_respond_to_with_overwrite
167 @request.accept = Mime::RSS.to_s
168 get :render_default_content_types_for_respond_to
169 assert_equal Mime::XML, @response.content_type
170 end
171 end