mime_responds_test.rb 22.0 KB
Newer Older
1
require 'abstract_unit'
2
require 'controller/fake_models'
3 4

class RespondToController < ActionController::Base
5 6
  layout :set_layout

7 8 9 10 11 12 13 14
  def html_xml_or_rss
    respond_to do |type|
      type.html { render :text => "HTML"    }
      type.xml  { render :text => "XML"     }
      type.rss  { render :text => "RSS"     }
      type.all  { render :text => "Nothing" }
    end
  end
15

16 17 18 19 20 21 22
  def js_or_html
    respond_to do |type|
      type.html { render :text => "HTML"    }
      type.js   { render :text => "JS"      }
      type.all  { render :text => "Nothing" }
    end
  end
23

24 25 26
  def json_or_yaml
    respond_to do |type|
      type.json { render :text => "JSON" }
27
      type.yaml { render :text => "YAML" }
28 29
    end
  end
30 31 32 33 34 35 36 37

  def html_or_xml
    respond_to do |type|
      type.html { render :text => "HTML"    }
      type.xml  { render :text => "XML"     }
      type.all  { render :text => "Nothing" }
    end
  end
38

39 40 41 42 43 44 45 46 47
  def forced_xml
    request.format = :xml

    respond_to do |type|
      type.html { render :text => "HTML"    }
      type.xml  { render :text => "XML"     }
    end
  end

48 49 50 51 52
  def just_xml
    respond_to do |type|
      type.xml  { render :text => "XML" }
    end
  end
53

54 55 56 57 58 59 60
  def using_defaults
    respond_to do |type|
      type.html
      type.js
      type.xml
    end
  end
61

62 63 64
  def using_defaults_with_type_list
    respond_to(:html, :js, :xml)
  end
65

66 67 68 69 70 71 72 73
  def made_for_content_type
    respond_to do |type|
      type.rss  { render :text => "RSS"  }
      type.atom { render :text => "ATOM" }
      type.all  { render :text => "Nothing" }
    end
  end

74 75 76 77 78 79 80
  def custom_type_handling
    respond_to do |type|
      type.html { render :text => "HTML"    }
      type.custom("application/crazy-xml")  { render :text => "Crazy XML"  }
      type.all  { render :text => "Nothing" }
    end
  end
81

82 83 84 85 86 87 88
  def custom_constant_handling
    Mime::Type.register("text/x-mobile", :mobile)

    respond_to do |type|
      type.html   { render :text => "HTML"   }
      type.mobile { render :text => "Mobile" }
    end
89
  ensure
90
    Mime::SET.delete(:mobile)
91
    Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
92
  end
93

94 95 96 97 98 99 100
  def custom_constant_handling_without_block
    Mime::Type.register("text/x-mobile", :mobile)

    respond_to do |type|
      type.html   { render :text => "HTML"   }
      type.mobile
    end
101

102
  ensure
103
    Mime::SET.delete(:mobile)
104
    Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
105
  end
106

107 108 109 110 111 112
  def handle_any
    respond_to do |type|
      type.html { render :text => "HTML" }
      type.any(:js, :xml) { render :text => "Either JS or XML" }
    end
  end
113

114 115 116 117 118 119
  def handle_any_any
    respond_to do |type|
      type.html { render :text => 'HTML' }
      type.any { render :text => 'Whatever you ask for, I got it' }
    end
  end
120

121 122 123 124 125
  def all_types_with_layout
    respond_to do |type|
      type.html
      type.js
    end
126 127 128
  end

  def iphone_with_html_response_type
129
    Mime::Type.register_alias("text/html", :iphone)
130
    request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
131

132 133
    respond_to do |type|
      type.html   { @type = "Firefox" }
134
      type.iphone { @type = "iPhone"  }
135 136
    end

137
  ensure
138
    Mime::SET.delete(:iphone)
139
    Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
140 141
  end

142 143 144
  def iphone_with_html_response_type_without_layout
    Mime::Type.register_alias("text/html", :iphone)
    request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
145

146 147 148 149 150
    respond_to do |type|
      type.html   { @type = "Firefox"; render :action => "iphone_with_html_response_type" }
      type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" }
    end

