Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / test / controller / integration_test.rb
1 require 'abstract_unit'
2
3 class SessionTest < Test::Unit::TestCase
4 StubApp = lambda { |env|
5 [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, "Hello, World!"]
6 }
7
8 def setup
9 @session = ActionController::Integration::Session.new(StubApp)
10 end
11
12 def test_https_bang_works_and_sets_truth_by_default
13 assert !@session.https?
14 @session.https!
15 assert @session.https?
16 @session.https! false
17 assert !@session.https?
18 end
19
20 def test_host!
21 assert_not_equal "glu.ttono.us", @session.host
22 @session.host! "rubyonrails.com"
23 assert_equal "rubyonrails.com", @session.host
24 end
25
26 def test_follow_redirect_raises_when_no_redirect
27 @session.stubs(:redirect?).returns(false)
28 assert_raise(RuntimeError) { @session.follow_redirect! }
29 end
30
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)
36 end
37
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)
43 end
44
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)
50 end
51
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)
56 end
57
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)
62 end
63
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)
68 end
69
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)
74 end
75
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
83
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
92
93 def test_redirect_bool_with_status_in_300s
94 @session.stubs(:status).returns 301
95 assert @session.redirect?
96 end
97
98 def test_redirect_bool_with_status_in_200s
99 @session.stubs(:status).returns 200
100 assert !@session.redirect?
101 end
102
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
108
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
114
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
120
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
126
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
132
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
142
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
152
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
162
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
172
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
182
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"
187 )
188 @session.expects(:process).with(:post,path,params,headers_after_xhr)
189 @session.xml_http_request(:post,path,params,headers)
190 end
191 end
192
193 class IntegrationTestTest < Test::Unit::TestCase
194 def setup
195 @test = ::ActionController::IntegrationTest.new(:default_test)
196 @test.class.stubs(:fixture_table_names).returns([])
197 @session = @test.open_session
198 end
199
200 def test_opens_new_session
201 @test.class.expects(:fixture_table_names).times(2).returns(['foo'])
202
203 session1 = @test.open_session { |sess| }
204 session2 = @test.open_session # implicit session
205
206 assert_equal ::ActionController::Integration::Session, session1.class
207 assert_equal ::ActionController::Integration::Session, session2.class
208 assert_not_equal session1, session2
209 end
210 end
211
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
216 []
217 end
218
219 def test_integration_methods_called
220 reset!
221 @integration_session.stubs(:generic_url_rewriter)
222 @integration_session.stubs(:process)
223
224 %w( get post head put delete ).each do |verb|
225 assert_nothing_raised("'#{verb}' should use integration test methods") { __send__(verb, '/') }
226 end
227 end
228 end
229
230 class IntegrationProcessTest < ActionController::IntegrationTest
231 class IntegrationController < ActionController::Base
232 def get
233 respond_to do |format|
234 format.html { render :text => "OK", :status => 200 }
235 format.js { render :text => "JS OK", :status => 200 }
236 end
237 end
238
239 def get_with_params
240 render :text => "foo: #{params[:foo]}", :status => 200
241 end
242
243 def post
244 render :text => "Created", :status => 201
245 end
246
247 def cookie_monster
248 cookies["cookie_1"] = nil
249 cookies["cookie_3"] = "chocolate"
250 render :text => "Gone", :status => 410
251 end
252
253 def redirect
254 redirect_to :action => "get"
255 end
256 end
257
258 def test_get
259 with_test_route_set do
260 get '/get'
261 assert_equal 200, status
262 assert_equal "OK", status_message
263 assert_response 200
264 assert_response :success
265 assert_response :ok
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
271 end
272 end
273
274 def test_post
275 with_test_route_set do
276 post '/post'
277 assert_equal 201, status
278 assert_equal "Created", status_message
279 assert_response 201
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
287 end
288 end
289
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
297 assert_response 410
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
302 end
303 end
304
305 def test_redirect
306 with_test_route_set do
307 get '/redirect'
308 assert_equal 302, status
309 assert_equal "Found", status_message
310 assert_response 302
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
316
317 follow_redirect!
318 assert_response :success
319 assert_equal "/get", path
320 end
321 end
322
323 def test_xml_http_request_get
324 with_test_route_set do
325 xhr :get, '/get'
326 assert_equal 200, status
327 assert_equal "OK", status_message
328 assert_response 200
329 assert_response :success
330 assert_response :ok
331 assert_equal "JS OK", response.body
332 end
333 end
334
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']
343
344 assert_equal 200, status
345 assert_equal "foo: bar", response.body
346 end
347 end
348
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']
357
358 assert_equal 200, status
359 assert_equal "foo: bar", response.body
360 end
361 end
362
363 def test_head
364 with_test_route_set do
365 head '/get'
366 assert_equal 200, status
367 assert_equal "", body
368
369 head '/post'
370 assert_equal 201, status
371 assert_equal "", body
372 end
373 end
374
375 private
376 def with_test_route_set
377 with_routing do |set|
378 set.draw do |map|
379 map.with_options :controller => "IntegrationProcessTest::Integration" do |c|
380 c.connect "/:action"
381 end
382 end
383 yield
384 end
385 end
386 end
387
388 class MetalTest < ActionController::IntegrationTest
389 class Poller
390 def self.call(env)
391 if env["PATH_INFO"] =~ /^\/success/
392 [200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, "Hello World!"]
393 else
394 [404, {"Content-Type" => "text/plain", "Content-Length" => "0"}, '']
395 end
396 end
397 end
398
399 def setup
400 @integration_session = ActionController::Integration::Session.new(Poller)
401 end
402
403 def test_successful_get
404 get "/success"
405 assert_response 200
406 assert_response :success
407 assert_response :ok
408 assert_equal "Hello World!", response.body
409 end
410
411 def test_failed_get
412 get "/failure"
413 assert_response 404
414 assert_response :not_found
415 assert_equal '', response.body
416 end
417 end