stream plugin: fix #each loop when used with webmock

when response would be called inside the #each block, the webmock trigger would inject the body before attaching the response object to the request, thereby retriggering #each in a loop

Closes #281
This commit is contained in:
HoneyryderChuck 2023-11-20 23:04:46 +00:00
parent 716e98af5b
commit fa513a9ac9
2 changed files with 20 additions and 6 deletions

View File

@ -225,6 +225,21 @@ class WebmockTest < Minitest::Test
assert_requested(redirect_request)
end
def test_webmock_with_stream_plugin_each
request = stub_request(:get, MOCK_URL_HTTP).to_return(body: "body")
body = "".b
response = HTTPX.plugin(:stream).get(MOCK_URL_HTTP, stream: true)
response.each do |chunk|
next if (300..399).cover?(response.status)
body << chunk
end
assert_equal("body", body)
assert_requested(request)
end
def test_webmock_with_stream_plugin_each_line
session = HTTPX.plugin(:stream)
request = stub_request(:get, MOCK_URL_HTTP).to_return(body: "First line\nSecond line")

View File

@ -38,12 +38,10 @@ module WebMock
return build_error_response(request, webmock_response.exception) if webmock_response.exception
response = request.options.response_class.new(request,
webmock_response.status[0],
"2.0",
webmock_response.headers)
response << webmock_response.body.dup
response
request.options.response_class.new(request,
webmock_response.status[0],
"2.0",
webmock_response.headers)
end
def build_error_response(request, exception)
@ -90,6 +88,7 @@ module WebMock
log { "mocking #{request.uri} with #{mock_response.inspect}" }
request.response = response
request.emit(:response, response)
response << mock_response.body.dup unless response.is_a?(HTTPX::ErrorResponse)
elsif WebMock.net_connect_allowed?(request_signature.uri)
if WebMock::CallbackRegistry.any_callbacks?
request.on(:response) do |resp|