test_case_test.rb 24.1 KB
Newer Older
1 2
require 'abstract_unit'
require 'controller/fake_controllers'
3
require 'active_support/ordered_hash'
4

5
class TestCaseTest < ActionController::TestCase
6
  class TestController < ActionController::Base
7 8 9 10
    def no_op
      render :text => 'dummy'
    end

11 12
    def set_flash
      flash["test"] = ">#{flash["test"]}<"
13
      render :text => 'ignore me'
14
    end
15

16 17 18 19 20
    def set_flash_now
      flash.now["test_now"] = ">#{flash["test_now"]}<"
      render :text => 'ignore me'
    end

21 22 23 24 25 26
    def set_session
      session['string'] = 'A wonder'
      session[:symbol] = 'it works'
      render :text => 'Success'
    end

27 28 29 30 31
    def reset_the_session
      reset_session
      render :text => 'ignore me'
    end

32
    def render_raw_post
33
      raise ActiveSupport::TestCase::Assertion, "#raw_post is blank" if request.raw_post.blank?
34 35 36
      render :text => request.raw_post
    end

37 38 39 40
    def render_body
      render :text => request.body.read
    end

41 42
    def test_params
      render :text => params.inspect
43 44
    end

45
    def test_uri
46
      render :text => request.fullpath
47
    end
48

49 50 51 52
    def test_query_string
      render :text => request.query_string
    end

A
Andrew White 已提交
53 54 55 56
    def test_protocol
      render :text => request.protocol
    end

57
    def test_html_output
58
      render :text => <<HTML
59 60
<html>
  <body>
61
    <a href="/"><img src="/images/button.png" /></a>
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    <div id="foo">
      <ul>
        <li class="item">hello</li>
        <li class="item">goodbye</li>
      </ul>
    </div>
    <div id="bar">
      <form action="/somewhere">
        Name: <input type="text" name="person[name]" id="person_name" />
      </form>
    </div>
  </body>
</html>
HTML
    end
J
Joshua Peek 已提交
77

78 79 80 81 82 83 84 85 86
    def test_xml_output
      response.content_type = "application/xml"
      render :text => <<XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <area>area is an empty tag in HTML, raising an error if not in xml mode</area>
</root>
XML
    end
87

88
    def test_only_one_param
89 90 91 92 93
      render :text => (params[:left] && params[:right]) ? "EEP, Both here!" : "OK"
    end

    def test_remote_addr
      render :text => (request.remote_addr || "not specified")
94
    end
95

96 97 98
    def test_file_upload
      render :text => params[:file].size
    end
99

100 101 102 103
    def test_send_file
      send_file(File.expand_path(__FILE__))
    end

104 105 106 107 108 109 110 111
    def redirect_to_same_controller
      redirect_to :controller => 'test', :action => 'test_uri', :id => 5
    end

    def redirect_to_different_controller
      redirect_to :controller => 'fail', :id => 5
    end

112
    def create
J
Jeremy Kemper 已提交
113
      head :created, :location => 'created resource'
114 115
    end

116 117 118 119 120
    def delete_cookie
      cookies.delete("foo")
      render :nothing => true
    end

121 122 123 124 125
    def test_assigns
      @foo = "foo"
      render :nothing => true
    end

126 127 128 129 130
    private

      def generate_url(opts)
        url_for(opts.merge(:action => "test_uri"))
      end
131 132 133
  end

  def setup
134
    super
135 136 137
    @controller = TestController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
C
Carl Lerche 已提交
138
    @request.env['PATH_INFO'] = nil
139 140 141 142 143
    @routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
      r.draw do
        match ':controller(/:action(/:id))'
      end
    end
144 145
  end

146 147 148 149 150 151 152 153 154 155 156
  class ViewAssignsController < ActionController::Base
    def test_assigns
      @foo = "foo"
      render :nothing => true
    end

    def view_assigns
      { "bar" => "bar" }
    end
  end

157
  def test_raw_post_handling
158
    params = ActiveSupport::OrderedHash[:page, {:name => 'page name'}, 'some key', 123]
