1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
require 'ostruct'
module AsyncHttpClientSpecHelper
def http_request(method, url, options = {}, &block)
endpoint = Async::HTTP::Endpoint.parse(url)
path = endpoint.path
path = path + "?" + options[:query] if options[:query]
headers = (options[:headers] || {}).each_with_object([]) do |(k, v), o|
Array(v).each do |v|
o.push [k, v]
end
end
headers.push(
['authorization', WebMock::Util::Headers.basic_auth_header(options[:basic_auth])]
) if options[:basic_auth]
body = options[:body]
result = Async do
begin
Async::HTTP::Client.open(endpoint) do |client|
response = client.send(
method,
path,
headers,
body
)
OpenStruct.new(
build_hash_response(response)
)
end
rescue Exception => e
e
end
end.wait
result.is_a?(Exception) ? raise(result) : result
end
def client_timeout_exception_class
Async::TimeoutError
end
def connection_refused_exception_class
Errno::ECONNREFUSED
end
def connection_error_class
HTTP::ConnectionError
end
def http_library
:async_http_client
end
private
def build_hash_response(response)
{
status: response.status.to_s,
message: Protocol::HTTP1::Reason::DESCRIPTIONS[response.status],
headers: build_response_headers(response),
body: response.read
}
end
def build_response_headers(response)
response.headers.each.each_with_object({}) do |(k, v), o|
o[k] ||= []
o[k] << v
end.tap do |o|
o.each do |k, v|
o[k] = v.join(', ')
end
end
end
end
|