Class: WebMock::HttpLibAdapters::NetHttpAdapter

Inherits:
WebMock::HttpLibAdapter show all
Defined in:
lib/webmock/http_lib_adapters/net_http.rb

Constant Summary collapse

OriginalNetHTTP =
Net::HTTP
HAS_LEGACY_CONSTANT =

This will be removed in Ruby 3.5. In Ruby 3.4, const_remove will warn.

Net.const_defined?(:HTTPSession)

Class Method Summary collapse

Methods inherited from WebMock::HttpLibAdapter

adapter_for

Class Method Details

.disable!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/webmock/http_lib_adapters/net_http.rb', line 27

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

.enable!Object



18
19
20
21
22
23
24
25
# File 'lib/webmock/http_lib_adapters/net_http.rb', line 18

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

.remove_silently(mod, const) ⇒ Object

:nodoc:



46
47
48
49
50
51
52
# File 'lib/webmock/http_lib_adapters/net_http.rb', line 46

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