Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/webmock/http_lib_adapters/async_http_client_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ class AsyncHttpClientAdapter < HttpLibAdapter
OriginalAsyncHttpClient = Async::HTTP::Client unless const_defined?(:OriginalAsyncHttpClient)

class << self
# protocol-http1 0.40.0 removed Protocol::HTTP1::Reason in favour of
# Protocol::HTTP::Status. Resolved on first use, rather than at load,
# so it does not depend on which of the two is required by then.
def status_descriptions
@status_descriptions ||= if defined?(::Protocol::HTTP1::Reason)
::Protocol::HTTP1::Reason::DESCRIPTIONS
else
::Protocol::HTTP::Status::DESCRIPTIONS
end
end

def enable!
Async::HTTP.send(:remove_const, :Client)
Async::HTTP.send(:const_set, :Client, Async::HTTP::WebMockClientWrapper)
Expand Down Expand Up @@ -112,7 +123,7 @@ def build_webmock_response(response)
webmock_response = WebMock::Response.new
webmock_response.status = [
response.status,
::Protocol::HTTP1::Reason::DESCRIPTIONS[response.status]
WebMock::HttpLibAdapters::AsyncHttpClientAdapter.status_descriptions[response.status]
]
webmock_response.headers = build_webmock_response_headers(response)
webmock_response.body = body
Expand Down
22 changes: 22 additions & 0 deletions spec/acceptance/async_http_client/async_http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@
expect(callback_invoked).to eq(true)
end

context 'with a real response' do
# build_webmock_response is the only place the adapter reads a status
# description, and only real responses reach it.
let(:server_url) { "http://#{WebMockServer.instance.host_with_port}/" }

after do
WebMock.reset_callbacks
WebMock.disable_net_connect!
end

it 'builds a webmock response with a status message' do
WebMock.allow_net_connect!

recorded_response = nil
WebMock.after_request { |_request, response| recorded_response = response }

make_request(:get, server_url)

expect(recorded_response.status).to eq([200, 'OK'])
end
end

context 'scheme and protocol' do
let(:default_response_headers) { {} }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def build_hash_response(response)
{

status: response.status.to_s,
message: Protocol::HTTP1::Reason::DESCRIPTIONS[response.status],
message: WebMock::HttpLibAdapters::AsyncHttpClientAdapter.status_descriptions[response.status],
headers: build_response_headers(response),
body: response.read
}
Expand Down