151
  ensure
152
    Mime::SET.delete(:iphone)
153
    Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
154 155
  end

156
  def rescue_action(e)
J
Jamis Buck 已提交
157
    raise
158
  end
159

160 161
  protected
    def set_layout
162
      if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name)
D
David Heinemeier Hansson 已提交
163
        "respond_to/layouts/standard"
164 165
      elsif action_name == "iphone_with_html_response_type_without_layout"
        "respond_to/layouts/missing"
166 167
      end
    end
168 169
end

170
class RespondToControllerTest < ActionController::TestCase
171 172
  tests RespondToController

173
  def setup
174
    super
175
    ActionController::Base.use_accept_header = true
176 177
    @request.host = "www.example.com"
  end
178

179
  def teardown
180
    super
181 182 183
    ActionController::Base.use_accept_header = false
  end

184
  def test_html
185
    @request.accept = "text/html"
186 187
    get :js_or_html
    assert_equal 'HTML', @response.body
188

189 190 191 192 193 194 195 196
    get :html_or_xml
    assert_equal 'HTML', @response.body

    get :just_xml
    assert_response 406
  end

  def test_all
197
    @request.accept = "*/*"
198 199 200 201 202 203 204 205 206 207 208
    get :js_or_html
    assert_equal 'HTML', @response.body # js is not part of all

    get :html_or_xml
    assert_equal 'HTML', @response.body

    get :just_xml
    assert_equal 'XML', @response.body
  end

  def test_xml
209
    @request.accept = "application/xml"
210 211 212 213 214
    get :html_xml_or_rss
    assert_equal 'XML', @response.body
  end

  def test_js_or_html
215
    @request.accept = "text/javascript, text/html"
216 217 218 219 220 221 222 223 224
    get :js_or_html
    assert_equal 'JS', @response.body

    get :html_or_xml
    assert_equal 'HTML', @response.body

    get :just_xml
    assert_response 406
  end
225

226 227 228
  def test_json_or_yaml
    get :json_or_yaml
    assert_equal 'JSON', @response.body
229 230

    get :json_or_yaml, :format => 'json'
231
    assert_equal 'JSON', @response.body
232 233 234 235 236 237 238 239

    get :json_or_yaml, :format => 'yaml'
    assert_equal 'YAML', @response.body

    { 'YAML' => %w(text/yaml),
      'JSON' => %w(application/json text/x-json)
    }.each do |body, content_types|
      content_types.each do |content_type|
240
        @request.accept = content_type
241 242 243 244
        get :json_or_yaml
        assert_equal body, @response.body
      end
    end
245
  end
246 247

  def test_js_or_anything
248
    @request.accept = "text/javascript, */*"
249 250 251 252 253 254 255 256 257
    get :js_or_html
    assert_equal 'JS', @response.body

    get :html_or_xml
    assert_equal 'HTML', @response.body

    get :just_xml
    assert_equal 'XML', @response.body
  end
258

259
  def test_using_defaults
260
    @request.accept = "*/*"
261
    get :using_defaults
262
    assert_equal "text/html", @response.content_type
263 264
    assert_equal 'Hello world!', @response.body

265
    @request.accept = "text/javascript"
266
    get :using_defaults
267
    assert_equal "text/javascript", @response.content_type
268
    assert_equal '$("body").visualEffect("highlight");', @response.body
269

270
    @request.accept = "application/xml"
271
    get :using_defaults
272
    assert_equal "application/xml", @response.content_type
273 274
    assert_equal "<p>Hello world!</p>\n", @response.body
  end
275

276
  def test_using_defaults_with_type_list
277
    @request.accept = "*/*"
278
    get :using_defaults_with_type_list
279
    assert_equal "text/html", @response.content_type
280 281
    assert_equal 'Hello world!', @response.body

282
    @request.accept = "text/javascript"
283
    get :using_defaults_with_type_list
284
    assert_equal "text/javascript", @response.content_type
285 286
    assert_equal '$("body").visualEffect("highlight");', @response.body

287
    @request.accept = "application/xml"
288
    get :using_defaults_with_type_list
