提交 77bbf1e9 编写于 作者: K Kasper Timm Hansen

Make `parsed_body` extract parser from the content type.

We're not guaranteed to have a `RequestEncoder` to assign on `get` requests
because we aren't extracting the parser from the response content type.

Until now.
上级 f9f98b75
......@@ -26,7 +26,7 @@
class ApiTest < ActionDispatch::IntegrationTest
test 'creates articles' do
assert_difference -> { Article.count } do
post articles_path, { article: { title: 'Ahoy!' } }, as: :json
post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json
end
assert_equal({ id: Article.last.id, title: 'Ahoy!' }, response.parsed_body)
......
......@@ -381,7 +381,7 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: n
response = _mock_session.last_response
@response = ActionDispatch::TestResponse.from_response(response)
@response.request = @request
@response.response_parser = request_encoder
@response.response_parser = RequestEncoder.parser(@response.content_type)
@html_document = nil
@url_options = nil
......@@ -397,6 +397,8 @@ def build_full_uri(path, env)
class RequestEncoder # :nodoc:
@encoders = {}
attr_reader :response_parser
def initialize(mime_name, param_encoder, response_parser, url_encoded_form = false)
@mime = Mime[mime_name]
......@@ -424,8 +426,9 @@ def encode_params(params)
@param_encoder.call(params)
end
def parse_body(body)
@response_parser.call(body)
def self.parser(content_type)
mime = Mime::Type.lookup(content_type)
encoder(mime ? mime.ref : nil).response_parser
end
def self.encoder(name)
......@@ -726,7 +729,8 @@ def method_missing(sym, *args, &block)
# response_parser: -> body { body }
#
# Where `param_encoder` defines how the params should be encoded and
# `response_parser` defines how the response body should be parsed.
# `response_parser` defines how the response body should be parsed through
# `parsed_body`.
#
# Consult the Rails Testing Guide for more.
......
......@@ -22,7 +22,7 @@ def self.from_response(response)
attr_writer :response_parser # :nodoc:
def parsed_body
@response_parser.parse_body(body)
@parsed_body ||= @response_parser.call(body)
end
end
end
......@@ -1171,6 +1171,16 @@ def test_registering_custom_encoder
Mime::Type.unregister :wibble
end
def test_parsed_body_without_as_option
with_routing do |routes|
routes.draw { get ':action' => FooController }
get '/foos_json.json', params: { foo: 'heyo' }
assert_equal({ 'foo' => 'heyo' }, response.parsed_body)
end
end
private
def post_to_foos(as:)
with_routing do |routes|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册