159
    post :render_raw_post, params.dup
160

161 162 163 164
    assert_equal params.to_query, @response.body
  end

  def test_body_stream
165
    params = ActiveSupport::OrderedHash[:page, { :name => 'page name' }, 'some key', 123]
166

167
    post :render_body, params.dup
168 169

    assert_equal params.to_query, @response.body
170
  end
171

172 173
  def test_document_body_and_params_with_post
    post :test_params, :id => 1
174
    assert_equal("{\"id\"=>\"1\", \"controller\"=>\"test_case_test/test\", \"action\"=>\"test_params\"}", @response.body)
175
  end
176

177 178 179 180
  def test_document_body_with_post
    post :render_body, "document body"
    assert_equal "document body", @response.body
  end
181

182 183 184 185
  def test_document_body_with_put
    put :render_body, "document body"
    assert_equal "document body", @response.body
  end
186

187 188 189 190 191
  def test_head
    head :test_params
    assert_equal 200, @response.status
  end

192 193 194 195
  def test_head_params_as_sting
    assert_raise(NoMethodError) { head :test_params, "document body", :id => 10 }
  end

196 197
  def test_process_without_flash
    process :set_flash
198
    assert_equal '><', flash['test']
199 200
  end

201
  def test_process_with_flash
202
    process :set_flash, "GET", nil, nil, { "test" => "value" }
203
    assert_equal '>value<', flash['test']
204
  end
205

206
  def test_process_with_flash_now
207
    process :set_flash_now, "GET", nil, nil, { "test_now" => "value_now" }
208 209 210
    assert_equal '>value_now<', flash['test_now']
  end

211 212 213 214 215 216 217 218 219
  def test_process_with_session
    process :set_session
    assert_equal 'A wonder', session['string'], "A value stored in the session should be available by string key"
    assert_equal 'A wonder', session[:string], "Test session hash should allow indifferent access"
    assert_equal 'it works', session['symbol'], "Test session hash should allow indifferent access"
    assert_equal 'it works', session[:symbol], "Test session hash should allow indifferent access"
  end

  def test_process_with_session_arg
220
    process :no_op, "GET", nil, { 'string' => 'value1', :symbol => 'value2' }
221 222 223 224 225 226
    assert_equal 'value1', session['string']
    assert_equal 'value1', session[:string]
    assert_equal 'value2', session['symbol']
    assert_equal 'value2', session[:symbol]
  end

227 228 229 230 231 232 233 234 235 236 237 238
  def test_session_is_cleared_from_controller_after_reset_session
    process :set_session
    process :reset_the_session
    assert_equal Hash.new, @controller.session.to_hash
  end

  def test_session_is_cleared_from_request_after_reset_session
    process :set_session
    process :reset_the_session
    assert_equal Hash.new, @request.session.to_hash
  end

239 240 241 242 243 244
  def test_response_and_request_have_nice_accessors
    process :no_op
    assert_equal @response, response
    assert_equal @request, request
  end

245 246
  def test_process_with_request_uri_with_no_params
    process :test_uri
247
    assert_equal "/test_case_test/test/test_uri", @response.body
248 249 250
  end

  def test_process_with_request_uri_with_params
251
    process :test_uri, "GET", :id => 7
252
    assert_equal "/test_case_test/test/test_uri/7", @response.body
253
  end
254

A
Aaron Patterson 已提交
255 256 257
  def test_process_with_old_api
    assert_deprecated do
      process :test_uri, :id => 7
258
      assert_equal "/test_case_test/test/test_uri/7", @response.body
A
Aaron Patterson 已提交
259 260
    end
  end
261 262

  def test_process_with_request_uri_with_params_with_explicit_uri
C
Carl Lerche 已提交
263
    @request.env['PATH_INFO'] = "/explicit/uri"
264
    process :test_uri, "GET", :id => 7
265
    assert_equal "/explicit/uri", @response.body
266 267
  end

268
  def test_process_with_query_string
269
    process :test_query_string, "GET", :q => 'test'