289
    assert_equal "application/xml", @response.content_type
290 291
    assert_equal "<p>Hello world!</p>\n", @response.body
  end
292

293
  def test_with_atom_content_type
294 295 296
    @request.env["CONTENT_TYPE"] = "application/atom+xml"
    get :made_for_content_type
    assert_equal "ATOM", @response.body
297
  end
298

299
  def test_with_rss_content_type
300 301 302 303
    @request.env["CONTENT_TYPE"] = "application/rss+xml"
    get :made_for_content_type
    assert_equal "RSS", @response.body
  end
304

305
  def test_synonyms
306
    @request.accept = "application/javascript"
307 308 309
    get :js_or_html
    assert_equal 'JS', @response.body

310
    @request.accept = "application/x-xml"
311 312
    get :html_xml_or_rss
    assert_equal "XML", @response.body
313
  end
314

315
  def test_custom_types
316
    @request.accept = "application/crazy-xml"
317
    get :custom_type_handling
318
    assert_equal "application/crazy-xml", @response.content_type
319 320
    assert_equal 'Crazy XML', @response.body

321
    @request.accept = "text/html"
322
    get :custom_type_handling
323
    assert_equal "text/html", @response.content_type
324 325
    assert_equal 'HTML', @response.body
  end
326 327

  def test_xhtml_alias
328
    @request.accept = "application/xhtml+xml,application/xml"
329 330 331
    get :html_or_xml
    assert_equal 'HTML', @response.body
  end
332

333
  def test_firefox_simulation
334
    @request.accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
335 336 337
    get :html_or_xml
    assert_equal 'HTML', @response.body
  end
338 339

  def test_handle_any
340
    @request.accept = "*/*"
341 342 343
    get :handle_any
    assert_equal 'HTML', @response.body

344
    @request.accept = "text/javascript"
345 346 347
    get :handle_any
    assert_equal 'Either JS or XML', @response.body

348
    @request.accept = "text/xml"
349 350 351
    get :handle_any
    assert_equal 'Either JS or XML', @response.body
  end
352

353
  def test_handle_any_any
354
    @request.accept = "*/*"
355 356 357
    get :handle_any_any
    assert_equal 'HTML', @response.body
  end
358

359 360 361 362
  def test_handle_any_any_parameter_format
    get :handle_any_any, {:format=>'html'}
    assert_equal 'HTML', @response.body
  end
363

364
  def test_handle_any_any_explicit_html
365
    @request.accept = "text/html"
366 367 368 369 370
    get :handle_any_any
    assert_equal 'HTML', @response.body
  end

  def test_handle_any_any_javascript
371
    @request.accept = "text/javascript"
372 373 374
    get :handle_any_any
    assert_equal 'Whatever you ask for, I got it', @response.body
  end
375

376
  def test_handle_any_any_xml
377
    @request.accept = "text/xml"
378 379 380 381
    get :handle_any_any
    assert_equal 'Whatever you ask for, I got it', @response.body
  end

382
  def test_rjs_type_skips_layout
383 384 385
    @request.accept = "text/javascript"
    get :all_types_with_layout
    assert_equal 'RJS for all_types_with_layout', @response.body
386
  end
387

388
  def test_html_type_with_layout
389
    @request.accept = "text/html"
390
    get :all_types_with_layout
D
David Heinemeier Hansson 已提交
391
    assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
392
  end
393 394 395 396 397 398 399 400

  def test_xhr
    xhr :get, :js_or_html
    assert_equal 'JS', @response.body

    xhr :get, :using_defaults
    assert_equal '$("body").visualEffect("highlight");', @response.body
  end
401

402 403
  def test_custom_constant
    get :custom_constant_handling, :format => "mobile"
404
    assert_equal "text/x-mobile", @response.content_type
405 406
    assert_equal "Mobile", @response.body
  end
407 408 409 410 411

  def test_custom_constant_handling_without_block
    get :custom_constant_handling_without_block, :format => "mobile"
    assert_equal "text/x-mobile", @response.content_type
    assert_equal "Mobile", @response.body
412
  end
413

