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
< Test
::Unit::TestCase
108 @controller = RedirectController
.new
109 @request = ActionController
::TestRequest.new
110 @response = ActionController
::TestResponse.new
113 def test_simple_redirect
115 assert_response
:redirect
116 assert_equal
"http://test.host/redirect/hello_world", redirect_to_url
119 def test_redirect_with_no_status
122 assert_equal
"http://test.host/redirect/hello_world", redirect_to_url
125 def test_redirect_with_status
126 get
:redirect_with_status
128 assert_equal
"http://test.host/redirect/hello_world", redirect_to_url
131 def test_redirect_with_status_hash
132 get
:redirect_with_status_hash
134 assert_equal
"http://test.host/redirect/hello_world", redirect_to_url
137 def test_url_redirect_with_status
138 get
:url_redirect_with_status
140 assert_equal
"http://www.example.com", redirect_to_url
143 def test_url_redirect_with_status_hash
144 get
:url_redirect_with_status_hash
146 assert_equal
"http://www.example.com", redirect_to_url
150 def test_relative_url_redirect_with_status
151 get
:relative_url_redirect_with_status
153 assert_equal
"http://test.host/things/stuff", redirect_to_url
156 def test_relative_url_redirect_with_status_hash
157 get
:relative_url_redirect_with_status_hash
159 assert_equal
"http://test.host/things/stuff", redirect_to_url
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
166 assert_equal
"http://www.example.com/coming/from", redirect_to_url
169 def test_simple_redirect_using_options
171 assert_response
:redirect
172 assert_redirected_to
:action => "other_host", :only_path => false, :host => 'other.test.host'
175 def test_module_redirect
177 assert_response
:redirect
178 assert_redirected_to
"http://test.host/module_test/module_redirect/hello_world"
181 def test_module_redirect_using_options
183 assert_response
:redirect
184 assert_redirected_to
:controller => 'module_test/module_redirect', :action => 'hello_world'
187 def test_redirect_with_assigns
188 get
:redirect_with_assigns
189 assert_response
:redirect
190 assert_equal
"world", assigns
["hello"]
193 def test_redirect_to_url
195 assert_response
:redirect
196 assert_redirected_to
"http://www.rubyonrails.org/"
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"
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
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
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
225 def test_redirect_to_record
226 ActionController
::Routing::Routes.draw
do |map
|
227 map
.resources
:workshops
228 map
.connect
':controller/:action/:id'
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)
235 get
:redirect_to_new_record
236 assert_equal
"http://test.host/workshops", redirect_to_url
237 assert_redirected_to Workshop
.new(5, true)
240 def test_redirect_with_partial_params
242 assert_redirected_to
:action => 'hello_world'
245 def test_redirect_to_nil
246 assert_raises(ActionController
::ActionControllerError) do
253 class ModuleRedirectController
< ::RedirectController
255 redirect_to
:controller => '/redirect', :action => "hello_world"
259 class ModuleRedirectTest
< Test
::Unit::TestCase
261 @controller = ModuleRedirectController
.new
262 @request = ActionController
::TestRequest.new
263 @response = ActionController
::TestResponse.new
266 def test_simple_redirect
268 assert_response
:redirect
269 assert_equal
"http://test.host/module_test/module_redirect/hello_world", redirect_to_url
272 def test_simple_redirect_using_options
274 assert_response
:redirect
275 assert_redirected_to
:action => "other_host", :only_path => false, :host => 'other.test.host'
278 def test_module_redirect
280 assert_response
:redirect
281 assert_equal
"http://test.host/redirect/hello_world", redirect_to_url
284 def test_module_redirect_using_options
286 assert_response
:redirect
287 assert_redirected_to
:controller => '/redirect', :action => "hello_world"