class WebMock::HttpLibAdapters::NetHttpAdapter
def self.disable!
def self.disable! Net.send(:remove_const, :HTTP) Net.send(:const_set, :HTTP, OriginalNetHTTP) if HAS_LEGACY_CONSTANT remove_silently(Net, :HTTPSession) Net.send(:const_set, :HTTPSession, OriginalNetHTTP) end #copy all constants from @webMockNetHTTP to original Net::HTTP #in case any constants were added to @webMockNetHTTP instead of Net::HTTP #after WebMock was enabled. #i.e Net::HTTP::DigestAuth @webMockNetHTTP.constants.each do |constant| if !OriginalNetHTTP.constants.map(&:to_s).include?(constant.to_s) OriginalNetHTTP.send(:const_set, constant, @webMockNetHTTP.const_get(constant)) end end end
def self.enable!
def self.enable! Net.send(:remove_const, :HTTP) Net.send(:const_set, :HTTP, @webMockNetHTTP) if HAS_LEGACY_CONSTANT remove_silently(Net, :HTTPSession) Net.send(:const_set, :HTTPSession, @webMockNetHTTP) end end
def self.remove_silently(mod, const) #:nodoc:
def self.remove_silently(mod, const) #:nodoc: # Don't warn on removing the deprecated constant verbose, $VERBOSE = $VERBOSE, nil mod.send(:remove_const, const) ensure $VERBOSE = verbose end
def build_net_http_response(webmock_response, request_uri, &block)
def build_net_http_response(webmock_response, request_uri, &block) response = Net::HTTPResponse.send(:response_class, webmock_response.status[0].to_s).new("1.0", webmock_response.status[0].to_s, webmock_response.status[1]) body = webmock_response.body body = nil if webmock_response.status[0].to_s == '204' response.instance_variable_set(:@body, body) webmock_response.headers.to_a.each do |name, values| values = [values] unless values.is_a?(Array) values.each do |value| response.add_field(name, value) end end response.instance_variable_set(:@read, true) response.uri = request_uri response.extend Net::WebMockHTTPResponse if webmock_response.should_timeout raise Net::OpenTimeout, "execution expired" end webmock_response.raise_error_if_any yield response if block_given? response end
def build_webmock_response(net_http_response)
def build_webmock_response(net_http_response) webmock_response = WebMock::Response.new webmock_response.status = [ net_http_response.code.to_i, net_http_response.message] webmock_response.headers = net_http_response.to_hash webmock_response.body = net_http_response.body webmock_response end
def check_right_http_connection
def check_right_http_connection unless @@alredy_checked_for_right_http_connection ||= false WebMock::NetHTTPUtility.puts_warning_for_right_http_if_needed @@alredy_checked_for_right_http_connection = true end end
def const_defined?(name)
def const_defined?(name) super || self.superclass.const_defined?(name) end
def const_defined?(name, inherit=true)
def const_defined?(name, inherit=true) super || self.superclass.const_defined?(name, inherit) end
def const_get(name, inherit=true)
def const_get(name, inherit=true) super rescue NameError self.superclass.const_get(name, inherit) end
def constants(inherit=true)
def constants(inherit=true) (super + self.superclass.constants(inherit)).uniq end
def ensure_actual_connection
def ensure_actual_connection if @socket.is_a?(StubSocket) @socket&.close @socket = nil do_start end end
def request(request, body = nil, &block)
def request(request, body = nil, &block) request_signature = WebMock::NetHTTPUtility.request_signature_from_request(self, request, body) WebMock::RequestRegistry.instance.requested_signatures.put(request_signature) if webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature) @socket = Net::HTTP.socket_type.new WebMock::CallbackRegistry.invoke_callbacks( {lib: :net_http}, request_signature, webmock_response) build_net_http_response(webmock_response, request.uri, &block) elsif WebMock.net_connect_allowed?(request_signature.uri) check_right_http_connection after_request = lambda do |response| if WebMock::CallbackRegistry.any_callbacks? webmock_response = build_webmock_response(response) WebMock::CallbackRegistry.invoke_callbacks( {lib: :net_http, real_request: true}, request_signature, webmock_response) end response.extend Net::WebMockHTTPResponse block.call response if block response end super_with_after_request = lambda { response = super(request, nil, &nil) after_request.call(response) } if started? ensure_actual_connection super_with_after_request.call else start_with_connect { super_with_after_request.call } end else raise WebMock::NetConnectNotAllowedError.new(request_signature) end end
def socket_type
def socket_type StubSocket end
def start(&block)
def start(&block) uri = Addressable::URI.parse(WebMock::NetHTTPUtility.get_uri(self)) if WebMock.net_http_connect_on_start?(uri) super(&block) else start_without_connect(&block) end end
def start_without_connect
def start_without_connect raise IOError, 'HTTP session already opened' if @started if block_given? begin @socket = Net::HTTP.socket_type.new @started = true return yield(self) ensure do_finish end end @socket = Net::HTTP.socket_type.new @started = true self end