| 1 |
require 'abstract_unit' |
|---|
| 2 |
|
|---|
| 3 |
class RespondToController < ActionController::Base |
|---|
| 4 |
layout :set_layout |
|---|
| 5 |
|
|---|
| 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 |
|
|---|
| 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 |
|---|
| 22 |
|
|---|
| 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 |
|---|
| 29 |
|
|---|
| 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 |
|---|
| 37 |
|
|---|
| 38 |
def forced_xml |
|---|
| 39 |
request.format = :xml |
|---|
| 40 |
|
|---|
| 41 |
respond_to do |type| |
|---|
| 42 |
type.html { render :text => "HTML" } |
|---|
| 43 |
type.xml { render :text => "XML" } |
|---|
| 44 |
end |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def just_xml |
|---|
| 48 |
respond_to do |type| |
|---|
| 49 |
type.xml { render :text => "XML" } |
|---|
| 50 |
end |
|---|
| 51 |
end |
|---|
| 52 |
|
|---|
| 53 |
def using_defaults |
|---|
| 54 |
respond_to do |type| |
|---|
| 55 |
type.html |
|---|
| 56 |
type.js |
|---|
| 57 |
type.xml |
|---|
| 58 |
end |
|---|
| 59 |
end |
|---|
| 60 |
|
|---|
| 61 |
def using_defaults_with_type_list |
|---|
| 62 |
respond_to(:html, :js, :xml) |
|---|
| 63 |
end |
|---|
| 64 |
|
|---|
| 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 |
|---|
| 72 |
|
|---|
| 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 |
|---|
| 80 |
|
|---|
| 81 |
def custom_constant_handling |
|---|
| 82 |
Mime::Type.register("text/x-mobile", :mobile) |
|---|
| 83 |
|
|---|
| 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 |
|---|
| 91 |
|
|---|
| 92 |
def custom_constant_handling_without_block |
|---|
| 93 |
Mime::Type.register("text/x-mobile", :mobile) |
|---|
| 94 |
|
|---|
| 95 |
respond_to do |type| |
|---|
| 96 |
type.html { render :text => "HTML" } |
|---|
| 97 |
type.mobile |
|---|
| 98 |
end |
|---|
| 99 |
|
|---|
| 100 |
ensure |
|---|
| 101 |
Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) } |
|---|
| 102 |
end |
|---|
| 103 |
|
|---|
| 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 |
|---|
| 110 |
|
|---|
| 111 |
def handle_any_any |
|---|
| 112 |
respond_to do |type| |
|---|
| 113 |
type.html { render :text => 'HTML' } |
|---|
| 114 |
type.any { render :text => 'Whatever you ask for, I got it' } |
|---|
| 115 |
end |
|---|
| 116 |
end |
|---|
| 117 |
|
|---|
| 118 |
def all_types_with_layout |
|---|
| 119 |
respond_to do |type| |
|---|
| 120 |
type.html |
|---|
| 121 |
type.js |
|---|
| 122 |
end |
|---|
| 123 |
end |
|---|
| 124 |
|
|---|
| 125 |
def iphone_with_html_response_type |
|---|
| 126 |
Mime::Type.register_alias("text/html", :iphone) |
|---|
| 127 |
request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone" |
|---|
| 128 |
|
|---|
| 129 |
respond_to do |type| |
|---|
| 130 |
type.html { @type = "Firefox" } |
|---|
| 131 |
type.iphone { @type = "iPhone" } |
|---|
| 132 |
end |
|---|
| 133 |
|
|---|
| 134 |
ensure |
|---|
| 135 |
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) } |
|---|
| 136 |
end |
|---|
| 137 |
|
|---|
| 138 |
def iphone_with_html_response_type_without_layout |
|---|
| 139 |
Mime::Type.register_alias("text/html", :iphone) |
|---|
| 140 |
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone" |
|---|
| 141 |
|
|---|
| 142 |
respond_to do |type| |
|---|
| 143 |
type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" } |
|---|
| 144 |
type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" } |
|---|
| 145 |
end |
|---|
| 146 |
|
|---|
| 147 |
ensure |
|---|
| 148 |
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) } |
|---|
| 149 |
end |
|---|
| 150 |
|
|---|
| 151 |
def rescue_action(e) |
|---|
| 152 |
raise |
|---|
| 153 |
end |
|---|
| 154 |
|
|---|
| 155 |
protected |
|---|
| 156 |
def set_layout |
|---|
| 157 |
if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name) |
|---|
| 158 |
"respond_to/layouts/standard" |
|---|
| 159 |
elsif action_name == "iphone_with_html_response_type_without_layout" |
|---|
| 160 |
"respond_to/layouts/missing" |
|---|
| 161 |
end |
|---|
| 162 |
end |
|---|
| 163 |
end |
|---|
| 164 |
|
|---|
| 165 |
RespondToController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] |
|---|
| 166 |
|
|---|
| 167 |
class MimeControllerTest < Test::Unit::TestCase |
|---|
| 168 |
def setup |
|---|
| 169 |
@request = ActionController::TestRequest.new |
|---|
| 170 |
@response = ActionController::TestResponse.new |
|---|
| 171 |
|
|---|
| 172 |
@controller = RespondToController.new |
|---|
| 173 |
@request.host = "www.example.com" |
|---|
| 174 |
end |
|---|
| 175 |
|
|---|
| 176 |
def test_html |
|---|
| 177 |
@request.env["HTTP_ACCEPT"] = "text/html" |
|---|
| 178 |
get :js_or_html |
|---|
| 179 |
assert_equal 'HTML', @response.body |
|---|
| 180 |
|
|---|
| 181 |
get :html_or_xml |
|---|
| 182 |
assert_equal 'HTML', @response.body |
|---|
| 183 |
|
|---|
| 184 |
get :just_xml |
|---|
| 185 |
assert_response 406 |
|---|
| 186 |
end |
|---|
| 187 |
|
|---|
| 188 |
def test_all |
|---|
| 189 |
@request.env["HTTP_ACCEPT"] = "*/*" |
|---|
| 190 |
get :js_or_html |
|---|
| 191 |
assert_equal 'HTML', @response.body |
|---|
| 192 |
|
|---|
| 193 |
get :html_or_xml |
|---|
| 194 |
assert_equal 'HTML', @response.body |
|---|
| 195 |
|
|---|
| 196 |
get :just_xml |
|---|
| 197 |
assert_equal 'XML', @response.body |
|---|
| 198 |
end |
|---|
| 199 |
|
|---|
| 200 |
def test_xml |
|---|
| 201 |
@request.env["HTTP_ACCEPT"] = "application/xml" |
|---|
| 202 |
get :html_xml_or_rss |
|---|
| 203 |
assert_equal 'XML', @response.body |
|---|
| 204 |
end |
|---|
| 205 |
|
|---|
| 206 |
def test_js_or_html |
|---|
| 207 |
@request.env["HTTP_ACCEPT"] = "text/javascript, text/html" |
|---|
| 208 |
get :js_or_html |
|---|
| 209 |
assert_equal 'JS', @response.body |
|---|
| 210 |
|
|---|
| 211 |
get :html_or_xml |
|---|
| 212 |
assert_equal 'HTML', @response.body |
|---|
| 213 |
|
|---|
| 214 |
get :just_xml |
|---|
| 215 |
assert_response 406 |
|---|
| 216 |
end |
|---|
| 217 |
|
|---|
| 218 |
def test_json_or_yaml |
|---|
| 219 |
get :json_or_yaml |
|---|
| 220 |
assert_equal 'JSON', @response.body |
|---|
| 221 |
|
|---|
| 222 |
get :json_or_yaml, :format => 'json' |
|---|
| 223 |
assert_equal 'JSON', @response.body |
|---|
| 224 |
|
|---|
| 225 |
get :json_or_yaml, :format => 'yaml' |
|---|
| 226 |
assert_equal 'YAML', @response.body |
|---|
| 227 |
|
|---|
| 228 |
{ 'YAML' => %w(text/yaml), |
|---|
| 229 |
'JSON' => %w(application/json text/x-json) |
|---|
| 230 |
}.each do |body, content_types| |
|---|
| 231 |
content_types.each do |content_type| |
|---|
| 232 |
@request.env['HTTP_ACCEPT'] = content_type |
|---|
| 233 |
get :json_or_yaml |
|---|
| 234 |
assert_equal body, @response.body |
|---|
| 235 |
end |
|---|
| 236 |
end |
|---|
| 237 |
end |
|---|
| 238 |
|
|---|
| 239 |
def test_js_or_anything |
|---|
| 240 |
@request.env["HTTP_ACCEPT"] = "text/javascript, */*" |
|---|
| 241 |
get :js_or_html |
|---|
| 242 |
assert_equal 'JS', @response.body |
|---|
| 243 |
|
|---|
| 244 |
get :html_or_xml |
|---|
| 245 |
assert_equal 'HTML', @response.body |
|---|
| 246 |
|
|---|
| 247 |
get :just_xml |
|---|
| 248 |
assert_equal 'XML', @response.body |
|---|
| 249 |
end |
|---|
| 250 |
|
|---|
| 251 |
def test_using_defaults |
|---|
| 252 |
@request.env["HTTP_ACCEPT"] = "*/*" |
|---|
| 253 |
get :using_defaults |
|---|
| 254 |
assert_equal "text/html", @response.content_type |
|---|
| 255 |
assert_equal 'Hello world!', @response.body |
|---|
| 256 |
|
|---|
| 257 |
@request.env["HTTP_ACCEPT"] = "text/javascript" |
|---|
| 258 |
get :using_defaults |
|---|
| 259 |
assert_equal "text/javascript", @response.content_type |
|---|
| 260 |
assert_equal '$("body").visualEffect("highlight");', @response.body |
|---|
| 261 |
|
|---|
| 262 |
@request.env["HTTP_ACCEPT"] = "application/xml" |
|---|
| 263 |
get :using_defaults |
|---|
| 264 |
assert_equal "application/xml", @response.content_type |
|---|
| 265 |
assert_equal "<p>Hello world!</p>\n", @response.body |
|---|
| 266 |
end |
|---|
| 267 |
|
|---|
| 268 |
def test_using_defaults_with_type_list |
|---|
| 269 |
@request.env["HTTP_ACCEPT"] = "*/*" |
|---|
| 270 |
get :using_defaults_with_type_list |
|---|
| 271 |
assert_equal "text/html", @response.content_type |
|---|
| 272 |
assert_equal 'Hello world!', @response.body |
|---|
| 273 |
|
|---|
| 274 |
@request.env["HTTP_ACCEPT"] = "text/javascript" |
|---|
| 275 |
get :using_defaults_with_type_list |
|---|
| 276 |
assert_equal "text/javascript", @response.content_type |
|---|
| 277 |
assert_equal '$("body").visualEffect("highlight");', @response.body |
|---|
| 278 |
|
|---|
| 279 |
@request.env["HTTP_ACCEPT"] = "application/xml" |
|---|
| 280 |
get :using_defaults_with_type_list |
|---|
| 281 |
assert_equal "application/xml", @response.content_type |
|---|
| 282 |
assert_equal "<p>Hello world!</p>\n", @response.body |
|---|
| 283 |
end |
|---|
| 284 |
|
|---|
| 285 |
def test_with_atom_content_type |
|---|
| 286 |
@request.env["CONTENT_TYPE"] = "application/atom+xml" |
|---|
| 287 |
get :made_for_content_type |
|---|
| 288 |
assert_equal "ATOM", @response.body |
|---|
| 289 |
end |
|---|
| 290 |
|
|---|
| 291 |
def test_with_rss_content_type |
|---|
| 292 |
@request.env["CONTENT_TYPE"] = "application/rss+xml" |
|---|
| 293 |
get :made_for_content_type |
|---|
| 294 |
assert_equal "RSS", @response.body |
|---|
| 295 |
end |
|---|
| 296 |
|
|---|
| 297 |
def test_synonyms |
|---|
| 298 |
@request.env["HTTP_ACCEPT"] = "application/javascript" |
|---|
| 299 |
get :js_or_html |
|---|
| 300 |
assert_equal 'JS', @response.body |
|---|
| 301 |
|
|---|
| 302 |
@request.env["HTTP_ACCEPT"] = "application/x-xml" |
|---|
| 303 |
get :html_xml_or_rss |
|---|
| 304 |
assert_equal "XML", @response.body |
|---|
| 305 |
end |
|---|
| 306 |
|
|---|
| 307 |
def test_custom_types |
|---|
| 308 |
@request.env["HTTP_ACCEPT"] = "application/crazy-xml" |
|---|
| 309 |
get :custom_type_handling |
|---|
| 310 |
assert_equal "application/crazy-xml", @response.content_type |
|---|
| 311 |
assert_equal 'Crazy XML', @response.body |
|---|
| 312 |
|
|---|
| 313 |
@request.env["HTTP_ACCEPT"] = "text/html" |
|---|
| 314 |
get :custom_type_handling |
|---|
| 315 |
assert_equal "text/html", @response.content_type |
|---|
| 316 |
assert_equal 'HTML', @response.body |
|---|
| 317 |
end |
|---|
| 318 |
|
|---|
| 319 |
def test_xhtml_alias |
|---|
| 320 |
@request.env["HTTP_ACCEPT"] = "application/xhtml+xml,application/xml" |
|---|
| 321 |
get :html_or_xml |
|---|
| 322 |
assert_equal 'HTML', @response.body |
|---|
| 323 |
end |
|---|
| 324 |
|
|---|
| 325 |
def test_firefox_simulation |
|---|
| 326 |
@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" |
|---|
| 327 |
get :html_or_xml |
|---|
| 328 |
assert_equal 'HTML', @response.body |
|---|
| 329 |
end |
|---|
| 330 |
|
|---|
| 331 |
def test_handle_any |
|---|
| 332 |
@request.env["HTTP_ACCEPT"] = "*/*" |
|---|
| 333 |
get :handle_any |
|---|
| 334 |
assert_equal 'HTML', @response.body |
|---|
| 335 |
|
|---|
| 336 |
@request.env["HTTP_ACCEPT"] = "text/javascript" |
|---|
| 337 |
get :handle_any |
|---|
| 338 |
assert_equal 'Either JS or XML', @response.body |
|---|
| 339 |
|
|---|
| 340 |
@request.env["HTTP_ACCEPT"] = "text/xml" |
|---|
| 341 |
get :handle_any |
|---|
| 342 |
assert_equal 'Either JS or XML', @response.body |
|---|
| 343 |
end |
|---|
| 344 |
|
|---|
| 345 |
def test_handle_any_any |
|---|
| 346 |
@request.env["HTTP_ACCEPT"] = "*/*" |
|---|
| 347 |
get :handle_any_any |
|---|
| 348 |
assert_equal 'HTML', @response.body |
|---|
| 349 |
end |
|---|
| 350 |
|
|---|
| 351 |
def test_handle_any_any_parameter_format |
|---|
| 352 |
get :handle_any_any, {:format=>'html'} |
|---|
| 353 |
assert_equal 'HTML', @response.body |
|---|
| 354 |
end |
|---|
| 355 |
|
|---|
| 356 |
def test_handle_any_any_explicit_html |
|---|
| 357 |
@request.env["HTTP_ACCEPT"] = "text/html" |
|---|
| 358 |
get :handle_any_any |
|---|
| 359 |
assert_equal 'HTML', @response.body |
|---|
| 360 |
end |
|---|
| 361 |
|
|---|
| 362 |
def test_handle_any_any_javascript |
|---|
| 363 |
@request.env["HTTP_ACCEPT"] = "text/javascript" |
|---|
| 364 |
get :handle_any_any |
|---|
| 365 |
assert_equal 'Whatever you ask for, I got it', @response.body |
|---|
| 366 |
end |
|---|
| 367 |
|
|---|
| 368 |
def test_handle_any_any_xml |
|---|
| 369 |
@request.env["HTTP_ACCEPT"] = "text/xml" |
|---|
| 370 |
get :handle_any_any |
|---|
| 371 |
assert_equal 'Whatever you ask for, I got it', @response.body |
|---|
| 372 |
end |
|---|
| 373 |
|
|---|
| 374 |
def test_rjs_type_skips_layout |
|---|
| 375 |
@request.env["HTTP_ACCEPT"] = "text/javascript" |
|---|
| 376 |
get :all_types_with_layout |
|---|
| 377 |
assert_equal 'RJS for all_types_with_layout', @response.body |
|---|
| 378 |
end |
|---|
| 379 |
|
|---|
| 380 |
def test_html_type_with_layout |
|---|
| 381 |
@request.env["HTTP_ACCEPT"] = "text/html" |
|---|
| 382 |
get :all_types_with_layout |
|---|
| 383 |
assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body |
|---|
| 384 |
end |
|---|
| 385 |
|
|---|
| 386 |
def test_xhr |
|---|
| 387 |
xhr :get, :js_or_html |
|---|
| 388 |
assert_equal 'JS', @response.body |
|---|
| 389 |
|
|---|
| 390 |
xhr :get, :using_defaults |
|---|
| 391 |
assert_equal '$("body").visualEffect("highlight");', @response.body |
|---|
| 392 |
end |
|---|
| 393 |
|
|---|
| 394 |
def test_custom_constant |
|---|
| 395 |
get :custom_constant_handling, :format => "mobile" |
|---|
| 396 |
assert_equal "text/x-mobile", @response.content_type |
|---|
| 397 |
assert_equal "Mobile", @response.body |
|---|
| 398 |
end |
|---|
| 399 |
|
|---|
| 400 |
def test_custom_constant_handling_without_block |
|---|
| 401 |
get :custom_constant_handling_without_block, :format => "mobile" |
|---|
| 402 |
assert_equal "text/x-mobile", @response.content_type |
|---|
| 403 |
assert_equal "Mobile", @response.body |
|---|
| 404 |
end |
|---|
| 405 |
|
|---|
| 406 |
def test_forced_format |
|---|
| 407 |
get :html_xml_or_rss |
|---|
| 408 |
assert_equal "HTML", @response.body |
|---|
| 409 |
|
|---|
| 410 |
get :html_xml_or_rss, :format => "html" |
|---|
| 411 |
assert_equal "HTML", @response.body |
|---|
| 412 |
|
|---|
| 413 |
get :html_xml_or_rss, :format => "xml" |
|---|
| 414 |
assert_equal "XML", @response.body |
|---|
| 415 |
|
|---|
| 416 |
get :html_xml_or_rss, :format => "rss" |
|---|
| 417 |
assert_equal "RSS", @response.body |
|---|
| 418 |
end |
|---|
| 419 |
|
|---|
| 420 |
def test_internally_forced_format |
|---|
| 421 |
get :forced_xml |
|---|
| 422 |
assert_equal "XML", @response.body |
|---|
| 423 |
|
|---|
| 424 |
get :forced_xml, :format => "html" |
|---|
| 425 |
assert_equal "XML", @response.body |
|---|
| 426 |
end |
|---|
| 427 |
|
|---|
| 428 |
def test_extension_synonyms |
|---|
| 429 |
get :html_xml_or_rss, :format => "xhtml" |
|---|
| 430 |
assert_equal "HTML", @response.body |
|---|
| 431 |
end |
|---|
| 432 |
|
|---|
| 433 |
def test_render_action_for_html |
|---|
| 434 |
@controller.instance_eval do |
|---|
| 435 |
def render(*args) |
|---|
| 436 |
unless args.empty? |
|---|
| 437 |
@action = args.first[:action] |
|---|
| 438 |
end |
|---|
| 439 |
response.body = "#{@action} - #{@template.template_format}" |
|---|
| 440 |
end |
|---|
| 441 |
end |
|---|
| 442 |
|
|---|
| 443 |
get :using_defaults |
|---|
| 444 |
assert_equal "using_defaults - html", @response.body |
|---|
| 445 |
|
|---|
| 446 |
get :using_defaults, :format => "xml" |
|---|
| 447 |
assert_equal "using_defaults - xml", @response.body |
|---|
| 448 |
end |
|---|
| 449 |
|
|---|
| 450 |
def test_format_with_custom_response_type |
|---|
| 451 |
get :iphone_with_html_response_type |
|---|
| 452 |
assert_equal '<html><div id="html">Hello future from Firefox!</div></html>', @response.body |
|---|
| 453 |
|
|---|
| 454 |
get :iphone_with_html_response_type, :format => "iphone" |
|---|
| 455 |
assert_equal "text/html", @response.content_type |
|---|
| 456 |
assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body |
|---|
| 457 |
end |
|---|
| 458 |
|
|---|
| 459 |
def test_format_with_custom_response_type_and_request_headers |
|---|
| 460 |
@request.env["HTTP_ACCEPT"] = "text/iphone" |
|---|
| 461 |
get :iphone_with_html_response_type |
|---|
| 462 |
assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body |
|---|
| 463 |
assert_equal "text/html", @response.content_type |
|---|
| 464 |
end |
|---|
| 465 |
|
|---|
| 466 |
def test_format_with_custom_response_type_and_request_headers_with_only_one_layout_present |
|---|
| 467 |
get :iphone_with_html_response_type_without_layout |
|---|
| 468 |
assert_equal '<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body |
|---|
| 469 |
|
|---|
| 470 |
@request.env["HTTP_ACCEPT"] = "text/iphone" |
|---|
| 471 |
assert_raises(ActionController::MissingTemplate) { get :iphone_with_html_response_type_without_layout } |
|---|
| 472 |
end |
|---|
| 473 |
end |
|---|
| 474 |
|
|---|
| 475 |
class AbstractPostController < ActionController::Base |
|---|
| 476 |
self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/" |
|---|
| 477 |
end |
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 |
class PostController < AbstractPostController |
|---|
| 481 |
around_filter :with_iphone |
|---|
| 482 |
|
|---|
| 483 |
def index |
|---|
| 484 |
respond_to do |type| |
|---|
| 485 |
type.html |
|---|
| 486 |
type.iphone |
|---|
| 487 |
end |
|---|
| 488 |
end |
|---|
| 489 |
|
|---|
| 490 |
protected |
|---|
| 491 |
def with_iphone |
|---|
| 492 |
Mime::Type.register_alias("text/html", :iphone) |
|---|
| 493 |
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone" |
|---|
| 494 |
yield |
|---|
| 495 |
ensure |
|---|
| 496 |
Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) } |
|---|
| 497 |
end |
|---|
| 498 |
end |
|---|
| 499 |
|
|---|
| 500 |
class SuperPostController < PostController |
|---|
| 501 |
def index |
|---|
| 502 |
respond_to do |type| |
|---|
| 503 |
type.html |
|---|
| 504 |
type.iphone |
|---|
| 505 |
end |
|---|
| 506 |
end |
|---|
| 507 |
end |
|---|
| 508 |
|
|---|
| 509 |
class MimeControllerLayoutsTest < Test::Unit::TestCase |
|---|
| 510 |
def setup |
|---|
| 511 |
@request = ActionController::TestRequest.new |
|---|
| 512 |
@response = ActionController::TestResponse.new |
|---|
| 513 |
|
|---|
| 514 |
@controller = PostController.new |
|---|
| 515 |
@request.host = "www.example.com" |
|---|
| 516 |
end |
|---|
| 517 |
|
|---|
| 518 |
def test_missing_layout_renders_properly |
|---|
| 519 |
get :index |
|---|
| 520 |
assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body |
|---|
| 521 |
|
|---|
| 522 |
@request.env["HTTP_ACCEPT"] = "text/iphone" |
|---|
| 523 |
get :index |
|---|
| 524 |
assert_equal 'Hello iPhone', @response.body |
|---|
| 525 |
end |
|---|
| 526 |
|
|---|
| 527 |
def test_format_with_inherited_layouts |
|---|
| 528 |
@controller = SuperPostController.new |
|---|
| 529 |
|
|---|
| 530 |
get :index |
|---|
| 531 |
assert_equal 'Super Firefox', @response.body |
|---|
| 532 |
|
|---|
| 533 |
@request.env["HTTP_ACCEPT"] = "text/iphone" |
|---|
| 534 |
get :index |
|---|
| 535 |
assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body |
|---|
| 536 |
end |
|---|
| 537 |
end |
|---|
| 538 |
|
|---|