mime_responds_test.rb 25.1 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 465
  respond_to :xml, :except => :using_resource_with_block
  respond_to :js,  :only => [ :using_resource_with_block, :using_resource ]
466

467 468
  def using_resource
    respond_with(resource)
469 470
  end

471 472 473 474
  def using_resource_with_block
    respond_with(resource) do |format|
      format.csv { render :text => "CSV" }
    end
475
  end
J
José Valim 已提交
476

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

483
  def using_resource_with_collection
484
    respond_with([resource, Customer.new("jamis", 9)])
485 486
  end

J
José Valim 已提交
487
  def using_resource_with_parent
488
    respond_with(Quiz::Store.new("developer?", 11), Customer.new("david", 13))
489 490
  end

491
  def using_resource_with_status_and_location
492
    respond_with(resource, :location => "http://test.host/", :status => :created)
493 494
  end

495
  def using_resource_with_responder
496
    responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
497
    respond_with(resource, :responder => responder)
498 499
  end

500
  def using_resource_with_action
501
    respond_with(resource, :action => :foo) do |format|
502
      format.html { raise ActionView::MissingTemplate.new([], "foo/bar", {}, false) }
503 504 505
    end
  end

506 507 508 509
  def using_responder_with_respond
    responder = Class.new(ActionController::Responder) do
      def respond; @controller.render :text => "respond #{format}"; end
    end
510
    respond_with(resource, :responder => responder)
511 512
  end

J
José Valim 已提交
513 514
protected

515 516 517 518
  def resource
    Customer.new("david", 13)
  end

J
José Valim 已提交
519 520 521 522
  def _render_js(js, options)
    self.content_type ||= Mime::JS
    self.response_body = js.respond_to?(:to_js) ? js.to_js : js
  end
523 524
end

525 526 527 528 529
class InheritedRespondWithController < RespondWithController
  clear_respond_to
  respond_to :xml, :json

  def index
530
    respond_with(resource) do |format|
531 532 533 534 535
      format.json { render :text => "JSON" }
    end
  end
end

536 537 538 539 540 541
class EmptyRespondWithController < ActionController::Base
  def index
    respond_with(Customer.new("david", 13))
  end
end

542 543 544 545 546 547 548 549 550 551 552 553 554 555
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

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
  def test_using_resource
    @request.accept = "text/javascript"
    get :using_resource
    assert_equal "text/javascript", @response.content_type
    assert_equal '$("body").visualEffect("highlight");', @response.body

    @request.accept = "application/xml"
    get :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal "<name>david</name>", @response.body

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

  def test_using_resource_with_block
574
    @request.accept = "*/*"
575
    get :using_resource_with_block
576 577 578 579
    assert_equal "text/html", @response.content_type
    assert_equal 'Hello world!', @response.body

    @request.accept = "text/csv"
580
    get :using_resource_with_block
581 582 583 584
    assert_equal "text/csv", @response.content_type
    assert_equal "CSV", @response.body

    @request.accept = "application/xml"
585
    get :using_resource
586
    assert_equal "application/xml", @response.content_type
587
    assert_equal "<name>david</name>", @response.body
588 589
  end

590 591
  def test_using_resource_with_overwrite_block
    get :using_resource_with_overwrite_block
J
José Valim 已提交
592 593 594 595
    assert_equal "text/html", @response.content_type
    assert_equal "HTML", @response.body
  end

596
  def test_not_acceptable
J
José Valim 已提交
597
    @request.accept = "application/xml"
598 599
    get :using_resource_with_block
    assert_equal 406, @response.status
J
José Valim 已提交
600

601 602 603
    @request.accept = "text/javascript"
    get :using_resource_with_overwrite_block
    assert_equal 406, @response.status
J
José Valim 已提交
604 605
  end

606
  def test_using_resource_for_post_with_html_redirects_on_success
607 608 609 610 611 612
    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?
613 614
    end
  end
615

616 617
  def test_using_resource_for_post_with_html_rerender_on_failure
    with_test_route_set do
618 619 620 621 622 623 624 625
      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
626 627
  end

628
  def test_using_resource_for_post_with_xml_yields_created_on_success
629 630 631 632 633 634 635
    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
636 637
    end
  end
638

639 640 641
  def test_using_resource_for_post_with_xml_yields_unprocessable_entity_on_failure
    with_test_route_set do
      @request.accept = "application/xml"
642 643 644 645 646 647 648 649
      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
650 651
  end

652
  def test_using_resource_for_put_with_html_redirects_on_success
653 654 655 656 657 658
    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?
659 660
    end
  end
661

662 663
  def test_using_resource_for_put_with_html_rerender_on_failure
    with_test_route_set do
664 665 666 667 668 669 670 671
      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
672 673
  end

