1 require File.dirname(__FILE__) + '/../abstract_unit'
3 $:.unshift File.dirname(__FILE__) + '/../../../railties/lib'
4 require 'action_controller/integration'
6 uses_mocha 'integration' do
8 # Stub process for testing.
9 module ActionController
15 def generic_url_rewriter
21 class SessionTest < Test::Unit::TestCase
23 @session = ActionController::Integration::Session.new
25 def test_https_bang_works_and_sets_truth_by_default
26 assert !@session.https?
28 assert @session.https?
30 assert !@session.https?
34 assert_not_equal "glu.ttono.us", @session.host
35 @session.host! "rubyonrails.com"
36 assert_equal "rubyonrails.com", @session.host
39 def test_follow_redirect_raises_when_no_redirect
40 @session.stubs(:redirect?).returns(false)
41 assert_raise(RuntimeError) { @session.follow_redirect! }
44 def test_follow_redirect_calls_get_and_returns_status
45 @session.stubs(:redirect?).returns(true)
46 @session.stubs(:headers).returns({"location" => ["www.google.com"]})
47 @session.stubs(:status).returns(200)
48 @session.expects(:get)
49 assert_equal 200, @session.follow_redirect!
52 def test_get_via_redirect
53 path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
55 @session.expects(:get).with(path,args,headers)
57 @session.stubs(:redirect?).returns(true, true, false)
58 @session.expects(:follow_redirect!).times(2)
60 @session.stubs(:status).returns(200)
61 assert_equal 200, @session.get_via_redirect(path, args, headers)
64 def test_post_via_redirect
65 path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
67 @session.expects(:post).with(path,args,headers)
69 @session.stubs(:redirect?).returns(true, true, false)
70 @session.expects(:follow_redirect!).times(2)
72 @session.stubs(:status).returns(200)
73 assert_equal 200, @session.post_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)
184 class IntegrationTestTest < Test::Unit::TestCase
187 @test = ::ActionController::IntegrationTest.new(:default_test)
188 @test.class.stubs(:fixture_table_names).returns([])
189 @session = @test.open_session
192 def test_opens_new_session
193 @test.class.expects(:fixture_table_names).times(2).returns(['foo'])
195 session1 = @test.open_session { |sess| }
196 session2 = @test.open_session # implicit session
198 assert_equal ::ActionController::Integration::Session, session1.class
199 assert_equal ::ActionController::Integration::Session, session2.class
200 assert_not_equal session1, session2
205 # Tests that integration tests don't call Controller test methods for processing.
206 # Integration tests have their own setup and teardown.
207 class IntegrationTestUsesCorrectClass < ActionController::IntegrationTest
209 def self.fixture_table_names
213 def test_integration_methods_called
214 %w( get post head put delete ).each do |verb|
215 assert_nothing_raised("'#{verb}' should use integration test methods") { send!(verb, '/') }
222 # class MockCGITest < Test::Unit::TestCase