class Async::HTTP::WebMockClientWrapper

Public Class Methods

new( endpoint, protocol = endpoint.protocol, scheme = endpoint.scheme, authority = endpoint.authority, **options ) click to toggle source
# File lib/webmock/http_lib_adapters/async_http_client_adapter.rb, line 34
def initialize(
  endpoint,
  protocol = endpoint.protocol,
  scheme = endpoint.scheme,
  authority = endpoint.authority,
  **options
)
  webmock_endpoint = WebMockEndpoint.new(scheme, authority, protocol)

  @network_client = WebMockClient.new(endpoint, protocol, scheme, authority, **options)
  @webmock_client = WebMockClient.new(webmock_endpoint, protocol, scheme, authority, **options)

  @scheme = scheme
  @authority = authority
end

Public Instance Methods

call(request) click to toggle source
# File lib/webmock/http_lib_adapters/async_http_client_adapter.rb, line 50
def call(request)
  request.scheme ||= self.scheme
  request.authority ||= self.authority

  request_signature = build_request_signature(request)
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
  webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
  net_connect_allowed = WebMock.net_connect_allowed?(request_signature.uri)

  if webmock_response
    webmock_response.raise_error_if_any
    raise Async::TimeoutError, 'WebMock timeout error' if webmock_response.should_timeout
    WebMockApplication.add_webmock_response(request, webmock_response)
    response = @webmock_client.call(request)
  elsif net_connect_allowed
    response = @network_client.call(request)
  else
    raise WebMock::NetConnectNotAllowedError.new(request_signature) unless webmock_response
  end

  if WebMock::CallbackRegistry.any_callbacks?
    webmock_response ||= build_webmock_response(response)
    WebMock::CallbackRegistry.invoke_callbacks(
      {
        lib: :async_http_client,
        real_request: net_connect_allowed
      },
      request_signature,
      webmock_response
    )
  end

  response
end
close() click to toggle source
# File lib/webmock/http_lib_adapters/async_http_client_adapter.rb, line 85
def close
  @network_client.close
  @webmock_client.close
end

Private Instance Methods

build_request_signature(request) click to toggle source
# File lib/webmock/http_lib_adapters/async_http_client_adapter.rb, line 92
def build_request_signature(request)
  body = request.read
  request.body = ::Protocol::HTTP::Body::Buffered.wrap(body)
  WebMock::RequestSignature.new(
    request.method.downcase.to_sym,
    "#{request.scheme}://#{request.authority}#{request.path}",
    headers: request.headers.to_h,
    body: body
  )
end
build_webmock_response(response) click to toggle source
# File lib/webmock/http_lib_adapters/async_http_client_adapter.rb, line 103
def build_webmock_response(response)
  body = response.read
  response.body = ::Protocol::HTTP::Body::Buffered.wrap(body)

  webmock_response = WebMock::Response.new
  webmock_response.status = [
    response.status,
    ::Protocol::HTTP1::Reason::DESCRIPTIONS[response.status]
  ]
  webmock_response.headers = build_webmock_response_headers(response)
  webmock_response.body = body
  webmock_response
end
build_webmock_response_headers(response) click to toggle source
# File lib/webmock/http_lib_adapters/async_http_client_adapter.rb, line 117
def build_webmock_response_headers(response)
  response.headers.each.each_with_object({}) do |(k, v), o|
    o[k] ||= []
    o[k] << v
  end
end