Skip to content

Commit

Permalink
Merge pull request rails#45189 from shouichi/document-parsed-body
Browse files Browse the repository at this point in the history
Document ActionDispatch::TestResponse#parsed_body [ci-skip]
  • Loading branch information
jonathanhefner committed May 29, 2022
2 parents 61dca3f + 2bab22c commit 6be4e96
Showing 1 changed file with 18 additions and 0 deletions.
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"}
def parsed_body
@parsed_body ||= response_parser.call(body)
end
Expand Down

0 comments on commit 6be4e96

Please sign in to comment.