414 415 416 417 418 419 420 421 422 423 424 425 426
  def test_forced_format
    get :html_xml_or_rss
    assert_equal "HTML", @response.body

    get :html_xml_or_rss, :format => "html"
    assert_equal "HTML", @response.body

    get :html_xml_or_rss, :format => "xml"
    assert_equal "XML", @response.body

    get :html_xml_or_rss, :format => "rss"
    assert_equal "RSS", @response.body
  end
427

428 429 430 431 432 433 434 435
  def test_internally_forced_format
    get :forced_xml
    assert_equal "XML", @response.body

    get :forced_xml, :format => "html"
    assert_equal "XML", @response.body
  end

436 437 438 439
  def test_extension_synonyms
    get :html_xml_or_rss, :format => "xhtml"
    assert_equal "HTML", @response.body
  end
440 441 442 443

  def test_render_action_for_html
    @controller.instance_eval do
      def render(*args)
444 445 446
        @action = args.first[:action] unless args.empty?
        @action ||= action_name

J
José Valim 已提交
447
        response.body = "#{@action} - #{formats}"
448 449 450 451
      end
    end

    get :using_defaults
452
    assert_equal "using_defaults - #{[:html].to_s}", @response.body
453 454

    get :using_defaults, :format => "xml"
455
    assert_equal "using_defaults - #{[:xml].to_s}", @response.body
456 457
  end

458 459
  def test_format_with_custom_response_type
    get :iphone_with_html_response_type
460 461
    assert_equal '<html><div id="html">Hello future from Firefox!</div></html>', @response.body

462 463
    get :iphone_with_html_response_type, :format => "iphone"
    assert_equal "text/html", @response.content_type
D
David Heinemeier Hansson 已提交
464
    assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
465 466
  end

467
  def test_format_with_custom_response_type_and_request_headers
468
    @request.accept = "text/iphone"
469
    get :iphone_with_html_response_type
D
David Heinemeier Hansson 已提交
470
    assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
471
    assert_equal "text/html", @response.content_type
472
  end
473
end
474

475
class RespondWithController < ActionController::Base
J
José Valim 已提交
476
  respond_to :html, :json
477
  respond_to :xml, :except => :using_defaults
478
  respond_to :js,  :only => [ :using_defaults, :using_resource ]
479

480 481 482 483 484 485 486 487 488
  def using_defaults
    respond_to do |format|
      format.csv { render :text => "CSV" }
    end
  end

  def using_defaults_with_type_list
    respond_to(:js, :xml)
  end
J
José Valim 已提交
489

J
José Valim 已提交
490 491 492 493 494 495
  def default_overwritten
    respond_to do |format|
      format.html { render :text => "HTML" }
    end
  end

J
José Valim 已提交
496
  def using_resource
497
    respond_with(Customer.new("david", 13))
J
José Valim 已提交
498 499 500
  end

  def using_resource_with_options
501
    respond_with(Customer.new("david", 13), :status => :unprocessable_entity) do |format|
J
José Valim 已提交
502 503 504 505
      format.js
    end
  end

J
José Valim 已提交
506
  def using_resource_with_parent
507
    respond_with([Quiz::Store.new("developer?", 11), Customer.new("david", 13)])
508 509
  end

J
José Valim 已提交
510 511 512 513 514 515
protected

  def _render_js(js, options)
    self.content_type ||= Mime::JS
    self.response_body = js.respond_to?(:to_js) ? js.to_js : js
  end
516 517
end

518 519 520 521 522
class InheritedRespondWithController < RespondWithController
  clear_respond_to
  respond_to :xml, :json

  def index
523
    respond_with(Customer.new("david", 13)) do |format|
524 525 526 527 528
      format.json { render :text => "JSON" }
    end
  end
end

529 530 531 532 533 534 535
class RespondWithControllerTest < ActionController::TestCase
  tests RespondWithController

  def setup
    super
    ActionController::Base.use_accept_header = true
    @request.host = "www.example.com"
