ActionDispatch::Testing::TestResponse#parsed_body parse HTML with Nokogiri - #47144
Conversation
There was a problem hiding this comment.
Does this mean that people that were expecting strings will have to rewrite their tests?
There was a problem hiding this comment.
Yes, it would be a breaking change.
people that were expecting strings will have to rewrite their tests?
I wasn't sure about this. Since response.parsed_body and response.body return the same type (String), would they be motivated to use response.parsed_body?
Would upgrading from 7.0 to 7.1 be an appropriate time to introduce a breaking change? Would a breaking change require introducing a configuration? Something like action_dispatch.html_parsed_body_as_document = true that <= 7.0 applications would set to false?
There was a problem hiding this comment.
I think it is fine to just explicitly document that the return type of that method is going to change.
250c6d4 to
3bc7d1d
Compare
There was a problem hiding this comment.
This version was copied from actiontext/actiontext.gemspec.
There was a problem hiding this comment.
Just a note that the Nokogiri::HTML5 module/namespace will not exist for Nokogiri < 1.12.0 or for any JRuby user (see sparklemotion/nokogiri#2227).
I think we should take care to gracefully fall back to Nokogiri::HTML.parse(body) unless defined?(::Nokogiri::HTML5)
3bc7d1d to
8b3835e
Compare
8b3835e to
180fe3e
Compare
…okogiri Prior to this commit, the only out-of-the-box parsing that `ActionDispatch::Testing::TestResponse#parsed_body` supported was for `application/json` requests. This meant that `response.body == response.parsed_body` for HTML requests. ```ruby get "/posts" response.content_type # => "text/html; charset=utf-8" response.parsed_body.class # => Nokogiri::HTML5::Document response.parsed_body.to_html # => "<!DOCTYPE html>\n<html>\n..." ``` Using `parsed_body` for JSON requests supports `Hash#fetch`, `Hash#dig`, and Ruby 3.2 destructuring assignment and pattern matching. The introduction of [Nokogiri support for pattern matching][nokogiri-pattern-matching] poses an opportunity to make assertions about the structure of the HTML response. On top of that, there is ongoing work to [introduce pattern matching support in MiniTest][minitest-pattern-matching]. [nokogiri-pattern-matching]: sparklemotion/nokogiri#2523 [minitest-pattern-matching]: minitest/minitest#936
180fe3e to
ad79ed0
Compare
flavorjones
left a comment
There was a problem hiding this comment.
LGTM, thanks for making those changes!
Use the helpers introduced in rails-dom-testing 2.2.0 instead of managing the HTML parsers as was done in rails#48523. See also related rails#47144 / ad79ed0
Add notes for [rails#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 (sparklemotion/nokogiri#2523 and minitest/minitest#936, respectively). This commit adds coverage for those new assertions, and incorporates guidance into the release notes. [rails#47144]: rails#47144
Use the helpers introduced in rails-dom-testing 2.2.0 instead of managing the HTML parsers as was done in rails#48523. See also related rails#47144 / ad79ed0
Add notes for [rails#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 (sparklemotion/nokogiri#2523 and minitest/minitest#936, respectively). This commit adds coverage for those new assertions, and incorporates guidance into the release notes. [rails#47144]: rails#47144
As of [rails#47144][], `response.parsed_body` supports parsing both `format: :json` and `format: :html` responses. This commit updates the documentation to reflect that. Additionally, it cribs the `ActionDispatch::IntegrationTest` documentation that mentions `register_encoder`, and copies it to the method definition so that the documentation entry for that method has descriptive information. [rails#47144]: rails#47144
Motivation / Background
Prior to this commit, the only out-of-the-box parsing that
ActionDispatch::Testing::TestResponse#parsed_bodysupported was forapplication/jsonrequests. This meant thatresponse.body == response.parsed_bodyfor HTML requests.Using
parsed_bodyfor JSON requests supportsHash#fetch,Hash#dig, and Ruby 3.2 destructuring assignment and pattern matching.Detail
The introduction of Nokogiri support for pattern
matching poses an opportunity to make assertions about the structure of the HTML response.
On top of that, there is ongoing work to introduce pattern matching support in MiniTest.
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]