ultra-pure-gem 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/ultra-pure-gem.gemspec +12 -0
  3. data/webmock-3.26.2/CHANGELOG.md +2148 -0
  4. data/webmock-3.26.2/LICENSE +20 -0
  5. data/webmock-3.26.2/README.md +1229 -0
  6. data/webmock-3.26.2/lib/webmock/api.rb +111 -0
  7. data/webmock-3.26.2/lib/webmock/assertion_failure.rb +13 -0
  8. data/webmock-3.26.2/lib/webmock/callback_registry.rb +37 -0
  9. data/webmock-3.26.2/lib/webmock/config.rb +20 -0
  10. data/webmock-3.26.2/lib/webmock/cucumber.rb +12 -0
  11. data/webmock-3.26.2/lib/webmock/deprecation.rb +11 -0
  12. data/webmock-3.26.2/lib/webmock/errors.rb +19 -0
  13. data/webmock-3.26.2/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +228 -0
  14. data/webmock-3.26.2/lib/webmock/http_lib_adapters/curb_adapter.rb +354 -0
  15. data/webmock-3.26.2/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +239 -0
  16. data/webmock-3.26.2/lib/webmock/http_lib_adapters/excon_adapter.rb +167 -0
  17. data/webmock-3.26.2/lib/webmock/http_lib_adapters/http_lib_adapter.rb +9 -0
  18. data/webmock-3.26.2/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +21 -0
  19. data/webmock-3.26.2/lib/webmock/http_lib_adapters/http_rb/client.rb +17 -0
  20. data/webmock-3.26.2/lib/webmock/http_lib_adapters/http_rb/request.rb +28 -0
  21. data/webmock-3.26.2/lib/webmock/http_lib_adapters/http_rb/response.rb +95 -0
  22. data/webmock-3.26.2/lib/webmock/http_lib_adapters/http_rb/streamer.rb +44 -0
  23. data/webmock-3.26.2/lib/webmock/http_lib_adapters/http_rb/webmock.rb +77 -0
  24. data/webmock-3.26.2/lib/webmock/http_lib_adapters/http_rb_adapter.rb +39 -0
  25. data/webmock-3.26.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb +260 -0
  26. data/webmock-3.26.2/lib/webmock/http_lib_adapters/manticore_adapter.rb +147 -0
  27. data/webmock-3.26.2/lib/webmock/http_lib_adapters/net_http.rb +310 -0
  28. data/webmock-3.26.2/lib/webmock/http_lib_adapters/net_http_response.rb +36 -0
  29. data/webmock-3.26.2/lib/webmock/http_lib_adapters/patron_adapter.rb +132 -0
  30. data/webmock-3.26.2/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +190 -0
  31. data/webmock-3.26.2/lib/webmock/matchers/any_arg_matcher.rb +15 -0
  32. data/webmock-3.26.2/lib/webmock/matchers/hash_argument_matcher.rb +23 -0
  33. data/webmock-3.26.2/lib/webmock/matchers/hash_excluding_matcher.rb +17 -0
  34. data/webmock-3.26.2/lib/webmock/matchers/hash_including_matcher.rb +19 -0
  35. data/webmock-3.26.2/lib/webmock/minitest.rb +43 -0
  36. data/webmock-3.26.2/lib/webmock/rack_response.rb +73 -0
  37. data/webmock-3.26.2/lib/webmock/request_body_diff.rb +66 -0
  38. data/webmock-3.26.2/lib/webmock/request_execution_verifier.rb +79 -0
  39. data/webmock-3.26.2/lib/webmock/request_pattern.rb +428 -0
  40. data/webmock-3.26.2/lib/webmock/request_registry.rb +37 -0
  41. data/webmock-3.26.2/lib/webmock/request_signature.rb +56 -0
  42. data/webmock-3.26.2/lib/webmock/request_signature_snippet.rb +63 -0
  43. data/webmock-3.26.2/lib/webmock/request_stub.rb +134 -0
  44. data/webmock-3.26.2/lib/webmock/response.rb +161 -0
  45. data/webmock-3.26.2/lib/webmock/responses_sequence.rb +42 -0
  46. data/webmock-3.26.2/lib/webmock/rspec/matchers/request_pattern_matcher.rb +80 -0
  47. data/webmock-3.26.2/lib/webmock/rspec/matchers/webmock_matcher.rb +69 -0
  48. data/webmock-3.26.2/lib/webmock/rspec/matchers.rb +29 -0
  49. data/webmock-3.26.2/lib/webmock/rspec.rb +44 -0
  50. data/webmock-3.26.2/lib/webmock/stub_registry.rb +84 -0
  51. data/webmock-3.26.2/lib/webmock/stub_request_snippet.rb +40 -0
  52. data/webmock-3.26.2/lib/webmock/test_unit.rb +22 -0
  53. data/webmock-3.26.2/lib/webmock/util/hash_counter.rb +45 -0
  54. data/webmock-3.26.2/lib/webmock/util/hash_keys_stringifier.rb +27 -0
  55. data/webmock-3.26.2/lib/webmock/util/hash_validator.rb +19 -0
  56. data/webmock-3.26.2/lib/webmock/util/headers.rb +77 -0
  57. data/webmock-3.26.2/lib/webmock/util/parsers/json.rb +72 -0
  58. data/webmock-3.26.2/lib/webmock/util/parsers/parse_error.rb +7 -0
  59. data/webmock-3.26.2/lib/webmock/util/parsers/xml.rb +16 -0
  60. data/webmock-3.26.2/lib/webmock/util/query_mapper.rb +283 -0
  61. data/webmock-3.26.2/lib/webmock/util/uri.rb +113 -0
  62. data/webmock-3.26.2/lib/webmock/util/values_stringifier.rb +22 -0
  63. data/webmock-3.26.2/lib/webmock/util/version_checker.rb +113 -0
  64. data/webmock-3.26.2/lib/webmock/version.rb +5 -0
  65. data/webmock-3.26.2/lib/webmock/webmock.rb +175 -0
  66. data/webmock-3.26.2/lib/webmock.rb +61 -0
  67. metadata +106 -0
