mime_responds_test.rb 23.7 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
  Mime::Type.register("text/x-mobile", :mobile)
83

84
  def custom_constant_handling
85 86 87 88 89
    respond_to do |type|
      type.html   { render :text => "HTML"   }
      type.mobile { render :text => "Mobile" }
    end
  end
90

91 92 93 94 95 96
  def custom_constant_handling_without_block
    respond_to do |type|
      type.html   { render :text => "HTML"   }
      type.mobile
    end
  end
97

98 99 100 101 102 103
  def handle_any
    respond_to do |type|
      type.html { render :text => "HTML" }
      type.any(:js, :xml) { render :text => "Either JS or XML" }
    end
  end
104

105 106 107 108 109 110
  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
111

112 113 114 115 116
  def all_types_with_layout
    respond_to do |type|
      type.html
      type.js
    end
117 118
  end

119 120
  Mime::Type.register_alias("text/html", :iphone)

121
  def iphone_with_html_response_type
122
    request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
123

124 125
    respond_to do |type|
      type.html   { @type = "Firefox" }
126
      type.iphone { @type = "iPhone"  }
127
    end
128 129
  end

130 131
  def iphone_with_html_response_type_without_layout
    request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
132

133 134 135 136 137 138
    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
  end

139
  def rescue_action(e)
J
Jamis Buck 已提交
140
    raise
141
  end
142

143 144
  protected
    def set_layout
145
      if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name)
D
David Heinemeier Hansson 已提交
146
        "respond_to/layouts/standard"
147 148
      elsif action_name == "iphone_with_html_response_type_without_layout"
        "respond_to/layouts/missing"
149 150
      end
    end
151 152
end

153
class RespondToControllerTest < ActionController::TestCase
154 155
  tests RespondToController

156
  def setup
157
    super
158
    ActionController::Base.use_accept_header = true
159 160
    @request.host = "www.example.com"
  end
161

162
  def teardown
163
    super
164 165 166
    ActionController::Base.use_accept_header = false
  end

167
  def test_html
168
    @request.accept = "text/html"
169 170
    get :js_or_html
    assert_equal 'HTML', @response.body
171

172 173 174 175 176 177 178 179
    get :html_or_xml
    assert_equal 'HTML', @response.body

    get :just_xml
    assert_response 406
  end

  def test_all
180
    @request.accept = "*/*"
181 182 183 184 185 186 187 188 189 190 191
    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
192
    @request.accept = "application/xml"
193 194 195 196 197
    get :html_xml_or_rss
    assert_equal 'XML', @response.body
  end

  def test_js_or_html
198
    @request.accept = "text/javascript, text/html"
199
    xhr :get, :js_or_html
200 201
    assert_equal 'JS', @response.body

202 203
    @request.accept = "text/javascript, text/html"
    xhr :get, :html_or_xml
204 205
    assert_equal 'HTML', @response.body

206 207
    @request.accept = "text/javascript, text/html"
    xhr :get, :just_xml
208 209
    assert_response 406
  end
210

211
  def test_json_or_yaml
212
    xhr :get, :json_or_yaml
213
    assert_equal 'JSON', @response.body
214 215

    get :json_or_yaml, :format => 'json'
216
    assert_equal 'JSON', @response.body
217 218 219 220 221 222 223 224

    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|
225
        @request.accept = content_type
226 227 228 229
        get :json_or_yaml
        assert_equal body, @response.body
      end
    end
230
  end
231 232

  def test_js_or_anything
233
    @request.accept = "text/javascript, */*"
234
    xhr :get, :js_or_html
235 236
    assert_equal 'JS', @response.body

237
    xhr :get, :html_or_xml
238 239
    assert_equal 'HTML', @response.body

240
    xhr :get, :just_xml
241 242
    assert_equal 'XML', @response.body
  end
243

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

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

255
    @request.accept = "application/xml"
256
    get :using_defaults
257
    assert_equal "application/xml", @response.content_type
258 259
    assert_equal "<p>Hello world!</p>\n", @response.body
  end
260

261
  def test_using_defaults_with_type_list
262
    @request.accept = "*/*"
263
    get :using_defaults_with_type_list
264
    assert_equal "text/html", @response.content_type
265 266
    assert_equal 'Hello world!', @response.body

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

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

278
  def test_with_atom_content_type
279
    @request.accept = ""
280
    @request.env["CONTENT_TYPE"] = "application/atom+xml"
281
    xhr :get, :made_for_content_type
282
    assert_equal "ATOM", @response.body
