PageRenderTime 29ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/integration-tests/apps/rails2/frozen/vendor/rails/actionpack/test/controller/integration_test.rb

https://gitlab.com/meetly/torquebox
Ruby | 526 lines | 439 code | 78 blank | 9 comment | 2 complexity | ca4c89fa2622b408dade0b0689c660cf MD5 | raw file
  1. require 'abstract_unit'
  2. class SessionTest < Test::Unit::TestCase
  3. StubApp = lambda { |env|
  4. [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, ["Hello, World!"]]
  5. }
  6. def setup
  7. @session = ActionController::Integration::Session.new(StubApp)
  8. end
  9. def test_https_bang_works_and_sets_truth_by_default
  10. assert !@session.https?
  11. @session.https!
  12. assert @session.https?
  13. @session.https! false
  14. assert !@session.https?
  15. end
  16. def test_host!
  17. assert_not_equal "glu.ttono.us", @session.host
  18. @session.host! "rubyonrails.com"
  19. assert_equal "rubyonrails.com", @session.host
  20. end
  21. def test_follow_redirect_raises_when_no_redirect
  22. @session.stubs(:redirect?).returns(false)
  23. assert_raise(RuntimeError) { @session.follow_redirect! }
  24. end
  25. def test_request_via_redirect_uses_given_method
  26. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
  27. @session.expects(:put).with(path, args, headers)
  28. @session.stubs(:redirect?).returns(false)
  29. @session.request_via_redirect(:put, path, args, headers)
  30. end
  31. def test_request_via_redirect_follows_redirects
  32. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
  33. @session.stubs(:redirect?).returns(true, true, false)
  34. @session.expects(:follow_redirect!).times(2)
  35. @session.request_via_redirect(:get, path, args, headers)
  36. end
  37. def test_request_via_redirect_returns_status
  38. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
  39. @session.stubs(:redirect?).returns(false)
  40. @session.stubs(:status).returns(200)
  41. assert_equal 200, @session.request_via_redirect(:get, path, args, headers)
  42. end
  43. def test_get_via_redirect
  44. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
  45. @session.expects(:request_via_redirect).with(:get, path, args, headers)
  46. @session.get_via_redirect(path, args, headers)
  47. end
  48. def test_post_via_redirect
  49. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
  50. @session.expects(:request_via_redirect).with(:post, path, args, headers)
  51. @session.post_via_redirect(path, args, headers)
  52. end
  53. def test_put_via_redirect
  54. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
  55. @session.expects(:request_via_redirect).with(:put, path, args, headers)
  56. @session.put_via_redirect(path, args, headers)
  57. end
  58. def test_delete_via_redirect
  59. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
  60. @session.expects(:request_via_redirect).with(:delete, path, args, headers)
  61. @session.delete_via_redirect(path, args, headers)
  62. end
  63. def test_url_for_with_controller
  64. options = {:action => 'show'}
  65. mock_controller = mock()
  66. mock_controller.expects(:url_for).with(options).returns('/show')
  67. @session.stubs(:controller).returns(mock_controller)
  68. assert_equal '/show', @session.url_for(options)
  69. end
  70. def test_url_for_without_controller
  71. options = {:action => 'show'}
  72. mock_rewriter = mock()
  73. mock_rewriter.expects(:rewrite).with(options).returns('/show')
  74. @session.stubs(:generic_url_rewriter).returns(mock_rewriter)
  75. @session.stubs(:controller).returns(nil)
  76. assert_equal '/show', @session.url_for(options)
  77. end
  78. def test_redirect_bool_with_status_in_300s
  79. @session.stubs(:status).returns 301
  80. assert @session.redirect?
  81. end
  82. def test_redirect_bool_with_status_in_200s
  83. @session.stubs(:status).returns 200
  84. assert !@session.redirect?
  85. end
  86. def test_get
  87. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  88. @session.expects(:process).with(:get,path,params,headers)
  89. @session.get(path,params,headers)
  90. end
  91. def test_post
  92. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  93. @session.expects(:process).with(:post,path,params,headers)
  94. @session.post(path,params,headers)
  95. end
  96. def test_put
  97. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  98. @session.expects(:process).with(:put,path,params,headers)
  99. @session.put(path,params,headers)
  100. end
  101. def test_delete
  102. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  103. @session.expects(:process).with(:delete,path,params,headers)
  104. @session.delete(path,params,headers)
  105. end
  106. def test_head
  107. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  108. @session.expects(:process).with(:head,path,params,headers)
  109. @session.head(path,params,headers)
  110. end
  111. def test_xml_http_request_get
  112. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  113. headers_after_xhr = headers.merge(
  114. "X-Requested-With" => "XMLHttpRequest",
  115. "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
  116. )
  117. @session.expects(:process).with(:get,path,params,headers_after_xhr)
  118. @session.xml_http_request(:get,path,params,headers)
  119. end
  120. def test_xml_http_request_post
  121. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  122. headers_after_xhr = headers.merge(
  123. "X-Requested-With" => "XMLHttpRequest",
  124. "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
  125. )
  126. @session.expects(:process).with(:post,path,params,headers_after_xhr)
  127. @session.xml_http_request(:post,path,params,headers)
  128. end
  129. def test_xml_http_request_put
  130. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  131. headers_after_xhr = headers.merge(
  132. "X-Requested-With" => "XMLHttpRequest",
  133. "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
  134. )
  135. @session.expects(:process).with(:put,path,params,headers_after_xhr)
  136. @session.xml_http_request(:put,path,params,headers)
  137. end
  138. def test_xml_http_request_delete
  139. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  140. headers_after_xhr = headers.merge(
  141. "X-Requested-With" => "XMLHttpRequest",
  142. "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
  143. )
  144. @session.expects(:process).with(:delete,path,params,headers_after_xhr)
  145. @session.xml_http_request(:delete,path,params,headers)
  146. end
  147. def test_xml_http_request_head
  148. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  149. headers_after_xhr = headers.merge(
  150. "X-Requested-With" => "XMLHttpRequest",
  151. "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
  152. )
  153. @session.expects(:process).with(:head,path,params,headers_after_xhr)
  154. @session.xml_http_request(:head,path,params,headers)
  155. end
  156. def test_xml_http_request_override_accept
  157. path = "/index"; params = "blah"; headers = {:location => 'blah', "Accept" => "application/xml"}
  158. headers_after_xhr = headers.merge(
  159. "X-Requested-With" => "XMLHttpRequest"
  160. )
  161. @session.expects(:process).with(:post,path,params,headers_after_xhr)
  162. @session.xml_http_request(:post,path,params,headers)
  163. end
  164. end
  165. class IntegrationTestTest < Test::Unit::TestCase
  166. def setup
  167. @test = ::ActionController::IntegrationTest.new(:default_test)
  168. @test.class.stubs(:fixture_table_names).returns([])
  169. @session = @test.open_session
  170. end
  171. def test_opens_new_session
  172. @test.class.expects(:fixture_table_names).times(2).returns(['foo'])
  173. session1 = @test.open_session { |sess| }
  174. session2 = @test.open_session # implicit session
  175. assert_equal ::ActionController::Integration::Session, session1.class
  176. assert_equal ::ActionController::Integration::Session, session2.class
  177. assert_not_equal session1, session2
  178. end
  179. # RSpec mixes Matchers (which has a #method_missing) into
  180. # IntegrationTest's superclass. Make sure IntegrationTest does not
  181. # try to delegate these methods to the session object.
  182. def test_does_not_prevent_method_missing_passing_up_to_ancestors
  183. mixin = Module.new do
  184. def method_missing(name, *args)
  185. name.to_s == 'foo' ? 'pass' : super
  186. end
  187. end
  188. @test.class.superclass.__send__(:include, mixin)
  189. begin
  190. assert_equal 'pass', @test.foo
  191. ensure
  192. # leave other tests as unaffected as possible
  193. mixin.__send__(:remove_method, :method_missing)
  194. end
  195. end
  196. end
  197. require 'active_record_unit'
  198. # Tests that fixtures are accessible in the integration test sessions
  199. class IntegrationTestWithFixtures < ActiveRecordTestCase
  200. include ActionController::Integration::Runner
  201. fixtures :companies
  202. def test_fixtures_in_new_session
  203. sym = :thirty_seven_signals
  204. # fixtures are accessible in main session
  205. assert_not_nil companies(sym)
  206. # create a new session and the fixtures should be accessible in it as well
  207. session1 = open_session { |sess| }
  208. assert_not_nil session1.companies(sym)
  209. end
  210. end
  211. # Tests that integration tests don't call Controller test methods for processing.
  212. # Integration tests have their own setup and teardown.
  213. class IntegrationTestUsesCorrectClass < ActionController::IntegrationTest
  214. def self.fixture_table_names
  215. []
  216. end
  217. def test_integration_methods_called
  218. reset!
  219. @integration_session.stubs(:generic_url_rewriter)
  220. @integration_session.stubs(:process)
  221. %w( get post head put delete ).each do |verb|
  222. assert_nothing_raised("'#{verb}' should use integration test methods") { __send__(verb, '/') }
  223. end
  224. end
  225. end
  226. class IntegrationProcessTest < ActionController::IntegrationTest
  227. class IntegrationController < ActionController::Base
  228. def get
  229. respond_to do |format|
  230. format.html { render :text => "OK", :status => 200 }
  231. format.js { render :text => "JS OK", :status => 200 }
  232. end
  233. end
  234. def get_with_params
  235. render :text => "foo: #{params[:foo]}", :status => 200
  236. end
  237. def post_with_multiparameter_params
  238. render :text => "foo(1i): #{params[:"foo(1i)"]}, foo(2i): #{params[:"foo(2i)"]}", :status => 200
  239. end
  240. def multipart_post_with_multiparameter_params
  241. render :text => "foo(1i): #{params[:"foo(1i)"]}, foo(2i): #{params[:"foo(2i)"]}, filesize: #{params[:file].size}", :status => 200
  242. end
  243. def multipart_post_with_nested_params
  244. render :text => "foo: #{params[:foo][0]}, #{params[:foo][1]}; [filesize: #{params[:file_list][0][:content].size}, filesize: #{params[:file_list][1][:content].size}]", :status => 200
  245. end
  246. def multipart_post_with_multiparameter_complex_params
  247. render :text => "foo(1i): #{params[:"foo(1i)"]}, foo(2i): #{params[:"foo(2i)"]}, [filesize: #{params[:file_list][0][:content].size}, filesize: #{params[:file_list][1][:content].size}]", :status => 200
  248. end
  249. def post
  250. render :text => "Created", :status => 201
  251. end
  252. def cookie_monster
  253. cookies["cookie_1"] = nil
  254. cookies["cookie_3"] = "chocolate"
  255. render :text => "Gone", :status => 410
  256. end
  257. def redirect
  258. redirect_to :action => "get"
  259. end
  260. end
  261. FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart'
  262. def test_get
  263. with_test_route_set do
  264. get '/get'
  265. assert_equal 200, status
  266. assert_equal "OK", status_message
  267. assert_response 200
  268. assert_response :success
  269. assert_response :ok
  270. assert_equal({}, cookies)
  271. assert_equal "OK", body
  272. assert_equal "OK", response.body
  273. assert_kind_of HTML::Document, html_document
  274. assert_equal 1, request_count
  275. end
  276. end
  277. def test_post
  278. with_test_route_set do
  279. post '/post'
  280. assert_equal 201, status
  281. assert_equal "Created", status_message
  282. assert_response 201
  283. assert_response :success
  284. assert_response :created
  285. assert_equal({}, cookies)
  286. assert_equal "Created", body
  287. assert_equal "Created", response.body
  288. assert_kind_of HTML::Document, html_document
  289. assert_equal 1, request_count
  290. end
  291. end
  292. def test_cookie_monster
  293. with_test_route_set do
  294. self.cookies['cookie_1'] = "sugar"
  295. self.cookies['cookie_2'] = "oatmeal"
  296. get '/cookie_monster'
  297. assert_equal 410, status
  298. assert_equal "Gone", status_message
  299. assert_response 410
  300. assert_response :gone
  301. assert_equal({"cookie_1"=>"", "cookie_2"=>"oatmeal", "cookie_3"=>"chocolate"}, cookies)
  302. assert_equal "Gone", response.body
  303. end
  304. end
  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. follow_redirect!
  317. assert_response :success
  318. assert_equal "/get", path
  319. end
  320. end
  321. def test_xml_http_request_get
  322. with_test_route_set do
  323. xhr :get, '/get'
  324. assert_equal 200, status
  325. assert_equal "OK", status_message
  326. assert_response 200
  327. assert_response :success
  328. assert_response :ok
  329. assert_equal "JS OK", response.body
  330. end
  331. end
  332. def test_get_with_query_string
  333. with_test_route_set do
  334. get '/get_with_params?foo=bar'
  335. assert_equal '/get_with_params?foo=bar', request.env["REQUEST_URI"]
  336. assert_equal '/get_with_params?foo=bar', request.request_uri
  337. assert_equal "", request.env["QUERY_STRING"]
  338. assert_equal 'foo=bar', request.query_string
  339. assert_equal 'bar', request.parameters['foo']
  340. assert_equal 200, status
  341. assert_equal "foo: bar", response.body
  342. end
  343. end
  344. def test_get_with_parameters
  345. with_test_route_set do
  346. get '/get_with_params', :foo => "bar"
  347. assert_equal '/get_with_params', request.env["REQUEST_URI"]
  348. assert_equal '/get_with_params', request.request_uri
  349. assert_equal 'foo=bar', request.env["QUERY_STRING"]
  350. assert_equal 'foo=bar', request.query_string
  351. assert_equal 'bar', request.parameters['foo']
  352. assert_equal 200, status
  353. assert_equal "foo: bar", response.body
  354. end
  355. end
  356. def test_post_with_multiparameter_attribute_parameters
  357. with_test_route_set do
  358. post '/post_with_multiparameter_params', :"foo(1i)" => "bar", :"foo(2i)" => "baz"
  359. assert_equal 200, status
  360. assert_equal "foo(1i): bar, foo(2i): baz", response.body
  361. end
  362. end
  363. def test_multipart_post_with_multiparameter_attribute_parameters
  364. with_test_route_set do
  365. post '/multipart_post_with_multiparameter_params', :"foo(1i)" => "bar", :"foo(2i)" => "baz", :file => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")
  366. assert_equal 200, status
  367. assert_equal "foo(1i): bar, foo(2i): baz, filesize: 159528", response.body
  368. end
  369. end
  370. def test_multipart_post_with_nested_params
  371. with_test_route_set do
  372. post '/multipart_post_with_nested_params', :"foo" => ['a', 'b'], :file_list => [{:content => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")}, {:content => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")}]
  373. assert_equal 200, status
  374. assert_equal "foo: a, b; [filesize: 159528, filesize: 159528]", response.body
  375. end
  376. end
  377. def test_multipart_post_with_multiparameter_complex_attribute_parameters
  378. with_test_route_set do
  379. post '/multipart_post_with_multiparameter_complex_params', :"foo(1i)" => "bar", :"foo(2i)" => "baz", :file_list => [{:content => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")}, {:content => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")}]
  380. assert_equal 200, status
  381. assert_equal "foo(1i): bar, foo(2i): baz, [filesize: 159528, filesize: 159528]", response.body
  382. end
  383. end
  384. def test_head
  385. with_test_route_set do
  386. head '/get'
  387. assert_equal 200, status
  388. assert_equal "", body
  389. head '/post'
  390. assert_equal 201, status
  391. assert_equal "", body
  392. end
  393. end
  394. private
  395. def with_test_route_set
  396. with_routing do |set|
  397. set.draw do |map|
  398. map.with_options :controller => "IntegrationProcessTest::Integration" do |c|
  399. c.connect "/:action"
  400. end
  401. end
  402. yield
  403. end
  404. end
  405. end
  406. class MetalTest < ActionController::IntegrationTest
  407. class Poller
  408. def self.call(env)
  409. if env["PATH_INFO"] =~ /^\/success/
  410. [200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, ["Hello World!"]]
  411. else
  412. [404, {"Content-Type" => "text/plain", "Content-Length" => "0"}, []]
  413. end
  414. end
  415. end
  416. def setup
  417. @integration_session = ActionController::Integration::Session.new(Poller)
  418. end
  419. def test_successful_get
  420. get "/success"
  421. assert_response 200
  422. assert_response :success
  423. assert_response :ok
  424. assert_equal "Hello World!", response.body
  425. end
  426. def test_failed_get
  427. get "/failure"
  428. assert_response 404
  429. assert_response :not_found
  430. assert_equal '', response.body
  431. end
  432. end
  433. class StringSubclassBodyTest < ActionController::IntegrationTest
  434. class SafeString < String
  435. end
  436. class SafeStringMiddleware
  437. def self.call(env)
  438. [200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, [SafeString.new("Hello World!")]]
  439. end
  440. end
  441. def setup
  442. @integration_session = ActionController::Integration::Session.new(SafeStringMiddleware)
  443. end
  444. def test_string_subclass_body
  445. get '/'
  446. assert_equal 'Hello World!', response.body
  447. end
  448. end