Module: WebMock::HttpLibAdapters::Plugin::ConnectionMethods

Defined in:
lib/httpx/adapters/webmock.rb

Instance Method Summary collapse

Instance Method Details

#initializeObject



88
89
90
91
# File 'lib/httpx/adapters/webmock.rb', line 88

def initialize(*)
  super
  @mocked = true
end

#interestsObject



99
100
101
102
103
# File 'lib/httpx/adapters/webmock.rb', line 99

def interests
  return if @mocked

  super
end

#open?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/httpx/adapters/webmock.rb', line 93

def open?
  return true if @mocked

  super
end

#send(request) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/httpx/adapters/webmock.rb', line 109

def send(request)
  request_signature = Plugin.build_webmock_request_signature(request)
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)

  if (mock_response = WebMock::StubRegistry.instance.response_for_request(request_signature))
    response = Plugin.build_from_webmock_response(request, mock_response)
    WebMock::CallbackRegistry.invoke_callbacks({ lib: :httpx }, request_signature, mock_response)
    log { "mocking #{request.uri} with #{mock_response.inspect}" }
    request.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)
    if WebMock::CallbackRegistry.any_callbacks?
      request.on(:response) do |resp|
        unless resp.is_a?(HTTPX::ErrorResponse)
          webmock_response = Plugin.build_webmock_response(request, resp)
          WebMock::CallbackRegistry.invoke_callbacks(
            { lib: :httpx, real_request: true }, request_signature,
            webmock_response
          )
        end
      end
    end
    @mocked = false
    emit(:unmock_connection, self)
    super
  else
    raise WebMock::NetConnectNotAllowedError, request_signature
  end
end

#terminateObject



105
106
107
# File 'lib/httpx/adapters/webmock.rb', line 105

def terminate
  force_reset
end