httpx/test/support/session_with_mock_response.rb
HoneyryderChuck 3c22f36a6c session refactor: remove @responses hash
this was being used as an internal cache for finished responses; this can be however superseded by Request#response, which fulfills the same role alongside the #finished? call; this allows us to drop one variable-size hash which would grow at least as large as the number of requests per call, and was inadvertedly shared across threads when using the same session (even at no danger of colliding, but could cause perhaps problems in jruby?)

also allows to remove one less callback
2025-04-04 11:05:27 +01:00

38 lines
827 B
Ruby

# frozen_string_literal: true
module SessionWithMockResponse
module OptionsMethods
def option_mock_status(status)
status
end
def option_mock_headers(headers)
headers
end
end
module ResponseMethods
attr_writer :status
end
module InstanceMethods
def initialize(*)
super
@mock_responses_counter = 1
end
def set_request_callbacks(request)
request.on(:response) do |response|
next unless response && @mock_responses_counter.positive?
@mock_responses_counter -= 1
unless response.is_a?(HTTPX::ErrorResponse)
response.status = request.options.mock_status if request.options.mock_status
response.merge_headers(request.options.mock_headers) if request.options.mock_headers
end
end
end
end
end