提交 e0b5a773 编写于 作者: J Jon Moss

Remove last uses of `@env[]` and `@env[]=`

Last August (2015), @tenderlove worked to remove all `@env[]` and `@env[]=`, in
favor of using `set_header`, `get_header`, etc. (Here's an [example
commit](https://github.com/rails/rails/commit/f16a33b68efc3dc57cfafa27651b9a765e363fbf)).

This PR should remove the last uses of these methods, and fully convert
them to the newly standardized API.
上级 39144740
...@@ -164,7 +164,7 @@ def use_accept_header ...@@ -164,7 +164,7 @@ def use_accept_header
end end
def format_from_path_extension def format_from_path_extension
path = @env['action_dispatch.original_path'] || @env['PATH_INFO'] path = get_header('action_dispatch.original_path') || get_header('PATH_INFO')
if match = path && path.match(/\.(\w+)\z/) if match = path && path.match(/\.(\w+)\z/)
Mime[match.captures.first] Mime[match.captures.first]
end end
......
...@@ -22,23 +22,23 @@ def self.default_env ...@@ -22,23 +22,23 @@ def self.default_env
private_class_method :default_env private_class_method :default_env
def request_method=(method) def request_method=(method)
@env['REQUEST_METHOD'] = method.to_s.upcase set_header('REQUEST_METHOD', method.to_s.upcase)
end end
def host=(host) def host=(host)
@env['HTTP_HOST'] = host set_header('HTTP_HOST', host)
end end
def port=(number) def port=(number)
@env['SERVER_PORT'] = number.to_i set_header('SERVER_PORT', number.to_i)
end end
def request_uri=(uri) def request_uri=(uri)
@env['REQUEST_URI'] = uri set_header('REQUEST_URI', uri)
end end
def path=(path) def path=(path)
@env['PATH_INFO'] = path set_header('PATH_INFO', path)
end end
def action=(action_name) def action=(action_name)
...@@ -46,24 +46,24 @@ def action=(action_name) ...@@ -46,24 +46,24 @@ def action=(action_name)
end end
def if_modified_since=(last_modified) def if_modified_since=(last_modified)
@env['HTTP_IF_MODIFIED_SINCE'] = last_modified set_header('HTTP_IF_MODIFIED_SINCE', last_modified)
end end
def if_none_match=(etag) def if_none_match=(etag)
@env['HTTP_IF_NONE_MATCH'] = etag set_header('HTTP_IF_NONE_MATCH', etag)
end end
def remote_addr=(addr) def remote_addr=(addr)
@env['REMOTE_ADDR'] = addr set_header('REMOTE_ADDR', addr)
end end
def user_agent=(user_agent) def user_agent=(user_agent)
@env['HTTP_USER_AGENT'] = user_agent set_header('HTTP_USER_AGENT', user_agent)
end end
def accept=(mime_types) def accept=(mime_types)
@env.delete('action_dispatch.request.accepts') delete_header('action_dispatch.request.accepts')
@env['HTTP_ACCEPT'] = Array(mime_types).collect(&:to_s).join(",") set_header('HTTP_ACCEPT', Array(mime_types).collect(&:to_s).join(","))
end end
end end
end end
...@@ -88,6 +88,33 @@ class TestRequestTest < ActiveSupport::TestCase ...@@ -88,6 +88,33 @@ class TestRequestTest < ActiveSupport::TestCase
assert_equal 'GoogleBot', req.user_agent assert_equal 'GoogleBot', req.user_agent
end end
test "setter methods" do
req = ActionDispatch::TestRequest.create({})
get = 'GET'
[
'request_method=', 'host=', 'request_uri=', 'path=', 'if_modified_since=', 'if_none_match=',
'remote_addr=', 'user_agent=', 'accept='
].each do |method|
req.send(method, get)
end
req.port = 8080
req.accept = 'hello goodbye'
assert_equal(get, req.get_header('REQUEST_METHOD'))
assert_equal(get, req.get_header('HTTP_HOST'))
assert_equal(8080, req.get_header('SERVER_PORT'))
assert_equal(get, req.get_header('REQUEST_URI'))
assert_equal(get, req.get_header('PATH_INFO'))
assert_equal(get, req.get_header('HTTP_IF_MODIFIED_SINCE'))
assert_equal(get, req.get_header('HTTP_IF_NONE_MATCH'))
assert_equal(get, req.get_header('REMOTE_ADDR'))
assert_equal(get, req.get_header('HTTP_USER_AGENT'))
assert_nil(req.get_header('action_dispatch.request.accepts'))
assert_equal('hello goodbye', req.get_header('HTTP_ACCEPT'))
end
private private
def assert_cookies(expected, cookie_jar) def assert_cookies(expected, cookie_jar)
assert_equal(expected, cookie_jar.instance_variable_get("@cookies")) assert_equal(expected, cookie_jar.instance_variable_get("@cookies"))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册