webmock 3.34.0 → 3.36.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: 7ac78ad5e14ee19d53223a19af699b7b22b18ba06fdfcd58837daa4810df48e3
4
+ data.tar.gz: 18bea0ee19e26728813e849777d377cb048ffc7da4f2927901cd80d86e579b90
5
5
  SHA512:
6
- metadata.gz: 99395c27d7c6c270f37309e69da7f3688c43b8f158327d23b62b9829b3fff7a667280542017c71f18e2b822de6639394b2aeaa078aae7b7c67d7fd497363e918
7
- data.tar.gz: 9378a048d2a03b1309eff3b5264ddca35e4f9268399a92e98147fa0e2d60e376d31ca2f1ccaca912f6d7ca859268b1d9ce173530e885c5055b99f3c2632bfd46
6
+ metadata.gz: 91d74df9d7351cad99a7b51b162f3d2beb2e89e76969d570d650cbabcf3d3123f2a2d098c00f73af1e45983f4757f2693d894b5875d3ec941d57918c19304a3b
7
+ data.tar.gz: f8d50c967121c7e843dcf6d0ea52ad467359cb023ae72c928151bd9cb1ff71188a46cd258470357a02156bb51682486e621726679eb48b1ac86ad2b0f60b5a77
data/CHANGELOG.md CHANGED
@@ -1,5 +1,93 @@
1
1
  # Changelog
2
2
 