270 271 272 273
    assert_equal "q=test", @response.body
  end

  def test_process_with_query_string_with_explicit_uri
C
Carl Lerche 已提交
274 275
    @request.env['PATH_INFO'] = '/explicit/uri'
    @request.env['QUERY_STRING'] = 'q=test?extra=question'
276 277 278 279
    process :test_query_string
    assert_equal "q=test?extra=question", @response.body
  end

280
  def test_multiple_calls
281
    process :test_only_one_param, "GET", :left => true
282
    assert_equal "OK", @response.body
283
    process :test_only_one_param, "GET", :right => true
284
    assert_equal "OK", @response.body
285
  end
286

287 288 289 290 291 292 293 294 295 296 297
  def test_assigns
    process :test_assigns
    # assigns can be accessed using assigns(key)
    # or assigns[key], where key is a string or
    # a symbol
    assert_equal "foo", assigns(:foo)
    assert_equal "foo", assigns("foo")
    assert_equal "foo", assigns[:foo]
    assert_equal "foo", assigns["foo"]
  end

298 299 300 301 302 303 304 305 306
  def test_view_assigns
    @controller = ViewAssignsController.new
    process :test_assigns
    assert_equal nil, assigns(:foo)
    assert_equal nil, assigns[:foo]
    assert_equal "bar", assigns(:bar)
    assert_equal "bar", assigns[:bar]
  end

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  def test_assert_tag_tag
    process :test_html_output

    # there is a 'form' tag
    assert_tag :tag => 'form'
    # there is not an 'hr' tag
    assert_no_tag :tag => 'hr'
  end

  def test_assert_tag_attributes
    process :test_html_output

    # there is a tag with an 'id' of 'bar'
    assert_tag :attributes => { :id => "bar" }
    # there is no tag with a 'name' of 'baz'
    assert_no_tag :attributes => { :name => "baz" }
  end

  def test_assert_tag_parent
    process :test_html_output

    # there is a tag with a parent 'form' tag
    assert_tag :parent => { :tag => "form" }
    # there is no tag with a parent of 'input'
    assert_no_tag :parent => { :tag => "input" }
  end

  def test_assert_tag_child
    process :test_html_output

    # there is a tag with a child 'input' tag
    assert_tag :child => { :tag => "input" }
339 340
    # there is no tag with a child 'strong' tag
    assert_no_tag :child => { :tag => "strong" }
341 342 343 344 345 346 347 348 349 350 351 352 353 354
  end

  def test_assert_tag_ancestor
    process :test_html_output

    # there is a 'li' tag with an ancestor having an id of 'foo'
    assert_tag :ancestor => { :attributes => { :id => "foo" } }, :tag => "li"
    # there is no tag of any kind with an ancestor having an href matching 'foo'
    assert_no_tag :ancestor => { :attributes => { :href => /foo/ } }
  end

  def test_assert_tag_descendant
    process :test_html_output

355
    # there is a tag with a descendant 'li' tag
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    assert_tag :descendant => { :tag => "li" }
    # there is no tag with a descendant 'html' tag
    assert_no_tag :descendant => { :tag => "html" }
  end

  def test_assert_tag_sibling
    process :test_html_output

    # there is a tag with a sibling of class 'item'
    assert_tag :sibling => { :attributes => { :class => "item" } }
    # there is no tag with a sibling 'ul' tag
    assert_no_tag :sibling => { :tag => "ul" }
  end

  def test_assert_tag_after
    process :test_html_output

    # there is a tag following a sibling 'div' tag
    assert_tag :after => { :tag => "div" }
    # there is no tag following a sibling tag with id 'bar'
    assert_no_tag :after => { :attributes => { :id => "bar" } }
  end

  def test_assert_tag_before
    process :test_html_output

382
    # there is a tag preceding a tag with id 'bar'
383
    assert_tag :before => { :attributes => { :id => "bar" } }
384
    # there is no tag preceding a 'form' tag
385 386 387 388 389 390 391 392
    assert_no_tag :before => { :tag => "form" }
  end

  def test_assert_tag_children_count
    process :test_html_output

    # there is a tag with 2 children
    assert_tag :children => { :count => 2 }
