Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

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  
    132132    @session.head(path,params,headers) 
    133133  end 
    134134 
    135   def test_xml_http_request 
     135  def test_xml_http_request_deprecated_call 
    136136    path = "/index"; params = "blah"; headers = {:location => 'blah'} 
    137137    headers_after_xhr = headers.merge( 
    138138      "X-Requested-With" => "XMLHttpRequest", 
    139139      "Accept"           => "text/javascript, text/html, application/xml, text/xml, */*" 
    140140    ) 
    141141    @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) } 
    143143  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 
    144194end 
    145195 
    146196class IntegrationTestTest < Test::Unit::TestCase 
     
    150200    @test.class.stubs(:fixture_table_names).returns([]) 
    151201    @session = @test.open_session 
    152202  end 
    153    
     203 
    154204  def test_opens_new_session 
    155205    @test.class.expects(:fixture_table_names).times(2).returns(['foo']) 
    156206 
  • actionpack/lib/action_controller/integration.rb

    old new  
    6767        @https = false 
    6868        @cookies = {} 
    6969        @controller = @request = @response = nil 
    70        
     70 
    7171        self.host        = "www.example.com" 
    7272        self.remote_addr = "127.0.0.1" 
    7373        self.accept      = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" 
     
    8989      #   session.https! 
    9090      #   session.https!(false) 
    9191      def https!(flag=true) 
    92         @https = flag         
     92        @https = flag 
    9393      end 
    9494 
    9595      # Return +true+ if the session is mimicing a secure HTTPS request. 
     
    143143      # Performs a GET request with the given parameters. The parameters may 
    144144      # be +nil+, a Hash, or a string that is appropriately encoded 
    145145      # (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 
    147147      # prefix 'HTTP_' added if needed. 
    148148      # 
    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, 
    150150      # #put, #delete, and #head. 
    151151      def get(path, parameters=nil, headers=nil) 
    152152        process :get, path, parameters, headers 
     
    161161      def put(path, parameters=nil, headers=nil) 
    162162        process :put, path, parameters, headers 
    163163      end 
    164        
     164 
    165165      # Performs a DELETE request with the given parameters. See get() for more details. 
    166166      def delete(path, parameters=nil, headers=nil) 
    167167        process :delete, path, parameters, headers 
    168168      end 
    169        
     169 
    170170      # Performs a HEAD request with the given parameters. See get() for more details. 
    171171      def head(path, parameters=nil, headers=nil) 
    172172        process :head, path, parameters, headers 
    173173      end 
    174174 
    175175      # 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 
    178179      # (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 
    180181      # 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 
    188204      end 
     205      alias xhr :xml_http_request 
    189206 
    190207      # Returns the URL for the given options, according to the rules specified 
    191208      # in the application's routes. 
     
    222239          @path = path 
    223240          env = {} 
    224241 
    225           if method == :get 
     242          if method.to_s.downcase == 'get' 
    226243            env["QUERY_STRING"] = data 
    227244            data = nil 
    228245          end 
     
    291308          @status = @status.to_i 
    292309        end 
    293310 
    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 
    295312        # request. 
    296313        def encode_cookies 
    297314          cookies.inject("") do |string, (name, value)| 
     
    449466    # without any test methods. 
    450467    def run(*args) #:nodoc: 
    451468      return if @method_name == "default_test" 
    452       super    
     469      super 
    453470    end 
    454471 
    455472    # Because of how use_instantiated_fixtures and use_transactional_fixtures