283
  end
284

285
  def test_with_rss_content_type
286
    @request.accept = ""
287
    @request.env["CONTENT_TYPE"] = "application/rss+xml"
288
    xhr :get, :made_for_content_type
289 290
    assert_equal "RSS", @response.body
  end
291

292
  def test_synonyms
293
    @request.accept = "application/javascript"
294 295 296
    get :js_or_html
    assert_equal 'JS', @response.body

297
    @request.accept = "application/x-xml"
298 299
    get :html_xml_or_rss
    assert_equal "XML", @response.body
300
  end
301

302
  def test_custom_types
303
    @request.accept = "application/crazy-xml"
304
    get :custom_type_handling
305
    assert_equal "application/crazy-xml", @response.content_type
306 307
    assert_equal 'Crazy XML', @response.body

308
    @request.accept = "text/html"
309
    get :custom_type_handling
310
    assert_equal "text/html", @response.content_type
311 312
    assert_equal 'HTML', @response.body
  end
313 314

  def test_xhtml_alias
315
    @request.accept = "application/xhtml+xml,application/xml"
316 317 318
    get :html_or_xml
    assert_equal 'HTML', @response.body
  end
319

320
  def test_firefox_simulation
321
    @request.accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
322 323 324
    get :html_or_xml
    assert_equal 'HTML', @response.body
  end
325 326

  def test_handle_any
327
    @request.accept = "*/*"
328 329 330
    get :handle_any
    assert_equal 'HTML', @response.body

331
    @request.accept = "text/javascript"
332 333 334
    get :handle_any
    assert_equal 'Either JS or XML', @response.body

335
    @request.accept = "text/xml"
336 337 338
    get :handle_any
    assert_equal 'Either JS or XML', @response.body
  end
339

340
  def test_handle_any_any
341
    @request.accept = "*/*"
342 343 344
    get :handle_any_any
    assert_equal 'HTML', @response.body
  end
345

346 347 348 349
  def test_handle_any_any_parameter_format
    get :handle_any_any, {:format=>'html'}
    assert_equal 'HTML', @response.body
  end
350

351
  def test_handle_any_any_explicit_html
352
    @request.accept = "text/html"
353 354 355 356 357
    get :handle_any_any
    assert_equal 'HTML', @response.body
  end

  def test_handle_any_any_javascript
358
    @request.accept = "text/javascript"
359 360 361
    get :handle_any_any
    assert_equal 'Whatever you ask for, I got it', @response.body
  end
362

363
  def test_handle_any_any_xml
364
    @request.accept = "text/xml"
365 366 367 368
    get :handle_any_any
    assert_equal 'Whatever you ask for, I got it', @response.body
  end

369
  def test_rjs_type_skips_layout
370 371 372
    @request.accept = "text/javascript"
    get :all_types_with_layout
    assert_equal 'RJS for all_types_with_layout', @response.body
373
  end
374

375
  def test_html_type_with_layout
376
    @request.accept = "text/html"
377
    get :all_types_with_layout
D
David Heinemeier Hansson 已提交
378
    assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
379
  end
380 381 382 383 384 385 386 387

  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
388

389 390
  def test_custom_constant
    get :custom_constant_handling, :format => "mobile"
391
    assert_equal "text/x-mobile", @response.content_type
392 393
    assert_equal "Mobile", @response.body
  end
394 395 396 397 398

  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
399
  end
400

401 402 403 404 405 406 407 408 409 410 411 412 413
  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
414

415 416 417 418 419 420 421 422
  def test_internally_forced_format
    get :forced_xml
    assert_equal "XML", @response.body

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

423 424 425 426
  def test_extension_synonyms
    get :html_xml_or_rss, :format => "xhtml"
    assert_equal "HTML", @response.body
  end
427 428 429 430

  def test_render_action_for_html
    @controller.instance_eval do
      def render(*args)
431 432 433
        @action = args.first[:action] unless args.empty?
        @action ||= action_name

J
José Valim 已提交
434
        response.body = "#{@action} - #{formats}"
435 436 437 438
      end
    end

    get :using_defaults
439
    assert_equal "using_defaults - #{[:html].to_s}", @response.body
440 441

    get :using_defaults, :format => "xml"
442
    assert_equal "using_defaults - #{[:xml].to_s}", @response.body
443 444
  end

445 446
  def test_format_with_custom_response_type
    get :iphone_with_html_response_type
447 448
    assert_equal '<html><div id="html">Hello future from Firefox!</div></html>', @response.body

