1 require 'abstract_unit'
2 require 'action_controller/integration'
3 require 'action_controller/routing'
5 uses_mocha
'integration' do
7 module IntegrationSessionStubbing
8 def stub_integration_session(session
)
9 session
.stubs(:process)
10 session
.stubs(:generic_url_rewriter)
14 class SessionTest
< Test
::Unit::TestCase
15 include IntegrationSessionStubbing
18 @session = ActionController
::Integration::Session.new
19 stub_integration_session(@session)
22 def test_https_bang_works_and_sets_truth_by_default
23 assert
!@session.https
?
25 assert
@session.https
?
27 assert
!@session.https
?
31 assert_not_equal
"glu.ttono.us", @session.host
32 @session.host
! "rubyonrails.com"
33 assert_equal
"rubyonrails.com", @session.host
36 def test_follow_redirect_raises_when_no_redirect
37 @session.stubs(:redirect?).returns(false)
38 assert_raise(RuntimeError
) { @session.follow_redirect
! }
41 def test_follow_redirect_calls_get_and_returns_status
42 @session.stubs(:redirect?).returns(true)
43 @session.stubs(:headers).returns({"location" => ["www.google.com"]})
44 @session.stubs(:status).returns(200)
45 @session.expects(:get)
46 assert_equal
200, @session.follow_redirect
!
49 def test_request_via_redirect_uses_given_method
50 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue"}
51 @session.expects(:put).with(path
, args
, headers
)
52 @session.stubs(:redirect?).returns(false)
53 @session.request_via_redirect(:put, path
, args
, headers
)
56 def test_request_via_redirect_follows_redirects
57 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue"}
58 @session.stubs(:redirect?).returns(true, true, false)
59 @session.expects(:follow_redirect!).times(2)
60 @session.request_via_redirect(:get, path
, args
, headers
)
63 def test_request_via_redirect_returns_status
64 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue"}
65 @session.stubs(:redirect?).returns(false)
66 @session.stubs(:status).returns(200)
67 assert_equal
200, @session.request_via_redirect(:get, path
, args
, headers
)
70 def test_get_via_redirect
71 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue" }
72 @session.expects(:request_via_redirect).with(:get, path
, args
, headers
)
73 @session.get_via_redirect(path
, args
, headers
)
76 def test_post_via_redirect
77 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue" }
78 @session.expects(:request_via_redirect).with(:post, path
, args
, headers
)
79 @session.post_via_redirect(path
, args
, headers
)
82 def test_put_via_redirect
83 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue" }
84 @session.expects(:request_via_redirect).with(:put, path
, args
, headers
)
85 @session.put_via_redirect(path
, args
, headers
)
88 def test_delete_via_redirect
89 path
= "/somepath"; args
= {:id => '1'}; headers
= {"X-Test-Header" => "testvalue" }
90 @session.expects(:request_via_redirect).with(:delete, path
, args
, headers
)
91 @session.delete_via_redirect(path
, args
, headers
)
94 def test_url_for_with_controller
95 options
= {:action => 'show'}
96 mock_controller
= mock()
97 mock_controller
.expects(:url_for).with(options
).returns('/show')
98 @session.stubs(:controller).returns(mock_controller
)
99 assert_equal
'/show', @session.url_for(options
)
102 def test_url_for_without_controller
103 options
= {:action => 'show'}
104 mock_rewriter
= mock()
105 mock_rewriter
.expects(:rewrite).with(options
).returns('/show')
106 @session.stubs(:generic_url_rewriter).returns(mock_rewriter
)
107 @session.stubs(:controller).returns(nil)
108 assert_equal
'/show', @session.url_for(options
)
111 def test_redirect_bool_with_status_in_300s
112 @session.stubs(:status).returns
301
113 assert
@session.redirect
?
116 def test_redirect_bool_with_status_in_200s
117 @session.stubs(:status).returns
200
118 assert
!@session.redirect
?
122 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
123 @session.expects(:process).with(:get,path
,params
,headers
)
124 @session.get(path
,params
,headers
)
128 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
129 @session.expects(:process).with(:post,path
,params
,headers
)
130 @session.post(path
,params
,headers
)
134 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
135 @session.expects(:process).with(:put,path
,params
,headers
)
136 @session.put(path
,params
,headers
)
140 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
141 @session.expects(:process).with(:delete,path
,params
,headers
)
142 @session.delete(path
,params
,headers
)
146 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
147 @session.expects(:process).with(:head,path
,params
,headers
)
148 @session.head(path
,params
,headers
)
151 def test_xml_http_request_get
152 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
153 headers_after_xhr
= headers
.merge(
154 "X-Requested-With" => "XMLHttpRequest",
155 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
157 @session.expects(:process).with(:get,path
,params
,headers_after_xhr
)
158 @session.xml_http_request(:get,path
,params
,headers
)
161 def test_xml_http_request_post
162 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
163 headers_after_xhr
= headers
.merge(
164 "X-Requested-With" => "XMLHttpRequest",
165 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
167 @session.expects(:process).with(:post,path
,params
,headers_after_xhr
)
168 @session.xml_http_request(:post,path
,params
,headers
)
171 def test_xml_http_request_put
172 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
173 headers_after_xhr
= headers
.merge(
174 "X-Requested-With" => "XMLHttpRequest",
175 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
177 @session.expects(:process).with(:put,path
,params
,headers_after_xhr
)
178 @session.xml_http_request(:put,path
,params
,headers
)
181 def test_xml_http_request_delete
182 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
183 headers_after_xhr
= headers
.merge(
184 "X-Requested-With" => "XMLHttpRequest",
185 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
187 @session.expects(:process).with(:delete,path
,params
,headers_after_xhr
)
188 @session.xml_http_request(:delete,path
,params
,headers
)
191 def test_xml_http_request_head
192 path
= "/index"; params
= "blah"; headers
= {:location => 'blah'}
193 headers_after_xhr
= headers
.merge(
194 "X-Requested-With" => "XMLHttpRequest",
195 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
197 @session.expects(:process).with(:head,path
,params
,headers_after_xhr
)
198 @session.xml_http_request(:head,path
,params
,headers
)
201 def test_xml_http_request_override_accept
202 path
= "/index"; params
= "blah"; headers
= {:location => 'blah', "Accept" => "application/xml"}
203 headers_after_xhr
= headers
.merge(
204 "X-Requested-With" => "XMLHttpRequest"
206 @session.expects(:process).with(:post,path
,params
,headers_after_xhr
)
207 @session.xml_http_request(:post,path
,params
,headers
)
211 class IntegrationTestTest
< Test
::Unit::TestCase
212 include IntegrationSessionStubbing
215 @test = ::ActionController::IntegrationTest.new(:default_test)
216 @test.class.stubs(:fixture_table_names).returns([])
217 @session = @test.open_session
218 stub_integration_session(@session)
221 def test_opens_new_session
222 @test.class.expects(:fixture_table_names).times(2).returns(['foo'])
224 session1
= @test.open_session
{ |sess
| }
225 session2
= @test.open_session
# implicit session
227 assert_equal
::ActionController::Integration::Session, session1
.class
228 assert_equal
::ActionController::Integration::Session, session2
.class
229 assert_not_equal session1
, session2
233 # Tests that integration tests don't call Controller test methods for processing.
234 # Integration tests have their own setup and teardown.
235 class IntegrationTestUsesCorrectClass
< ActionController
::IntegrationTest
236 include IntegrationSessionStubbing
238 def self.fixture_table_names
242 def test_integration_methods_called
244 stub_integration_session(@integration_session)
245 %w( get post head put delete
).each
do |verb
|
246 assert_nothing_raised("'#{verb}' should use integration test methods") { __send__(verb
, '/') }
251 class IntegrationProcessTest
< ActionController
::IntegrationTest
252 class IntegrationController
< ActionController
::Base
256 respond_to
do |format
|
257 format
.html
{ render
:text => "OK", :status => 200 }
258 format
.js
{ render
:text => "JS OK", :status => 200 }
263 render
:text => "foo: #{params[:foo]}", :status => 200
267 render
:text => "Created", :status => 201
271 cookies
["cookie_1"] = nil
272 cookies
["cookie_3"] = "chocolate"
273 render
:text => "Gone", :status => 410
277 redirect_to
:action => "get"
282 with_test_route_set
do
284 assert_equal
200, status
285 assert_equal
"OK", status_message
286 assert_equal
"200 OK", response
.headers
["Status"]
287 assert_equal
["200 OK"], headers
["status"]
289 assert_response
:success
291 assert_equal
[], response
.headers
["cookie"]
292 assert_equal
[], headers
["cookie"]
293 assert_equal({}, cookies
)
294 assert_equal
"OK", response
.body
295 assert_kind_of HTML
::Document, html_document
296 assert_equal
1, request_count
301 with_test_route_set
do
303 assert_equal
201, status
304 assert_equal
"Created", status_message
305 assert_equal
"201 Created", response
.headers
["Status"]
306 assert_equal
["201 Created"], headers
["status"]
308 assert_response
:success
309 assert_response
:created
310 assert_equal
[], response
.headers
["cookie"]
311 assert_equal
[], headers
["cookie"]
312 assert_equal({}, cookies
)
313 assert_equal
"Created", response
.body
314 assert_kind_of HTML
::Document, html_document
315 assert_equal
1, request_count
319 def test_cookie_monster
320 with_test_route_set
do
321 self.cookies
['cookie_1'] = "sugar"
322 self.cookies
['cookie_2'] = "oatmeal"
323 get
'/cookie_monster'
324 assert_equal
410, status
325 assert_equal
"Gone", status_message
326 assert_equal
"410 Gone", response
.headers
["Status"]
327 assert_equal
["410 Gone"], headers
["status"]
329 assert_response
:gone
330 assert_equal
["cookie_1=; path=/", "cookie_3=chocolate; path=/"], response
.headers
["Set-Cookie"]
331 assert_equal
["cookie_1=; path=/", "cookie_3=chocolate; path=/"], headers
['set-cookie']
333 CGI
::Cookie::new("name" => "cookie_1", "value" => ""),
334 CGI
::Cookie::new("name" => "cookie_3", "value" => "chocolate")
335 ], response
.headers
["cookie"]
336 assert_equal
[], headers
["cookie"]
337 assert_equal({"cookie_1"=>"", "cookie_2"=>"oatmeal", "cookie_3"=>"chocolate"}, cookies
)
338 assert_equal
"Gone", response
.body
343 with_test_route_set
do
345 assert_equal
302, status
346 assert_equal
"Found", status_message
347 assert_equal
"302 Found", response
.headers
["Status"]
348 assert_equal
["302 Found"], headers
["status"]
350 assert_response
:redirect
351 assert_response
:found
352 assert_equal
"<html><body>You are being <a href=\"http://www.example.com/get\">redirected</a>.</body></html>", response
.body
353 assert_kind_of HTML
::Document, html_document
354 assert_equal
1, request_count
358 def test_xml_http_request_get
359 with_test_route_set
do
361 assert_equal
200, status
362 assert_equal
"OK", status_message
363 assert_equal
"200 OK", response
.headers
["Status"]
364 assert_equal
["200 OK"], headers
["status"]
366 assert_response
:success
368 assert_equal
"JS OK", response
.body
372 def test_get_with_query_string
373 with_test_route_set
do
374 get
'/get_with_params?foo=bar'
375 assert_equal
'/get_with_params?foo=bar', request
.env["REQUEST_URI"]
376 assert_equal
'/get_with_params?foo=bar', request
.request_uri
377 assert_equal
nil, request
.env["QUERY_STRING"]
378 assert_equal
'foo=bar', request
.query_string
379 assert_equal
'bar', request
.parameters
['foo']
381 assert_equal
200, status
382 assert_equal
"foo: bar", response
.body
386 def test_get_with_parameters
387 with_test_route_set
do
388 get
'/get_with_params', :foo => "bar"
389 assert_equal
'/get_with_params', request
.env["REQUEST_URI"]
390 assert_equal
'/get_with_params', request
.request_uri
391 assert_equal
'foo=bar', request
.env["QUERY_STRING"]
392 assert_equal
'foo=bar', request
.query_string
393 assert_equal
'bar', request
.parameters
['foo']
395 assert_equal
200, status
396 assert_equal
"foo: bar", response
.body
401 def with_test_route_set
402 with_routing
do |set
|
404 map
.with_options
:controller => "IntegrationProcessTest::Integration" do |c
|