393 394
    # in particular, there is a <ul> tag with two children (a nameless pair of <li>s)
    assert_tag :tag => 'ul', :children => { :count => 2 }
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    # there is no tag with 4 children
    assert_no_tag :children => { :count => 4 }
  end

  def test_assert_tag_children_less_than
    process :test_html_output

    # there is a tag with less than 5 children
    assert_tag :children => { :less_than => 5 }
    # there is no 'ul' tag with less than 2 children
    assert_no_tag :children => { :less_than => 2 }, :tag => "ul"
  end

  def test_assert_tag_children_greater_than
    process :test_html_output

    # there is a 'body' tag with more than 1 children
    assert_tag :children => { :greater_than => 1 }, :tag => "body"
    # there is no tag with more than 10 children
    assert_no_tag :children => { :greater_than => 10 }
  end

  def test_assert_tag_children_only
    process :test_html_output

    # there is a tag containing only one child with an id of 'foo'
421
    assert_tag :children => { :count => 1,
422 423 424 425 426 427 428 429 430
                              :only => { :attributes => { :id => "foo" } } }
    # there is no tag containing only one 'li' child
    assert_no_tag :children => { :count => 1, :only => { :tag => "li" } }
  end

  def test_assert_tag_content
    process :test_html_output

    # the output contains the string "Name"
431
    assert_tag :content => /Name/
432
    # the output does not contain the string "test"
433
    assert_no_tag :content => /test/
434
  end
435

436
  def test_assert_tag_multiple
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    process :test_html_output

    # there is a 'div', id='bar', with an immediate child whose 'action'
    # attribute matches the regexp /somewhere/.
    assert_tag :tag => "div", :attributes => { :id => "bar" },
               :child => { :attributes => { :action => /somewhere/ } }

    # there is no 'div', id='foo', with a 'ul' child with more than
    # 2 "li" children.
    assert_no_tag :tag => "div", :attributes => { :id => "foo" },
                  :child => {
                    :tag => "ul",
                    :children => { :greater_than => 2,
                                   :only => { :tag => "li" } } }
  end
452

453 454
  def test_assert_tag_children_without_content
    process :test_html_output
455

456 457 458 459 460 461 462 463 464 465 466 467
    # there is a form tag with an 'input' child which is a self closing tag
    assert_tag :tag => "form",
      :children => { :count => 1,
        :only => { :tag => "input" } }

    # the body tag has an 'a' child which in turn has an 'img' child
    assert_tag :tag => "body",
      :children => { :count => 1,
        :only => { :tag => "a",
          :children => { :count => 1,
            :only => { :tag => "img" } } } }
  end
J
Joshua Peek 已提交
468

469 470 471 472 473 474 475 476 477 478 479 480 481
  def test_should_not_impose_childless_html_tags_in_xml
    process :test_xml_output

    begin
      $stderr = StringIO.new
      assert_select 'area' #This will cause a warning if content is processed as HTML
      $stderr.rewind && err = $stderr.read
    ensure
      $stderr = STDERR
    end

    assert err.empty?
  end
482

483 484 485 486 487 488 489 490 491 492
  def test_assert_tag_attribute_matching
    @response.body = '<input type="text" name="my_name">'
    assert_tag :tag => 'input',
                 :attributes => { :name => /my/, :type => 'text' }
    assert_no_tag :tag => 'input',
                 :attributes => { :name => 'my', :type => 'text' }
    assert_no_tag :tag => 'input',
                 :attributes => { :name => /^my$/, :type => 'text' }
  end

493 494 495 496 497 498 499
  def test_assert_tag_content_matching
    @response.body = "<p>hello world</p>"
    assert_tag :tag => "p", :content => "hello world"
    assert_tag :tag => "p", :content => /hello/
    assert_no_tag :tag => "p", :content => "hello"
  end

500
  def test_assert_generates
501
    assert_generates 'controller/action/5', :controller => 'controller', :action => 'action', :id => '5'
