webmock 3.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/.gitignore +34 -0
  4. data/.rspec-tm +2 -0
  5. data/.travis.yml +19 -0
  6. data/CHANGELOG.md +1698 -0
  7. data/Gemfile +9 -0
  8. data/LICENSE +20 -0
  9. data/README.md +1125 -0
  10. data/Rakefile +28 -0
  11. data/lib/webmock.rb +59 -0
  12. data/lib/webmock/api.rb +109 -0
  13. data/lib/webmock/assertion_failure.rb +11 -0
  14. data/lib/webmock/callback_registry.rb +35 -0
  15. data/lib/webmock/config.rb +18 -0
  16. data/lib/webmock/cucumber.rb +10 -0
  17. data/lib/webmock/deprecation.rb +9 -0
  18. data/lib/webmock/errors.rb +17 -0
  19. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +214 -0
  20. data/lib/webmock/http_lib_adapters/curb_adapter.rb +347 -0
  21. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +228 -0
  22. data/lib/webmock/http_lib_adapters/excon_adapter.rb +162 -0
  23. data/lib/webmock/http_lib_adapters/http_lib_adapter.rb +7 -0
  24. data/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +19 -0
  25. data/lib/webmock/http_lib_adapters/http_rb/client.rb +14 -0
  26. data/lib/webmock/http_lib_adapters/http_rb/request.rb +16 -0
  27. data/lib/webmock/http_lib_adapters/http_rb/response.rb +43 -0
  28. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +29 -0
  29. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +68 -0
  30. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +37 -0
  31. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +242 -0
  32. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +130 -0
  33. data/lib/webmock/http_lib_adapters/net_http.rb +361 -0
  34. data/lib/webmock/http_lib_adapters/net_http_response.rb +34 -0
  35. data/lib/webmock/http_lib_adapters/patron_adapter.rb +130 -0
  36. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +174 -0
  37. data/lib/webmock/matchers/any_arg_matcher.rb +13 -0
  38. data/lib/webmock/matchers/hash_argument_matcher.rb +21 -0
  39. data/lib/webmock/matchers/hash_excluding_matcher.rb +15 -0
  40. data/lib/webmock/matchers/hash_including_matcher.rb +17 -0
  41. data/lib/webmock/minitest.rb +41 -0
  42. data/lib/webmock/rack_response.rb +69 -0
  43. data/lib/webmock/request_body_diff.rb +64 -0
  44. data/lib/webmock/request_execution_verifier.rb +77 -0
  45. data/lib/webmock/request_pattern.rb +370 -0
  46. data/lib/webmock/request_registry.rb +35 -0
  47. data/lib/webmock/request_signature.rb +54 -0
  48. data/lib/webmock/request_signature_snippet.rb +61 -0
  49. data/lib/webmock/request_stub.rb +100 -0
  50. data/lib/webmock/response.rb +153 -0
  51. data/lib/webmock/responses_sequence.rb +40 -0
  52. data/lib/webmock/rspec.rb +41 -0
  53. data/lib/webmock/rspec/matchers.rb +27 -0
  54. data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +78 -0
  55. data/lib/webmock/rspec/matchers/webmock_matcher.rb +67 -0
  56. data/lib/webmock/stub_registry.rb +67 -0
  57. data/lib/webmock/stub_request_snippet.rb +38 -0
  58. data/lib/webmock/test_unit.rb +22 -0
  59. data/lib/webmock/util/hash_counter.rb +39 -0
  60. data/lib/webmock/util/hash_keys_stringifier.rb +25 -0
  61. data/lib/webmock/util/hash_validator.rb +17 -0
  62. data/lib/webmock/util/headers.rb +64 -0
  63. data/lib/webmock/util/json.rb +67 -0
  64. data/lib/webmock/util/query_mapper.rb +281 -0
  65. data/lib/webmock/util/uri.rb +110 -0
  66. data/lib/webmock/util/values_stringifier.rb +20 -0
  67. data/lib/webmock/util/version_checker.rb +111 -0
  68. data/lib/webmock/version.rb +3 -0
  69. data/lib/webmock/webmock.rb +161 -0
  70. data/minitest/test_helper.rb +34 -0
  71. data/minitest/test_webmock.rb +9 -0
  72. data/minitest/webmock_spec.rb +60 -0
  73. data/spec/acceptance/async_http_client/async_http_client_spec.rb +349 -0
  74. data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +73 -0
  75. data/spec/acceptance/curb/curb_spec.rb +492 -0
  76. data/spec/acceptance/curb/curb_spec_helper.rb +147 -0
  77. data/spec/acceptance/em_http_request/em_http_request_spec.rb +406 -0
  78. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +77 -0
  79. data/spec/acceptance/excon/excon_spec.rb +77 -0
  80. data/spec/acceptance/excon/excon_spec_helper.rb +50 -0
  81. data/spec/acceptance/http_rb/http_rb_spec.rb +82 -0
  82. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +54 -0
  83. data/spec/acceptance/httpclient/httpclient_spec.rb +217 -0
  84. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +57 -0
  85. data/spec/acceptance/manticore/manticore_spec.rb +56 -0
  86. data/spec/acceptance/manticore/manticore_spec_helper.rb +35 -0
  87. data/spec/acceptance/net_http/net_http_shared.rb +153 -0
  88. data/spec/acceptance/net_http/net_http_spec.rb +331 -0
  89. data/spec/acceptance/net_http/net_http_spec_helper.rb +64 -0
  90. data/spec/acceptance/net_http/real_net_http_spec.rb +20 -0
  91. data/spec/acceptance/patron/patron_spec.rb +125 -0
  92. data/spec/acceptance/patron/patron_spec_helper.rb +54 -0
  93. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +313 -0
  94. data/spec/acceptance/shared/callbacks.rb +148 -0
  95. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +36 -0
  96. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +95 -0
  97. data/spec/acceptance/shared/precedence_of_stubs.rb +15 -0
  98. data/spec/acceptance/shared/request_expectations.rb +930 -0
  99. data/spec/acceptance/shared/returning_declared_responses.rb +409 -0
  100. data/spec/acceptance/shared/stubbing_requests.rb +643 -0
  101. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +135 -0
  102. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +60 -0
  103. data/spec/acceptance/webmock_shared.rb +41 -0
  104. data/spec/fixtures/test.txt +1 -0
  105. data/spec/quality_spec.rb +84 -0
  106. data/spec/spec_helper.rb +48 -0
  107. data/spec/support/example_curl_output.txt +22 -0
  108. data/spec/support/failures.rb +9 -0
  109. data/spec/support/my_rack_app.rb +53 -0
  110. data/spec/support/network_connection.rb +19 -0
  111. data/spec/support/webmock_server.rb +70 -0
  112. data/spec/unit/api_spec.rb +175 -0
  113. data/spec/unit/errors_spec.rb +129 -0
  114. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +17 -0
  115. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +12 -0
  116. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +61 -0
  117. data/spec/unit/matchers/hash_including_matcher_spec.rb +87 -0
  118. data/spec/unit/rack_response_spec.rb +112 -0
  119. data/spec/unit/request_body_diff_spec.rb +90 -0
  120. data/spec/unit/request_execution_verifier_spec.rb +208 -0
  121. data/spec/unit/request_pattern_spec.rb +601 -0
  122. data/spec/unit/request_registry_spec.rb +95 -0
  123. data/spec/unit/request_signature_snippet_spec.rb +89 -0
  124. data/spec/unit/request_signature_spec.rb +155 -0
  125. data/spec/unit/request_stub_spec.rb +199 -0
  126. data/spec/unit/response_spec.rb +282 -0
  127. data/spec/unit/stub_registry_spec.rb +103 -0
  128. data/spec/unit/stub_request_snippet_spec.rb +115 -0
  129. data/spec/unit/util/hash_counter_spec.rb +39 -0
  130. data/spec/unit/util/hash_keys_stringifier_spec.rb +27 -0
  131. data/spec/unit/util/headers_spec.rb +28 -0
  132. data/spec/unit/util/json_spec.rb +33 -0
  133. data/spec/unit/util/query_mapper_spec.rb +157 -0
  134. data/spec/unit/util/uri_spec.rb +361 -0
  135. data/spec/unit/util/version_checker_spec.rb +65 -0
  136. data/spec/unit/webmock_spec.rb +19 -0
  137. data/test/http_request.rb +24 -0
  138. data/test/shared_test.rb +108 -0
  139. data/test/test_helper.rb +23 -0
  140. data/test/test_webmock.rb +6 -0
  141. data/webmock.gemspec +45 -0
  142. metadata +496 -0