536 537 538 539 540

    ActionController::Routing::Routes.draw do |map|
      map.resources :customers
      map.resources :quiz_stores, :has_many => :customers
    end
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
  end

  def teardown
    super
    ActionController::Base.use_accept_header = false
  end

  def test_using_defaults
    @request.accept = "*/*"
    get :using_defaults
    assert_equal "text/html", @response.content_type
    assert_equal 'Hello world!', @response.body

    @request.accept = "text/csv"
    get :using_defaults
    assert_equal "text/csv", @response.content_type
    assert_equal "CSV", @response.body

    @request.accept = "text/javascript"
    get :using_defaults
    assert_equal "text/javascript", @response.content_type
    assert_equal '$("body").visualEffect("highlight");', @response.body
  end

  def test_using_defaults_with_type_list
    @request.accept = "*/*"
    get :using_defaults_with_type_list
    assert_equal "text/javascript", @response.content_type
    assert_equal '$("body").visualEffect("highlight");', @response.body

    @request.accept = "application/xml"
    get :using_defaults_with_type_list
    assert_equal "application/xml", @response.content_type
    assert_equal "<p>Hello world!</p>\n", @response.body
  end

J
José Valim 已提交
577 578 579 580 581 582
  def test_default_overwritten
    get :default_overwritten
    assert_equal "text/html", @response.content_type
    assert_equal "HTML", @response.body
  end

J
José Valim 已提交
583
  def test_using_resource
584
    @request.accept = "text/javascript"
J
José Valim 已提交
585
    get :using_resource
586 587
    assert_equal "text/javascript", @response.content_type
    assert_equal '$("body").visualEffect("highlight");', @response.body
J
José Valim 已提交
588 589 590 591 592 593 594 595 596 597 598 599

    @request.accept = "application/xml"
    get :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal "XML", @response.body

    @request.accept = "application/json"
    assert_raise ActionView::MissingTemplate do
      get :using_resource
    end
  end

600 601 602 603
  def test_using_resource_for_post_with_html
    post :using_resource
    assert_equal "text/html", @response.content_type
    assert_equal 302, @response.status
604
    assert_equal "http://www.example.com/customers/13", @response.location
605 606 607
    assert @response.redirect?

    errors = { :name => :invalid }
608
    Customer.any_instance.stubs(:errors).returns(errors)
609 610 611 612 613 614 615 616
    post :using_resource
    assert_equal "text/html", @response.content_type
    assert_equal 200, @response.status
    assert_equal "New world!\n", @response.body
    assert_nil @response.location
  end

  def test_using_resource_for_post_with_xml
617 618 619 620 621 622
    @request.accept = "application/xml"

    post :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal 201, @response.status
    assert_equal "XML", @response.body
623
    assert_equal "http://www.example.com/customers/13", @response.location
624 625

    errors = { :name => :invalid }
626
    Customer.any_instance.stubs(:errors).returns(errors)
627 628 629 630 631 632 633
    post :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal 422, @response.status
    assert_equal errors.to_xml, @response.body
    assert_nil @response.location
  end

634 635 636 637
  def test_using_resource_for_put_with_html
    put :using_resource
    assert_equal "text/html", @response.content_type
    assert_equal 302, @response.status
638
    assert_equal "http://www.example.com/customers/13", @response.location
639 640 641
    assert @response.redirect?

    errors = { :name => :invalid }
642
    Customer.any_instance.stubs(:errors).returns(errors)
643 644 645 646 647 648 649 650
    put :using_resource
    assert_equal "text/html", @response.content_type
    assert_equal 200, @response.status
    assert_equal "Edit world!\n", @response.body
    assert_nil @response.location
  end

  def test_using_resource_for_put_with_xml
651 652 653 654 655 656
    @request.accept = "application/xml"

    put :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal 200, @response.status
    assert_equal " ", @response.body
657
    assert_equal "http://www.example.com/customers/13", @response.location
658 659

    errors = { :name => :invalid }
660
    Customer.any_instance.stubs(:errors).returns(errors)
661 662 663 664
    put :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal 422, @response.status
    assert_equal errors.to_xml, @response.body
665
    assert_nil @response.location
666 667
  end

668
  def test_using_resource_for_delete_with_html
669
    Customer.any_instance.stubs(:destroyed?).returns(true)
