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

Ticket #10497: request_via_redirect.patch

File request_via_redirect.patch, 5.5 kB (added by philodespotos, 2 years ago)
  • a/actionpack/lib/action_controller/integration.rb

    old new  
    121121        status 
    122122      end 
    123123 
    124       # Performs a GET request, following any subsequent redirect. Note tha
    125       # the redirects are followed until the response is not a redirect--this 
    126       # means you may run into an infinite loop if your redirect loops back to 
    127       # itself. Headers are treated in the same way as #get
    128       def get_via_redirect(path, args={}, headers = {}
    129         get path, args, headers 
     124      # Performs a request using the specified method, following any subsequen
     125      # redirect. Note that the redirects are followed until the response is 
     126      # not a redirect--this means you may run into an infinite loop if your 
     127      # redirect loops back to itself
     128      def request_via_redirect(http_method, path, parameters = nil, headers = nil
     129        send(http_method, path, parameters, headers) 
    130130        follow_redirect! while redirect? 
    131131        status 
    132132      end 
    133133 
    134       # Performs a POST request, following any subsequent redirect. This is 
    135       # vulnerable to infinite loops, the same as #get_via_redirect. Headers are 
    136       # treated in the same way as #get. 
    137       def post_via_redirect(path, args={}, headers = {}) 
    138         post path, args, headers 
    139         follow_redirect! while redirect? 
    140         status 
     134      # Performs a GET request, following any subsequent redirect. 
     135      # See #request_via_redirect() for more information. 
     136      def get_via_redirect(path, parameters = nil, headers = nil) 
     137        request_via_redirect(:get, path, parameters, headers) 
     138      end 
     139 
     140      # Performs a POST request, following any subsequent redirect. 
     141      # See #request_via_redirect() for more information. 
     142      def post_via_redirect(path, parameters = nil, headers = nil) 
     143        request_via_redirect(:post, path, parameters, headers) 
     144      end 
     145 
     146      # Performs a PUT request, following any subsequent redirect. 
     147      # See #request_via_redirect() for more information. 
     148      def put_via_redirect(path, parameters = nil, headers = nil) 
     149        request_via_redirect(:put, path, parameters, headers) 
     150      end 
     151 
     152      # Performs a DELETE request, following any subsequent redirect. 
     153      # See #request_via_redirect() for more information. 
     154      def delete_via_redirect(path, parameters = nil, headers = nil) 
     155        request_via_redirect(:delete, path, parameters, headers) 
    141156      end 
    142157 
    143158      # Returns +true+ if the last response was a redirect. 
  • a/actionpack/test/controller/integration_test.rb

    old new  
    4949    assert_equal 200, @session.follow_redirect! 
    5050  end 
    5151 
    52   def test_get_via_redirect 
    53     path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" } 
    54  
    55     @session.expects(:get).with(path,args,headers) 
     52  def test_request_via_redirect_uses_given_method 
     53    path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"} 
     54    @session.expects(:put).with(path, args, headers) 
     55    @session.stubs(:redirect?).returns(false) 
     56    @session.request_via_redirect(:put, path, args, headers) 
     57  end 
    5658 
     59  def test_request_via_redirect_follows_redirects 
     60    path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"} 
    5761    @session.stubs(:redirect?).returns(true, true, false) 
    5862    @session.expects(:follow_redirect!).times(2) 
     63    @session.request_via_redirect(:get, path, args, headers) 
     64  end 
    5965 
     66  def test_request_via_redirect_returns_status 
     67    path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"} 
     68    @session.stubs(:redirect?).returns(false) 
    6069    @session.stubs(:status).returns(200) 
    61     assert_equal 200, @session.get_via_redirect(path, args, headers) 
     70    assert_equal 200, @session.request_via_redirect(:get, path, args, headers) 
    6271  end 
    6372 
    64   def test_post_via_redirect 
     73  def test_get_via_redirect 
    6574    path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" } 
     75    @session.expects(:request_via_redirect).with(:get, path, args, headers) 
     76    @session.get_via_redirect(path, args, headers) 
     77  end 
    6678 
    67     @session.expects(:post).with(path,args,headers) 
     79  def test_post_via_redirect 
     80    path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" } 
     81    @session.expects(:request_via_redirect).with(:post, path, args, headers) 
     82    @session.post_via_redirect(path, args, headers) 
     83  end 
    6884 
    69     @session.stubs(:redirect?).returns(true, true, false) 
    70     @session.expects(:follow_redirect!).times(2) 
     85  def test_put_via_redirect 
     86    path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" } 
     87    @session.expects(:request_via_redirect).with(:put, path, args, headers) 
     88    @session.put_via_redirect(path, args, headers) 
     89  end 
    7190 
    72     @session.stubs(:status).returns(200) 
    73     assert_equal 200, @session.post_via_redirect(path, args, headers) 
     91  def test_delete_via_redirect 
     92    path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue" } 
     93    @session.expects(:request_via_redirect).with(:delete, path, args, headers) 
     94    @session.delete_via_redirect(path, args, headers) 
    7495  end 
    7596 
    7697  def test_url_for_with_controller