674
  def test_using_resource_for_put_with_xml_yields_ok_on_success
675 676 677 678 679
    @request.accept = "application/xml"
    put :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal 200, @response.status
    assert_equal " ", @response.body
680
  end
681

682 683
  def test_using_resource_for_put_with_xml_yields_unprocessable_entity_on_failure
    @request.accept = "application/xml"
684
    errors = { :name => :invalid }
685
    Customer.any_instance.stubs(:errors).returns(errors)
686 687 688 689
    put :using_resource
    assert_equal "application/xml", @response.content_type
    assert_equal 422, @response.status
    assert_equal errors.to_xml, @response.body
690
    assert_nil @response.location
691 692
  end

693
  def test_using_resource_for_delete_with_html_redirects_on_success
694 695 696 697 698 699 700
    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
701 702
  end

703
  def test_using_resource_for_delete_with_xml_yields_ok_on_success
704
    Customer.any_instance.stubs(:destroyed?).returns(true)
705 706 707 708 709
    @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 已提交
710 711
  end

712 713 714 715 716 717 718 719 720 721 722 723
  def test_using_resource_for_delete_with_html_redirects_on_failure
    with_test_route_set do
      errors = { :name => :invalid }
      Customer.any_instance.stubs(:errors).returns(errors)
      Customer.any_instance.stubs(:destroyed?).returns(false)
      delete :using_resource
      assert_equal "text/html", @response.content_type
      assert_equal 302, @response.status
      assert_equal "http://www.example.com/customers/13", @response.location
    end
  end

724 725 726 727 728
  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
729
    assert_equal "<name>david</name>", @response.body
730 731 732
  end

  def test_using_resource_with_parent_for_post
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    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
750 751
  end

752 753 754 755 756 757 758 759 760
  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

761 762 763 764 765 766 767 768 769 770 771 772 773 774
  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

775 776 777 778 779 780 781 782 783 784
  def test_respond_as_responder_entry_point
    @request.accept = "text/html"
    get :using_responder_with_respond
    assert_equal "respond html", @response.body

    @request.accept = "application/xml"
    get :using_responder_with_respond
    assert_equal "respond xml", @response.body
  end

785 786 787 788 789 790 791 792 793 794 795 796
  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
797
    assert_equal "<name>david</name>", @response.body
798 799
  end

800 801 802 803 804 805 806
  def test_block_inside_respond_with_is_rendered
    @controller = InheritedRespondWithController.new
    @request.accept = "application/json"
    get :index
    assert_equal "JSON", @response.body
  end

807 808 809 810 811 812 813
  def test_no_double_render_is_raised
    @request.accept = "text/html"
    assert_raise ActionView::MissingTemplate do
      get :using_resource
    end
  end

814
  def test_using_resource_with_status_and_location
815
    @request.accept = "text/html"
816
    post :using_resource_with_status_and_location
817 818
    assert @response.redirect?
    assert_equal "http://test.host/", @response.location
819 820 821 822 823 824

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

825 826
  def test_using_resource_with_responder
    get :using_resource_with_responder
827
    assert_equal "Resource name is david", @response.body
828 829
  end

830
  def test_using_resource_with_set_responder
831 832 833 834 835 836 837
    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

838 839 840 841 842 843
  def test_error_is_raised_if_no_respond_to_is_declared_and_respond_with_is_called
    @controller = EmptyRespondWithController.new
    @request.accept = "*/*"
    assert_raise RuntimeError do
      get :index
    end
844
  end
845 846 847 848 849

  private
    def with_test_route_set
      with_routing do |set|
        set.draw do |map|
J
Joshua Peek 已提交
850 851 852 853 854
          resources :customers
          resources :quiz_stores do
            resources :customers
          end
          match ":controller/:action"
855 856 857 858
        end
        yield
      end
    end
859 860
end

861
class AbstractPostController < ActionController::Base
862
  self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/"
863 864 865 866 867
end

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

869
  def index
870
    respond_to(:html, :iphone)
871
  end
872

873 874 875 876 877 878
protected

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

881
class SuperPostController < PostController
882 883
end

884 885
class MimeControllerLayoutsTest < ActionController::TestCase
  tests PostController
886

887
  def setup
888
    super
889 890
    @request.host = "www.example.com"
  end
891

892 893
  def test_missing_layout_renders_properly
    get :index
894
    assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body
895

896
    @request.accept = "text/iphone"
897 898 899
    get :index
    assert_equal 'Hello iPhone', @response.body
  end
900

901 902
  def test_format_with_inherited_layouts
    @controller = SuperPostController.new
903

904 905
    get :index
    assert_equal '<html><div id="html">Super Firefox</div></html>', @response.body
906

907 908 909
    @request.accept = "text/iphone"
    get :index
    assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
910 911
  end
end