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 3, 2023
1 parent 642b420 commit 1130eb8
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 modifying the response body encoding when the body is frozen. This could lead to errors when using `opensearch-ruy` together with `webmock`.
### 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.frozen?
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 modify the body encoding if the response body is frozen" do
body = "Hello Frozen!".encode(Encoding::ISO_8859_1).freeze
response = OpenSearch::Transport::Transport::Response.new 200, body

assert_equal 'ISO-8859-1', body.encoding.name
end
end
end

0 comments on commit 1130eb8

Please sign in to comment.