Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / integration_test.rb
1 require 'abstract_unit'
2 require 'action_controller/integration'
3 require 'action_controller/routing'
4
5 uses_mocha 'integration' do
6
7 module IntegrationSessionStubbing
8 def stub_integration_session(session)
9 session.stubs(:process)
10 session.stubs(:generic_url_rewriter)
11 end
12 end
13
14 class SessionTest < Test::Unit::TestCase
15 include IntegrationSessionStubbing
16
17 def setup
18 @session = ActionController::Integration::Session.new
19 stub_integration_session(@session)
20 end
21
22 def test_https_bang_works_and_sets_truth_by_default
23 assert !@session.https?
24 @session.https!
25 assert @session.https?
26 @session.https! false
27 assert !@session.https?
28 end
29
30 def test_host!
31 assert_not_equal "glu.ttono.us", @session.host
32 @session.host! "rubyonrails.com"
33 assert_equal "rubyonrails.com", @session.host
34 end
35
36 def test_follow_redirect_raises_when_no_redirect
37 @session.stubs(:redirect?).returns(false)
38 assert_raise(RuntimeError) { @session.follow_redirect! }
39 end
40
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!
47 end
48
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)
54 end
55
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)
61 end
62
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)
68 end
69
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)
74 end
75
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)
80 end
81
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)
86 end
87
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)
92 end
93
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)
100 end
101
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)
109 end
110
111 def test_redirect_bool_with_status_in_300s
112 @session.stubs(:status).returns 301
113 assert @session.redirect?
114 end
115
116 def test_redirect_bool_with_status_in_200s
117 @session.stubs(:status).returns 200
118 assert !@session.redirect?
119 end
120
121 def test_get
122 path = "/index"; params = "blah"; headers = {:location => 'blah'}
123 @session.expects(:process).with(:get,path,params,headers)
124 @session.get(path,params,headers)
125 end
126
127 def test_post
128 path = "/index"; params = "blah"; headers = {:location => 'blah'}
129 @session.expects(:process).with(:post,path,params,headers)
130 @session.post(path,params,headers)
131 end
132
133 def test_put
134 path = "/index"; params = "blah"; headers = {:location => 'blah'}
135 @session.expects(:process).with(:put,path,params,headers)
136 @session.put(path,params,headers)
137 end
138
139 def test_delete
140 path = "/index"; params = "blah"; headers = {:location => 'blah'}
141 @session.expects(:process).with(:delete,path,params,headers)
142 @session.delete(path,params,headers)
143 end
144
145 def test_head
146 path = "/index"; params = "blah"; headers = {:location => 'blah'}
147 @session.expects(:process).with(:head,path,params,headers)
148 @session.head(path,params,headers)
149 end
150
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, */*"
156 )
157 @session.expects(:process).with(:get,path,params,headers_after_xhr)
158 @session.xml_http_request(:get,path,params,headers)
159 end
160
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, */*"
166 )
167 @session.expects(:process).with(:post,path,params,headers_after_xhr)
168 @session.xml_http_request(:post,path,params,headers)
169 end
170
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, */*"
176 )
177 @session.expects(:process).with(:put,path,params,headers_after_xhr)
178 @session.xml_http_request(:put,path,params,headers)
179 end
180
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, */*"
186 )
187 @session.expects(:process).with(:delete,path,params,headers_after_xhr)
188 @session.xml_http_request(:delete,path,params,headers)
189 end
190
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, */*"
196 )
197 @session.expects(:process).with(:head,path,params,headers_after_xhr)
198 @session.xml_http_request(:head,path,params,headers)
199 end
200
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"
205 )
206 @session.expects(:process).with(:post,path,params,headers_after_xhr)
207 @session.xml_http_request(:post,path,params,headers)
208 end
209 end
210
211 class IntegrationTestTest < Test::Unit::TestCase
212 include IntegrationSessionStubbing
213
214 def setup
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)
219 end
220
221 def test_opens_new_session
222 @test.class.expects(:fixture_table_names).times(2).returns(['foo'])
223
224 session1 = @test.open_session { |sess| }
225 session2 = @test.open_session # implicit session
226
227 assert_equal ::ActionController::Integration::Session, session1.class
228 assert_equal ::ActionController::Integration::Session, session2.class
229 assert_not_equal session1, session2
230 end
231 end
232
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
237
238 def self.fixture_table_names
239 []
240 end
241
242 def test_integration_methods_called
243 reset!
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, '/') }
247 end
248 end
249 end
250
251 class IntegrationProcessTest < ActionController::IntegrationTest
252 class IntegrationController < ActionController::Base
253 session :off
254
255 def get
256 respond_to do |format|
257 format.html { render :text => "OK", :status => 200 }
258 format.js { render :text => "JS OK", :status => 200 }
259 end
260 end
261
262 def get_with_params
263 render :text => "foo: #{params[:foo]}", :status => 200
264 end
265
266 def post
267 render :text => "Created", :status => 201
268 end
269
270 def cookie_monster
271 cookies["cookie_1"] = nil
272 cookies["cookie_3"] = "chocolate"
273 render :text => "Gone", :status => 410
274 end
275
276 def redirect
277 redirect_to :action => "get"
278 end
279 end
280
281 def test_get
282 with_test_route_set do
283 get '/get'
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"]
288 assert_response 200
289 assert_response :success
290 assert_response :ok
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
297 end
298 end
299
300 def test_post
301 with_test_route_set do
302 post '/post'
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"]
307 assert_response 201
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
316 end
317 end
318
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"]
328 assert_response 410
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']
332 assert_equal [
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
339 end
340 end
341
342 def test_redirect
343 with_test_route_set do
344 get '/redirect'
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"]
349 assert_response 302
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
355 end
356 end
357
358 def test_xml_http_request_get
359 with_test_route_set do
360 xhr :get, '/get'
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"]
365 assert_response 200
366 assert_response :success
367 assert_response :ok
368 assert_equal "JS OK", response.body
369 end
370 end
371
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']
380
381 assert_equal 200, status
382 assert_equal "foo: bar", response.body
383 end
384 end
385
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']
394
395 assert_equal 200, status
396 assert_equal "foo: bar", response.body
397 end
398 end
399
400 private
401 def with_test_route_set
402 with_routing do |set|
403 set.draw do |map|
404 map.with_options :controller => "IntegrationProcessTest::Integration" do |c|
405 c.connect "/:action"
406 end
407 end
408 yield
409 end
410 end
411 end
412
413 end