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

Changeset 6796

Show
Ignore:
Timestamp:
05/21/07 09:15:16 (1 year ago)
Author:
bitsweat
Message:

Integration tests: alias xhr to xml_http_request and add a request_method argument instead of always using POST. Closes #7124.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r6787 r6796  
    11*SVN* 
     2 
     3* 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] 
    24 
    35* Document caches_action.  #5419 [Jarkko Laine] 
  • trunk/actionpack/lib/action_controller/integration.rb

    r6764 r6796  
    6868        @cookies = {} 
    6969        @controller = @request = @response = nil 
    70        
     70 
    7171        self.host        = "www.example.com" 
    7272        self.remote_addr = "127.0.0.1" 
     
    9090      #   session.https!(false) 
    9191      def https!(flag=true) 
    92         @https = flag         
     92        @https = flag 
    9393      end 
    9494 
     
    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) 
     
    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) 
     
    173173      end 
    174174 
    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 
    178       # (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       # 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) 
    188       end 
     175      # Performs an XMLHttpRequest request with the given parameters, mirroring 
     176      # a request from the Prototype library. 
     177      # 
     178      # The request_method is :get, :post, :put, :delete or :head; the 
     179      # parameters are +nil+, a hash, or a url-encoded or multipart string; 
     180      # the headers are a hash.  Keys are automatically upcased and prefixed 
     181      # with 'HTTP_' if not already. 
     182      # 
     183      # This method used to omit the request_method parameter, assuming it 
     184      # was :post. This was deprecated in Rails 1.2.4. Always pass the request 
     185      # method as the first argument. 
     186      def xml_http_request(request_method, path, parameters = nil, headers = nil) 
     187        unless request_method.is_a?(Symbol) 
     188          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.' 
     189          request_method, path, parameters, headers = :post, request_method, path, parameters 
     190        end 
     191 
     192        headers ||= {} 
     193        headers['X-Requested-With'] = 'XMLHttpRequest' 
     194        headers['Accept'] = 'text/javascript, text/html, application/xml, text/xml, */*' 
     195 
     196        process(request_method, path, parameters, headers) 
     197      end 
     198      alias xhr :xml_http_request 
    189199 
    190200      # Returns the URL for the given options, according to the rules specified 
     
    292302        end 
    293303 
    294         # Encode the cookies hash in a format suitable for passing to a  
     304        # Encode the cookies hash in a format suitable for passing to a 
    295305        # request. 
    296306        def encode_cookies 
     
    450460    def run(*args) #:nodoc: 
    451461      return if @method_name == "default_test" 
    452       super    
     462      super 
    453463    end 
    454464 
  • trunk/actionpack/test/controller/integration_test.rb

    r6203 r6796  
    133133  end 
    134134 
    135   def test_xml_http_request 
    136     path = "/index"; params = "blah"; headers = {:location => 'blah'} 
    137     headers_after_xhr = headers.merge( 
    138       "X-Requested-With" => "XMLHttpRequest", 
    139       "Accept"           => "text/javascript, text/html, application/xml, text/xml, */*" 
    140     ) 
    141     @session.expects(:post).with(path,params,headers_after_xhr) 
    142     @session.xml_http_request(path,params,headers) 
     135  def test_xml_http_request_deprecated_call 
     136    path = "/index"; params = "blah"; headers = {:location => 'blah'} 
     137    headers_after_xhr = headers.merge( 
     138      "X-Requested-With" => "XMLHttpRequest", 
     139      "Accept"           => "text/javascript, text/html, application/xml, text/xml, */*" 
     140    ) 
     141    @session.expects(:process).with(:post,path,params,headers_after_xhr) 
     142    assert_deprecated { @session.xml_http_request(path,params,headers) } 
     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) 
    143193  end 
    144194end 
     
    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'])