Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / test / controller / integration_test.rb
blob00269ca3b8f3e942289ed069ffc14ed5f4392c54
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
10   module Integration
11     class Session
12       def process(*args)
13       end
15       def generic_url_rewriter
16       end
17     end
18   end
19 end
21 class SessionTest < Test::Unit::TestCase
22   def setup
23     @session = ActionController::Integration::Session.new
24   end
25   def test_https_bang_works_and_sets_truth_by_default
26     assert !@session.https?
27     @session.https!
28     assert @session.https?
29     @session.https! false
30     assert !@session.https?
31   end
33   def test_host!
34     assert_not_equal "glu.ttono.us", @session.host
35     @session.host! "rubyonrails.com"
36     assert_equal "rubyonrails.com", @session.host
37   end
39   def test_follow_redirect_raises_when_no_redirect
40     @session.stubs(:redirect?).returns(false)
41     assert_raise(RuntimeError) { @session.follow_redirect! }
42   end
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!
50   end
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)
62   end
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)
74   end
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)
82   end
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)
91   end
93   def test_redirect_bool_with_status_in_300s
94     @session.stubs(:status).returns 301
95     assert @session.redirect?
96   end
98   def test_redirect_bool_with_status_in_200s
99     @session.stubs(:status).returns 200
100     assert !@session.redirect?
101   end
103   def test_get
104     path = "/index"; params = "blah"; headers = {:location => 'blah'}
105     @session.expects(:process).with(:get,path,params,headers)
106     @session.get(path,params,headers)
107   end
109   def test_post
110     path = "/index"; params = "blah"; headers = {:location => 'blah'}
111     @session.expects(:process).with(:post,path,params,headers)
112     @session.post(path,params,headers)
113   end
115   def test_put
116     path = "/index"; params = "blah"; headers = {:location => 'blah'}
117     @session.expects(:process).with(:put,path,params,headers)
118     @session.put(path,params,headers)
119   end
121   def test_delete
122     path = "/index"; params = "blah"; headers = {:location => 'blah'}
123     @session.expects(:process).with(:delete,path,params,headers)
124     @session.delete(path,params,headers)
125   end
127   def test_head
128     path = "/index"; params = "blah"; headers = {:location => 'blah'}
129     @session.expects(:process).with(:head,path,params,headers)
130     @session.head(path,params,headers)
131   end
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, */*"
138     )
139     @session.expects(:process).with(:get,path,params,headers_after_xhr)
140     @session.xml_http_request(:get,path,params,headers)
141   end
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, */*"
148     )
149     @session.expects(:process).with(:post,path,params,headers_after_xhr)
150     @session.xml_http_request(:post,path,params,headers)
151   end
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, */*"
158     )
159     @session.expects(:process).with(:put,path,params,headers_after_xhr)
160     @session.xml_http_request(:put,path,params,headers)
161   end
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, */*"
168     )
169     @session.expects(:process).with(:delete,path,params,headers_after_xhr)
170     @session.xml_http_request(:delete,path,params,headers)
171   end
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, */*"
178     )
179     @session.expects(:process).with(:head,path,params,headers_after_xhr)
180     @session.xml_http_request(:head,path,params,headers)
181   end
184 class IntegrationTestTest < Test::Unit::TestCase
186   def setup
187     @test = ::ActionController::IntegrationTest.new(:default_test)
188     @test.class.stubs(:fixture_table_names).returns([])
189     @session = @test.open_session
190   end
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
201   end
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
210     []
211   end
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, '/') }
216     end
217   end
221 # TODO
222 # class MockCGITest < Test::Unit::TestCase
223 # end
225 end # uses_mocha