Skip to content

Commit

Permalink
handle failure to upcase invalid strings
Browse files Browse the repository at this point in the history
Some strings cannot be upper cased and raise an ArgumentError instead.
Let's capture this error and log its occurence to the environment via
rack.errors
  • Loading branch information
mclark committed Apr 16, 2018
1 parent 274d934 commit b27dd86
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/rack/methodoverride.rb
Expand Up @@ -26,7 +26,11 @@ def method_override(env)
req = Request.new(env)
method = method_override_param(req) ||
env[HTTP_METHOD_OVERRIDE_HEADER]
method.to_s.upcase
begin
method.to_s.upcase
rescue ArgumentError
env["rack.errors"].puts "Invalid string for method"
end
end

private
Expand Down
18 changes: 17 additions & 1 deletion test/spec_methodoverride.rb
Expand Up @@ -8,7 +8,7 @@ def app
[200, {"Content-Type" => "text/plain"}, []]
}))
end

should "not affect GET requests" do
env = Rack::MockRequest.env_for("/?_method=delete", :method => "GET")
app.call env
Expand All @@ -23,6 +23,22 @@ def app
env["REQUEST_METHOD"].should.equal "PUT"
end

if RUBY_VERSION >= "1.9"
should "set rack.errors for invalid UTF8 _method values" do
errors = StringIO.new
env = Rack::MockRequest.env_for("/",
:method => "POST",
:input => "_method=\xBF".force_encoding("ASCII-8BIT"),
"rack.errors" => errors)

app.call env

errors.rewind
errors.read.should.equal "Invalid string for method\n"
env["REQUEST_METHOD"].should.equal "POST"
end
end

should "modify REQUEST_METHOD for POST requests when X-HTTP-Method-Override is set" do
env = Rack::MockRequest.env_for("/",
:method => "POST",
Expand Down

0 comments on commit b27dd86

Please sign in to comment.