Skip to content

Commit

Permalink
Don't modify the response if the body is frozen
Browse files Browse the repository at this point in the history
Fixes an incompatibility with webmock since 3.19, where it started to
freeze the response body for performance reasons

Signed-off-by: Earlopain <[email protected]>
  • Loading branch information
Earlopain committed Dec 4, 2023
1 parent 642b420 commit 866fa06
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Deprecated
### Removed
### Fixed
- Fixed the response body being force-encoded when it was already in UTF-8 ([#212](https://github.com/opensearch-project/opensearch-ruby/issues/212))
### Security

## [3.0.1]
Expand Down
2 changes: 1 addition & 1 deletion lib/opensearch/transport/transport/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def initialize(status, body, headers = {})
@status = status
@body = body
@headers = headers
@body = body.force_encoding('UTF-8') if body.respond_to?(:force_encoding)
@body = body.force_encoding('UTF-8') if body.respond_to?(:force_encoding) && body.encoding != Encoding::UTF_8
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions test/transport/unit/response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@

class OpenSearch::Transport::Transport::ResponseTest < Minitest::Test
context "Response" do

should "force-encode the body into UTF" do
should "force-encode the body into UTF-8" do
body = "Hello Encoding!".encode(Encoding::ISO_8859_1)
assert_equal 'ISO-8859-1', body.encoding.name

response = OpenSearch::Transport::Transport::Response.new 200, body
assert_equal 'UTF-8', response.body.encoding.name
end

should "not force-encode the body if it is already encoded as UTF-8" do
body = "Hello Frozen!".freeze
response = OpenSearch::Transport::Transport::Response.new 200, body

assert_equal body, response.body
end
end
end

0 comments on commit 866fa06

Please sign in to comment.