Class: Net::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_web/ext/net_http.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.socket_type_with_fakewebObject Also known as: socket_type



31
32
33
# File 'lib/fake_web/ext/net_http.rb', line 31

def socket_type_with_fakeweb
  FakeWeb::StubSocket
end

Instance Method Details

#connect_with_fakewebObject Also known as: connect



70
71
72
73
74
75
76
# File 'lib/fake_web/ext/net_http.rb', line 70

def connect_with_fakeweb
  unless @@alredy_checked_for_net_http_replacement_libs ||= false
    FakeWeb::Utility.puts_warning_for_net_http_replacement_libs_if_needed
    @@alredy_checked_for_net_http_replacement_libs = true
  end
  nil
end

#request_with_fakeweb(request, body = nil, &block) ⇒ Object Also known as: request



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
# File 'lib/fake_web/ext/net_http.rb', line 38

def request_with_fakeweb(request, body = nil, &block)
  protocol = use_ssl? ? "https" : "http"

  path = request.path
  path = URI.parse(request.path).request_uri if request.path =~ /^http/

  if request["authorization"] =~ /^Basic /
    userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
    userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
  else
    userinfo = ""
  end

  uri = "#{protocol}://#{userinfo}#{self.address}:#{self.port}#{path}"
  method = request.method.downcase.to_sym

  if FakeWeb.registered_uri?(method, uri)
    @socket = Net::HTTP.socket_type.new
    FakeWeb.response_for(method, uri, &block)
  elsif FakeWeb.allow_net_connect?
    connect_without_fakeweb
    request_without_fakeweb(request, body, &block)
  else
    uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
    raise FakeWeb::NetConnectNotAllowedError,
          "Real HTTP connections are disabled. Unregistered request: #{request.method} #{uri}"
  end
end