Ticket #7124: xml_http_request_integration.diff
| File xml_http_request_integration.diff, 8.4 kB (added by Wizard, 2 years ago) |
|---|
-
actionpack/test/controller/integration_test.rb
old new 132 132 @session.head(path,params,headers) 133 133 end 134 134 135 def test_xml_http_request 135 def test_xml_http_request_deprecated_call 136 136 path = "/index"; params = "blah"; headers = {:location => 'blah'} 137 137 headers_after_xhr = headers.merge( 138 138 "X-Requested-With" => "XMLHttpRequest", 139 139 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" 140 140 ) 141 141 @session.expects(:post).with(path,params,headers_after_xhr) 142 @session.xml_http_request(path,params,headers)142 assert_deprecated { @session.xml_http_request(path,params,headers) } 143 143 end 144 145 def test_xml_http_request_get 146 path = "/index"; params = "blah"; headers = {:location => 'blah'} 147 headers_after_xhr = headers.merge( 148 "X-Requested-With" => "XMLHttpRequest", 149 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" 150 ) 151 @session.expects(:process).with(:get,path,params,headers_after_xhr) 152 @session.xml_http_request(:get,path,params,headers) 153 end 154 155 def test_xml_http_request_post 156 path = "/index"; params = "blah"; headers = {:location => 'blah'} 157 headers_after_xhr = headers.merge( 158 "X-Requested-With" => "XMLHttpRequest", 159 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" 160 ) 161 @session.expects(:process).with(:post,path,params,headers_after_xhr) 162 @session.xml_http_request(:post,path,params,headers) 163 end 164 165 def test_xml_http_request_put 166 path = "/index"; params = "blah"; headers = {:location => 'blah'} 167 headers_after_xhr = headers.merge( 168 "X-Requested-With" => "XMLHttpRequest", 169 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" 170 ) 171 @session.expects(:process).with(:put,path,params,headers_after_xhr) 172 @session.xml_http_request(:put,path,params,headers) 173 end 174 175 def test_xml_http_request_delete 176 path = "/index"; params = "blah"; headers = {:location => 'blah'} 177 headers_after_xhr = headers.merge( 178 "X-Requested-With" => "XMLHttpRequest", 179 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" 180 ) 181 @session.expects(:process).with(:delete,path,params,headers_after_xhr) 182 @session.xml_http_request(:delete,path,params,headers) 183 end 184 185 def test_xml_http_request_head 186 path = "/index"; params = "blah"; headers = {:location => 'blah'} 187 headers_after_xhr = headers.merge( 188 "X-Requested-With" => "XMLHttpRequest", 189 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" 190 ) 191 @session.expects(:process).with(:head,path,params,headers_after_xhr) 192 @session.xml_http_request(:head,path,params,headers) 193 end 144 194 end 145 195 146 196 class IntegrationTestTest < Test::Unit::TestCase … … 150 200 @test.class.stubs(:fixture_table_names).returns([]) 151 201 @session = @test.open_session 152 202 end 153 203 154 204 def test_opens_new_session 155 205 @test.class.expects(:fixture_table_names).times(2).returns(['foo']) 156 206 -
actionpack/lib/action_controller/integration.rb
old new 67 67 @https = false 68 68 @cookies = {} 69 69 @controller = @request = @response = nil 70 70 71 71 self.host = "www.example.com" 72 72 self.remote_addr = "127.0.0.1" 73 73 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 89 # session.https! 90 90 # session.https!(false) 91 91 def https!(flag=true) 92 @https = flag 92 @https = flag 93 93 end 94 94 95 95 # Return +true+ if the session is mimicing a secure HTTPS request. … … 143 143 # Performs a GET request with the given parameters. The parameters may 144 144 # be +nil+, a Hash, or a string that is appropriately encoded 145 145 # (application/x-www-form-urlencoded or multipart/form-data). The headers 146 # should be a hash. The keys will automatically be upcased, with the 146 # should be a hash. The keys will automatically be upcased, with the 147 147 # prefix 'HTTP_' added if needed. 148 148 # 149 # You can also perform POST, PUT, DELETE, and HEAD requests with #post, 149 # You can also perform POST, PUT, DELETE, and HEAD requests with #post, 150 150 # #put, #delete, and #head. 151 151 def get(path, parameters=nil, headers=nil) 152 152 process :get, path, parameters, headers … … 161 161 def put(path, parameters=nil, headers=nil) 162 162 process :put, path, parameters, headers 163 163 end 164 164 165 165 # Performs a DELETE request with the given parameters. See get() for more details. 166 166 def delete(path, parameters=nil, headers=nil) 167 167 process :delete, path, parameters, headers 168 168 end 169 169 170 170 # Performs a HEAD request with the given parameters. See get() for more details. 171 171 def head(path, parameters=nil, headers=nil) 172 172 process :head, path, parameters, headers 173 173 end 174 174 175 175 # Performs an XMLHttpRequest request with the given parameters, mimicing 176 # the request environment created by the Prototype library. The parameters 177 # may be +nil+, a Hash, or a string that is appropriately encoded 176 # the request environment created by the Prototype library. 177 # Specify the request_method with :get, :post, :put, :delete or :head. 178 # The parameters may be +nil+, a Hash, or a string that is appropriately encoded 178 179 # (application/x-www-form-urlencoded or multipart/form-data). The headers 179 # should be a hash. The keys will automatically be upcased, with the 180 # should be a hash. The keys will automatically be upcased, with the 180 181 # prefix 'HTTP_' added if needed. 181 def xml_http_request(path, parameters=nil, headers=nil) 182 headers = (headers || {}).merge( 183 "X-Requested-With" => "XMLHttpRequest", 184 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" 185 ) 186 187 post(path, parameters, headers) 182 # 183 # This method was previously implemented as: 184 # xml_http_request(path, parameters=nil, headers=nil, *extras) 185 def xml_http_request(request_method, path, parameters=nil, headers=nil) 186 if %w( get head put post delete ).include?(request_method.to_s.downcase) 187 headers = (headers || {}).merge( 188 "X-Requested-With" => "XMLHttpRequest", 189 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" 190 ) 191 process(request_method, path, parameters, headers) 192 else 193 ActiveSupport::Deprecation.warn( 194 "The API call xml_http_request(path, parameters=nil, headers=nil) " + 195 "now requires a request_method. xml_http_request(request_method, path, parameters=nil, headers=nil)." 196 ) 197 path, parameters, headers = request_method, path, parameters 198 headers = (headers || {}).merge( 199 "X-Requested-With" => "XMLHttpRequest", 200 "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" 201 ) 202 post(path, parameters, headers) 203 end 188 204 end 205 alias xhr :xml_http_request 189 206 190 207 # Returns the URL for the given options, according to the rules specified 191 208 # in the application's routes. … … 222 239 @path = path 223 240 env = {} 224 241 225 if method == :get242 if method.to_s.downcase == 'get' 226 243 env["QUERY_STRING"] = data 227 244 data = nil 228 245 end … … 291 308 @status = @status.to_i 292 309 end 293 310 294 # Encode the cookies hash in a format suitable for passing to a 311 # Encode the cookies hash in a format suitable for passing to a 295 312 # request. 296 313 def encode_cookies 297 314 cookies.inject("") do |string, (name, value)| … … 449 466 # without any test methods. 450 467 def run(*args) #:nodoc: 451 468 return if @method_name == "default_test" 452 super 469 super 453 470 end 454 471 455 472 # Because of how use_instantiated_fixtures and use_transactional_fixtures