1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

ActionController::VerbPiggybacking middleware

This commit is contained in:
Joshua Peek 2008-12-23 13:36:05 -06:00
parent 3562d54d18
commit 9c1e48eaea
7 changed files with 49 additions and 38 deletions

View file

@ -74,6 +74,7 @@ module ActionController
autoload :Translation, 'action_controller/translation'
autoload :UrlRewriter, 'action_controller/url_rewriter'
autoload :UrlWriter, 'action_controller/url_rewriter'
autoload :VerbPiggybacking, 'action_controller/verb_piggybacking'
autoload :Verification, 'action_controller/verification'
module Assertions

View file

@ -2,6 +2,17 @@ require 'stringio'
require 'uri'
require 'active_support/test_case'
# Monkey patch Rack::Lint to support rewind
module Rack
class Lint
class InputWrapper
def rewind
@input.rewind
end
end
end
end
module ActionController
module Integration #:nodoc:
# An integration Session instance represents a set of requests and responses

View file

@ -17,3 +17,5 @@ use "ActiveRecord::QueryCache", :if => lambda { defined?(ActiveRecord) }
}
)
end
use ActionController::VerbPiggybacking

View file

@ -45,8 +45,6 @@ module ActionController
# UnknownHttpMethod is raised for invalid methods not listed in ACCEPTED_HTTP_METHODS.
def request_method
method = @env['REQUEST_METHOD']
method = parameters[:_method] if method == 'POST' && !parameters[:_method].blank?
HTTP_METHOD_LOOKUP[method] || raise(UnknownHttpMethod, "#{method}, accepted HTTP methods are #{HTTP_METHODS.to_sentence}")
end
memoize :request_method
@ -143,15 +141,15 @@ module ActionController
# supplied, both must match, or the request is not considered fresh.
def fresh?(response)
case
when if_modified_since && if_none_match
not_modified?(response.last_modified) && etag_matches?(response.etag)
when if_modified_since
not_modified?(response.last_modified)
when if_none_match
etag_matches?(response.etag)
else
false
end
when if_modified_since && if_none_match
not_modified?(response.last_modified) && etag_matches?(response.etag)
when if_modified_since
not_modified?(response.last_modified)
when if_none_match
etag_matches?(response.etag)
else
false
end
end
# Returns the Mime type for the \format used in the request.

View file

@ -0,0 +1,24 @@
module ActionController
# TODO: Use Rack::MethodOverride when it is released
class VerbPiggybacking
HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
def initialize(app)
@app = app
end
def call(env)
if env["REQUEST_METHOD"] == "POST"
req = Request.new(env)
if method = (req.parameters[:_method] || env["HTTP_X_HTTP_METHOD_OVERRIDE"])
method = method.to_s.upcase
if HTTP_METHODS.include?(method)
env["REQUEST_METHOD"] = method
end
end
end
@app.call(env)
end
end
end

View file

@ -187,29 +187,6 @@ class RackRequestContentTypeTest < BaseRackTest
end
end
class RackRequestMethodTest < BaseRackTest
def test_get
assert_equal :get, @request.request_method
end
def test_post
@request.env['REQUEST_METHOD'] = 'POST'
assert_equal :post, @request.request_method
end
def test_put
set_content_data '_method=put'
assert_equal :put, @request.request_method
end
def test_delete
set_content_data '_method=delete'
assert_equal :delete, @request.request_method
end
end
class RackRequestNeedsRewoundTest < BaseRackTest
def test_body_should_be_rewound
data = 'foo'

View file

@ -303,18 +303,16 @@ class RequestTest < ActiveSupport::TestCase
end
def test_allow_method_hacking_on_post
self.request_method = :post
[:get, :head, :options, :put, :post, :delete].each do |method|
@request.instance_eval { @parameters = { :_method => method.to_s } ; @request_method = nil }
self.request_method = method
@request.request_method(true)
assert_equal(method == :head ? :get : method, @request.method)
end
end
def test_invalid_method_hacking_on_post_raises_exception
self.request_method = :post
@request.instance_eval { @parameters = { :_method => :random_method } ; @request_method = nil }
assert_raises(ActionController::UnknownHttpMethod) do
self.request_method = :_random_method
@request.request_method(true)
end
end