@@ -0,0 +1,260 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'httpclient'
5
+ require 'jsonclient' # defined in 'httpclient' gem as well
6
+ rescue LoadError
7
+ # httpclient not found
8
+ # or jsonclient not defined (in old versions of httclient gem)
9
+ end
10
+
11
+ if defined?(::HTTPClient)
12
+
13
+ module WebMock
14
+ module HttpLibAdapters
15
+ class HTTPClientAdapter < HttpLibAdapter
16
+ adapter_for :httpclient
17
+
18
+ unless const_defined?(:OriginalHttpClient)
19
+ OriginalHttpClient = ::HTTPClient
20
+ end
21
+
22
+ unless const_defined?(:OriginalJsonClient)
23
+ OriginalJsonClient = ::JSONClient if defined?(::JSONClient)
24
+ end
25
+
26
+ def self.enable!
27
+ Object.send(:remove_const, :HTTPClient)
28
+ Object.send(:const_set, :HTTPClient, WebMockHTTPClient)
29
+ if defined? ::JSONClient
30
+ Object.send(:remove_const, :JSONClient)
31
+ Object.send(:const_set, :JSONClient, WebMockJSONClient)
32
+ end
33
+ end
34
+
35
+ def self.disable!
36
+ Object.send(:remove_const, :HTTPClient)
37
+ Object.send(:const_set, :HTTPClient, OriginalHttpClient)
38
+ if defined? ::JSONClient
39
+ Object.send(:remove_const, :JSONClient)
40
+ Object.send(:const_set, :JSONClient, OriginalJsonClient)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ module WebMockHTTPClients
48
+ WEBMOCK_HTTPCLIENT_RESPONSES = :webmock_httpclient_responses
49
+ WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES = :webmock_httpclient_request_signatures
50
+
51
+ REQUEST_RESPONSE_LOCK = Mutex.new
52
+
53
+ def do_get_block(req, proxy, conn, &block)
54
+ do_get(req, proxy, conn, false, &block)
55
+ end
56
+
57
+ def do_get_stream(req, proxy, conn, &block)
58
+ do_get(req, proxy, conn, true, &block)
59
+ end
60
+
61
+ def do_get(req, proxy, conn, stream = false, &block)
62
+ clear_thread_variables unless conn.async_thread
63
+
64
+ request_signature = build_request_signature(req, :reuse_existing)
65
+
66
+ WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
67
+
68
+ if webmock_responses[request_signature]
69
+ webmock_response = webmock_responses.delete(request_signature)
70
+ response = build_httpclient_response(webmock_response, stream, req.header, &block)
71
+ @request_filter.each do |filter|
72
+ filter.filter_response(req, response)
73
+ end
74
+ res = conn.push(response)
75
+ WebMock::CallbackRegistry.invoke_callbacks(
76
+ {lib: :httpclient}, request_signature, webmock_response)
77
+ res
78
+ elsif WebMock.net_connect_allowed?(request_signature.uri)
79
+ # in case there is a nil entry in the hash...
80
+ webmock_responses.delete(request_signature)
81
+
82
+ res = if stream
83
+ do_get_stream_without_webmock(req, proxy, conn, &block)
84
+ elsif block
85
+ body = ''
86
+ do_get_block_without_webmock(req, proxy, conn) do |http_res, chunk|
87
+ if chunk && chunk.bytesize > 0
88
+ body += chunk
89
+ block.call(http_res, chunk)
90
+ end
91
+ end
92
+ else
93
+ do_get_block_without_webmock(req, proxy, conn)
94
+ end
95
+ res = conn.pop
96
+ conn.push(res)
97
+ if WebMock::CallbackRegistry.any_callbacks?
98
+ webmock_response = build_webmock_response(res, body)
99
+ WebMock::CallbackRegistry.invoke_callbacks(
100
+ {lib: :httpclient, real_request: true}, request_signature,
101
+ webmock_response)
102
+ end
103
+ res
104
+ else
105
+ raise WebMock::NetConnectNotAllowedError.new(request_signature)
106
+ end
107
+ end
108
+
109
+ def do_request_async(method, uri, query, body, extheader)
110
+ clear_thread_variables
111
+ req = create_request(method, uri, query, body, extheader)
112
+ request_signature = build_request_signature(req)
113
+ webmock_request_signatures << request_signature
114
+
115
+ if webmock_responses[request_signature] || WebMock.net_connect_allowed?(request_signature.uri)
116
+ conn = super
117
+ conn.async_thread[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] = Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES]
118
+ conn.async_thread[WEBMOCK_HTTPCLIENT_RESPONSES] = Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES]
119
+ conn
120
+ else
121
+ raise WebMock::NetConnectNotAllowedError.new(request_signature)
122
+ end
123
+ end
124
+
125
+ def build_httpclient_response(webmock_response, stream = false, req_header = nil, &block)
126
+ body = stream ? StringIO.new(webmock_response.body) : webmock_response.body
127
+ response = HTTP::Message.new_response(body, req_header)
128
+ response.header.init_response(webmock_response.status[0])
129
+ response.reason=webmock_response.status[1]
130
+ webmock_response.headers.to_a.each { |name, value| response.header.set(name, value) }
131
+
132
+ raise HTTPClient::TimeoutError if webmock_response.should_timeout
133
+ webmock_response.raise_error_if_any
134
+
135
+ block.call(response, body) if block && body && body.bytesize > 0
136
+
137
+ response
138
+ end
139
+
140
+ def build_webmock_response(httpclient_response, body = nil)
141
+ webmock_response = WebMock::Response.new
142
+ webmock_response.status = [httpclient_response.status, httpclient_response.reason]
143
+
144
+ webmock_response.headers = {}.tap do |hash|
145
+ httpclient_response.header.all.each do |(key, value)|
146
+ if hash.has_key?(key)
147
+ hash[key] = Array(hash[key]) + [value]
148
+ else
149
+ hash[key] = value
150
+ end
151
+ end
152
+ end
153
+
154
+ if body
155
+ webmock_response.body = body
156
+ elsif httpclient_response.content.respond_to?(:read)
157
+ webmock_response.body = httpclient_response.content.read
158
+ body = HTTP::Message::Body.new
159
+ body.init_response(StringIO.new(webmock_response.body))
160
+ httpclient_response.body = body
161
+ else
162
+ webmock_response.body = httpclient_response.content
163
+ end
164
+ webmock_response
165
+ end
166
+
167
+ def build_request_signature(req, reuse_existing = false)
168
+ @request_filter.each do |filter|
169
+ filter.filter_request(req)
170
+ end
171
+
172
+ uri = WebMock::Util::URI.heuristic_parse(req.header.request_uri.to_s)
173
+ uri.query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query, notation: WebMock::Config.instance.query_values_notation) if req.header.request_query
174
+ uri.port = req.header.request_uri.port
175
+
176
+ headers = req.header.all.inject({}) do |hdrs, header|
177
+ hdrs[header[0]] ||= []
178
+ hdrs[header[0]] << header[1]
179
+ hdrs
180
+ end
181
+ headers = headers_from_session(uri).merge(headers)
182
+
183
+ signature = WebMock::RequestSignature.new(
184
+ req.header.request_method.downcase.to_sym,
185
+ uri.to_s,
186
+ body: req.http_body.dump,
187
+ headers: headers
188
+ )
189
+
190
+ # reuse a previous identical signature object if we stored one for later use
191
+ if reuse_existing && previous_signature = previous_signature_for(signature)
192
+ return previous_signature
193
+ end
194
+
195
+ signature
196
+ end
197
+
198
+ def webmock_responses
199
+ Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES] ||= Hash.new do |hash, request_signature|
200
+ hash[request_signature] = WebMock::StubRegistry.instance.response_for_request(request_signature)
201
+ end
202
+ end
203
+
204
+ def webmock_request_signatures
205
+ Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] ||= []
206
+ end
207
+
208
+ def previous_signature_for(signature)
209
+ return nil unless index = webmock_request_signatures.index(signature)
210
+ webmock_request_signatures.delete_at(index)
211
+ end
212
+
213
+ private
214
+
215
+ # some of the headers sent by HTTPClient are derived from
216
+ # the client session
217
+ def headers_from_session(uri)
218
+ session_headers = HTTP::Message::Headers.new
219
+ @session_manager.send(:open, uri).send(:set_header, MessageMock.new(session_headers))
220
+ session_headers.all.inject({}) do |hdrs, header|
221
+ hdrs[header[0]] = header[1]
222
+ hdrs
223
+ end
224
+ end
225
+
226
+ def clear_thread_variables
227
+ Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] = nil
228
+ Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES] = nil
229
+ end
230
+ end
231
+
232
+ class WebMockHTTPClient < HTTPClient
233
+ alias_method :do_get_block_without_webmock, :do_get_block
234
+ alias_method :do_get_stream_without_webmock, :do_get_stream
235
+
236
+ include WebMockHTTPClients
237
+ end
238
+
239
+ if defined? ::JSONClient
240
+ class WebMockJSONClient < JSONClient
241
+ alias_method :do_get_block_without_webmock, :do_get_block
242
+ alias_method :do_get_stream_without_webmock, :do_get_stream
243
+
244
+ include WebMockHTTPClients
245
+ end
246
+ end
247
+
248
+
249
+ # Mocks a HTTPClient HTTP::Message
250
+ class MessageMock
251
+ attr_reader :header
252
+
253
+ def initialize(headers)
254
+ @header = headers
255
+ end
256
+
257
+ def http_version=(value);end
258
+ end
259
+
260
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'manticore'
5
+ rescue LoadError
6
+ # manticore not found
7
+ end
8
+
9
+ if defined?(Manticore)
10
+ module WebMock
11
+ module HttpLibAdapters
12
+ class ManticoreAdapter < HttpLibAdapter
13
+ adapter_for :manticore
14
+
15
+ OriginalManticoreClient = Manticore::Client
16
+
17
+ def self.enable!
18
+ Manticore.send(:remove_const, :Client)
19
+ Manticore.send(:const_set, :Client, WebMockManticoreClient)
20
+ Manticore.instance_variable_set(:@manticore_facade, WebMockManticoreClient.new)
21
+ end
22
+
23
+ def self.disable!
24
+ Manticore.send(:remove_const, :Client)
25
+ Manticore.send(:const_set, :Client, OriginalManticoreClient)
26
+ Manticore.instance_variable_set(:@manticore_facade, OriginalManticoreClient.new)
27
+ end
28
+
29
+ class StubbedTimeoutResponse < Manticore::StubbedResponse
30
+ def call
31
+ @handlers[:failure].call(Manticore::ConnectTimeout.new("Too slow (mocked timeout)"))
32
+ end
33
+ end
34
+
35
+ class WebMockManticoreClient < Manticore::Client
36
+ def request(klass, url, options={}, &block)
37
+ super(klass, WebMock::Util::URI.normalize_uri(url).to_s, format_options(options))
38
+ end
39
+
40
+ private
41
+
42
+ def format_options(options)
43
+ return options unless headers = options[:headers]
44
+
45
+ options.merge(headers: join_array_values(headers))
46
+ end
47
+
48
+ def join_array_values(headers)
49
+ headers.reduce({}) do |h, (k,v)|
50
+ v = v.join(', ') if v.is_a?(Array)
51
+ h.merge(k => v)
52
+ end
53
+ end
54
+
55
+ def response_object_for(request, context, &block)
56
+ request_signature = generate_webmock_request_signature(request, context)
57
+ WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
58
+
59
+ if webmock_response = registered_response_for(request_signature)
60
+ webmock_response.raise_error_if_any
61
+ manticore_response = generate_manticore_response(webmock_response)
62
+ manticore_response.on_success do
63
+ WebMock::CallbackRegistry.invoke_callbacks({lib: :manticore, real_request: false}, request_signature, webmock_response)
64
+ end
65
+
66
+ elsif real_request_allowed?(request_signature.uri)
67
+ manticore_response = Manticore::Response.new(self, request, context, &block)
68
+ manticore_response.on_complete do |completed_response|
69
+ webmock_response = generate_webmock_response(completed_response)
70
+ WebMock::CallbackRegistry.invoke_callbacks({lib: :manticore, real_request: true}, request_signature, webmock_response)
71
+ end
72
+
73
+ else
74
+ raise WebMock::NetConnectNotAllowedError.new(request_signature)
75
+ end
76
+
77
+ manticore_response
78
+ end
79
+
80
+ def registered_response_for(request_signature)
81
+ WebMock::StubRegistry.instance.response_for_request(request_signature)
82
+ end
83
+
84
+ def real_request_allowed?(uri)
85
+ WebMock.net_connect_allowed?(uri)
86
+ end
87
+
88
+ def generate_webmock_request_signature(request, context)
89
+ method = request.method.downcase
90
+ uri = request.uri.to_s
91
+ body = read_body(request)
92
+ headers = split_array_values(request.headers)
93
+
94
+ if context.get_credentials_provider && credentials = context.get_credentials_provider.get_credentials(AuthScope::ANY)
95
+ headers['Authorization'] = WebMock::Util::Headers.basic_auth_header(credentials.get_user_name,credentials.get_password)
96
+ end
97
+
98
+ WebMock::RequestSignature.new(method, uri, {body: body, headers: headers})
99
+ end
100
+
101
+ def read_body(request)
102
+ if request.respond_to?(:entity) && !request.entity.nil?
103
+ Manticore::EntityConverter.new.read_entity(request.entity)
104
+ end
105
+ end
106
+
107
+ def split_array_values(headers = [])
108
+ headers.each_with_object({}) do |(k, v), h|
109
+ h[k] = case v
110
+ when /,/ then v.split(',').map(&:strip)
111
+ else v
112
+ end
113
+ end
114
+ end
115
+
116
+ def generate_manticore_response(webmock_response)
117
+ if webmock_response.should_timeout
118
+ StubbedTimeoutResponse.new
119
+ else
120
+ Manticore::StubbedResponse.stub(
121
+ code: webmock_response.status[0],
122
+ body: webmock_response.body,
123
+ headers: webmock_response.headers,
124
+ cookies: {}
125
+ )
126
+ end
127
+ end
128
+
129
+ def generate_webmock_response(manticore_response)
130
+ webmock_response = WebMock::Response.new
131
+ webmock_response.status = [manticore_response.code, manticore_response.message]
132
+ webmock_response.headers = manticore_response.headers
133
+
134
+ # The attempt to read the body could fail if manticore is used in a streaming mode
135
+ webmock_response.body = begin
136
+ manticore_response.body
137
+ rescue ::Manticore::StreamClosedException
138
+ nil
139
+ end
140
+
141
+ webmock_response
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end