From 90c901137fd5f0d56274c06eda62f3072e5717c4 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Tue, 22 Aug 2023 14:44:07 -0400 Subject: [PATCH] Document `response.parsed_body` in 7.1 release notes Add notes for [#47144][] to the 7.1 Release Notes. Additionally, in the time since that was merged, both Nokogiri and Minitest have merged the PRs mentioned to integrate support for Ruby's Pattern matching (https://github.com/sparklemotion/nokogiri/pull/2523 and https://github.com/minitest/minitest/pull/936, respectively). This commit adds coverage for those new assertions, and incorporates guidance into the release notes. [#47144]: https://github.com/rails/rails/pull/47144 --- guides/.rubocop.yml | 3 ++ guides/source/7_1_release_notes.md | 66 ++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/guides/.rubocop.yml b/guides/.rubocop.yml index 3afdcbd16f8a6..0762a0fd0c02d 100644 --- a/guides/.rubocop.yml +++ b/guides/.rubocop.yml @@ -1,6 +1,9 @@ inherit_from: - '../.rubocop.yml' +AllCops: + TargetRubyVersion: 3.0 + Style/StringLiterals: Enabled: false diff --git a/guides/source/7_1_release_notes.md b/guides/source/7_1_release_notes.md index 887651cc0361b..d30ea1505757e 100644 --- a/guides/source/7_1_release_notes.md +++ b/guides/source/7_1_release_notes.md @@ -65,6 +65,72 @@ TODO: https://github.com/rails/rails/pull/45602 TODO: https://github.com/rails/rails/pull/46049 +### Support pattern matching for JSON `response.parsed_body` + +When `ActionDispatch::IntegrationTest` tests blocks invoke +`response.parsed_body` for JSON responses, their payloads will be available with +indifferent access. This enables integration with [Ruby's Pattern +Matching][pattern-matching], and built-in [Minitest support for pattern +matching][minitest-pattern-matching]: + +```ruby +get "/posts.json" + +response.content_type # => "application/json; charset=utf-8" +response.parsed_body.class # => Array +response.parsed_body # => [{"id"=>42, "title"=>"Title"},... + +assert_pattern { response.parsed_body => [{ id: 42 }] } + +get "/posts/42.json" + +response.content_type # => "application/json; charset=utf-8" +response.parsed_body.class # => ActiveSupport::HashWithIndifferentAccess +response.parsed_body # => {"id"=>42, "title"=>"Title"} + +assert_pattern { response.parsed_body => [{ title: /title/i }] } +``` + +[pattern-matching]: https://docs.ruby-lang.org/en/master/syntax/pattern_matching_rdoc.html +[minitest-pattern-matching]: https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-assert_pattern + +### Extend `response.parsed_body` to parse HTML with Nokogiri + +[Extend the `ActionDispatch::Testing` module][#47144] to support parsing the +value of an HTML `response.body` into a `Nokogiri::HTML5::Document` instance: + +```ruby +get "/posts" + +response.content_type # => "text/html; charset=utf-8" +response.parsed_body.class # => Nokogiri::HTML5::Document +response.parsed_body.to_html # => "\n\n..." +``` + +Newly added [Nokogiri support for pattern matching][nokogiri-pattern-matching], +along with built-in [Minitest support for pattern +matching][minitest-pattern-matching] presents opportunities to make test +assertions about the structure and content of the HTML response: + +```ruby +get "/posts" + +html = response.parsed_body # => + # + # + #

Some main content

+ # + # + +assert_pattern { html.at("main") => { content: "Some main content" } } +assert_pattern { html.at("main") => { content: /content/ } } +assert_pattern { html.at("main") => { children: [{ name: "h1", content: /content/ }] } } +``` + +[#47144]: https://github.com/rails/rails/pull/47144 +[nokogiri-pattern-matching]: https://nokogiri.org/rdoc/Nokogiri/XML/Attr.html#method-i-deconstruct_keys +[minitest-pattern-matching]: https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-assert_pattern + Railties --------