class WebMock::HttpLibAdapters::NetHttpAdapter

Constants

OriginalNetBufferedIO
OriginalNetHTTP

Public Class Methods

disable!() click to toggle source
# File lib/webmock/http_lib_adapters/net_http.rb, line 24
def self.disable!
  Net.send(:remove_const, :BufferedIO)
  Net.send(:remove_const, :HTTP)
  Net.send(:remove_const, :HTTPSession)
  Net.send(:const_set, :HTTP, OriginalNetHTTP)
  Net.send(:const_set, :HTTPSession, OriginalNetHTTP)
  Net.send(:const_set, :BufferedIO, OriginalNetBufferedIO)

  #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
enable!() click to toggle source
# File lib/webmock/http_lib_adapters/net_http.rb, line 15
def self.enable!
  Net.send(:remove_const, :BufferedIO)
  Net.send(:remove_const, :HTTP)
  Net.send(:remove_const, :HTTPSession)
  Net.send(:const_set, :HTTP, @webMockNetHTTP)
  Net.send(:const_set, :HTTPSession, @webMockNetHTTP)
  Net.send(:const_set, :BufferedIO, Net::WebMockNetBufferedIO)
end

Public Instance Methods

build_net_http_response(webmock_response) { |response| ... } click to toggle source
# File lib/webmock/http_lib_adapters/net_http.rb, line 154
def build_net_http_response(webmock_response, &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 body.to_s == ''

  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.extend Net::WebMockHTTPResponse

  raise Timeout::Error, "execution expired" if webmock_response.should_timeout

  webmock_response.raise_error_if_any

  yield response if block_given?

  response
end
build_webmock_response(net_http_response) click to toggle source
# File lib/webmock/http_lib_adapters/net_http.rb, line 180
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
check_right_http_connection() click to toggle source
# File lib/webmock/http_lib_adapters/net_http.rb, line 191
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
const_defined?(name) click to toggle source
Calls superclass method
# File lib/webmock/http_lib_adapters/net_http.rb, line 50
def const_defined?(name)
  super || self.superclass.const_defined?(name)
end
const_get(name, inherit=true) click to toggle source
Calls superclass method
# File lib/webmock/http_lib_adapters/net_http.rb, line 60
def const_get(name, inherit=true)
  super
rescue NameError
  self.superclass.const_get(name, inherit)
end
constants(inherit=true) click to toggle source
Calls superclass method
# File lib/webmock/http_lib_adapters/net_http.rb, line 68
def constants(inherit=true)
  (super + self.superclass.constants(inherit)).uniq
end
request(request, body = nil, &block) click to toggle source
Calls superclass method
# File lib/webmock/http_lib_adapters/net_http.rb, line 74
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, &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?
      if WebMock::Config.instance.net_http_connect_on_start
        super_with_after_request.call
      else
        start_with_connect_without_finish {
          super_with_after_request.call
        }
      end
    else
      start_with_connect {
        super_with_after_request.call
      }
    end
  else
    raise WebMock::NetConnectNotAllowedError.new(request_signature)
  end
end
socket_type() click to toggle source
# File lib/webmock/http_lib_adapters/net_http.rb, line 45
def socket_type
  StubSocket
end
start(&block) click to toggle source
Calls superclass method
# File lib/webmock/http_lib_adapters/net_http.rb, line 146
def start(&block)
  if WebMock::Config.instance.net_http_connect_on_start
    super(&block)
  else
    start_without_connect(&block)
  end
end
start_with_connect_without_finish() { |http| ... } click to toggle source
# File lib/webmock/http_lib_adapters/net_http.rb, line 133
def start_with_connect_without_finish  # :yield: http
  if block_given?
    begin
      do_start
      return yield(self)
    end
  end
  do_start
  self
end
start_without_connect() { |self| ... } click to toggle source
# File lib/webmock/http_lib_adapters/net_http.rb, line 118
def start_without_connect
  raise IOError, 'HTTP session already opened' if @started
  if block_given?
    begin
      @started = true
      return yield(self)
    ensure
      do_finish
    end
  end
  @started = true
  self
end