提交 7e350c67 编写于 作者: K Kir Shatrov

Fix memoization bug on ActionDispatch::TestRequest#request_method=

TestRequest have been overrriding request_method setter since 2009,
but the actual implementation in Request (not TestRequest) has been
changed since that. Now it's also using @request_method instance
variable to keep the state.

The override in TestRequest have not been calling `super`, which caused
a bug that after accessing #requst_method the value was memoized and
then we've never been able to change it anymore:

```
req = ActionDispatch::TestRequest.create
puts "was: #{req.request_method}" # memoized here
req.request_method = "POST"
puts "became: #{req.request_method}"
```

output:

```
was: GET
became: GET
```

Since the whole purpose of overriding the setter in TestRequest is to
upcase it, I'm changing it to `super(method.to_s.upcase)`
上级 cf5f55cd
......@@ -22,7 +22,7 @@ def self.default_env
private_class_method :default_env
def request_method=(method)
set_header("REQUEST_METHOD", method.to_s.upcase)
super(method.to_s.upcase)
end
def host=(host)
......
......@@ -88,6 +88,13 @@ class TestRequestTest < ActiveSupport::TestCase
assert_equal "GoogleBot", req.user_agent
end
test "request_method getter and setter" do
req = ActionDispatch::TestRequest.create
req.request_method # to reproduce bug caused by memoization
req.request_method = "POST"
assert_equal "POST", req.request_method
end
test "setter methods" do
req = ActionDispatch::TestRequest.create({})
get = "GET"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册