502 503 504 505
    assert_generates 'controller/action/7', {:id => "7"}, {:controller => "controller", :action => "action"}
    assert_generates 'controller/action/5', {:controller => "controller", :action => "action", :id => "5", :name => "bob"}, {}, {:name => "bob"}
    assert_generates 'controller/action/7', {:id => "7", :name => "bob"}, {:controller => "controller", :action => "action"}, {:name => "bob"}
    assert_generates 'controller/action/7', {:id => "7"}, {:controller => "controller", :action => "action", :name => "bob"}, {}
506
  end
507

508 509 510 511
  def test_assert_routing
    assert_routing 'content', :controller => 'content', :action => 'index'
  end

512 513
  def test_assert_routing_with_method
    with_routing do |set|
514
      set.draw { resources(:content) }
515 516 517 518
      assert_routing({ :method => 'post', :path => 'content' }, { :controller => 'content', :action => 'create' })
    end
  end

519
  def test_assert_routing_in_module
520 521 522 523 524 525 526 527 528
    with_routing do |set|
      set.draw do
        namespace :admin do
          match 'user' => 'user#index'
        end
      end

      assert_routing 'admin/user', :controller => 'admin/user', :action => 'index'
    end
529
  end
530

531 532
  def test_assert_routing_with_glob
    with_routing do |set|
533
      set.draw { match('*path' => "pages#show") }
534 535 536
      assert_routing('/company/about', { :controller => 'pages', :action => 'show', :path => 'company/about' })
    end
  end
537

538 539 540 541
  def test_params_passing
    get :test_params, :page => {:name => "Page name", :month => '4', :year => '2004', :day => '6'}
    parsed_params = eval(@response.body)
    assert_equal(
542
      {'controller' => 'test_case_test/test', 'action' => 'test_params',
543
       'page' => {'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6'}},
544 545 546 547 548 549 550 551
      parsed_params
    )
  end

  def test_params_passing_with_fixnums
    get :test_params, :page => {:name => "Page name", :month => 4, :year => 2004, :day => 6}
    parsed_params = eval(@response.body)
    assert_equal(
552
      {'controller' => 'test_case_test/test', 'action' => 'test_params',
553
       'page' => {'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6'}},
554 555 556 557
      parsed_params
    )
  end

558 559 560 561 562 563
  def test_params_passing_with_frozen_values
    assert_nothing_raised do
      get :test_params, :frozen => 'icy'.freeze, :frozens => ['icy'.freeze].freeze
    end
    parsed_params = eval(@response.body)
    assert_equal(
564
      {'controller' => 'test_case_test/test', 'action' => 'test_params',
565 566 567 568 569
       'frozen' => 'icy', 'frozens' => ['icy']},
      parsed_params
    )
  end

570 571 572 573 574 575
  def test_params_passing_doesnt_modify_in_place
    page = {:name => "Page name", :month => 4, :year => 2004, :day => 6}
    get :test_params, :page => page
    assert_equal 2004, page[:year]
  end

576
  def test_id_converted_to_string
577
    get :test_params, :id => 20, :foo => Object.new
578 579 580 581 582
    assert_kind_of String, @request.path_parameters['id']
  end

  def test_array_path_parameter_handled_properly
    with_routing do |set|
583
      set.draw do
584
        match 'file/*path', :to => 'test_case_test/test#test_params'
J
Joshua Peek 已提交
585
        match ':controller/:action'
586
      end
587

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
      get :test_params, :path => ['hello', 'world']
      assert_equal ['hello', 'world'], @request.path_parameters['path']
      assert_equal 'hello/world', @request.path_parameters['path'].to_s
    end
  end

  def test_assert_realistic_path_parameters
    get :test_params, :id => 20, :foo => Object.new

    # All elements of path_parameters should use string keys
    @request.path_parameters.keys.each do |key|
      assert_kind_of String, key
    end
  end

  def test_with_routing_places_routes_back
J
Joshua Peek 已提交
604 605
    assert @routes
    routes_id = @routes.object_id
606

607 608 609 610
    begin
      with_routing { raise 'fail' }
      fail 'Should not be here.'
    rescue RuntimeError
