Skip to content

Commit

Permalink
Merge [6796] to stable from trunk. Integration tests: alias xhr to xm…
Browse files Browse the repository at this point in the history
…l_http_request and add a request_method argument instead of always using POST. References rails#7124.

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@6797 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed May 21, 2007
1 parent 788d4b0 commit 07f92e8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 23 deletions.
2 changes: 2 additions & 0 deletions 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]
Expand Down
50 changes: 30 additions & 20 deletions actionpack/lib/action_controller/integration.rb
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)|
Expand Down Expand Up @@ -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
Expand Down
56 changes: 53 additions & 3 deletions actionpack/test/controller/integration_test.rb
Expand Up @@ -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

Expand Down

0 comments on commit 07f92e8

Please sign in to comment.