Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ActionController::VerbPiggybacking middleware

  • Loading branch information...
commit 9c1e48eaea921efa67fbeed1ff1876dc710f8fd2 1 parent 3562d54
Joshua Peek josh authored
1  actionpack/lib/action_controller.rb
View
@@ -74,6 +74,7 @@ def self.load_all!
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
11 actionpack/lib/action_controller/integration.rb
View
@@ -2,6 +2,17 @@
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
2  actionpack/lib/action_controller/middlewares.rb
View
@@ -17,3 +17,5 @@
}
)
end
+
+use ActionController::VerbPiggybacking
20 actionpack/lib/action_controller/request.rb
View
@@ -45,8 +45,6 @@ def key?(key)
# 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 @@ def etag_matches?(etag)
# 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.
24 actionpack/lib/action_controller/verb_piggybacking.rb
View
@@ -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
23 actionpack/test/controller/rack_test.rb
View
@@ -187,29 +187,6 @@ def test_xml_content_type_verification
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'
6 actionpack/test/controller/request_test.rb
View
@@ -303,18 +303,16 @@ def test_invalid_http_method_raises_exception
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
Please sign in to comment.
Something went wrong with that request. Please try again.