From 2652f2840b4f392a45f1c06cb5f1da9250a43081 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Sun, 3 Dec 2023 19:51:11 +0100 Subject: [PATCH] Don't modify the response if the body is frozen Fixes an incompatibility with webmock since 3.19, where it started to freeze the response body for performance reasons Signed-off-by: Earlopain <14981592+Earlopain@users.noreply.github.com> --- CHANGELOG.md | 1 + lib/opensearch/transport/transport/response.rb | 2 +- test/transport/unit/response_test.rb | 9 +++++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 435d8bc4f..dd2ef2e12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/lib/opensearch/transport/transport/response.rb b/lib/opensearch/transport/transport/response.rb index 69a71d10f..f598287ed 100644 --- a/lib/opensearch/transport/transport/response.rb +++ b/lib/opensearch/transport/transport/response.rb @@ -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 diff --git a/test/transport/unit/response_test.rb b/test/transport/unit/response_test.rb index a71246e85..6291cb10d 100644 --- a/test/transport/unit/response_test.rb +++ b/test/transport/unit/response_test.rb @@ -28,8 +28,7 @@ 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 @@ -37,5 +36,11 @@ class OpenSearch::Transport::Transport::ResponseTest < Minitest::Test 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', response.body.encoding.name + end end end