webmock 3.34.0 → 3.35.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b66b694440da9739d8d23ccd8a260260739199235f076f4854387f7029c0c976
4
- data.tar.gz: 96abe5bbe31e475bcec618aff66d43ac724b7cf15b08883ae864d62cac826e7c
3
+ metadata.gz: acb33845b6ff4fbb890271cbed06608a6095c823ce57a9be5ca2121090ff117f
4
+ data.tar.gz: f3bf9d36ba38af4e134016cf4d941589d271e75cb06d2dc314781d8f1ffe341e
5
5
  SHA512:
6
- metadata.gz: 99395c27d7c6c270f37309e69da7f3688c43b8f158327d23b62b9829b3fff7a667280542017c71f18e2b822de6639394b2aeaa078aae7b7c67d7fd497363e918
7
- data.tar.gz: 9378a048d2a03b1309eff3b5264ddca35e4f9268399a92e98147fa0e2d60e376d31ca2f1ccaca912f6d7ca859268b1d9ce173530e885c5055b99f3c2632bfd46
6
+ metadata.gz: e99abdb908ce47c4bba164cfa574a2b94f54e042f7bd55f1ba4f64bf88b43f3c721c7a7e39f8870c7f0d3ecdc139ee79d7c14de0c201ca0937de8355ded288c7
7
+ data.tar.gz: 9a75e24decc112843f7e89ee47e68fbf312bec7d23590d0b17c1661295d5b3c2b690cc4800757e1e2c2d62b58990ddd217f492c6e095209c282ed6107d732367
data/CHANGELOG.md CHANGED
@@ -1,5 +1,47 @@
1
1
  # Changelog
2
2
 
