aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/components_test.rb
blob: c45a59bdbe5496a165b61d496ee4b91dd08e75f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require File.dirname(__FILE__) + '/../abstract_unit'

class CallerController < ActionController::Base
  def calling_from_controller
    render_component(:controller => "callee", :action => "being_called")
  end

  def calling_from_controller_with_params
    render_component(:controller => "callee", :action => "being_called", :params => { "name" => "David" })
  end

  def calling_from_controller_with_different_status_code
    render_component(:controller => "callee", :action => "blowing_up")
  end

  def calling_from_template
    render_template "Ring, ring: <%= render_component(:controller => 'callee', :action => 'being_called') %>"
  end

  def internal_caller
    render_template "Are you there? <%= render_component(:action => 'internal_callee') %>"
  end

  def internal_callee
    render_text "Yes, ma'am"
  end

  def set_flash
    render_component(:controller => "callee", :action => "set_flash")
  end

  def use_flash
    render_component(:controller => "callee", :action => "use_flash")
  end

  def calling_redirected
    render_component(:controller => "callee", :action => "redirected")
  end

  def calling_redirected_as_string
    render_template "<%= render_component(:controller => 'callee', :action => 'redirected') %>"
  end

  def rescue_action(e) raise end
end

class CalleeController < ActionController::Base
  def being_called
    render_text "#{params[:name] || "Lady"} of the House, speaking"
  end

  def blowing_up
    render_text "It's game over, man, just game over, man!", "500 Internal Server Error"
  end

  def set_flash
    flash[:notice] = 'My stoney baby'
    render :text => 'flash is set'
  end

  def use_flash
    render :text => flash[:notice] || 'no flash'
  end

  def redirected
    redirect_to :controller => "callee", :action => "being_called"
  end

  def rescue_action(e) raise end
end

class ComponentsTest < Test::Unit::TestCase
  def setup
    @controller = CallerController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

  def test_calling_from_controller
    get :calling_from_controller
    assert_equal "Lady of the House, speaking", @response.body
  end

  def test_calling_from_controller_with_params
    get :calling_from_controller_with_params
    assert_equal "David of the House, speaking", @response.body
  end

  def test_calling_from_controller_with_different_status_code
    get :calling_from_controller_with_different_status_code
    assert_equal 500, @response.response_code
  end

  def test_calling_from_template
    get :calling_from_template
    assert_equal "Ring, ring: Lady of the House, speaking", @response.body
  end

  def test_etag_is_set_for_parent_template_when_calling_from_template
    get :calling_from_template
    expected_etag = etag_for("Ring, ring: Lady of the House, speaking")
    assert_equal expected_etag, @response.headers['ETag']
  end

  def test_internal_calling
    get :internal_caller
    assert_equal "Are you there? Yes, ma'am", @response.body
  end

  def test_flash
    get :set_flash
    assert_equal 'My stoney baby', flash[:notice]
    get :use_flash
    assert_equal 'My stoney baby', @response.body
    get :use_flash
    assert_equal 'no flash', @response.body
  end

  def test_component_redirect_redirects
    get :calling_redirected

    assert_redirected_to :action => "being_called"
  end

  def test_component_multiple_redirect_redirects
    test_component_redirect_redirects
    test_internal_calling
  end

  def test_component_as_string_redirect_renders_redirecte_action
    get :calling_redirected_as_string

    assert_equal "Lady of the House, speaking", @response.body
  end

  protected
    def etag_for(text)
      %("#{Digest::MD5.hexdigest(text)}")
    end
end