449 450
    get :iphone_with_html_response_type, :format => "iphone"
    assert_equal "text/html", @response.content_type
D
David Heinemeier Hansson 已提交
451
    assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
452 453
  end

454
  def test_format_with_custom_response_type_and_request_headers
455
    @request.accept = "text/iphone"
456
    get :iphone_with_html_response_type
D
David Heinemeier Hansson 已提交
457
    assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
458
    assert_equal "text/html", @response.content_type
459
  end
460
end
461

462
class RespondWithController < ActionController::Base
J
José Valim 已提交
463
  respond_to :html, :json
464
  respond_to :xml, :except => :using_defaults
465
  respond_to :js,  :only => [ :using_defaults, :using_resource ]
466

467 468 469 470 471 472 473 474 475
  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 已提交
476

J
José Valim 已提交
477 478 479 480 481 482
  def default_overwritten
    respond_to do |format|
      format.html { render :text => "HTML" }
    end
  end

J
José Valim 已提交
483
  def using_resource
484
    respond_with(Customer.new("david", 13))
J
José Valim 已提交
485 486
  end

487 488 489 490
  def using_resource_with_collection
    respond_with([Customer.new("david", 13), Customer.new("jamis", 9)])
  end

J
José Valim 已提交
491
  def using_resource_with_parent
492
    respond_with(Quiz::Store.new("developer?", 11), Customer.new("david", 13))
493 494
  end

495 496 497 498
  def using_resource_with_status_and_location
    respond_with(Customer.new("david", 13), :location => "http://test.host/", :status => :created)
  end

499
  def using_resource_with_responder
500
    responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
501
    respond_with(Customer.new("david", 13), :responder => responder)
502 503
  end

504 505 506 507 508 509
  def using_resource_with_action
    respond_with(Customer.new("david", 13), :action => :foo) do |format|
      format.html { raise ActionView::MissingTemplate.new([], "method") }
    end
  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 536 537 538 539 540 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
class RespondWithControllerTest < ActionController::TestCase
  tests RespondWithController

  def setup
    super
    ActionController::Base.use_accept_header = true
    @request.host = "www.example.com"
  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 已提交
572 573 574 575 576 577
  def test_default_overwritten
    get :default_overwritten
    assert_equal "text/html", @response.content_type
    assert_equal "HTML", @response.body
  end

J
José Valim 已提交
578
  def test_using_resource
579
    @request.accept = "text/javascript"
J
José Valim 已提交
580
    get :using_resource
581 582
    assert_equal "text/javascript", @response.content_type
    assert_equal '$("body").visualEffect("highlight");', @response.body
J
José Valim 已提交
583 584 585 586

    @request.accept = "application/xml"
    get :using_resource
    assert_equal "application/xml", @response.content_type
587
    assert_equal "<name>david</name>", @response.body
J
José Valim 已提交
588 589 590 591 592 593 594

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

595
  def test_using_resource_for_post_with_html
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    with_test_route_set do
      post :using_resource
      assert_equal "text/html", @response.content_type
      assert_equal 302, @response.status
      assert_equal "http://www.example.com/customers/13", @response.location
      assert @response.redirect?

      errors = { :name => :invalid }
      Customer.any_instance.stubs(:errors).returns(errors)
      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
611 612 613
  end

  def test_using_resource_for_post_with_xml
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    with_test_route_set do
      @request.accept = "application/xml"

      post :using_resource
      assert_equal "application/xml", @response.content_type
      assert_equal 201, @response.status
      assert_equal "<name>david</name>", @response.body
      assert_equal "http://www.example.com/customers/13", @response.location

      errors = { :name => :invalid }
      Customer.any_instance.stubs(:errors).returns(errors)
      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
631 632
  end

633
  def test_using_resource_for_put_with_html
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    with_test_route_set do
      put :using_resource
      assert_equal "text/html", @response.content_type
      assert_equal 302, @response.status
      assert_equal "http://www.example.com/customers/13", @response.location
      assert @response.redirect?

      errors = { :name => :invalid }
      Customer.any_instance.stubs(:errors).returns(errors)
      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
649 650 651
  end

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

    put :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal 200, @response.status
    assert_equal " ", @response.body

    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 670 671 672 673 674 675
    with_test_route_set do
      Customer.any_instance.stubs(:destroyed?).returns(true)
      delete :using_resource
      assert_equal "text/html", @response.content_type
      assert_equal 302, @response.status
      assert_equal "http://www.example.com/customers", @response.location
    end