670 671 672
    delete :using_resource
    assert_equal "text/html", @response.content_type
    assert_equal 302, @response.status
673
    assert_equal "http://www.example.com/customers", @response.location
674 675 676
  end

  def test_using_resource_for_delete_with_xml
677
    Customer.any_instance.stubs(:destroyed?).returns(true)
678 679 680 681 682
    @request.accept = "application/xml"
    delete :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal 200, @response.status
    assert_equal " ", @response.body
683
    assert_equal "http://www.example.com/customers", @response.location
684 685
  end

J
José Valim 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699
  def test_using_resource_with_options
    @request.accept = "application/xml"
    get :using_resource_with_options
    assert_equal "application/xml", @response.content_type
    assert_equal 422, @response.status
    assert_equal "XML", @response.body

    @request.accept = "text/javascript"
    get :using_resource_with_options
    assert_equal "text/javascript", @response.content_type
    assert_equal 422, @response.status
    assert_equal "JS", @response.body
  end

700 701 702 703 704 705 706 707 708
  def test_using_resource_with_parent_for_get
    @request.accept = "application/xml"
    get :using_resource_with_parent
    assert_equal "application/xml", @response.content_type
    assert_equal 200, @response.status
    assert_equal "XML", @response.body
  end

  def test_using_resource_with_parent_for_post
J
José Valim 已提交
709 710 711 712 713 714
    @request.accept = "application/xml"

    post :using_resource_with_parent
    assert_equal "application/xml", @response.content_type
    assert_equal 201, @response.status
    assert_equal "XML", @response.body
715
    assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location
J
José Valim 已提交
716 717

    errors = { :name => :invalid }
718
    Customer.any_instance.stubs(:errors).returns(errors)
J
José Valim 已提交
719 720 721 722 723
    post :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal 422, @response.status
    assert_equal errors.to_xml, @response.body
    assert_nil @response.location
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
  end

  def test_clear_respond_to
    @controller = InheritedRespondWithController.new
    @request.accept = "text/html"
    get :index
    assert_equal 406, @response.status
  end

  def test_first_in_respond_to_has_higher_priority
    @controller = InheritedRespondWithController.new
    @request.accept = "*/*"
    get :index
    assert_equal "application/xml", @response.content_type
    assert_equal "XML", @response.body
  end

741 742 743 744 745 746 747 748
  def test_not_acceptable
    @request.accept = "application/xml"
    get :using_defaults
    assert_equal 406, @response.status

    @request.accept = "text/html"
    get :using_defaults_with_type_list
    assert_equal 406, @response.status
J
José Valim 已提交
749 750 751 752 753 754

    @request.accept = "application/json"
    get :using_defaults_with_type_list
    assert_equal 406, @response.status

    @request.accept = "text/javascript"
755
    get :default_overwritten
J
José Valim 已提交
756
    assert_equal 406, @response.status
757
  end
758 759
end

760
class AbstractPostController < ActionController::Base
761
  self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/"
762 763 764 765 766
end

# For testing layouts which are set automatically
class PostController < AbstractPostController
  around_filter :with_iphone
767

768
  def index
769
    respond_to(:html, :iphone)
770
  end
771

772 773 774 775 776 777 778
protected

  def with_iphone
    Mime::Type.register_alias("text/html", :iphone)
    request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
    yield
  ensure
779
    Mime::SET.delete(:iphone)
780 781
    Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
  end
782
end
783

784
class SuperPostController < PostController
785 786
end

787 788
class MimeControllerLayoutsTest < ActionController::TestCase
  tests PostController
789

790
  def setup
791
    super
792 793
    @request.host = "www.example.com"
  end
794

795 796
  def test_missing_layout_renders_properly
    get :index
797
    assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body
798

799
    @request.accept = "text/iphone"
800 801 802
    get :index
    assert_equal 'Hello iPhone', @response.body
  end
803

804 805
  def test_format_with_inherited_layouts
    @controller = SuperPostController.new
806

807 808
    get :index
    assert_equal '<html><div id="html">Super Firefox</div></html>', @response.body
809

810 811 812
    @request.accept = "text/iphone"
    get :index
    assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
813 814
  end
end