611
    end
612

J
Joshua Peek 已提交
613 614
    assert @routes
    assert_equal routes_id, @routes.object_id
615
  end
616 617 618 619 620 621 622 623 624

  def test_remote_addr
    get :test_remote_addr
    assert_equal "0.0.0.0", @response.body

    @request.remote_addr = "192.0.0.1"
    get :test_remote_addr
    assert_equal "192.0.0.1", @response.body
  end
625

626 627 628 629 630
  def test_header_properly_reset_after_remote_http_request
    xhr :get, :test_params
    assert_nil @request.env['HTTP_X_REQUESTED_WITH']
  end

J
Joshua Peek 已提交
631
  def test_header_properly_reset_after_get_request
632 633 634 635 636
    get :test_params
    @request.recycle!
    assert_nil @request.instance_variable_get("@request_method")
  end

637 638 639 640 641
  def test_params_reset_after_post_request
    post :no_op, :foo => "bar"
    assert_equal "bar", @request.params[:foo]
    @request.recycle!
    post :no_op
642
    assert_blank @request.params[:foo]
643 644
  end

645 646 647 648 649 650 651 652
  def test_symbolized_path_params_reset_after_request
    get :test_params, :id => "foo"
    assert_equal "foo", @request.symbolized_path_parameters[:id]
    @request.recycle!
    get :test_params
    assert_nil @request.symbolized_path_parameters[:id]
  end

A
Andrew White 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665
  def test_request_protocol_is_reset_after_request
    get :test_protocol
    assert_equal "http://", @response.body

    @request.env["HTTPS"] = "on"
    get :test_protocol
    assert_equal "https://", @response.body

    @request.env.delete("HTTPS")
    get :test_protocol
    assert_equal "http://", @response.body
  end

666
  def test_should_have_knowledge_of_client_side_cookie_state_even_if_they_are_not_set
667
    cookies['foo'] = 'bar'
668 669 670 671 672
    get :no_op
    assert_equal 'bar', cookies['foo']
  end

  def test_should_detect_if_cookie_is_deleted
673
    cookies['foo'] = 'bar'
674 675 676 677
    get :delete_cookie
    assert_nil cookies['foo']
  end

678 679 680 681 682 683 684 685
  %w(controller response request).each do |variable|
    %w(get post put delete head process).each do |method|
      define_method("test_#{variable}_missing_for_#{method}_raises_error") do
        remove_instance_variable "@#{variable}"
        begin
          send(method, :test_remote_addr)
          assert false, "expected RuntimeError, got nothing"
        rescue RuntimeError => error
