shuber / proxy

Allows rails applications to respond to multiple hosts/domains and proxied requests

This URL has Read+Write access

proxy / test / base_test.rb
100644 174 lines (145 sloc) 6.443 kb
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
require File.dirname(__FILE__) + '/init'
 
class BaseTest < Test::Unit::TestCase
 
  def setup
    @controller = TestController.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
    @request.host = 'example.com'
    ActionController::UrlWriter.default_url_options[:host] = nil
    ActionController::Base.relative_url_root = '/app'
  end
  
  def test_should_get_normal_action
    get :normal_action
    assert_equal "http://#{@request.host}/normal_action", @response.body
  end
  
  def test_should_set_a_forwarded_host_as_a_default_host
    add_forwarded_host_headers
    get :normal_action
    assert_equal "http://domain.com/app/normal_action", @response.body
  end
  
  def test_should_set_forwarded_host_in_a_named_route
    add_forwarded_host_headers
    get :named_route_action
    assert_equal "http://domain.com/app/normal_action", @response.body
  end
  
  def test_should_restore_the_original_default_host
    ::ActionController::UrlWriter.default_url_options[:host] = 'some-other-domain.com'
    add_forwarded_host_headers
    get :normal_action
    assert_equal "http://domain.com/app/normal_action", @response.body
  end
  
  def test_url_generators_in_views_should_use_forwarded_host
    add_forwarded_host_headers
    get :view_action
    assert_equal "/app/normal_action, http://domain.com/app/normal_action, http://domain.com/app/normal_action, /app/normal_action", @response.body
  end
  
  def test_asset_tag_helpers_should_not_use_forwarded_host
    add_forwarded_host_headers
    get :asset_action
    assert_nil(/domain\.com/.match(@response.body))
  end
  
  def test_should_set_the_session_domain_with_a_forwarded_host
    add_forwarded_host_headers
    get :session_action
    assert_equal '.domain.com', @response.body
  end
  
  def test_should_restore_original_session_domain
    ::ActionController::Base.session_options[:session_domain] = '.example.com'
    add_forwarded_host_headers
    get :session_action
    assert_equal '.domain.com', @response.body
    assert_equal '.example.com', ::ActionController::Base.session_options[:session_domain]
  end
  
  def test_should_restore_original_session_if_exception_is_raised
    ::ActionController::Base.session_options[:session_domain] = '.example.com'
    add_forwarded_host_headers
    assert_raises(RuntimeError) { get :exception_action }
    assert_equal '.example.com', ::ActionController::Base.session_options[:session_domain]
  end
  
  def test_should_restore_original_session_if_redirected
    ::ActionController::Base.session_options[:session_domain] = '.example.com'
    add_forwarded_host_headers
    get :redirect_action
    assert_equal '.example.com', ::ActionController::Base.session_options[:session_domain]
  end
  
  def test_should_restore_original_host_if_exception_is_raised
    ::ActionController::UrlWriter.default_url_options[:host] = 'some-other-domain.com'
    add_forwarded_host_headers
    assert_raises(RuntimeError) { get :exception_action }
    assert_equal 'some-other-domain.com', ::ActionController::UrlWriter.default_url_options[:host]
  end
  
  def test_should_restore_original_host_if_redirected
    ::ActionController::UrlWriter.default_url_options[:host] = 'some-other-domain.com'
    add_forwarded_host_headers
    get :redirect_action
    assert_equal 'some-other-domain.com', ::ActionController::UrlWriter.default_url_options[:host]
  end
  
  def test_should_get_normal_action
    get :normal_action
    assert_equal "http://#{@request.host}/app/normal_action", @response.body
  end
  
  def test_should_swap_relative_url_root
    add_forwarded_uri_headers
    get :normal_action
    assert_equal "http://#{@request.host}/test/ing/normal_action", @response.body
    assert_equal '/app', ActionController::Base.relative_url_root
  end
  
  def test_should_set_proxy_relative_url_root
    add_forwarded_uri_headers
    get :normal_action
    assert_equal '/test/ing', ActionController::Base.proxy_relative_url_root
  end
  
  def test_should_set_proxy_relative_url_root_in_a_named_route
    @request.env['HTTP_X_FORWARDED_URI'] = '/test/ing/named_route_action'
    @request.env['PATH_INFO'] = '/named_route_action'
    get :named_route_action
    assert_equal "http://#{@request.host}/test/ing/normal_action", @response.body
  end
  
  def test_should_set_original_relative_url_root
    add_forwarded_uri_headers
    get :normal_action
    assert_equal '/app', ActionController::Base.original_relative_url_root
  end
  
  def test_should_restore_relative_url_root_if_exception_is_raised
    add_forwarded_uri_headers
    assert_raises(RuntimeError) { get :exception_action }
    assert_equal '/app', ActionController::Base.relative_url_root
  end
  
  def test_should_restore_relative_url_root_if_exception_is_raised
    add_forwarded_uri_headers
    get :redirect_action
    assert_equal '/app', ActionController::Base.relative_url_root
  end
  
  def test_url_generators_in_views_should_use_forwarded_uri
    @request.env['HTTP_X_FORWARDED_URI'] = '/test/ing/view_action'
    @request.env['PATH_INFO'] = '/view_action'
    get :view_action
    assert_equal "/test/ing/normal_action, http://example.com/test/ing/normal_action, /test/ing/normal_action, /test/ing/normal_action", @response.body
  end
  
  def test_asset_tag_helpers_should_use_forwarded_uri
    @request.env['HTTP_X_FORWARDED_URI'] = '/test/ing/asset_action'
    @request.env['PATH_INFO'] = '/asset_action'
    get :asset_action
    assert !@response.body.scan(/img[^>]+src\="\/test\/ing\/images\/test\.gif"/).empty?
    assert !@response.body.scan(/script[^>]+src\="\/test\/ing\/javascripts\/test\.js"/).empty?
    assert !@response.body.scan(/link[^>]+href\="\/test\/ing\/stylesheets\/test\.css"/).empty?
  end
  
  def test_should_use_forwarded_host_in_a_redirect
    add_forwarded_host_headers
    get :redirect_action
    assert_redirected_to 'http://domain.com/app/normal_action'
  end
  
  def test_should_use_forwarded_host_in_a_redirect_with_named_routes
    add_forwarded_host_headers
    get :redirect_with_named_route_action
    assert_redirected_to 'http://domain.com/app/normal_action'
  end
  
  protected
  
    def add_forwarded_host_headers
      @request.env['HTTP_X_FORWARDED_HOST'] = 'domain.com'
    end
  
    def add_forwarded_uri_headers
      @request.env['HTTP_X_FORWARDED_URI'] = '/test/ing/normal_action'
      @request.env['PATH_INFO'] = '/normal_action'
    end
  
end