3
+ # 3.36.0
4
+
5
+ * Fix matching of indexed arrays nested inside arrays of hashes (#1144)
6
+
7
+ Previously, WebMock read indexed arrays nested inside an array of hashes, such as `a[][b][0][c]=1`, as hashes with `"0"`, `"1"` keys. WebMock writes that form itself, so `with(query: {"a" => [{"b" => [{"c" => "1"}]}]})` with a regexp URI did not match `?a[][b][][c]=1`, and a Typhoeus request with that hash as its body did not match a stub with the same hash.
8
+
9
+ If a stub uses the old shape, such as `{"a" => [{"b" => {"0" => {"c" => "1"}}}]}`, for example one copied from WebMock's suggested stub, use arrays instead.
10
+
11
+ Thanks to [Hamza Tarar](https://github.com/ihamzatarar)
12
+
13
+ * Fix matching of arrays of hashes that repeat a nested key
14
+
15
+ Previously, WebMock put a nested key repeated under `[]` into the first hash of the array, so `items[][address][city]=X&items[][address][city]=Y` was read as one item with the city `Y`. A stub listing both items did not match, and a stub for `X` and `Y` also matched a request for `Z` and `Y`. Rails and Faraday encode arrays of nested hashes this way.
16
+
17
+ Such keys now go into a new hash, as in Rack. If a stub lists such an array as one item, for example one copied from WebMock's suggested stub, list every item.
18
+
19
+ Queries that mix a value and nested keys under the same name, such as `a[][b]=1&a[][b][c]=2` or `a[c]=1&a[c][]=2`, no longer raise `IndexError` or join the values into one string.
20
+
21
+ * Apply Net::HTTP `response_body_encoding` to stubbed response bodies (#1138)
22
+
23
+ Previously, stubbed Net::HTTP responses ignored `Net::HTTP#response_body_encoding=` and `Net::HTTPResponse#body_encoding=`. A body stubbed as binary, for example from a raw response file, stayed `ASCII-8BIT` even with `http.response_body_encoding = true` and a `Content-Type` of `text/plain; charset=utf-8`.
24
+
25
+ Stubbed bodies now get the encoding real responses get: with `true`, the charset from the `Content-Type` header or an HTML `<meta>` tag, and otherwise the encoding the setting names. Without the setting, a body keeps the encoding it was stubbed with.
26
+
27
+ Thanks to [Yang Bai](https://github.com/ybai08)
28
+
29
+ * Give each stubbed Net::HTTP response its own copy of the body
30
+
31
+ Previously, a stubbed Net::HTTP response returned the stubbed body string itself. A body stubbed with a frozen string, such as a literal in a file with `# frozen_string_literal: true`, made `response.body.force_encoding("UTF-8")` raise `FrozenError`, which a real response never does. Changing the body in place, for example with `response.body << "!"`, changed it for later requests to the same stub too.
32
+
33
+ * Keep the size of async-http responses to HEAD requests (#1143)
34
+
35
+ async-http gives the size of a response to a HEAD request as `response.body.length`, not as a header. Previously, a response stubbed with `stub_request(:head, url).to_return(headers: {"Content-Length" => "42"})` ignored the `Content-Length`: its body was `nil` over HTTP/1 and had a length of 0 over HTTP/2. A real response lost its size, with a `nil` body, when a `WebMock.after_request` callback was registered, for example while VCR was recording, and the recorded response had no `Content-Length`.
36
+
37
+ A stubbed response now takes its size from the stubbed `Content-Length`, or from the stubbed body without one, as before. A real response keeps its size when a callback is registered, and the recorded response has it as its `Content-Length`.
38
+
39
+ Thanks to [Andrew K. Fader](https://github.com/andrewfader)
40
+
41
+ * Keep the URI query of HTTPClient requests that pass a query argument (#1145)
42
+
43
+ Previously, WebMock dropped the query in the URI when an HTTPClient request also passed the query argument. `HTTPClient.new.get("http://example.com/?a=1", {"b" => "2"})` requests `/?a=1&b=2`, but WebMock saw `/?b=2`, and with `{}` as the query argument it saw no query at all. A stub or `a_request` expectation could match a URL that was never requested.
44
+
45
+ If a stub or expectation for such a request leaves out the query in the URI, add it, for example `stub_request(:get, "http://example.com/?a=1&b=2")`. Record VCR cassettes with such requests again.
46
+
47
+ Thanks to [AmeenKhan12345](https://github.com/AmeenKhan12345)
48
+
49
+ # 3.35.0
50
+
51
+ * Fix `get_file` with Patron 1.1 (#1147)
52
+
53
+ 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.
54
+
55
+ With Patron 1.1, a stubbed `get_file` returns a response with a `nil` body, as Patron does. Read the body from the file.
56
+
57
+ Thanks to [Bob Aman](https://github.com/sporkmonger)
58
+
59
+ * Stub and block streamed requests with Patron 1.1 (#1147)
60
+
61
+ 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.
62
+
63
+ 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.
64
+
65
+ Thanks to [Bob Aman](https://github.com/sporkmonger)
66
+
67
+ * Fix Patron digest authentication when real requests are allowed
68
+
69
+ 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.
70
+
71
+ * Fix a deprecation warning with Patron 1.0 and later (#1142)
72
+
73
+ Previously, the first stubbed request printed `Patron::Response.new with positional arguments is deprecated`.
74
+
75
+ Thanks to [Julik Tarkhanov](https://github.com/julik)
76
+
77
+ * Fix `require "webmock"` failing next to other libraries that define `HTTP::VERSION` (#1139)
78
+
79
+ 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'`.
80
+
81
+ Thanks to [thoran](https://github.com/thoran)
82
+
83
+ * Fix matching of indexed params with more than ten entries (#1141)
84
+
85
+ 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.
86
+
87
+ If a stub lists such values in the old order, for example one copied from WebMock's suggested stub, put them in index order.
88
+
89
+ Thanks to [Andrii Furmanets](https://github.com/afurm)
90
+
3
91
  # 3.34.0
4
92
 
5
93
  * Allow `with(query: :any)` to match any query string (#1099)
@@ -129,19 +129,32 @@ if defined?(Async::HTTP)
129
129
  end
130
130
 
131
131
  def build_webmock_response(response)
132
- body = response.read
133
- response.body = ::Protocol::HTTP::Body::Buffered.wrap(body)
132
+ # async-http takes the Content-Length out of the headers of a response
133
+ # to a HEAD request and gives it a body that only holds that size.
134
+ # Keep that body, and record the size as the Content-Length sent.
135
+ if head_body?(response.body)
136
+ content_length = response.body.length
137
+ else
138
+ body = response.read
139
+ response.body = ::Protocol::HTTP::Body::Buffered.wrap(body)
140
+ end
134
141
 
135
142
  webmock_response = WebMock::Response.new
136
143
  webmock_response.status = [
137
144
  response.status,
138
145
  WebMock::HttpLibAdapters::AsyncHttpClientAdapter.status_descriptions[response.status]
139
146
  ]
140
- webmock_response.headers = build_webmock_response_headers(response)
147
+ headers = build_webmock_response_headers(response)
148
+ headers['content-length'] = [content_length.to_s] if content_length
149
+ webmock_response.headers = headers
141
150
  webmock_response.body = body
142
151
  webmock_response
143
152
  end
144
153
 
154
+ def head_body?(body)
155
+ defined?(::Protocol::HTTP::Body::Head) && body.is_a?(::Protocol::HTTP::Body::Head)
156
+ end
157
+
145
158
  def build_webmock_response_headers(response)
146
159
  response.headers.each.each_with_object({}) do |(k, v), o|
147
160
  o[k] ||= []
@@ -215,7 +228,8 @@ if defined?(Async::HTTP)
215
228
  def call(request)
216
229
  request.read
217
230
  webmock_response = get_webmock_response(request)
218
- build_response(webmock_response)
231
+ body = request.head? ? head_body(webmock_response) : webmock_response.body
232
+ build_response(webmock_response, body)
219
233
  end
220
234
 
221
235
  def add_webmock_response(request, webmock_response)
@@ -235,7 +249,7 @@ if defined?(Async::HTTP)
235
249
  @webmock_responses ||= {}
236
250
  end
237
251
 
238
- def build_response(webmock_response)
252
+ def build_response(webmock_response, body)
239
253
  headers = (webmock_response.headers || {}).each_with_object([]) do |(k, value), o|
240
254
  next if FRAMING_HEADERS.include?(k.downcase)
241
255
 
@@ -247,9 +261,22 @@ if defined?(Async::HTTP)
247
261
  ::Protocol::HTTP::Response[
248
262
  webmock_response.status[0],
249
263
  headers,
250
- webmock_response.body
264
+ body
251
265
  ]
252
266
  end
267
+
268
+ # async-http answers a HEAD request with the size of the body as the
269
+ # Content-Length, and leaves the body out. A stub's own Content-Length
270
+ # is the size of the body it stands for, so it takes precedence.
271
+ def head_body(webmock_response)
272
+ content_length = Array(webmock_response.headers&.[]('Content-Length')).first.to_s
273
+
274
+ if content_length.match?(/\A\d+\z/) && defined?(::Protocol::HTTP::Body::Head)
275
+ ::Protocol::HTTP::Body::Head.new(content_length.to_i)
276
+ else
277
+ webmock_response.body
278
+ end
279
+ end
253
280
  end
254
281
  end
255
282
  end
@@ -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
@@ -173,7 +173,11 @@ if defined?(::HTTPClient)
173
173
  end
174
174
 
175
175
  uri = WebMock::Util::URI.heuristic_parse(req.header.request_uri.to_s)
176
- uri.query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query, notation: WebMock::Config.instance.query_values_notation) if req.header.request_query
176
+ if req.header.request_query
177
+ # HTTPClient sends the query argument after the query in the URI
178
+ query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query, notation: WebMock::Config.instance.query_values_notation)
179
+ uri.query = [uri.query, query].reject { |part| part.to_s.empty? }.join('&')
180
+ end
177
181
  uri.port = req.header.request_uri.port
178
182
 
179
183
  headers = req.header.all.inject({}) do |hdrs, header|
@@ -118,6 +118,8 @@ module WebMock
118
118
  {lib: :net_http, real_request: true}, request_signature, webmock_response)
119
119
  end
120
120
  response.extend Net::WebMockHTTPResponse
121
+ # Net::HTTP has read the body and applied its encoding already
122
+ response.instance_variable_set(:@__body_encoding_applied, true)
121
123
  block.call response if block
122
124
  response
123
125
  end
@@ -181,7 +183,9 @@ module WebMock
181
183
  body = webmock_response.body
182
184
  body = nil if webmock_response.status[0].to_s == '204'
183
185
 
184
- response.instance_variable_set(:@body, body)
186
+ # A copy, so that changing the response body, for example its
187
+ # encoding, does not change the stubbed body
188
+ response.instance_variable_set(:@body, body&.dup)
185
189
  webmock_response.headers.to_a.each do |name, values|
186
190
  values = [values] unless values.is_a?(Array)
187
191
  values.each do |value|
@@ -192,6 +196,7 @@ module WebMock
192
196
  response.instance_variable_set(:@read, true)
193
197
 
194
198
  response.uri = request_uri
199
+ response.body_encoding = response_body_encoding if respond_to?(:response_body_encoding)
195
200
 
196
201
  response.extend Net::WebMockHTTPResponse
197
202
 
@@ -202,6 +207,7 @@ module WebMock
202
207
  webmock_response.raise_error_if_any
203
208
 
204
209
  yield response if block_given?
210
+ response.send(:webmock_apply_body_encoding)
205
211
 
206
212
  response
207
213
  end
@@ -20,17 +20,43 @@ module Net
20
20
  if !(defined?(@__read_body_previously_called).nil?) && @__read_body_previously_called
21
21
  return super
22
22
  end
23
- return @body if dest.nil? && block.nil?
23
+ if dest.nil? && block.nil?
24
+ webmock_apply_body_encoding
25
+ return @body
26
+ end
24
27
  raise ArgumentError.new("both arg and block given for HTTP method") if dest && block
25
28
  return nil if @body.nil?
26
29
 
27
30
  dest ||= ::Net::ReadAdapter.new(block)
28
- dest << @body.dup
31
+ dest << @body
29
32
  @body = dest
33
+ webmock_apply_body_encoding
34
+ @body
30
35
  ensure
31
36
  # allow subsequent calls to #read_body to proceed as normal, without our hack...
32
37
  @__read_body_previously_called = true
33
38
  end
39
+
40
+ private
41
+
42
+ # Net::HTTPResponse#read_body applies the encoding set with #body_encoding=
43
+ # (or Net::HTTP#response_body_encoding=) once, to the body it reads, which
44
+ # happens in the request block or right after it. A stubbed body is set
45
+ # without reading, so apply the encoding the first time the body is read
46
+ # or when the request block returns, and ignore later changes to it.
47
+ def webmock_apply_body_encoding
48
+ return if defined?(@__body_encoding_applied) && @__body_encoding_applied
49
+ @__body_encoding_applied = true
50
+
51
+ encoding = defined?(@body_encoding) && @body_encoding
52
+ # A frozen body was assigned in the request block, which Net::HTTP
53
+ # would replace with the body it reads, so leave it as it is
54
+ return unless encoding && @body.is_a?(String) && !@body.frozen?
55
+
56
+ # Net::HTTP detects the encoding from the bytes it read
57
+ encoding = detect_encoding(@body.b) unless encoding.is_a?(Encoding)
58
+ @body.force_encoding(encoding) if encoding
59
+ end
34
60
  end
35
61
  end
36
62
 
@@ -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
@@ -65,7 +65,7 @@ module WebMock::Util
65
65
  if options[:notation] == :flat_array
66
66
  accumulator << [key, value]
67
67
  else
68
- accumulator[key] = value.kind_of?(Hash) ? dehash(value) : value
68
+ accumulator[key] = dehash_value(value)
69
69
  end
70
70
  accumulator
71
71
  end
@@ -125,44 +125,34 @@ module WebMock::Util
125
125
  def fill_accumulator_for_subscript(accumulator, key, value)
126
126
  current_node = accumulator
127
127
  subkeys = key.split(/(?=\[[^\[\]]+)/)
128
- subkeys[0..-2].each do |subkey|
128
+ subkeys[0..-2].each_with_index do |subkey, index|
129
129
  node = subkey =~ /\[\]\z/ ? [] : {}
130
+ current_node = array_element_for(current_node, subkeys[index..-1]) if current_node.is_a? Array
130
131
  subkey = subkey.gsub(/[\[\]]/, '')
131
- if current_node.is_a? Array
132
- container = current_node.find { |n| n.is_a?(Hash) && n.has_key?(subkey) }
133
- if container
134
- current_node = container[subkey]
135
- else
136
- current_node << {subkey => node}
137
- current_node = node
138
- end
139
- else
140
- current_node[subkey] = node unless current_node[subkey]
141
- current_node = current_node[subkey]
142
- end
132
+ current_node[subkey] = node unless current_node[subkey]
133
+ current_node = current_node[subkey]
143
134
  end
144
135
  last_key = subkeys.last
145
136
  array_value = !!(last_key =~ /\[\]$/)
146
137
  last_key = last_key.gsub(/[\[\]]/, '')
147
138
  if current_node.is_a? Array
148
- last_container = current_node.select { |n| n.is_a?(Hash) }.last
149
- if last_container && !last_container.has_key?(last_key)
150
- if array_value
151
- last_container[last_key] ||= []
152
- last_container[last_key] << value
153
- else
154
- last_container[last_key] = value
155
- end
139
+ # Unlike in Rack, a final key that the last hash already has starts
140
+ # a new hash even with [], so a[][b][]=1&a[][b][]=2 makes two hashes
141
+ last = current_node.last
142
+ current_node << {} unless last.is_a?(Hash) && !last.key?(last_key)
143
+ if array_value
144
+ (current_node.last[last_key] ||= []) << value
156
145
  else
157
- if array_value
158
- current_node << {last_key => [value]}
159
- else
160
- current_node << {last_key => value}
161
- end
146
+ current_node.last[last_key] = value
162
147
  end
163
148
  else
164
149
  if array_value
165
- current_node[last_key] ||= []
150
+ if current_node.is_a?(Hash)
151
+ current_node[last_key] = [current_node[last_key]].compact unless current_node[last_key].is_a?(Array)
152
+ else
153
+ # A value is in the way, as in x=5&x[5][]=1, so do what older versions did
154
+ current_node[last_key] ||= []
155
+ end
166
156
  current_node[last_key] << value unless value.nil?
167
157
  else
168
158
  current_node[last_key] = value
@@ -170,6 +160,30 @@ module WebMock::Util
170
160
  end
171
161
  end
172
162
 
163
+ # Returns the hash in an array of hashes that a nested key goes into.
164
+ # As in Rack, that is the last hash, unless the rest of the key is
165
+ # already set there, so a[][b][c]=1&a[][b][c]=2 makes two hashes.
166
+ def array_element_for(array, subkeys)
167
+ last = array.last
168
+ return last if last.is_a?(Hash) && key_path_free?(last, subkeys)
169
+ array << {}
170
+ array.last
171
+ end
172
+
173
+ # Whether the rest of a key fits into the hash without replacing a
174
+ # value. A part ending in [] fits while it holds an array.
175
+ def key_path_free?(hash, subkeys)
176
+ subkeys.each_with_index do |subkey, index|
177
+ key = subkey.gsub(/[\[\]]/, '')
178
+ return true unless hash.key?(key)
179
+ value = hash[key]
180
+ return value.is_a?(Array) if subkey.end_with?('[]')
181
+ return false if index == subkeys.size - 1 || !value.is_a?(Hash)
182
+ hash = value
183
+ end
184
+ false
185
+ end
186
+
173
187
  ##
174
188
  # Sets the query component for this URI from a Hash object.
175
189
  # This method produces a query string using the :subscript notation.
@@ -219,20 +233,25 @@ module WebMock::Util
219
233
  end
220
234
 
221
235
  def dehash(hash)
222
- hash.each do |(key, value)|
223
- if value.is_a?(::Hash)
224
- hash[key] = self.dehash(value)
225
- end
226
- end
227
- if hash != {} && hash.keys.all? { |key| key =~ /^\d+$/ }
228
- hash.sort.inject([]) do |accu, (_, value)|
229
- accu << value; accu
230
- end
236
+ hash.transform_values! { |value| dehash_value(value) }
237
+ if hash != {} && hash.keys.all? { |key| key =~ /\A\d+\z/ }
238
+ # By number, then by text, so "01" and "1" keep a fixed order
239
+ hash.sort_by { |key, _| [key.to_i, key] }.map(&:last)
231
240
  else
232
241
  hash
233
242
  end
234
243
  end
235
244
 
245
+ # A query such as a[][b][0][c]=1 produces an array whose element holds
246
+ # {"0" => ...}, so the numeric keys have to be collapsed inside arrays too.
247
+ def dehash_value(value)
248
+ case value
249
+ when ::Hash then dehash(value)
250
+ when ::Array then value.map { |element| dehash_value(element) }
251
+ else value
252
+ end
253
+ end
254
+
236
255
  ##
237
256
  # Joins and converts parent and value into a properly encoded and
238
257
  # ordered URL query.
@@ -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.36.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.36.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.36.0/CHANGELOG.md
364
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.36.0
365
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.36.0
366
366
  wiki_uri: https://github.com/bblimke/webmock/wiki
367
367
  rdoc_options: []
368
368
  require_paths: