1 require 'abstract_unit'
3 class WorkshopsController
< ActionController
::Base
7 attr_accessor
:id, :new_record
9 def initialize(id
, new_record
)
10 @id, @new_record = id
, new_record
22 class RedirectController
< ActionController
::Base
24 redirect_to
:action => "hello_world"
27 def redirect_with_status
28 redirect_to({:action => "hello_world", :status => 301})
31 def redirect_with_status_hash
32 redirect_to({:action => "hello_world"}, {:status => 301})
35 def url_redirect_with_status
36 redirect_to("http://www.example.com", :status => :moved_permanently)
39 def url_redirect_with_status_hash
40 redirect_to("http://www.example.com", {:status => 301})
43 def relative_url_redirect_with_status
44 redirect_to("/things/stuff", :status => :found)
47 def relative_url_redirect_with_status_hash
48 redirect_to("/things/stuff", {:status => 301})
51 def redirect_to_back_with_status
52 redirect_to
:back, :status => 307
56 redirect_to
:action => "other_host", :only_path => false, :host => 'other.test.host'
60 redirect_to
:controller => 'module_test/module_redirect', :action => "hello_world"
63 def redirect_with_assigns
65 redirect_to
:action => "hello_world"
69 redirect_to
"http://www.rubyonrails.org/"
72 def redirect_to_url_with_unescaped_query_string
73 redirect_to
"http://dev.rubyonrails.org/query?status=new"
76 def redirect_to_url_with_complex_scheme
77 redirect_to
"x-test+scheme.complex:redirect"
84 def redirect_to_existing_record
85 redirect_to Workshop
.new(5, false)
88 def redirect_to_new_record
89 redirect_to Workshop
.new(5, true)
96 def rescue_errors(e
) raise e
end
98 def rescue_action(e
) raise end
101 def dashbord_url(id
, message
)
102 url_for
:action => "dashboard", :params => { "id" => id
, "message" => message
}
106 class RedirectTest
< ActionController
::TestCase
107 tests RedirectController
109 def test_simple_redirect
111 assert_response
:redirect
112 assert_equal
"http://test.host/redirect/hello_world", redirect_to_url
115 def test_redirect_with_no_status
118 assert_equal
"http://test.host/redirect/hello_world", redirect_to_url
121 def test_redirect_with_status
122 get
:redirect_with_status
124 assert_equal
"http://test.host/redirect/hello_world", redirect_to_url
127 def test_redirect_with_status_hash
128 get
:redirect_with_status_hash
130 assert_equal
"http://test.host/redirect/hello_world", redirect_to_url
133 def test_url_redirect_with_status
134 get
:url_redirect_with_status
136 assert_equal
"http://www.example.com", redirect_to_url
139 def test_url_redirect_with_status_hash
140 get
:url_redirect_with_status_hash
142 assert_equal
"http://www.example.com", redirect_to_url
146 def test_relative_url_redirect_with_status
147 get
:relative_url_redirect_with_status
149 assert_equal
"http://test.host/things/stuff", redirect_to_url
152 def test_relative_url_redirect_with_status_hash
153 get
:relative_url_redirect_with_status_hash
155 assert_equal
"http://test.host/things/stuff", redirect_to_url
158 def test_redirect_to_back_with_status
159 @request.env["HTTP_REFERER"] = "http://www.example.com/coming/from"
160 get
:redirect_to_back_with_status
162 assert_equal
"http://www.example.com/coming/from", redirect_to_url
165 def test_simple_redirect_using_options
167 assert_response
:redirect
168 assert_redirected_to
:action => "other_host", :only_path => false, :host => 'other.test.host'
171 def test_module_redirect
173 assert_response
:redirect
174 assert_redirected_to
"http://test.host/module_test/module_redirect/hello_world"
177 def test_module_redirect_using_options
179 assert_response
:redirect
180 assert_redirected_to
:controller => 'module_test/module_redirect', :action => 'hello_world'
183 def test_redirect_with_assigns
184 get
:redirect_with_assigns
185 assert_response
:redirect
186 assert_equal
"world", assigns
["hello"]
189 def test_redirect_to_url
191 assert_response
:redirect
192 assert_redirected_to
"http://www.rubyonrails.org/"
195 def test_redirect_to_url_with_unescaped_query_string
196 get
:redirect_to_url_with_unescaped_query_string
197 assert_response
:redirect
198 assert_redirected_to
"http://dev.rubyonrails.org/query?status=new"
201 def test_redirect_to_url_with_complex_scheme
202 get
:redirect_to_url_with_complex_scheme
203 assert_response
:redirect
204 assert_equal
"x-test+scheme.complex:redirect", redirect_to_url
207 def test_redirect_to_back
208 @request.env["HTTP_REFERER"] = "http://www.example.com/coming/from"
209 get
:redirect_to_back
210 assert_response
:redirect
211 assert_equal
"http://www.example.com/coming/from", redirect_to_url
214 def test_redirect_to_back_with_no_referer
215 assert_raise(ActionController
::RedirectBackError) {
216 @request.env["HTTP_REFERER"] = nil
217 get
:redirect_to_back
221 def test_redirect_to_record
222 ActionController
::Routing::Routes.draw
do |map
|
223 map
.resources
:workshops
224 map
.connect
':controller/:action/:id'
227 get
:redirect_to_existing_record
228 assert_equal
"http://test.host/workshops/5", redirect_to_url
229 assert_redirected_to Workshop
.new(5, false)
231 get
:redirect_to_new_record
232 assert_equal
"http://test.host/workshops", redirect_to_url
233 assert_redirected_to Workshop
.new(5, true)
236 def test_redirect_with_partial_params
238 assert_redirected_to
:action => 'hello_world'
241 def test_redirect_to_nil
242 assert_raise(ActionController
::ActionControllerError) do
249 class ModuleRedirectController
< ::RedirectController
251 redirect_to
:controller => '/redirect', :action => "hello_world"
255 class ModuleRedirectTest
< ActionController
::TestCase
256 tests ModuleRedirectController
258 def test_simple_redirect
260 assert_response
:redirect
261 assert_equal
"http://test.host/module_test/module_redirect/hello_world", redirect_to_url
264 def test_simple_redirect_using_options
266 assert_response
:redirect
267 assert_redirected_to
:action => "other_host", :only_path => false, :host => 'other.test.host'
270 def test_module_redirect
272 assert_response
:redirect
273 assert_equal
"http://test.host/redirect/hello_world", redirect_to_url
276 def test_module_redirect_using_options
278 assert_response
:redirect
279 assert_redirected_to
:controller => '/redirect', :action => "hello_world"