PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/actionpack/test/controller/integration_test.rb

https://github.com/ghar/rails
Ruby | 538 lines | 447 code | 85 blank | 6 comment | 2 complexity | ddf5e9d4dde1ed0ecf1c63c8abd8088a MD5 | raw file
  1. require 'abstract_unit'
  2. require 'controller/fake_controllers'
  3. require 'action_controller/vendor/html-scanner'
  4. class SessionTest < Test::Unit::TestCase
  5. StubApp = lambda { |env|
  6. [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, ["Hello, World!"]]
  7. }
  8. def setup
  9. @session = ActionDispatch::Integration::Session.new(StubApp)
  10. end
  11. def test_https_bang_works_and_sets_truth_by_default
  12. assert !@session.https?
  13. @session.https!
  14. assert @session.https?
  15. @session.https! false
  16. assert !@session.https?
  17. end
  18. def test_host!
  19. assert_not_equal "glu.ttono.us", @session.host
  20. @session.host! "rubyonrails.com"
  21. assert_equal "rubyonrails.com", @session.host
  22. end
  23. def test_follow_redirect_raises_when_no_redirect
  24. @session.stubs(:redirect?).returns(false)
  25. assert_raise(RuntimeError) { @session.follow_redirect! }
  26. end
  27. def test_request_via_redirect_uses_given_method
  28. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
  29. @session.expects(:process).with(:put, path, args, headers)
  30. @session.stubs(:redirect?).returns(false)
  31. @session.request_via_redirect(:put, path, args, headers)
  32. end
  33. def test_request_via_redirect_follows_redirects
  34. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
  35. @session.stubs(:redirect?).returns(true, true, false)
  36. @session.expects(:follow_redirect!).times(2)
  37. @session.request_via_redirect(:get, path, args, headers)
  38. end
  39. def test_request_via_redirect_returns_status
  40. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
  41. @session.stubs(:redirect?).returns(false)
  42. @session.stubs(:status).returns(200)
  43. assert_equal 200, @session.request_via_redirect(:get, path, args, headers)
  44. end
  45. def test_get_via_redirect
  46. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
  47. @session.expects(:request_via_redirect).with(:get, path, args, headers)
  48. @session.get_via_redirect(path, args, headers)
  49. end
  50. def test_post_via_redirect
  51. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
  52. @session.expects(:request_via_redirect).with(:post, path, args, headers)
  53. @session.post_via_redirect(path, args, headers)
  54. end
  55. def test_put_via_redirect
  56. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
  57. @session.expects(:request_via_redirect).with(:put, path, args, headers)
  58. @session.put_via_redirect(path, args, headers)
  59. end
  60. def test_delete_via_redirect
  61. path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" }
  62. @session.expects(:request_via_redirect).with(:delete, path, args, headers)
  63. @session.delete_via_redirect(path, args, headers)
  64. end
  65. def test_get
  66. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  67. @session.expects(:process).with(:get,path,params,headers)
  68. @session.get(path,params,headers)
  69. end
  70. def test_post
  71. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  72. @session.expects(:process).with(:post,path,params,headers)
  73. @session.post(path,params,headers)
  74. end
  75. def test_put
  76. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  77. @session.expects(:process).with(:put,path,params,headers)
  78. @session.put(path,params,headers)
  79. end
  80. def test_delete
  81. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  82. @session.expects(:process).with(:delete,path,params,headers)
  83. @session.delete(path,params,headers)
  84. end
  85. def test_head
  86. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  87. @session.expects(:process).with(:head,path,params,headers)
  88. @session.head(path,params,headers)
  89. end
  90. def test_xml_http_request_get
  91. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  92. headers_after_xhr = headers.merge(
  93. "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
  94. "HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
  95. )
  96. @session.expects(:process).with(:get,path,params,headers_after_xhr)
  97. @session.xml_http_request(:get,path,params,headers)
  98. end
  99. def test_xml_http_request_post
  100. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  101. headers_after_xhr = headers.merge(
  102. "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
  103. "HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
  104. )
  105. @session.expects(:process).with(:post,path,params,headers_after_xhr)
  106. @session.xml_http_request(:post,path,params,headers)
  107. end
  108. def test_xml_http_request_put
  109. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  110. headers_after_xhr = headers.merge(
  111. "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
  112. "HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
  113. )
  114. @session.expects(:process).with(:put,path,params,headers_after_xhr)
  115. @session.xml_http_request(:put,path,params,headers)
  116. end
  117. def test_xml_http_request_delete
  118. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  119. headers_after_xhr = headers.merge(
  120. "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
  121. "HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
  122. )
  123. @session.expects(:process).with(:delete,path,params,headers_after_xhr)
  124. @session.xml_http_request(:delete,path,params,headers)
  125. end
  126. def test_xml_http_request_head
  127. path = "/index"; params = "blah"; headers = {:location => 'blah'}
  128. headers_after_xhr = headers.merge(
  129. "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
  130. "HTTP_ACCEPT" => "text/javascript, text/html, application/xml, text/xml, */*"
  131. )
  132. @session.expects(:process).with(:head,path,params,headers_after_xhr)
  133. @session.xml_http_request(:head,path,params,headers)
  134. end
  135. def test_xml_http_request_override_accept
  136. path = "/index"; params = "blah"; headers = {:location => 'blah', "HTTP_ACCEPT" => "application/xml"}
  137. headers_after_xhr = headers.merge(
  138. "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"
  139. )
  140. @session.expects(:process).with(:post,path,params,headers_after_xhr)
  141. @session.xml_http_request(:post,path,params,headers)
  142. end
  143. end
  144. class IntegrationTestTest < Test::Unit::TestCase
  145. def setup
  146. @test = ::ActionDispatch::IntegrationTest.new(:app)
  147. @test.class.stubs(:fixture_table_names).returns([])
  148. @session = @test.open_session
  149. end
  150. def test_opens_new_session
  151. session1 = @test.open_session { |sess| }
  152. session2 = @test.open_session # implicit session
  153. assert_respond_to session1, :assert_template, "open_session makes assert_template available"
  154. assert_respond_to session2, :assert_template, "open_session makes assert_template available"
  155. assert !session1.equal?(session2)
  156. end
  157. # RSpec mixes Matchers (which has a #method_missing) into
  158. # IntegrationTest's superclass. Make sure IntegrationTest does not
  159. # try to delegate these methods to the session object.
  160. def test_does_not_prevent_method_missing_passing_up_to_ancestors
  161. mixin = Module.new do
  162. def method_missing(name, *args)
  163. name.to_s == 'foo' ? 'pass' : super
  164. end
  165. end
  166. @test.class.superclass.__send__(:include, mixin)
  167. begin
  168. assert_equal 'pass', @test.foo
  169. ensure
  170. # leave other tests as unaffected as possible
  171. mixin.__send__(:remove_method, :method_missing)
  172. end
  173. end
  174. end
  175. # Tests that integration tests don't call Controller test methods for processing.
  176. # Integration tests have their own setup and teardown.
  177. class IntegrationTestUsesCorrectClass < ActionDispatch::IntegrationTest
  178. def self.fixture_table_names
  179. []
  180. end
  181. def test_integration_methods_called
  182. reset!
  183. @integration_session.stubs(:generic_url_rewriter)
  184. @integration_session.stubs(:process)
  185. %w( get post head put delete ).each do |verb|
  186. assert_nothing_raised("'#{verb}' should use integration test methods") { __send__(verb, '/') }
  187. end
  188. end
  189. end
  190. class IntegrationProcessTest < ActionDispatch::IntegrationTest
  191. class IntegrationController < ActionController::Base
  192. def get
  193. respond_to do |format|
  194. format.html { render :text => "OK", :status => 200 }
  195. format.js { render :text => "JS OK", :status => 200 }
  196. end
  197. end
  198. def get_with_params
  199. render :text => "foo: #{params[:foo]}", :status => 200
  200. end
  201. def post
  202. render :text => "Created", :status => 201
  203. end
  204. def method
  205. render :text => "method: #{request.method.downcase}"
  206. end
  207. def cookie_monster
  208. cookies["cookie_1"] = nil
  209. cookies["cookie_3"] = "chocolate"
  210. render :text => "Gone", :status => 410
  211. end
  212. def set_cookie
  213. cookies["foo"] = 'bar'
  214. head :ok
  215. end
  216. def get_cookie
  217. render :text => cookies["foo"]
  218. end
  219. def redirect
  220. redirect_to :action => "get"
  221. end
  222. end
  223. def test_get
  224. with_test_route_set do
  225. get '/get'
  226. assert_equal 200, status
  227. assert_equal "OK", status_message
  228. assert_response 200
  229. assert_response :success
  230. assert_response :ok
  231. assert_equal({}, cookies.to_hash)
  232. assert_equal "OK", body
  233. assert_equal "OK", response.body
  234. assert_kind_of HTML::Document, html_document
  235. assert_equal 1, request_count
  236. end
  237. end
  238. def test_post
  239. with_test_route_set do
  240. post '/post'
  241. assert_equal 201, status
  242. assert_equal "Created", status_message
  243. assert_response 201
  244. assert_response :success
  245. assert_response :created
  246. assert_equal({}, cookies.to_hash)
  247. assert_equal "Created", body
  248. assert_equal "Created", response.body
  249. assert_kind_of HTML::Document, html_document
  250. assert_equal 1, request_count
  251. end
  252. end
  253. test 'response cookies are added to the cookie jar for the next request' do
  254. with_test_route_set do
  255. self.cookies['cookie_1'] = "sugar"
  256. self.cookies['cookie_2'] = "oatmeal"
  257. get '/cookie_monster'
  258. assert_equal "cookie_1=; path=/\ncookie_3=chocolate; path=/", headers["Set-Cookie"]
  259. assert_equal({"cookie_1"=>"", "cookie_2"=>"oatmeal", "cookie_3"=>"chocolate"}, cookies.to_hash)
  260. end
  261. end
  262. test 'cookie persist to next request' do
  263. with_test_route_set do
  264. get '/set_cookie'
  265. assert_response :success
  266. assert_equal "foo=bar; path=/", headers["Set-Cookie"]
  267. assert_equal({"foo"=>"bar"}, cookies.to_hash)
  268. get '/get_cookie'
  269. assert_response :success
  270. assert_equal "bar", body
  271. assert_equal nil, headers["Set-Cookie"]
  272. assert_equal({"foo"=>"bar"}, cookies.to_hash)
  273. end
  274. end
  275. test 'cookie persist to next request on another domain' do
  276. with_test_route_set do
  277. host! "37s.backpack.test"
  278. get '/set_cookie'
  279. assert_response :success
  280. assert_equal "foo=bar; path=/", headers["Set-Cookie"]
  281. assert_equal({"foo"=>"bar"}, cookies.to_hash)
  282. get '/get_cookie'
  283. assert_response :success
  284. assert_equal "bar", body
  285. assert_equal nil, headers["Set-Cookie"]
  286. assert_equal({"foo"=>"bar"}, cookies.to_hash)
  287. end
  288. end
  289. def test_redirect
  290. with_test_route_set do
  291. get '/redirect'
  292. assert_equal 302, status
  293. assert_equal "Found", status_message
  294. assert_response 302
  295. assert_response :redirect
  296. assert_response :found
  297. assert_equal "<html><body>You are being <a href=\"http://www.example.com/get\">redirected</a>.</body></html>", response.body
  298. assert_kind_of HTML::Document, html_document
  299. assert_equal 1, request_count
  300. follow_redirect!
  301. assert_response :success
  302. assert_equal "/get", path
  303. end
  304. end
  305. def test_xml_http_request_get
  306. with_test_route_set do
  307. xhr :get, '/get'
  308. assert_equal 200, status
  309. assert_equal "OK", status_message
  310. assert_response 200
  311. assert_response :success
  312. assert_response :ok
  313. assert_equal "JS OK", response.body
  314. end
  315. end
  316. def test_get_with_query_string
  317. with_test_route_set do
  318. get '/get_with_params?foo=bar'
  319. assert_equal '/get_with_params?foo=bar', request.env["REQUEST_URI"]
  320. assert_equal '/get_with_params?foo=bar', request.fullpath
  321. assert_equal "foo=bar", request.env["QUERY_STRING"]
  322. assert_equal 'foo=bar', request.query_string
  323. assert_equal 'bar', request.parameters['foo']
  324. assert_equal 200, status
  325. assert_equal "foo: bar", response.body
  326. end
  327. end
  328. def test_get_with_parameters
  329. with_test_route_set do
  330. get '/get_with_params', :foo => "bar"
  331. assert_equal '/get_with_params', request.env["PATH_INFO"]
  332. assert_equal '/get_with_params', request.path_info
  333. assert_equal 'foo=bar', request.env["QUERY_STRING"]
  334. assert_equal 'foo=bar', request.query_string
  335. assert_equal 'bar', request.parameters['foo']
  336. assert_equal 200, status
  337. assert_equal "foo: bar", response.body
  338. end
  339. end
  340. def test_head
  341. with_test_route_set do
  342. head '/get'
  343. assert_equal 200, status
  344. assert_equal "", body
  345. head '/post'
  346. assert_equal 201, status
  347. assert_equal "", body
  348. get '/get/method'
  349. assert_equal 200, status
  350. assert_equal "method: get", body
  351. head '/get/method'
  352. assert_equal 200, status
  353. assert_equal "", body
  354. end
  355. end
  356. def test_generate_url_with_controller
  357. assert_equal 'http://www.example.com/foo', url_for(:controller => "foo")
  358. end
  359. private
  360. def with_test_route_set
  361. with_routing do |set|
  362. controller = ::IntegrationProcessTest::IntegrationController.clone
  363. controller.class_eval do
  364. include set.url_helpers
  365. end
  366. set.draw do
  367. match ':action', :to => controller
  368. get 'get/:action', :to => controller
  369. end
  370. self.singleton_class.send(:include, set.url_helpers)
  371. yield
  372. end
  373. end
  374. end
  375. class MetalIntegrationTest < ActionDispatch::IntegrationTest
  376. include SharedTestRoutes.url_helpers
  377. class Poller
  378. def self.call(env)
  379. if env["PATH_INFO"] =~ /^\/success/
  380. [200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, ["Hello World!"]]
  381. else
  382. [404, {"Content-Type" => "text/plain", "Content-Length" => "0"}, []]
  383. end
  384. end
  385. end
  386. def setup
  387. @app = Poller
  388. end
  389. def test_successful_get
  390. get "/success"
  391. assert_response 200
  392. assert_response :success
  393. assert_response :ok
  394. assert_equal "Hello World!", response.body
  395. end
  396. def test_failed_get
  397. get "/failure"
  398. assert_response 404
  399. assert_response :not_found
  400. assert_equal '', response.body
  401. end
  402. def test_generate_url_without_controller
  403. assert_equal 'http://www.example.com/foo', url_for(:controller => "foo")
  404. end
  405. end
  406. class ApplicationIntegrationTest < ActionDispatch::IntegrationTest
  407. class TestController < ActionController::Base
  408. def index
  409. render :text => "index"
  410. end
  411. end
  412. def self.call(env)
  413. routes.call(env)
  414. end
  415. def self.routes
  416. @routes ||= ActionDispatch::Routing::RouteSet.new
  417. end
  418. routes.draw do
  419. match '', :to => 'application_integration_test/test#index', :as => :empty_string
  420. match 'foo', :to => 'application_integration_test/test#index', :as => :foo
  421. match 'bar', :to => 'application_integration_test/test#index', :as => :bar
  422. end
  423. def app
  424. self.class
  425. end
  426. test "includes route helpers" do
  427. assert_equal '/', empty_string_path
  428. assert_equal '/foo', foo_path
  429. assert_equal '/bar', bar_path
  430. end
  431. test "route helpers after controller access" do
  432. get '/'
  433. assert_equal '/', empty_string_path
  434. get '/foo'
  435. assert_equal '/foo', foo_path
  436. get '/bar'
  437. assert_equal '/bar', bar_path
  438. end
  439. test "missing route helper before controller access" do
  440. assert_raise(NameError) { missing_path }
  441. end
  442. test "missing route helper after controller access" do
  443. get '/foo'
  444. assert_raise(NameError) { missing_path }
  445. end
  446. test "process reuse the env we pass as argument" do
  447. env = { :SERVER_NAME => 'server', 'action_dispatch.custom' => 'custom' }
  448. get '/foo', nil, env
  449. assert_equal :get, env[:method]
  450. assert_equal 'server', env[:SERVER_NAME]
  451. assert_equal 'custom', env['action_dispatch.custom']
  452. end
  453. end