Skip to content

Commit

Permalink
Preserving body encoding, when building request signature for execute…
Browse files Browse the repository at this point in the history
…d HTTP.rb requests.
  • Loading branch information
bblimke committed Feb 6, 2024
1 parent 39ef745 commit 4489297
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/webmock/http_lib_adapters/http_rb/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
module HTTP
class Request
def webmock_signature
request_body = if defined?(HTTP::Request::Body)
String.new.tap { |string| body.each { |part| string << part } }
else
body
end
request_body = nil

if defined?(HTTP::Request::Body)
request_body = String.new
first_chunk_encoding = nil
body.each do |part|
request_body << part
first_chunk_encoding ||= part.encoding
end

request_body.force_encoding(first_chunk_encoding) if first_chunk_encoding
request_body
else
request_body = body
end

::WebMock::RequestSignature.new(verb, uri.to_s, {
headers: headers.to_h,
Expand Down
8 changes: 8 additions & 0 deletions spec/acceptance/http_rb/http_rb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@
response.connection.close
end
end

it "should preserve request body encoding when matching requests" do
stub_request(:post, "www.example.com").with(body: (lambda {|body|
body.encoding == Encoding::UTF_8
}))
http_request(:post, "http://www.example.com/", body: "abc")
end

end

0 comments on commit 4489297

Please sign in to comment.