Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / redirect_test.rb
1 require 'abstract_unit'
2
3 class WorkshopsController < ActionController::Base
4 end
5
6 class Workshop
7 attr_accessor :id, :new_record
8
9 def initialize(id, new_record)
10 @id, @new_record = id, new_record
11 end
12
13 def new_record?
14 @new_record
15 end
16
17 def to_s
18 id.to_s
19 end
20 end
21
22 class RedirectController < ActionController::Base
23 def simple_redirect
24 redirect_to :action => "hello_world"
25 end
26
27 def redirect_with_status
28 redirect_to({:action => "hello_world", :status => 301})
29 end
30
31 def redirect_with_status_hash
32 redirect_to({:action => "hello_world"}, {:status => 301})
33 end
34
35 def url_redirect_with_status
36 redirect_to("http://www.example.com", :status => :moved_permanently)
37 end
38
39 def url_redirect_with_status_hash
40 redirect_to("http://www.example.com", {:status => 301})
41 end
42
43 def relative_url_redirect_with_status
44 redirect_to("/things/stuff", :status => :found)
45 end
46
47 def relative_url_redirect_with_status_hash
48 redirect_to("/things/stuff", {:status => 301})
49 end
50
51 def redirect_to_back_with_status
52 redirect_to :back, :status => 307
53 end
54
55 def host_redirect
56 redirect_to :action => "other_host", :only_path => false, :host => 'other.test.host'
57 end
58
59 def module_redirect
60 redirect_to :controller => 'module_test/module_redirect', :action => "hello_world"
61 end
62
63 def redirect_with_assigns
64 @hello = "world"
65 redirect_to :action => "hello_world"
66 end
67
68 def redirect_to_url
69 redirect_to "http://www.rubyonrails.org/"
70 end
71
72 def redirect_to_url_with_unescaped_query_string
73 redirect_to "http://dev.rubyonrails.org/query?status=new"
74 end
75
76 def redirect_to_url_with_complex_scheme
77 redirect_to "x-test+scheme.complex:redirect"
78 end
79
80 def redirect_to_back
81 redirect_to :back
82 end
83
84 def redirect_to_existing_record
85 redirect_to Workshop.new(5, false)
86 end
87
88 def redirect_to_new_record
89 redirect_to Workshop.new(5, true)
90 end
91
92 def redirect_to_nil
93 redirect_to nil
94 end
95
96 def rescue_errors(e) raise e end
97
98 def rescue_action(e) raise end
99
100 protected
101 def dashbord_url(id, message)
102 url_for :action => "dashboard", :params => { "id" => id, "message" => message }
103 end
104 end
105
106 class RedirectTest < Test::Unit::TestCase
107 def setup
108 @controller = RedirectController.new
109 @request = ActionController::TestRequest.new
110 @response = ActionController::TestResponse.new
111 end
112
113 def test_simple_redirect
114 get :simple_redirect
115 assert_response :redirect
116 assert_equal "http://test.host/redirect/hello_world", redirect_to_url
117 end
118
119 def test_redirect_with_no_status
120 get :simple_redirect
121 assert_response 302
122 assert_equal "http://test.host/redirect/hello_world", redirect_to_url
123 end
124
125 def test_redirect_with_status
126 get :redirect_with_status
127 assert_response 301
128 assert_equal "http://test.host/redirect/hello_world", redirect_to_url
129 end
130
131 def test_redirect_with_status_hash
132 get :redirect_with_status_hash
133 assert_response 301
134 assert_equal "http://test.host/redirect/hello_world", redirect_to_url
135 end
136
137 def test_url_redirect_with_status
138 get :url_redirect_with_status
139 assert_response 301
140 assert_equal "http://www.example.com", redirect_to_url
141 end
142
143 def test_url_redirect_with_status_hash
144 get :url_redirect_with_status_hash
145 assert_response 301
146 assert_equal "http://www.example.com", redirect_to_url
147 end
148
149
150 def test_relative_url_redirect_with_status
151 get :relative_url_redirect_with_status
152 assert_response 302
153 assert_equal "http://test.host/things/stuff", redirect_to_url
154 end
155
156 def test_relative_url_redirect_with_status_hash
157 get :relative_url_redirect_with_status_hash
158 assert_response 301
159 assert_equal "http://test.host/things/stuff", redirect_to_url
160 end
161
162 def test_redirect_to_back_with_status
163 @request.env["HTTP_REFERER"] = "http://www.example.com/coming/from"
164 get :redirect_to_back_with_status
165 assert_response 307
166 assert_equal "http://www.example.com/coming/from", redirect_to_url
167 end
168
169 def test_simple_redirect_using_options
170 get :host_redirect
171 assert_response :redirect
172 assert_redirected_to :action => "other_host", :only_path => false, :host => 'other.test.host'
173 end
174
175 def test_module_redirect
176 get :module_redirect
177 assert_response :redirect
178 assert_redirected_to "http://test.host/module_test/module_redirect/hello_world"
179 end
180
181 def test_module_redirect_using_options
182 get :module_redirect
183 assert_response :redirect
184 assert_redirected_to :controller => 'module_test/module_redirect', :action => 'hello_world'
185 end
186
187 def test_redirect_with_assigns
188 get :redirect_with_assigns
189 assert_response :redirect
190 assert_equal "world", assigns["hello"]
191 end
192
193 def test_redirect_to_url
194 get :redirect_to_url
195 assert_response :redirect
196 assert_redirected_to "http://www.rubyonrails.org/"
197 end
198
199 def test_redirect_to_url_with_unescaped_query_string
200 get :redirect_to_url_with_unescaped_query_string
201 assert_response :redirect
202 assert_redirected_to "http://dev.rubyonrails.org/query?status=new"
203 end
204
205 def test_redirect_to_url_with_complex_scheme
206 get :redirect_to_url_with_complex_scheme
207 assert_response :redirect
208 assert_equal "x-test+scheme.complex:redirect", redirect_to_url
209 end
210
211 def test_redirect_to_back
212 @request.env["HTTP_REFERER"] = "http://www.example.com/coming/from"
213 get :redirect_to_back
214 assert_response :redirect
215 assert_equal "http://www.example.com/coming/from", redirect_to_url
216 end
217
218 def test_redirect_to_back_with_no_referer
219 assert_raises(ActionController::RedirectBackError) {
220 @request.env["HTTP_REFERER"] = nil
221 get :redirect_to_back
222 }
223 end
224
225 def test_redirect_to_record
226 ActionController::Routing::Routes.draw do |map|
227 map.resources :workshops
228 map.connect ':controller/:action/:id'
229 end
230
231 get :redirect_to_existing_record
232 assert_equal "http://test.host/workshops/5", redirect_to_url
233 assert_redirected_to Workshop.new(5, false)
234
235 get :redirect_to_new_record
236 assert_equal "http://test.host/workshops", redirect_to_url
237 assert_redirected_to Workshop.new(5, true)
238 end
239
240 def test_redirect_with_partial_params
241 get :module_redirect
242 assert_redirected_to :action => 'hello_world'
243 end
244
245 def test_redirect_to_nil
246 assert_raises(ActionController::ActionControllerError) do
247 get :redirect_to_nil
248 end
249 end
250 end
251
252 module ModuleTest
253 class ModuleRedirectController < ::RedirectController
254 def module_redirect
255 redirect_to :controller => '/redirect', :action => "hello_world"
256 end
257 end
258
259 class ModuleRedirectTest < Test::Unit::TestCase
260 def setup
261 @controller = ModuleRedirectController.new
262 @request = ActionController::TestRequest.new
263 @response = ActionController::TestResponse.new
264 end
265
266 def test_simple_redirect
267 get :simple_redirect
268 assert_response :redirect
269 assert_equal "http://test.host/module_test/module_redirect/hello_world", redirect_to_url
270 end
271
272 def test_simple_redirect_using_options
273 get :host_redirect
274 assert_response :redirect
275 assert_redirected_to :action => "other_host", :only_path => false, :host => 'other.test.host'
276 end
277
278 def test_module_redirect
279 get :module_redirect
280 assert_response :redirect
281 assert_equal "http://test.host/redirect/hello_world", redirect_to_url
282 end
283
284 def test_module_redirect_using_options
285 get :module_redirect
286 assert_response :redirect
287 assert_redirected_to :controller => '/redirect', :action => "hello_world"
288 end
289 end
290 end