3
+ # 3.35.0
4
+
5
+ * Fix `get_file` with Patron 1.1 (#1147)
6
+
7
+ Previously, with Patron 1.1, `get_file` ignored stubs: it made a real request and wrote the real response to the file, even when real connections were disabled.
8
+
9
+ With Patron 1.1, a stubbed `get_file` returns a response with a `nil` body, as Patron does. Read the body from the file.
10
+
11
+ Thanks to [Bob Aman](https://github.com/sporkmonger)
12
+
13
+ * Stub and block streamed requests with Patron 1.1 (#1147)
14
+
15
+ Previously, WebMock did not see requests made with `Session#stream` or the `get_stream`, `post_stream`, `put_stream` and `patch_stream` methods added in Patron 1.1, so they always reached the network.
16
+
17
+ A stubbed streamed request yields a stream that reads the stubbed body, and returns a `Patron::TransferStats`. After-request callbacks run before the block, so they also run when the block exits early. For a real streamed request, callbacks receive the status and headers without the body.
18
+
19
+ Thanks to [Bob Aman](https://github.com/sporkmonger)
20
+
21
+ * Fix Patron digest authentication when real requests are allowed
22
+
23
+ Previously, when a Patron session had a username and password and a request reached the network, WebMock added an `Authorization: Basic ...` header to it. A session with `auth_type: :digest` sent the password in that header instead of waiting for the server's digest challenge.
24
+
25
+ * Fix a deprecation warning with Patron 1.0 and later (#1142)
26
+
27
+ Previously, the first stubbed request printed `Patron::Response.new with positional arguments is deprecated`.
28
+
29
+ Thanks to [Julik Tarkhanov](https://github.com/julik)
30
+
31
+ * Fix `require "webmock"` failing next to other libraries that define `HTTP::VERSION` (#1139)
32
+
33
+ Previously, WebMock loaded its adapter for the `http` gem whenever a top-level `HTTP` with a `VERSION` was defined. Other libraries define one too, such as the gem published on RubyGems as `http.rb` (a different gem from `http`, which is also called http.rb). With such a library installed, `require "webmock"` raised `NameError: undefined method 'perform' for class 'HTTP::Client'`.
34
+
35
+ Thanks to [thoran](https://github.com/thoran)
36
+
37
+ * Fix matching of indexed params with more than ten entries (#1141)
38
+
39
+ Previously, WebMock ordered indexed params such as `a[0]` to `a[10]` as text, so `a[10]` came right after `a[1]`. A stub with the values in index order, such as `with(body: {"a" => [...]})`, did not match a form body with more than ten indexed values, or a query with more than ten indexed hashes.
40
+
41
+ If a stub lists such values in the old order, for example one copied from WebMock's suggested stub, put them in index order.
42
+
43
+ Thanks to [Andrii Furmanets](https://github.com/afurm)
44
+
3
45
  # 3.34.0
4
46
 
5
47
  * Allow `with(query: :any)` to match any query string (#1099)
@@ -6,7 +6,10 @@ rescue LoadError
6
6
  # HTTP gem not found
7
7
  end
8
8
 
9
- if defined?(HTTP) && defined?(HTTP::VERSION)
9
+ # Other libraries define a top-level HTTP with a VERSION too, and
10
+ # `require "http"` can load one of them, so look for the HTTP::Client
11
+ # this adapter patches as well.
12
+ if defined?(HTTP::VERSION) && defined?(HTTP::Client)
10
13
  WebMock::VersionChecker.new("HTTP Gem", HTTP::VERSION, "0.6.0").check_version!
11
14
 
12
15
  module WebMock
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'stringio'
4
+
3
5
  begin
4
6
  require 'patron'
5
7
  rescue LoadError
@@ -14,35 +16,142 @@ if defined?(::Patron::Session)
14
16
 
15
17
  OriginalPatronSession = ::Patron::Session unless const_defined?(:OriginalPatronSession)
16
18
 
19
+ # Patron 1.0 moved Response.new to keyword arguments, see https://github.com/toland/patron/issues/209
20
+ PATRON_RESPONSE_TAKES_KEYWORDS =
21
+ ::Patron::Response.instance_method(:initialize).parameters.include?([:keyreq, :url])
22
+
17
23
  class WebMockPatronSession < ::Patron::Session
18
24
  def handle_request(req)
19
- request_signature =
20
- WebMock::HttpLibAdapters::PatronAdapter.build_request_signature(req)
21
-
22
- WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
23
-
24
- if webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
25
- WebMock::HttpLibAdapters::PatronAdapter.
26
- handle_file_name(req, webmock_response)
27
- res = WebMock::HttpLibAdapters::PatronAdapter.
28
- build_patron_response(webmock_response, default_response_charset)
29
- WebMock::CallbackRegistry.invoke_callbacks(
30
- {lib: :patron}, request_signature, webmock_response)
25
+ request_signature, webmock_response = PatronAdapter.register_request(req)
26
+
27
+ if webmock_response
28
+ PatronAdapter.handle_file_name(req, webmock_response)
29
+ res = PatronAdapter.build_patron_response(webmock_response, default_response_charset)
30
+ PatronAdapter.invoke_callbacks(request_signature, webmock_response)
31
31
  res
32
- elsif WebMock.net_connect_allowed?(request_signature.uri)
32
+ else
33
33
  res = super
34
- if WebMock::CallbackRegistry.any_callbacks?
35
- webmock_response = WebMock::HttpLibAdapters::PatronAdapter.
36
- build_webmock_response(res)
37
- WebMock::CallbackRegistry.invoke_callbacks(
38
- {lib: :patron, real_request: true}, request_signature,
39
- webmock_response)
40
- end
34
+ PatronAdapter.invoke_real_request_callbacks(request_signature, res)
41
35
  res
42
- else
43
- raise WebMock::NetConnectNotAllowedError.new(request_signature)
44
36
  end
45
37
  end
38
+
39
+ # Patron 1.1 sends #get_file and streamed requests past
40
+ # #handle_request
41
+ if ::Patron::Session.public_method_defined?(:stream)
42
+ def stream(action, url, headers = {}, **options, &block)
43
+ return super unless block
44
+
45
+ req = build_request(action, url, headers, **options)
46
+ request_signature, webmock_response = PatronAdapter.register_request(req)
47
+
48
+ if webmock_response
49
+ PatronAdapter.stream_stubbed_response(webmock_response, request_signature,
50
+ default_response_charset, &block)
51
+ else
52
+ # Callbacks run once the head has arrived, so they also run
53
+ # when the block leaves before the end of the body
54
+ super(action, url, headers, **options) do |stream|
55
+ PatronAdapter.invoke_real_request_callbacks(request_signature, stream)
56
+ yield stream
57
+ end
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ # Called by #get_file, and by #request for a GET with a file
64
+ def download(req, path)
65
+ request_signature, webmock_response = PatronAdapter.register_request(req)
66
+
67
+ if webmock_response
68
+ # Like Patron, with the body in the file only
69
+ res = PatronAdapter.build_patron_response(webmock_response, default_response_charset, with_body: false)
70
+ File.open(path, "wb") { |f| f.write(webmock_response.body) }
71
+ PatronAdapter.invoke_callbacks(request_signature, webmock_response)
72
+ res
73
+ else
74
+ res = super
75
+ PatronAdapter.invoke_real_request_callbacks(request_signature, res)
76
+ res
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ # Stands in for the Patron::ResponseStream that Patron::Session#stream
83
+ # yields, reading a stubbed body
84
+ class StubbedResponseStream
85
+ CHUNK_SIZE = 64 * 1024
86
+
87
+ HEAD_READERS = [:url, :status, :status_line, :redirect_count, :headers,
88
+ :charset, :ok?, :error?, :content_length, :content_type].freeze
89
+
90
+ HEAD_READERS.each do |name|
91
+ define_method(name) { @head.public_send(name) }
92
+ end
93
+
94
+ def initialize(head, body)
95
+ @head = head
96
+ @body = body
97
+ @closed = false
98
+ end
99
+
100
+ def read(length = nil, outbuf = nil)
101
+ ensure_open!
102
+ @body.read(length, outbuf)
103
+ end
104
+
105
+ def readpartial(maxlen, outbuf = nil)
106
+ ensure_open!
107
+ @body.readpartial(maxlen, outbuf)
108
+ end
109
+
110
+ def each
111
+ return enum_for(:each) unless block_given?
112
+ ensure_open!
113
+ while (chunk = @body.read(CHUNK_SIZE))
114
+ yield chunk
115
+ end
116
+ self
117
+ end
118
+
119
+ def each_line(&block)
120
+ return enum_for(:each_line) unless block
121
+ ensure_open!
122
+ @body.each_line(&block)
123
+ self
124
+ end
125
+
126
+ def drain!
127
+ ensure_open!
128
+ @body.read.bytesize
129
+ end
130
+
131
+ def eof?
132
+ ensure_open!
133
+ @body.eof?
134
+ end
135
+ alias eof eof?
136
+
137
+ def close
138
+ @closed = true
139
+ nil
140
+ end
141
+
142
+ def closed?
143
+ @closed
144
+ end
145
+
146
+ def inspect
147
+ "#<#{self.class.name} @status_line='#{status_line}'#{' (closed)' if @closed}>"
148
+ end
149
+
150
+ private
151
+
152
+ def ensure_open!
153
+ raise IOError, "closed stream" if @closed
154
+ end
46
155
  end
47
156
 
48
157
  def self.enable!
@@ -84,7 +193,9 @@ if defined?(::Patron::Session)
84
193
  end
85
194
  end
86
195
 
87
- headers = req.headers
196
+ # A copy, so the Authorization header added below for matching is
197
+ # not sent with a real request
198
+ headers = req.headers.dup
88
199
 
89
200
  if req.credentials
90
201
  headers['Authorization'] = WebMock::Util::Headers.basic_auth_header(req.credentials)
@@ -99,30 +210,102 @@ if defined?(::Patron::Session)
99
210
  request_signature
100
211
  end
101
212
 
102
- def self.build_patron_response(webmock_response, default_response_charset)
213
+ def self.build_patron_response(webmock_response, default_response_charset, with_body: true)
103
214
  raise ::Patron::TimeoutError if webmock_response.should_timeout
104
215
  webmock_response.raise_error_if_any
105
216
 
217
+ header_data = build_header_data(webmock_response)
218
+
219
+ if PATRON_RESPONSE_TAKES_KEYWORDS
220
+ ::Patron::Response.new(
221
+ url: "".dup,
222
+ status: webmock_response.status[0],
223
+ redirect_count: 0,
224
+ header_data: header_data,
225
+ body: with_body ? webmock_response.body.dup : nil,
226
+ default_charset: default_response_charset
227
+ )
228
+ else
229
+ ::Patron::Response.new(
230
+ "".dup,
231
+ webmock_response.status[0],
232
+ 0,
233
+ header_data,
234
+ webmock_response.body.dup,
235
+ default_response_charset
236
+ )
237
+ end
238
+ end
239
+
240
+ # Records the request and returns its signature with the stubbed
241
+ # response, which is nil when a real request is allowed
242
+ def self.register_request(req)
243
+ request_signature = build_request_signature(req)
244
+ WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
245
+
246
+ webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
247
+ if webmock_response.nil? && !WebMock.net_connect_allowed?(request_signature.uri)
248
+ raise WebMock::NetConnectNotAllowedError.new(request_signature)
249
+ end
250
+
251
+ [request_signature, webmock_response]
252
+ end
253
+
254
+ def self.invoke_callbacks(request_signature, webmock_response)
255
+ WebMock::CallbackRegistry.invoke_callbacks(
256
+ {lib: :patron}, request_signature, webmock_response)
257
+ end
258
+
259
+ def self.invoke_real_request_callbacks(request_signature, patron_response)
260
+ return unless WebMock::CallbackRegistry.any_callbacks?
261
+
262
+ WebMock::CallbackRegistry.invoke_callbacks(
263
+ {lib: :patron, real_request: true}, request_signature,
264
+ build_webmock_response(patron_response))
265
+ end
266
+
267
+ # Callbacks run before the block, so they also run when it leaves
268
+ # before the end of the body
269
+ def self.stream_stubbed_response(webmock_response, request_signature, default_response_charset)
270
+ head = build_patron_response(webmock_response, default_response_charset, with_body: false)
271
+ invoke_callbacks(request_signature, webmock_response)
272
+ # Kept out of the stream, so the stream has only the methods of
273
+ # Patron::ResponseStream
274
+ body = StringIO.new(webmock_response.body.to_s.b)
275
+ stream = StubbedResponseStream.new(head, body)
276
+ begin
277
+ yield stream
278
+ ensure
279
+ stream.close
280
+ end
281
+
282
+ ::Patron::TransferStats.new(
283
+ url: head.url,
284
+ status: head.status,
285
+ redirect_count: head.redirect_count,
286
+ header_data: build_header_data(webmock_response),
287
+ downloaded_bytes: body.pos,
288
+ uploaded_bytes: request_signature.body.to_s.bytesize,
289
+ timings: {},
290
+ complete: body.eof?,
291
+ default_charset: default_response_charset
292
+ )
293
+ end
294
+
295
+ def self.build_header_data(webmock_response)
106
296
  header_fields = (webmock_response.headers || []).map { |(k, vs)| Array(vs).map { |v| "#{k}: #{v}" } }.flatten
107
297
  status_line = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}"
108
- header_data = ([status_line] + header_fields).join("\r\n")
109
-
110
- ::Patron::Response.new(
111
- "".dup,
112
- webmock_response.status[0],
113
- 0,
114
- header_data,
115
- webmock_response.body.dup,
116
- default_response_charset
117
- )
298
+ ([status_line] + header_fields).join("\r\n")
118
299
  end
119
300
 
301
+ # Takes a Patron::Response, or the Patron::ResponseStream of a streamed
302
+ # request, whose body is not read here
120
303
  def self.build_webmock_response(patron_response)
121
304
  webmock_response = WebMock::Response.new
122
305
  reason = patron_response.status_line.
123
306
  scan(%r(\AHTTP/(\d+(?:\.\d+)?)\s+(\d\d\d)\s*([^\r\n]+)?))[0][2]
124
307
  webmock_response.status = [patron_response.status, reason]
125
- webmock_response.body = patron_response.body
308
+ webmock_response.body = patron_response.body if patron_response.respond_to?(:body)
126
309
  webmock_response.headers = patron_response.headers
127
310
  webmock_response
128
311
  end
@@ -224,10 +224,9 @@ module WebMock::Util
224
224
  hash[key] = self.dehash(value)
225
225
  end
226
226
  end
227
- if hash != {} && hash.keys.all? { |key| key =~ /^\d+$/ }
228
- hash.sort.inject([]) do |accu, (_, value)|
229
- accu << value; accu
230
- end
227
+ if hash != {} && hash.keys.all? { |key| key =~ /\A\d+\z/ }
228
+ # By number, then by text, so "01" and "1" keep a fixed order
229
+ hash.sort_by { |key, _| [key.to_i, key] }.map(&:last)
231
230
  else
232
231
  hash
233
232
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebMock
4
- VERSION = '3.34.0' unless defined?(::WebMock::VERSION)
4
+ VERSION = '3.35.0' unless defined?(::WebMock::VERSION)
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.34.0
4
+ version: 3.35.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
@@ -360,9 +360,9 @@ licenses:
360
360
  - MIT
361
361
  metadata:
362
362
  bug_tracker_uri: https://github.com/bblimke/webmock/issues
363
- changelog_uri: https://github.com/bblimke/webmock/blob/v3.34.0/CHANGELOG.md
364
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.34.0
365
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.34.0
363
+ changelog_uri: https://github.com/bblimke/webmock/blob/v3.35.0/CHANGELOG.md
364
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.35.0
365
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.35.0
366
366
  wiki_uri: https://github.com/bblimke/webmock/wiki
367
367
  rdoc_options: []
368
368
  require_paths: