Module: WebMock

Extended by:
API
Includes:
API
Defined in:
lib/webmock/util/json.rb,
lib/webmock/api.rb,
lib/webmock/config.rb,
lib/webmock/errors.rb,
lib/webmock/version.rb,
lib/webmock/webmock.rb,
lib/webmock/response.rb,
lib/webmock/util/uri.rb,
lib/webmock/deprecation.rb,
lib/webmock/request_stub.rb,
lib/webmock/util/headers.rb,
lib/webmock/rack_response.rb,
lib/webmock/stub_registry.rb,
lib/webmock/rspec/matchers.rb,
lib/webmock/request_pattern.rb,
lib/webmock/request_registry.rb,
lib/webmock/assertion_failure.rb,
lib/webmock/callback_registry.rb,
lib/webmock/request_body_diff.rb,
lib/webmock/request_signature.rb,
lib/webmock/util/hash_counter.rb,
lib/webmock/responses_sequence.rb,
lib/webmock/util/hash_validator.rb,
lib/webmock/stub_request_snippet.rb,
lib/webmock/util/version_checker.rb,
lib/webmock/matchers/any_arg_matcher.rb,
lib/webmock/request_signature_snippet.rb,
lib/webmock/http_lib_adapters/net_http.rb,
lib/webmock/http_lib_adapters/net_http.rb,
lib/webmock/request_execution_verifier.rb,
lib/webmock/util/hash_keys_stringifier.rb,
lib/webmock/http_lib_adapters/curb_adapter.rb,
lib/webmock/matchers/hash_argument_matcher.rb,
lib/webmock/rspec/matchers/webmock_matcher.rb,
lib/webmock/http_lib_adapters/excon_adapter.rb,
lib/webmock/matchers/hash_excluding_matcher.rb,
lib/webmock/matchers/hash_including_matcher.rb,
lib/webmock/http_lib_adapters/patron_adapter.rb,
lib/webmock/http_lib_adapters/http_rb_adapter.rb,
lib/webmock/http_lib_adapters/http_lib_adapter.rb,
lib/webmock/http_lib_adapters/manticore_adapter.rb,
lib/webmock/http_lib_adapters/httpclient_adapter.rb,
lib/webmock/rspec/matchers/request_pattern_matcher.rb,
lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb,
lib/webmock/http_lib_adapters/em_http_request_adapter.rb,
lib/webmock/http_lib_adapters/async_http_client_adapter.rb,
lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb

Overview

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: API, HttpLibAdapters, Matchers, NetHTTPUtility, RSpecMatcherDetector, Util Classes: AssertionFailure, BodyPattern, CallbackRegistry, Config, Deprecation, DynamicResponse, HashValidator, HeadersPattern, HttpLibAdapter, HttpLibAdapterRegistry, MethodPattern, NetConnectNotAllowedError, RackResponse, RequestBodyDiff, RequestExecutionVerifier, RequestPattern, RequestPatternMatcher, RequestRegistry, RequestSignature, RequestSignatureSnippet, RequestStub, Response, ResponseFactory, ResponsesSequence, StubRegistry, StubRequestSnippet, URIAddressablePattern, URICallablePattern, URIPattern, URIRegexpPattern, URIStringPattern, VersionChecker, WebMockMatcher

Constant Summary collapse

VERSION =
'3.18.1'

Class Method Summary collapse

Methods included from API

a_request, assert_not_requested, assert_requested, hash_excluding, hash_including, remove_request_stub, reset_executed_requests!, stub_request

Class Method Details

.after_request(options = {}, &block) ⇒ Object



141
142
143
# File 'lib/webmock/webmock.rb', line 141

def self.after_request(options={}, &block)
  WebMock::CallbackRegistry.add_callback(options, block)
end

.allow_net_connect!(options = {}) ⇒ Object Also known as: enable_net_connect!



44
45
46
47
# File 'lib/webmock/webmock.rb', line 44

def self.allow_net_connect!(options = {})
  Config.instance.allow_net_connect = true
  Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
end

.disable!(options = {}) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/webmock/webmock.rb', line 28

def self.disable!(options = {})
  except = [options[:except]].flatten.compact
  HttpLibAdapterRegistry.instance.each_adapter do |name, adapter|
    adapter.enable!
    adapter.disable! unless except.include?(name)
  end
end

.disable_net_connect!(options = {}) ⇒ Object Also known as: disallow_net_connect!



49
50
51
52
53
54
# File 'lib/webmock/webmock.rb', line 49

def self.disable_net_connect!(options = {})
  Config.instance.allow_net_connect = false
  Config.instance.allow_localhost = options[:allow_localhost]
  Config.instance.allow = options[:allow]
  Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
end

