From 07f92e832184a06c80cc2e5fcc18997ef580561b Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 21 May 2007 09:19:52 +0000 Subject: [PATCH] Merge [6796] to stable from trunk. Integration tests: alias xhr to xml_http_request and add a request_method argument instead of always using POST. References #7124. git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@6797 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 + .../lib/action_controller/integration.rb | 50 ++++++++++------- .../test/controller/integration_test.rb | 56 ++++++++++++++++++- 3 files changed, 85 insertions(+), 23 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 9e1d1eeb80e30..887c93215d4e9 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Integration tests: alias xhr to xml_http_request and add a request_method argument instead of always using POST. #7124 [Nik Wakelin, Francois Beausoleil, Wizard] + * Document caches_action. #5419 [Jarkko Laine] * observe_form always sends the serialized form. #5271 [manfred, normelton@gmail.com] diff --git a/actionpack/lib/action_controller/integration.rb b/actionpack/lib/action_controller/integration.rb index d77540a297bf4..c07e1b41a7102 100644 --- a/actionpack/lib/action_controller/integration.rb +++ b/actionpack/lib/action_controller/integration.rb @@ -67,7 +67,7 @@ def reset! @https = false @cookies = {} @controller = @request = @response = nil - + self.host = "www.example.com" self.remote_addr = "127.0.0.1" self.accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" @@ -89,7 +89,7 @@ def reset! # session.https! # session.https!(false) def https!(flag=true) - @https = flag + @https = flag end # Return +true+ if the session is mimicing a secure HTTPS request. @@ -143,10 +143,10 @@ def redirect? # Performs a GET request with the given parameters. The parameters may # be +nil+, a Hash, or a string that is appropriately encoded # (application/x-www-form-urlencoded or multipart/form-data). The headers - # should be a hash. The keys will automatically be upcased, with the + # should be a hash. The keys will automatically be upcased, with the # prefix 'HTTP_' added if needed. # - # You can also perform POST, PUT, DELETE, and HEAD requests with #post, + # You can also perform POST, PUT, DELETE, and HEAD requests with #post, # #put, #delete, and #head. def get(path, parameters=nil, headers=nil) process :get, path, parameters, headers @@ -161,31 +161,41 @@ def post(path, parameters=nil, headers=nil) def put(path, parameters=nil, headers=nil) process :put, path, parameters, headers end - + # Performs a DELETE request with the given parameters. See get() for more details. def delete(path, parameters=nil, headers=nil) process :delete, path, parameters, headers end - + # Performs a HEAD request with the given parameters. See get() for more details. def head(path, parameters=nil, headers=nil) process :head, path, parameters, headers end - # Performs an XMLHttpRequest request with the given parameters, mimicing - # the request environment created by the Prototype library. The parameters - # may be +nil+, a Hash, or a string that is appropriately encoded - # (application/x-www-form-urlencoded or multipart/form-data). The headers - # should be a hash. The keys will automatically be upcased, with the - # prefix 'HTTP_' added if needed. - def xml_http_request(path, parameters=nil, headers=nil) - headers = (headers || {}).merge( - "X-Requested-With" => "XMLHttpRequest", - "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" - ) + # Performs an XMLHttpRequest request with the given parameters, mirroring + # a request from the Prototype library. + # + # The request_method is :get, :post, :put, :delete or :head; the + # parameters are +nil+, a hash, or a url-encoded or multipart string; + # the headers are a hash. Keys are automatically upcased and prefixed + # with 'HTTP_' if not already. + # + # This method used to omit the request_method parameter, assuming it + # was :post. This was deprecated in Rails 1.2.4. Always pass the request + # method as the first argument. + def xml_http_request(request_method, path, parameters = nil, headers = nil) + unless request_method.is_a?(Symbol) + ActiveSupport::Deprecation.warn 'xml_http_request now takes the request_method (:get, :post, etc.) as the first argument. It used to assume :post, so add the :post argument to your existing method calls to silence this warning.' + request_method, path, parameters, headers = :post, request_method, path, parameters + end + + headers ||= {} + headers['X-Requested-With'] = 'XMLHttpRequest' + headers['Accept'] = 'text/javascript, text/html, application/xml, text/xml, */*' - post(path, parameters, headers) + process(request_method, path, parameters, headers) end + alias xhr :xml_http_request # Returns the URL for the given options, according to the rules specified # in the application's routes. @@ -292,7 +302,7 @@ def parse_result @status = @status.to_i end - # Encode the cookies hash in a format suitable for passing to a + # Encode the cookies hash in a format suitable for passing to a # request. def encode_cookies cookies.inject("") do |string, (name, value)| @@ -450,7 +460,7 @@ def initialize(name) #:nodoc: # without any test methods. def run(*args) #:nodoc: return if @method_name == "default_test" - super + super end # Because of how use_instantiated_fixtures and use_transactional_fixtures diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 665c5901e544b..b03fea75947a4 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -134,14 +134,64 @@ def test_head @session.head(path,params,headers) end - def test_xml_http_request + def test_xml_http_request_deprecated_call path = "/index"; params = "blah"; headers = {:location => 'blah'} headers_after_xhr = headers.merge( "X-Requested-With" => "XMLHttpRequest", "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" ) - @session.expects(:post).with(path,params,headers_after_xhr) - @session.xml_http_request(path,params,headers) + @session.expects(:process).with(:post,path,params,headers_after_xhr) + assert_deprecated { @session.xml_http_request(path,params,headers) } + end + + def test_xml_http_request_get + path = "/index"; params = "blah"; headers = {:location => 'blah'} + headers_after_xhr = headers.merge( + "X-Requested-With" => "XMLHttpRequest", + "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" + ) + @session.expects(:process).with(:get,path,params,headers_after_xhr) + @session.xml_http_request(:get,path,params,headers) + end + + def test_xml_http_request_post + path = "/index"; params = "blah"; headers = {:location => 'blah'} + headers_after_xhr = headers.merge( + "X-Requested-With" => "XMLHttpRequest", + "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" + ) + @session.expects(:process).with(:post,path,params,headers_after_xhr) + @session.xml_http_request(:post,path,params,headers) + end + + def test_xml_http_request_put + path = "/index"; params = "blah"; headers = {:location => 'blah'} + headers_after_xhr = headers.merge( + "X-Requested-With" => "XMLHttpRequest", + "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" + ) + @session.expects(:process).with(:put,path,params,headers_after_xhr) + @session.xml_http_request(:put,path,params,headers) + end + + def test_xml_http_request_delete + path = "/index"; params = "blah"; headers = {:location => 'blah'} + headers_after_xhr = headers.merge( + "X-Requested-With" => "XMLHttpRequest", + "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" + ) + @session.expects(:process).with(:delete,path,params,headers_after_xhr) + @session.xml_http_request(:delete,path,params,headers) + end + + def test_xml_http_request_head + path = "/index"; params = "blah"; headers = {:location => 'blah'} + headers_after_xhr = headers.merge( + "X-Requested-With" => "XMLHttpRequest", + "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" + ) + @session.expects(:process).with(:head,path,params,headers_after_xhr) + @session.xml_http_request(:head,path,params,headers) end end