Skip to content

Commit

Permalink
Use frozen string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
technicalpickles committed Jun 16, 2023
1 parent 8315dc5 commit d028c49
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/webmock/http_lib_adapters/http_rb/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module HTTP
class Request
def webmock_signature
request_body = if defined?(HTTP::Request::Body)
''.tap { |string| body.each { |part| string << part } }
String.new.tap { |string| body.each { |part| string << part } }
else
body
end
Expand Down

5 comments on commit d028c49

@znz
Copy link
Contributor

@znz znz commented on d028c49 Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is changed request_body.encoding from #<Encoding:UTF-8> to #<Encoding:ASCII-8BIT>.
Is it intentional?

@technicalpickles
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. I wonder if +'' keeps the original encoding?

@bblimke
Copy link
Owner

@bblimke bblimke commented on d028c49 Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@znz @technicalpickles

This was a surprise to me as well, but I believe it turned out for the better since the response body encoding is now preserved. Unless I'm overlooking something, is there a specific reason the response body should be encoded as utf8, despite the parts were encoded as ascii? The provided code snippets demonstrate the observed behavior.

irb(main):001> body = ["a","b","c"]
=> ["a", "b", "c"]
irb(main):002>
irb(main):003> body.each {|p| p.force_encoding("ascii-8bit")}
=> ["a", "b", "c"]
irb(main):004>
irb(main):005> body.map(&:encoding)
=> [#<Encoding:ASCII-8BIT>, #<Encoding:ASCII-8BIT>, #<Encoding:ASCII-8BIT>]
irb(main):006>
irb(main):007> String.new.tap { |string| body.each { |part| string << part } }.encoding
=> #<Encoding:ASCII-8BIT>
irb(main):008>
irb(main):009> ''.tap { |string| body.each { |part| string << part } }.encoding
=> #<Encoding:UTF-8>
irb(main):010>

@bblimke
Copy link
Owner

@bblimke bblimke commented on d028c49 Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, @znz you are right. if parts are already utf8, then this will return the body as ascii, since String.new produced ascii encoded string by default.

@bblimke
Copy link
Owner

@bblimke bblimke commented on d028c49 Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have applied a fix that preserves the request body encoding 4489297 Thank you for reporting @znz

Please sign in to comment.