676 677 678
  end

  def test_using_resource_for_delete_with_xml
679
    Customer.any_instance.stubs(:destroyed?).returns(true)
680 681 682 683 684
    @request.accept = "application/xml"
    delete :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal 200, @response.status
    assert_equal " ", @response.body
J
José Valim 已提交
685 686
  end

687 688 689 690 691
  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
692
    assert_equal "<name>david</name>", @response.body
693 694 695
  end

  def test_using_resource_with_parent_for_post
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    with_test_route_set do
      @request.accept = "application/xml"

      post :using_resource_with_parent
      assert_equal "application/xml", @response.content_type
      assert_equal 201, @response.status
      assert_equal "<name>david</name>", @response.body
      assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location

      errors = { :name => :invalid }
      Customer.any_instance.stubs(:errors).returns(errors)
      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
713 714
  end

715 716 717 718 719 720 721 722 723
  def test_using_resource_with_collection
    @request.accept = "application/xml"
    get :using_resource_with_collection
    assert_equal "application/xml", @response.content_type
    assert_equal 200, @response.status
    assert_match /<name>david<\/name>/, @response.body
    assert_match /<name>jamis<\/name>/, @response.body
  end

724 725 726 727 728 729 730 731 732 733 734 735 736 737
  def test_using_resource_with_action
    @controller.instance_eval do
      def render(params={})
        self.response_body = "#{params[:action]} - #{formats}"
      end
    end

    errors = { :name => :invalid }
    Customer.any_instance.stubs(:errors).returns(errors)

    post :using_resource_with_action
    assert_equal "foo - #{[:html].to_s}", @controller.response_body
  end

738 739 740 741 742 743 744 745 746 747 748 749
  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
750
    assert_equal "<name>david</name>", @response.body
751 752
  end

753 754 755 756 757 758 759
  def test_block_inside_respond_with_is_rendered
    @controller = InheritedRespondWithController.new
    @request.accept = "application/json"
    get :index
    assert_equal "JSON", @response.body
  end

760 761 762 763 764 765 766
  def test_no_double_render_is_raised
    @request.accept = "text/html"
    assert_raise ActionView::MissingTemplate do
      get :using_resource
    end
  end

767
  def test_using_resource_with_status_and_location
768
    @request.accept = "text/html"
769
    post :using_resource_with_status_and_location
770 771
    assert @response.redirect?
    assert_equal "http://test.host/", @response.location
772 773 774 775 776 777

    @request.accept = "application/xml"
    get :using_resource_with_status_and_location
    assert_equal 201, @response.status
  end

778 779
  def test_using_resource_with_responder
    get :using_resource_with_responder
780
    assert_equal "Resource name is david", @response.body
781 782
  end

783
  def test_using_resource_with_set_responder
784 785 786 787 788 789 790
    RespondWithController.responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
    get :using_resource
    assert_equal "Resource name is david", @response.body
  ensure
    RespondWithController.responder = ActionController::Responder
  end

791 792 793 794 795 796 797 798
  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 已提交
799 800 801 802 803 804

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

    @request.accept = "text/javascript"
805
    get :default_overwritten
J
José Valim 已提交
806
    assert_equal 406, @response.status
807
  end
808 809 810 811 812 813 814 815 816 817 818 819

  private
    def with_test_route_set
      with_routing do |set|
        set.draw do |map|
          map.resources :customers
          map.resources :quiz_stores, :has_many => :customers
          map.connect ":controller/:action/:id"
        end
        yield
      end
    end
820 821
end

822
class AbstractPostController < ActionController::Base
823
  self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/"
824 825 826 827 828
end

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

830
  def index
831
    respond_to(:html, :iphone)
832
  end
833

834 835 836 837 838 839
protected

  def with_iphone
    request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
    yield
  end
840
end
841

842
class SuperPostController < PostController
843 844
end

845 846
class MimeControllerLayoutsTest < ActionController::TestCase
  tests PostController
847

848
  def setup
849
    super
850 851
    @request.host = "www.example.com"
  end
852

853 854
  def test_missing_layout_renders_properly
    get :index
855
    assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body
856

857
    @request.accept = "text/iphone"
858 859 860
    get :index
    assert_equal 'Hello iPhone', @response.body
  end
861

862 863
  def test_format_with_inherited_layouts
    @controller = SuperPostController.new
864

865 866
    get :index
    assert_equal '<html><div id="html">Super Firefox</div></html>', @response.body
867

868 869 870
    @request.accept = "text/iphone"
    get :index
    assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
871 872
  end
end