Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document ActionDispatch::TestResponse#parsed_body [ci-skip] #45189

Merged
merged 1 commit into from
May 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions actionpack/lib/action_dispatch/testing/test_response.rb
Expand Up @@ -14,6 +14,24 @@ def self.from_response(response)
new response.status, response.headers, response.body
end

# Returns a parsed body depending on the response MIME type. When a parser
# corresponding to the MIME type is not found, it returns the raw body.
#
# ==== Examples
# get "/posts"
# response.content_type # => "text/html; charset=utf-8"
# response.parsed_body.class # => String
# response.parsed_body # => "<!DOCTYPE html>\n<html>\n..."
#
# get "/posts.json"
# response.content_type # => "application/json; charset=utf-8"
# response.parsed_body.class # => Array
# response.parsed_body # => [{"id"=>42, "title"=>"Title"},...
#
# get "/posts/42.json"
# response.content_type # => "application/json; charset=utf-8"
# response.parsed_body.class # => Hash
# response.parsed_body # => {"id"=>42, "title"=>"Title"}
shouichi marked this conversation as resolved.
Show resolved Hide resolved
def parsed_body
@parsed_body ||= response_parser.call(body)
end
Expand Down