.enable!(options = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/webmock/webmock.rb', line 36

def self.enable!(options = {})
  except = [options[:except]].flatten.compact
  HttpLibAdapterRegistry.instance.each_adapter do |name, adapter|
    adapter.disable!
    adapter.enable! unless except.include?(name)
  end
end

.globally_stub_request(order = :before_local_stubs, &block) ⇒ Object



153
154
155
# File 'lib/webmock/webmock.rb', line 153

def self.globally_stub_request(order = :before_local_stubs, &block)
  WebMock::StubRegistry.instance.register_global_stub(order, &block)
end

.hide_body_diff!Object



107
108
109
# File 'lib/webmock/webmock.rb', line 107

def self.hide_body_diff!
  Config.instance.show_body_diff = false
end

.hide_stubbing_instructions!Object



115
116
117
# File 'lib/webmock/webmock.rb', line 115

def self.hide_stubbing_instructions!
  Config.instance.show_stubbing_instructions = false
end

.included(clazz) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/webmock/webmock.rb', line 3

def self.included(clazz)
  WebMock::Deprecation.warning("include WebMock is deprecated. Please include WebMock::API instead")
  if clazz.instance_methods.map(&:to_s).include?('request')
    warn "WebMock#request was not included in #{clazz} to avoid name collision"
  else
    clazz.class_eval do
      def request(method, uri)
        WebMock::Deprecation.warning("WebMock#request is deprecated. Please use WebMock::API#a_request method instead")
        WebMock.a_request(method, uri)
      end
    end
  end
end

.net_connect_allowed?(uri = nil) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/webmock/webmock.rb', line 61

def self.net_connect_allowed?(uri = nil)
  return !!Config.instance.allow_net_connect if uri.nil?

  if uri.is_a?(String)
    uri = WebMock::Util::URI.normalize_uri(uri)
  end

  !!Config.instance.allow_net_connect ||
  ( Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri) ||
    Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri) )
end

.net_connect_explicit_allowed?(allowed, uri = nil) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/webmock/webmock.rb', line 83

def self.net_connect_explicit_allowed?(allowed, uri=nil)
  case allowed
  when Array
    allowed.any? { |allowed_item| net_connect_explicit_allowed?(allowed_item, uri) }
  when Regexp
    (uri.to_s =~ allowed) != nil ||
    (uri.omit(:port).to_s =~ allowed) != nil && uri.port == uri.default_port
  when String
    allowed == uri.to_s ||
    allowed == uri.host ||
    allowed == "#{uri.host}:#{uri.port}" ||
    allowed == "#{uri.scheme}://#{uri.host}:#{uri.port}" ||
    allowed == "#{uri.scheme}://#{uri.host}" && uri.port == uri.default_port
  else
    if allowed.respond_to?(:call)
      allowed.call(uri)
    end
  end
end

.net_http_connect_on_start?(uri) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
# File 'lib/webmock/webmock.rb', line 73

def self.net_http_connect_on_start?(uri)
  allowed = Config.instance.net_http_connect_on_start || false

  if [true, false].include?(allowed)
    allowed
  else
    net_connect_explicit_allowed?(allowed, uri)
  end
end


149
150
151
# File 'lib/webmock/webmock.rb', line 149

def self.print_executed_requests
  puts WebMock::RequestExecutionVerifier.executed_requests_message
end

.registered_request?(request_signature) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/webmock/webmock.rb', line 145

def self.registered_request?(request_signature)
  WebMock::StubRegistry.instance.registered_request?(request_signature)
end

.reset!Object



127
128
129
130
# File 'lib/webmock/webmock.rb', line 127

def self.reset!
  WebMock::RequestRegistry.instance.reset!
  WebMock::StubRegistry.instance.reset!
end

.reset_callbacksObject



137
138
139
# File 'lib/webmock/webmock.rb', line 137

def self.reset_callbacks
  WebMock::CallbackRegistry.reset
end

.reset_webmockObject



132
133
134
135
# File 'lib/webmock/webmock.rb', line 132

def self.reset_webmock
  WebMock::Deprecation.warning("WebMock.reset_webmock is deprecated. Please use WebMock.reset! method instead")
  reset!
end

.show_body_diff!Object



103
104
105
# File 'lib/webmock/webmock.rb', line 103

def self.show_body_diff!
  Config.instance.show_body_diff = true
end

.show_body_diff?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/webmock/webmock.rb', line 111

def self.show_body_diff?
  Config.instance.show_body_diff
end

.show_stubbing_instructions!Object



119
120
121
# File 'lib/webmock/webmock.rb', line 119

def self.show_stubbing_instructions!
  Config.instance.show_stubbing_instructions = true
end

.show_stubbing_instructions?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/webmock/webmock.rb', line 123

def self.show_stubbing_instructions?
  Config.instance.show_stubbing_instructions
end

.versionObject



24
25
26
# File 'lib/webmock/webmock.rb', line 24

def self.version
  VERSION
end