fix for webmock request body expecting a string

when building the request signature, the body is preemptively converted to a string, which fulfills the expectation for webmock, despite it being a bit of a perf penalty if the request contains a multipart request body, as the body will be fully read to memory

Closes #319

Closes https://github.com/HoneyryderChuck/httpx/issues/65
This commit is contained in:
HoneyryderChuck 2024-10-31 17:47:06 +00:00
parent 492097d551
commit 3e504fb511
4 changed files with 15 additions and 1 deletions

View File

@ -155,6 +155,12 @@ class WebmockTest < Minitest::Test
assert_requested(:get, MOCK_URL_HTTP, query: hash_excluding("a" => %w[b c]))
end
def test_verification_that_expected_request_with_hash_as_body
stub_request(:post, MOCK_URL_HTTP).with(body: { foo: "bar" })
http_request(:post, MOCK_URL_HTTP, form: { foo: "bar" })
assert_requested(:post, MOCK_URL_HTTP, body: { foo: "bar" })
end
def test_verification_that_non_expected_request_didnt_occur
expected_message = Regexp.new(
"The request GET #{MOCK_URL_HTTP}/ was not expected to execute but it executed 1 time\n\n" \

View File

@ -20,7 +20,7 @@ module WebMock
WebMock::RequestSignature.new(
request.verb.downcase.to_sym,
uri.to_s,
body: request.body,
body: request.body.to_s,
headers: request.headers.to_h
)
end

View File

@ -18,6 +18,12 @@ module HTTPX
"multipart/form-data; boundary=#{@boundary}"
end
def to_s
read
ensure
rewind
end
def read(length = nil, outbuf = nil)
data = String(outbuf).clear.force_encoding(Encoding::BINARY) if outbuf
data ||= "".b

View File

@ -28,6 +28,8 @@ module HTTPX
def content_type: () -> String
def to_s: () -> String
def read: (?int? length, ?string? buffer) -> String?
def rewind: () -> void