686
          assert_match(%r{@#{variable} is nil}, error.message)
687 688 689 690 691 692
        rescue => error
          assert false, "expected RuntimeError, got #{error.class}"
        end
      end
    end
  end
693

694
  FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart'
695

696 697
  READ_BINARY = 'rb:binary'
  READ_PLAIN = 'r:binary'
698

699 700 701 702
  def test_test_uploaded_file
    filename = 'mona_lisa.jpg'
    path = "#{FILES_DIR}/#{filename}"
    content_type = 'image/png'
703
    expected = File.read(path)
704
    expected.force_encoding(Encoding::BINARY)
705

J
Joshua Peek 已提交
706
    file = Rack::Test::UploadedFile.new(path, content_type)
707 708 709
    assert_equal filename, file.original_filename
    assert_equal content_type, file.content_type
    assert_equal file.path, file.local_path
710
    assert_equal expected, file.read
711 712 713 714 715

    new_content_type = "new content_type"
    file.content_type = new_content_type
    assert_equal new_content_type, file.content_type

716
  end
J
Joshua Peek 已提交
717

718
  def test_fixture_path_is_accessed_from_self_instead_of_active_support_test_case
719
    TestCaseTest.stubs(:fixture_path).returns(FILES_DIR)
720 721 722 723 724

    uploaded_file = fixture_file_upload('/mona_lisa.jpg', 'image/png')
    assert_equal File.open("#{FILES_DIR}/mona_lisa.jpg", READ_PLAIN).read, uploaded_file.read
  end

725 726 727 728
  def test_test_uploaded_file_with_binary
    filename = 'mona_lisa.jpg'
    path = "#{FILES_DIR}/#{filename}"
    content_type = 'image/png'
J
Joshua Peek 已提交
729

J
Joshua Peek 已提交
730
    binary_uploaded_file = Rack::Test::UploadedFile.new(path, content_type, :binary)
731
    assert_equal File.open(path, READ_BINARY).read, binary_uploaded_file.read
J
Joshua Peek 已提交
732

J
Joshua Peek 已提交
733
    plain_uploaded_file = Rack::Test::UploadedFile.new(path, content_type)
734
    assert_equal File.open(path, READ_PLAIN).read, plain_uploaded_file.read
735 736 737 738 739 740
  end

  def test_fixture_file_upload_with_binary
    filename = 'mona_lisa.jpg'
    path = "#{FILES_DIR}/#{filename}"
    content_type = 'image/jpg'
J
Joshua Peek 已提交
741

742
    binary_file_upload = fixture_file_upload(path, content_type, :binary)
743
    assert_equal File.open(path, READ_BINARY).read, binary_file_upload.read
J
Joshua Peek 已提交
744

745
    plain_file_upload = fixture_file_upload(path, content_type)
746
    assert_equal File.open(path, READ_PLAIN).read, plain_file_upload.read
747
  end
748

749 750
  def test_fixture_file_upload
    post :test_file_upload, :file => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")
J
Jeremy Kemper 已提交
751
    assert_equal '159528', @response.body
752
  end
753

754
  def test_test_uploaded_file_exception_when_file_doesnt_exist
J
Joshua Peek 已提交
755
    assert_raise(RuntimeError) { Rack::Test::UploadedFile.new('non_existent_file') }
756
  end
757

758 759 760 761 762 763 764 765 766
  def test_redirect_url_only_cares_about_location_header
    get :create
    assert_response :created

    # Redirect url doesn't care that it wasn't a :redirect response.
    assert_equal 'created resource', @response.redirect_url
    assert_equal @response.redirect_url, redirect_to_url

    # Must be a :redirect response.
767
    assert_raise(ActiveSupport::TestCase::Assertion) do
768 769 770
      assert_redirected_to 'created resource'
    end
  end
771
end
772

773
class InferringClassNameTest < ActionController::TestCase
774 775 776 777 778
  def test_determine_controller_class
    assert_equal ContentController, determine_class("ContentControllerTest")
  end

  def test_determine_controller_class_with_nonsense_name
779
    assert_nil determine_class("HelloGoodBye")
780 781 782
  end

  def test_determine_controller_class_with_sensible_name_where_no_controller_exists
783
    assert_nil determine_class("NoControllerWithThisNameTest")
784 785 786 787 788 789 790 791 792 793
  end

  private
    def determine_class(name)
      ActionController::TestCase.determine_default_controller_class(name)
    end
end

class CrazyNameTest < ActionController::TestCase
  tests ContentController
794

795 796 797 798
  def test_controller_class_can_be_set_manually_not_just_inferred
    assert_equal ContentController, self.class.controller_class
  end
end
799

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
class CrazySymbolNameTest < ActionController::TestCase
  tests :content

  def test_set_controller_class_using_symbol
    assert_equal ContentController, self.class.controller_class
  end
end

class CrazyStringNameTest < ActionController::TestCase
  tests 'content'

  def test_set_controller_class_using_string
    assert_equal ContentController, self.class.controller_class
  end
end

816 817
class NamedRoutesControllerTest < ActionController::TestCase
  tests ContentController
J
Joshua Peek 已提交
818

819 820
  def test_should_be_able_to_use_named_routes_before_a_request_is_done
    with_routing do |set|
821
      set.draw { resources :contents }
822
      assert_equal 'http://test.host/contents/new', new_content_url
823
      assert_equal 'http://test.host/contents/1', content_url(:id => 1)
824 825 826
    end
  end
end