mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-09 00:02:50 -04:00
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:
parent
716e98af5b
commit
fa513a9ac9
@ -225,6 +225,21 @@ class WebmockTest < Minitest::Test
|
|||||||
assert_requested(redirect_request)
|
assert_requested(redirect_request)
|
||||||
end
|
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
|
def test_webmock_with_stream_plugin_each_line
|
||||||
session = HTTPX.plugin(:stream)
|
session = HTTPX.plugin(:stream)
|
||||||
request = stub_request(:get, MOCK_URL_HTTP).to_return(body: "First line\nSecond line")
|
request = stub_request(:get, MOCK_URL_HTTP).to_return(body: "First line\nSecond line")
|
||||||
|
@ -38,12 +38,10 @@ module WebMock
|
|||||||
|
|
||||||
return build_error_response(request, webmock_response.exception) if webmock_response.exception
|
return build_error_response(request, webmock_response.exception) if webmock_response.exception
|
||||||
|
|
||||||
response = request.options.response_class.new(request,
|
request.options.response_class.new(request,
|
||||||
webmock_response.status[0],
|
webmock_response.status[0],
|
||||||
"2.0",
|
"2.0",
|
||||||
webmock_response.headers)
|
webmock_response.headers)
|
||||||
response << webmock_response.body.dup
|
|
||||||
response
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_error_response(request, exception)
|
def build_error_response(request, exception)
|
||||||
@ -90,6 +88,7 @@ module WebMock
|
|||||||
log { "mocking #{request.uri} with #{mock_response.inspect}" }
|
log { "mocking #{request.uri} with #{mock_response.inspect}" }
|
||||||
request.response = response
|
request.response = response
|
||||||
request.emit(: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)
|
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
||||||
if WebMock::CallbackRegistry.any_callbacks?
|
if WebMock::CallbackRegistry.any_callbacks?
|
||||||
request.on(:response) do |resp|
|
request.on(:response) do |resp|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user