Repository navigation
Apply Net::HTTP response_body_encoding to stubbed response bodies - #1138
Merged
Merged
Conversation
Net::HTTP forces the encoding chosen with Net::HTTP#response_body_encoding= or Net::HTTPResponse#body_encoding= onto the body it reads, detecting it from the Content-Type charset or a meta tag when the setting is true. Stubbed responses ignored the setting and always returned the body in the encoding it was stubbed with. Fixes bblimke#1028
bblimke
added a commit
that referenced
this pull request
Nov 20, 2026
Net::HTTP applies the body encoding once, when it reads the body, which happens in the request block or right after it. The stubbed response applied it at the first read of the body instead, so a body_encoding= set after request returned still took effect, and read_body with a string buffer left the buffer binary. Apply it when the request block returns too, apply it to a string buffer, and ignore later changes to the setting. Detect the encoding from the raw bytes, as Net::HTTP does, so a stubbed body tagged with another encoding does not raise, and leave alone a frozen body assigned in the request block. Real responses that pass through WebMock keep the encoding Net::HTTP gave them, as before #1138. Applying it again ran the detection twice and raised ArgumentError for a body that was invalid in its new encoding. Give each stubbed response its own copy of the body, so the encoding is forced in place as Net::HTTP does it. That also stops a frozen stubbed body making response.body frozen, and changes to response.body reaching later requests to the same stub. Fold the repeated request setup in the specs into a helper, add specs for the cases above, and document #1138 and the copied body in the CHANGELOG.
Owner
|
Thank you @ybai08! This is released in 3.36.0. I made a few changes on top, so stubbed responses behave like real Net::HTTP ones:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1028
Net::HTTP (net-http 0.2+, Ruby 3.0+) can set the encoding of response bodies, either for every request with
Net::HTTP#response_body_encoding=or per response withNet::HTTPResponse#body_encoding=.truedetects the encoding from theContent-Typecharset or an HTML<meta>tag; anEncodingor encoding name forces that encoding.Net::HTTP#transport_requestcopies the setting onto each response, andNet::HTTPResponse#read_bodyapplies it to the body it reads.Stubbed responses skipped both steps, so with WebMock the body always kept the encoding it was stubbed with. The body was usually BINARY when it came from a raw response file or
curl -isoutput, as in the issue:The change:
build_net_http_responsecopiesresponse_body_encodingonto the stubbed response, astransport_requestdoes. It is guarded withrespond_to?, so Rubies without the setting (2.6, 2.7) are unaffected.Net::WebMockHTTPResponse#read_bodyapplies@body_encodingthe same wayNet::HTTPResponse#read_bodydoes when the body is read without a block or destination. Because this happens when the body is read, settingres.body_encoding = "UTF-8"inside arequestblock also works, as the Net::HTTP docs show. The body is duplicated beforeforce_encoding, so the stored stub body is never changed. When the setting isfalse(the default), nothing changes.The issue suggested making
ResponseFactory.response_foralways detect the charset. I didn't do that: real Net::HTTP returns bodies as-is unless the client asks for an encoding, so detecting the charset by default would make stubs differ from real responses. With this change, the raw-response case from the issue gets the charset as soon as the client turns onresponse_body_encoding.Streaming with
read_body { |segment| ... }is unchanged: segments are still the raw stubbed bytes, as they are with real Net::HTTP. (Real Net::HTTP then raisesNoMethodErroronNet::ReadAdapter#force_encodingwhen an encoding is set. WebMock does not copy that.)Testing
Net:HTTP response body encodinginspec/acceptance/net_http/net_http_spec.rb. They cover: the default stays unchanged;truewith aContent-Typecharset;truewith a<meta charset>; a forced encoding name;body_encoding=set on the response inside the request block; and the stub body not being changed between requests. Four of the six fail without the change and all pass with it.<meta>and empty. The settings weretrue,Encoding::UTF_8and"ISO-8859-1", each with and without a request block. The encodings now match in all 30 combinations. Before the change, 28 of them differed: every one excepttruewith no charset, where real Net::HTTP leaves the body unchanged.bundle exec rake specon Ruby 4.0.7: 5691 examples, 142 failures. The same 142 examples fail onmasterwithout this change. All of them make real requests to outside hosts (TLS and connection errors in this environment).bundle exec rake spec_http_without_webmock,rake testandrake minitestall pass.respond_to?anddefined?guards keep the old behaviour) and JRuby.I didn't add a CHANGELOG entry, because entries seem to be written at release time. I can add one if you'd like.