@@ -0,0 +1,34 @@
1
+ # This code is entierly copied from VCR (http://github.com/myronmarston/vcr) by courtesy of Myron Marston
2
+
3
+ # A Net::HTTP response that has already been read raises an IOError when #read_body
4
+ # is called with a destination string or block.
5
+ #
6
+ # This causes a problem when VCR records a response--it reads the body before yielding
7
+ # the response, and if the code that is consuming the HTTP requests uses #read_body, it
8
+ # can cause an error.
9
+ #
10
+ # This is a bit of a hack, but it allows a Net::HTTP response to be "re-read"
11
+ # after it has aleady been read. This attemps to preserve the behavior of
12
+ # #read_body, acting just as if it had never been read.
13
+
14
+
15
+ module Net
16
+ module WebMockHTTPResponse
17
+ def read_body(dest = nil, &block)
18
+ if !(defined?(@__read_body_previously_called).nil?) && @__read_body_previously_called
19
+ return super
20
+ end
21
+ return @body if dest.nil? && block.nil?
22
+ raise ArgumentError.new("both arg and block given for HTTP method") if dest && block
23
+ return nil if @body.nil?
24
+
25
+ dest ||= ::Net::ReadAdapter.new(block)
26
+ dest << @body.dup
27
+ @body = dest
28
+ ensure
29
+ # allow subsequent calls to #read_body to proceed as normal, without our hack...
30
+ @__read_body_previously_called = true
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,130 @@
1
+ begin
2
+ require 'patron'
3
+ rescue LoadError
4
+ # patron not found
5
+ end
6
+
7
+ if defined?(::Patron)
8
+ module WebMock
9
+ module HttpLibAdapters
10
+ class PatronAdapter < ::WebMock::HttpLibAdapter
11
+ adapter_for :patron
12
+
13
+ OriginalPatronSession = ::Patron::Session unless const_defined?(:OriginalPatronSession)
14
+
15
+ class WebMockPatronSession < ::Patron::Session
16
+ def handle_request(req)
17
+ request_signature =
18
+ WebMock::HttpLibAdapters::PatronAdapter.build_request_signature(req)
19
+
20
+ WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
21
+
22
+ if webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
23
+ WebMock::HttpLibAdapters::PatronAdapter.
24
+ handle_file_name(req, webmock_response)
25
+ res = WebMock::HttpLibAdapters::PatronAdapter.
26
+ build_patron_response(webmock_response, default_response_charset)
27
+ WebMock::CallbackRegistry.invoke_callbacks(
28
+ {lib: :patron}, request_signature, webmock_response)
29
+ res
30
+ elsif WebMock.net_connect_allowed?(request_signature.uri)
31
+ res = super
32
+ if WebMock::CallbackRegistry.any_callbacks?
33
+ webmock_response = WebMock::HttpLibAdapters::PatronAdapter.
34
+ build_webmock_response(res)
35
+ WebMock::CallbackRegistry.invoke_callbacks(
36
+ {lib: :patron, real_request: true}, request_signature,
37
+ webmock_response)
38
+ end
39
+ res
40
+ else
41
+ raise WebMock::NetConnectNotAllowedError.new(request_signature)
42
+ end
43
+ end
44
+ end
45
+
46
+ def self.enable!
47
+ Patron.send(:remove_const, :Session)
48
+ Patron.send(:const_set, :Session, WebMockPatronSession)
49
+ end
50
+
51
+ def self.disable!
52
+ Patron.send(:remove_const, :Session)
53
+ Patron.send(:const_set, :Session, OriginalPatronSession)
54
+ end
55
+
56
+ def self.handle_file_name(req, webmock_response)
57
+ if req.action == :get && req.file_name
58
+ begin
59
+ File.open(req.file_name, "w") do |f|
60
+ f.write webmock_response.body
61
+ end
62
+ rescue Errno::EACCES
63
+ raise ArgumentError.new("Unable to open specified file.")
64
+ end
65
+ end
66
+ end
67
+
68
+ def self.build_request_signature(req)
69
+ uri = WebMock::Util::URI.heuristic_parse(req.url)
70
+ uri.path = uri.normalized_path.gsub("[^:]//","/")
71
+
72
+ if [:put, :post, :patch].include?(req.action)
73
+ if req.file_name
74
+ if !File.exist?(req.file_name) || !File.readable?(req.file_name)
75
+ raise ArgumentError.new("Unable to open specified file.")
76
+ end
77
+ request_body = File.read(req.file_name)
78
+ elsif req.upload_data
79
+ request_body = req.upload_data
80
+ else
81
+ raise ArgumentError.new("Must provide either data or a filename when doing a PUT or POST")
82
+ end
83
+ end
84
+
85
+ headers = req.headers
86
+
87
+ if req.credentials
88
+ headers['Authorization'] = WebMock::Util::Headers.basic_auth_header(req.credentials)
89
+ end
90
+
91
+ request_signature = WebMock::RequestSignature.new(
92
+ req.action,
93
+ uri.to_s,
94
+ body: request_body,
95
+ headers: headers
96
+ )
97
+ request_signature
98
+ end
99
+
100
+ def self.build_patron_response(webmock_response, default_response_charset)
101
+ raise ::Patron::TimeoutError if webmock_response.should_timeout
102
+ webmock_response.raise_error_if_any
103
+
104
+ header_fields = (webmock_response.headers || []).map { |(k, vs)| Array(vs).map { |v| "#{k}: #{v}" } }.flatten
105
+ status_line = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}"
106
+ header_data = ([status_line] + header_fields).join("\r\n")
107
+
108
+ ::Patron::Response.new(
109
+ "".dup,
110
+ webmock_response.status[0],
111
+ 0,
112
+ header_data,
113
+ webmock_response.body.dup,
114
+ default_response_charset
115
+ )
116
+ end
117
+
118
+ def self.build_webmock_response(patron_response)
119
+ webmock_response = WebMock::Response.new
120
+ reason = patron_response.status_line.
121
+ scan(%r(\AHTTP/(\d+(?:\.\d+)?)\s+(\d\d\d)\s*([^\r\n]+)?))[0][2]
122
+ webmock_response.status = [patron_response.status, reason]
123
+ webmock_response.body = patron_response.body
124
+ webmock_response.headers = patron_response.headers
125
+ webmock_response
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,174 @@
1
+ begin
2
+ require 'typhoeus'
3
+ rescue LoadError
4
+ # typhoeus not found
5
+ end
6
+
7
+ if defined?(Typhoeus)
8
+ WebMock::VersionChecker.new('Typhoeus', Typhoeus::VERSION, '0.3.2').check_version!
9
+
10
+ module WebMock
11
+ module HttpLibAdapters
12
+ class TyphoeusAdapter < HttpLibAdapter
13
+ adapter_for :typhoeus
14
+
15
+ def self.enable!
16
+ @disabled = false
17
+ add_before_callback
18
+ add_after_request_callback
19
+ ::Typhoeus::Config.block_connection = true
20
+ end
21
+
22
+ def self.disable!
23
+ @disabled = true
24
+ remove_after_request_callback
25
+ remove_before_callback
26
+ ::Typhoeus::Config.block_connection = false
27
+ end
28
+
29
+ def self.disabled?
30
+ !!@disabled
31
+ end
32
+
33
+ def self.add_before_callback
34
+ unless Typhoeus.before.include?(BEFORE_CALLBACK)
35
+ Typhoeus.before << BEFORE_CALLBACK
36
+ end
37
+ end
38
+
39
+ def self.remove_before_callback
40
+ Typhoeus.before.delete_if {|v| v == BEFORE_CALLBACK }
41
+ end
42
+
43
+ def self.add_after_request_callback
44
+ unless Typhoeus.on_complete.include?(AFTER_REQUEST_CALLBACK)
45
+ Typhoeus.on_complete << AFTER_REQUEST_CALLBACK
46
+ end
47
+ end
48
+
49
+ def self.remove_after_request_callback
50
+ Typhoeus.on_complete.delete_if {|v| v == AFTER_REQUEST_CALLBACK }
51
+ end
52
+
53
+ def self.build_request_signature(req)
54
+ uri = WebMock::Util::URI.heuristic_parse(req.url)
55
+ uri.path = uri.normalized_path.gsub("[^:]//","/")
56
+
57
+ headers = req.options[:headers]
58
+
59
+ if req.options[:userpwd]
60
+ headers['Authorization'] = WebMock::Util::Headers.basic_auth_header(req.options[:userpwd])
61
+ end
62
+
63
+ body = req.options[:body]
64
+
65
+ if body.is_a?(Hash)
66
+ body = WebMock::Util::QueryMapper.values_to_query(body)
67
+ end
68
+
69
+ request_signature = WebMock::RequestSignature.new(
70
+ req.options[:method] || :get,
71
+ uri.to_s,
72
+ body: body,
73
+ headers: headers
74
+ )
75
+
76
+ req.instance_variable_set(:@__webmock_request_signature, request_signature)
77
+
78
+ request_signature
79
+ end
80
+
81
+
82
+ def self.build_webmock_response(typhoeus_response)
83
+ webmock_response = WebMock::Response.new
84
+ webmock_response.status = [typhoeus_response.code, typhoeus_response.status_message]
85
+ webmock_response.body = typhoeus_response.body
86
+ webmock_response.headers = typhoeus_response.headers
87
+ webmock_response
88
+ end
89
+
90
+ def self.generate_typhoeus_response(request_signature, webmock_response)
91
+ response = if webmock_response.should_timeout
92
+ ::Typhoeus::Response.new(
93
+ code: 0,
94
+ status_message: "",
95
+ body: "",
96
+ headers: {},
97
+ return_code: :operation_timedout
98
+ )
99
+ else
100
+ ::Typhoeus::Response.new(
101
+ code: webmock_response.status[0],
102
+ status_message: webmock_response.status[1],
103
+ body: webmock_response.body,
104
+ headers: webmock_response.headers,
105
+ effective_url: request_signature.uri
106
+ )
107
+ end
108
+ response.mock = :webmock
109
+ response
110
+ end
111
+
112
+ def self.request_hash(request_signature)
113
+ hash = {}
114
+
115
+ hash[:body] = request_signature.body
116
+ hash[:headers] = request_signature.headers
117
+
118
+ hash
119
+ end
120
+
121
+ AFTER_REQUEST_CALLBACK = Proc.new do |response|
122
+ request = response.request
123
+ request_signature = request.instance_variable_get(:@__webmock_request_signature)
124
+ webmock_response =
125
+ ::WebMock::HttpLibAdapters::TyphoeusAdapter.
126
+ build_webmock_response(response)
127
+ if response.mock
128
+ WebMock::CallbackRegistry.invoke_callbacks(
129
+ {lib: :typhoeus},
130
+ request_signature,
131
+ webmock_response
132
+ )
133
+ else
134
+ WebMock::CallbackRegistry.invoke_callbacks(
135
+ {lib: :typhoeus, real_request: true},
136
+ request_signature,
137
+ webmock_response
138
+ )
139
+ end
140
+ end
141
+
142
+ BEFORE_CALLBACK = Proc.new do |request|
143
+ Typhoeus::Expectation.all.delete_if {|e| e.from == :webmock }
144
+ res = true
145
+
146
+ unless WebMock::HttpLibAdapters::TyphoeusAdapter.disabled?
147
+ request_signature = ::WebMock::HttpLibAdapters::TyphoeusAdapter.build_request_signature(request)
148
+ request.block_connection = false;
149
+
150
+ ::WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
151
+
152
+ if webmock_response = ::WebMock::StubRegistry.instance.response_for_request(request_signature)
153
+ # ::WebMock::HttpLibAdapters::TyphoeusAdapter.stub_typhoeus(request_signature, webmock_response, self)
154
+ response = ::WebMock::HttpLibAdapters::TyphoeusAdapter.generate_typhoeus_response(request_signature, webmock_response)
155
+ if request.respond_to?(:on_headers)
156
+ request.execute_headers_callbacks(response)
157
+ end
158
+ if request.respond_to?(:streaming?) && request.streaming?
159
+ response.options[:response_body] = ""
160
+ request.on_body.each { |callback| callback.call(webmock_response.body, response) }
161
+ end
162
+ request.finish(response)
163
+ webmock_response.raise_error_if_any
164
+ res = false
165
+ elsif !WebMock.net_connect_allowed?(request_signature.uri)
166
+ raise WebMock::NetConnectNotAllowedError.new(request_signature)
167
+ end
168
+ end
169
+ res
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,13 @@
1
+ module WebMock
2
+ module Matchers
3
+ # this is a based on RSpec::Mocks::ArgumentMatchers::AnyArgMatcher
4
+ class AnyArgMatcher
5
+ def initialize(ignore)
6
+ end
7
+
8
+ def ==(other)
9
+ true
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ module WebMock
2
+ module Matchers
3
+ # Base class for Hash matchers
4
+ # https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/argument_matchers.rb
5
+ class HashArgumentMatcher
6
+ def initialize(expected)
7
+ @expected = Hash[WebMock::Util::HashKeysStringifier.stringify_keys!(expected, deep: true).sort]
8
+ end
9
+
10
+ def ==(_actual, &block)
11
+ @expected.all?(&block)
12
+ rescue NoMethodError
13
+ false
14
+ end
15
+
16
+ def self.from_rspec_matcher(matcher)
17
+ new(matcher.instance_variable_get(:@expected))
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ module WebMock
2
+ module Matchers
3
+ # this is a based on RSpec::Mocks::ArgumentMatchers::HashExcludingMatcher
4
+ # https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/argument_matchers.rb
5
+ class HashExcludingMatcher < HashArgumentMatcher
6
+ def ==(actual)
7
+ super { |key, value| !actual.key?(key) || value != actual[key] }
8
+ end
9
+
10
+ def inspect
11
+ "hash_excluding(#{@expected.inspect})"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module WebMock
2
+ module Matchers
3
+ # this is a based on RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher
4
+ # https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/argument_matchers.rb
5
+ class HashIncludingMatcher < HashArgumentMatcher
6
+ def ==(actual)
7
+ super { |key, value| actual.key?(key) && value === actual[key] }
8
+ rescue NoMethodError
9
+ false
10
+ end
11
+
12
+ def inspect
13
+ "hash_including(#{@expected.inspect})"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,41 @@
1
+ begin
2
+ require 'minitest/test'
3
+ test_class = Minitest::Test
4
+ assertions = "assertions"
5
+ rescue LoadError
6
+ require "minitest/unit"
7
+ test_class = MiniTest::Unit::TestCase
8
+ assertions = "_assertions"
9
+ end
10
+
11
+ require 'webmock'
12
+
13
+ WebMock.enable!
14
+
15
+ test_class.class_eval do
16
+ include WebMock::API
17
+
18
+ alias_method :teardown_without_webmock, :teardown
19
+ def teardown_with_webmock
20
+ teardown_without_webmock
21
+ WebMock.reset!
22
+ end
23
+ alias_method :teardown, :teardown_with_webmock
24
+
25
+ [:assert_request_requested, :assert_request_not_requested].each do |name|
26
+ alias_method :"#{name}_without_assertions_count", name
27
+ define_method :"#{name}_with_assertions_count" do |*args|
28
+ self.send("#{assertions}=", self.send("#{assertions}") + 1)
29
+ send :"#{name}_without_assertions_count", *args
30
+ end
31
+ alias_method name, :"#{name}_with_assertions_count"
32
+ end
33
+ end
34
+
35
+ begin
36
+ error_class = MiniTest::Assertion
37
+ rescue NameError
38
+ error_class = Minitest::Assertion
39
+ end
40
+
41
+ WebMock::AssertionFailure.error_class = error_class