1 require 'abstract_unit'
3 class SessionTest
< Test
::Unit::TestCase
4 StubApp
= lambda
{ |env|
5 [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, "Hello, World!"]
9 @session = ActionController
::Integration::Session.new(StubApp
)
12 def test_https_bang_works_and_sets_truth_by_default
13 assert
!@session.https
?
15 assert
@session.https
?
17 assert
!@session.https
?
21 assert_not_equal
"glu.ttono.us", @session.host
22 @session.host
! "rubyonrails.com"
23 assert_equal
"rubyonrails.com", @session.host
26 def test_follow_redirect_raises_when_no_redirect
27 @session.stubs(:redirect?).returns(false)
28 assert_raise(RuntimeError
) { @session.follow_redirect
! }
31 def test_request_via_redirect_uses_given_method
32 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue"}
33 @session.expects(:put).with(path
, args
, headers
)
34 @session.stubs(:redirect?).returns(false)
35 @session.request_via_redirect(:put, path
, args
, headers
)
38 def test_request_via_redirect_follows_redirects
39 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue"}
40 @session.stubs(:redirect?).returns(true, true, false)
41 @session.expects(:follow_redirect!).times(2)
42 @session.request_via_redirect(:get, path
, args
, headers
)
45 def test_request_via_redirect_returns_status
46 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue"}
47 @session.stubs(:redirect?).returns(false)
48 @session.stubs(:status).returns(200)
49 assert_equal
200, @session.request_via_redirect(:get, path
, args
, headers
)
52 def test_get_via_redirect
53 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue" }
54 @session.expects(:request_via_redirect).with(:get, path
, args
, headers
)
55 @session.get_via_redirect(path
, args
, headers
)
58 def test_post_via_redirect
59 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue" }
60 @session.expects(:request_via_redirect).with(:post, path
, args
, headers
)
61 @session.post_via_redirect(path
, args
, headers
)
64 def test_put_via_redirect
65 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue" }
66 @session.expects(:request_via_redirect).with(:put, path
, args
, headers
)
67 @session.put_via_redirect(path
, args
, headers
)
70 def test_delete_via_redirect
71 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue" }
72 @session.expects(:request_via_redirect).with(:delete, path
, args
, headers
)
73 @session.delete_via_redirect(path
, args
, headers
)
76 def test_url_for_with_controller
77 options
= {:action => 'show'}
78 mock_controller
= mock()
79 mock_controller
.expects(:url_for).with(options
).returns('/show')
80 @session.stubs(:controller).returns(mock_controller
)
81 assert_equal
'/show', @session.url_for(options
)
84 def test_url_for_without_controller
85 options
= {:action => 'show'}
86 mock_rewriter
= mock()
87 mock_rewriter
.expects(:rewrite).with(options
).returns('/show')
88 @session.stubs(:generic_url_rewriter).returns(mock_rewriter
)
89 @session.stubs(:controller).returns(nil)
90 assert_equal
'/show', @session.url_for(options
)
93 def test_redirect_bool_with_status_in_300s
94 @session.stubs(:status).returns
301
95 assert
@session.redirect
?
98 def test_redirect_bool_with_status_in_200s
99 @session.stubs(:status).returns
200
100 assert
!@session.redirect
?
104 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
105 @session.expects(:process).with(:get,path
,params
,headers
)
106 @session.get(path
,params
,headers
)
110 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
111 @session.expects(:process).with(:post,path
,params
,headers
)
112 @session.post(path
,params
,headers
)
116 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
117 @session.expects(:process).with(:put,path
,params
,headers
)
118 @session.put(path
,params
,headers
)
122 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
123 @session.expects(:process).with(:delete,path
,params
,headers
)
124 @session.delete(path
,params
,headers
)
128 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
129 @session.expects(:process).with(:head,path
,params
,headers
)
130 @session.head(path
,params
,headers
)
133 def test_xml_http_request_get
134 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
135 headers_after_xhr
= headers
.merge(
136 "X-Requested-With" => "XMLHttpRequest",
137 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
139 @session.expects(:process).with(:get,path
,params
,headers_after_xhr
)
140 @session.xml_http_request(:get,path
,params
,headers
)
143 def test_xml_http_request_post
144 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
145 headers_after_xhr
= headers
.merge(
146 "X-Requested-With" => "XMLHttpRequest",
147 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
149 @session.expects(:process).with(:post,path
,params
,headers_after_xhr
)
150 @session.xml_http_request(:post,path
,params
,headers
)
153 def test_xml_http_request_put
154 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
155 headers_after_xhr
= headers
.merge(
156 "X-Requested-With" => "XMLHttpRequest",
157 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
159 @session.expects(:process).with(:put,path
,params
,headers_after_xhr
)
160 @session.xml_http_request(:put,path
,params
,headers
)
163 def test_xml_http_request_delete
164 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
165 headers_after_xhr
= headers
.merge(
166 "X-Requested-With" => "XMLHttpRequest",
167 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
169 @session.expects(:process).with(:delete,path
,params
,headers_after_xhr
)
170 @session.xml_http_request(:delete,path
,params
,headers
)
173 def test_xml_http_request_head
174 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
175 headers_after_xhr
= headers
.merge(
176 "X-Requested-With" => "XMLHttpRequest",
177 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
179 @session.expects(:process).with(:head,path
,params
,headers_after_xhr
)
180 @session.xml_http_request(:head,path
,params
,headers
)
183 def test_xml_http_request_override_accept
184 path
= "/index"; params
= "blah"; headers
= {:location => 'blah', "Accept" => "application/xml"}
185 headers_after_xhr
= headers
.merge(
186 "X-Requested-With" => "XMLHttpRequest"
188 @session.expects(:process).with(:post,path
,params
,headers_after_xhr
)
189 @session.xml_http_request(:post,path
,params
,headers
)
193 class IntegrationTestTest
< Test
::Unit::TestCase
195 @test = ::ActionController::IntegrationTest.new(:default_test)
196 @test.class.stubs(:fixture_table_names).returns([])
197 @session = @test.open_session
200 def test_opens_new_session
201 @test.class.expects(:fixture_table_names).times(2).returns(['foo'])
203 session1
= @test.open_session
{ |sess
| }
204 session2
= @test.open_session
# implicit session
206 assert_equal
::ActionController::Integration::Session, session1
.class
207 assert_equal
::ActionController::Integration::Session, session2
.class
208 assert_not_equal session1
, session2
212 # Tests that integration tests don't call Controller test methods for processing.
213 # Integration tests have their own setup and teardown.
214 class IntegrationTestUsesCorrectClass
< ActionController
::IntegrationTest
215 def self.fixture_table_names
219 def test_integration_methods_called
221 @integration_session.stubs(:generic_url_rewriter)
222 @integration_session.stubs(:process)
224 %w( get post head put delete
).each
do |verb
|
225 assert_nothing_raised("'#{verb}' should use integration test methods") { __send__(verb
, '/') }
230 class IntegrationProcessTest
< ActionController
::IntegrationTest
231 class IntegrationController
< ActionController
::Base
233 respond_to
do |format
|
234 format
.html
{ render
:text => "OK", :status => 200 }
235 format
.js
{ render
:text => "JS OK", :status => 200 }
240 render
:text => "foo: #{params[:foo]}", :status => 200
244 render
:text => "Created", :status => 201
248 cookies
["cookie_1"] = nil
249 cookies
["cookie_3"] = "chocolate"
250 render
:text => "Gone", :status => 410
254 redirect_to
:action => "get"
259 with_test_route_set
do
261 assert_equal
200, status
262 assert_equal
"OK", status_message
264 assert_response
:success
266 assert_equal({}, cookies
)
267 assert_equal
"OK", body
268 assert_equal
"OK", response
.body
269 assert_kind_of HTML
::Document, html_document
270 assert_equal
1, request_count
275 with_test_route_set
do
277 assert_equal
201, status
278 assert_equal
"Created", status_message
280 assert_response
:success
281 assert_response
:created
282 assert_equal({}, cookies
)
283 assert_equal
"Created", body
284 assert_equal
"Created", response
.body
285 assert_kind_of HTML
::Document, html_document
286 assert_equal
1, request_count
290 def test_cookie_monster
291 with_test_route_set
do
292 self.cookies
['cookie_1'] = "sugar"
293 self.cookies
['cookie_2'] = "oatmeal"
294 get
'/cookie_monster'
295 assert_equal
410, status
296 assert_equal
"Gone", status_message
298 assert_response
:gone
299 assert_equal
"cookie_1=; path=/\ncookie_3=chocolate; path=/", headers
["Set-Cookie"]
300 assert_equal({"cookie_1"=>"", "cookie_2"=>"oatmeal", "cookie_3"=>"chocolate"}, cookies
)
301 assert_equal
"Gone", response
.body
306 with_test_route_set
do
308 assert_equal
302, status
309 assert_equal
"Found", status_message
311 assert_response
:redirect
312 assert_response
:found
313 assert_equal
"<html><body>You are being <a href=\"http://www.example.com/get\">redirected</a>.</body></html>", response
.body
314 assert_kind_of HTML
::Document, html_document
315 assert_equal
1, request_count
318 assert_response
:success
319 assert_equal
"/get", path
323 def test_xml_http_request_get
324 with_test_route_set
do
326 assert_equal
200, status
327 assert_equal
"OK", status_message
329 assert_response
:success
331 assert_equal
"JS OK", response
.body
335 def test_get_with_query_string
336 with_test_route_set
do
337 get
'/get_with_params?foo=bar'
338 assert_equal
'/get_with_params?foo=bar', request
.env["REQUEST_URI"]
339 assert_equal
'/get_with_params?foo=bar', request
.request_uri
340 assert_equal
"", request
.env["QUERY_STRING"]
341 assert_equal
'foo=bar', request
.query_string
342 assert_equal
'bar', request
.parameters
['foo']
344 assert_equal
200, status
345 assert_equal
"foo: bar", response
.body
349 def test_get_with_parameters
350 with_test_route_set
do
351 get
'/get_with_params', :foo => "bar"
352 assert_equal
'/get_with_params', request
.env["REQUEST_URI"]
353 assert_equal
'/get_with_params', request
.request_uri
354 assert_equal
'foo=bar', request
.env["QUERY_STRING"]
355 assert_equal
'foo=bar', request
.query_string
356 assert_equal
'bar', request
.parameters
['foo']
358 assert_equal
200, status
359 assert_equal
"foo: bar", response
.body
364 with_test_route_set
do
366 assert_equal
200, status
367 assert_equal
"", body
370 assert_equal
201, status
371 assert_equal
"", body
376 def with_test_route_set
377 with_routing
do |set
|
379 map
.with_options
:controller => "IntegrationProcessTest::Integration" do |c
|
388 class MetalTest
< ActionController
::IntegrationTest
391 if env["PATH_INFO"] =~
/^\/success
/
392 [200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, "Hello World!"]
394 [404, {"Content-Type" => "text/plain", "Content-Length" => "0"}, '']
400 @integration_session = ActionController
::Integration::Session.new(Poller
)
403 def test_successful_get
406 assert_response
:success
408 assert_equal
"Hello World!", response
.body
414 assert_response
:not_found
415 assert_equal
'', response
.body