webmock 3.33.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: 41c4be4a917d40c0448cdf920de152862f1ce887e379e49121020d506757c16d
4
- data.tar.gz: 71ed3dc5e3df9671c7ce188fa579b86379ab365f95b666c7eeee7bf6851dc836
3
+ metadata.gz: acb33845b6ff4fbb890271cbed06608a6095c823ce57a9be5ca2121090ff117f
4
+ data.tar.gz: f3bf9d36ba38af4e134016cf4d941589d271e75cb06d2dc314781d8f1ffe341e
5
5
  SHA512:
6
- metadata.gz: 79843a07ef28cee5968bf6e3eb4b5eac10f688c3aba9caeb44cd30c74ec28ee8e0129eef69eab8d1a87e3991512b63c96589bbf52aaa912f12217ed86b20b4d2
7
- data.tar.gz: '009710347fc3bf01b14ab050ba19c316af32a8a2803b0d40b3388be7ed824a5e6de371a860810df8ebff0de2dd588ad6071c876cf79a029835ccab32c8d48fe4'
6
+ metadata.gz: e99abdb908ce47c4bba164cfa574a2b94f54e042f7bd55f1ba4f64bf88b43f3c721c7a7e39f8870c7f0d3ecdc139ee79d7c14de0c201ca0937de8355ded288c7
7
+ data.tar.gz: 9a75e24decc112843f7e89ee47e68fbf312bec7d23590d0b17c1661295d5b3c2b690cc4800757e1e2c2d62b58990ddd217f492c6e095209c282ed6107d732367
data/CHANGELOG.md CHANGED
@@ -1,5 +1,83 @@
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
+
45
+ # 3.34.0
46
+
47
+ * Allow `with(query: :any)` to match any query string (#1099)
48
+
49
+ `stub_request(:get, "www.example.com").with(query: :any)` matches requests with any query string, or none. RSpec's `anything` works the same way. Previously, this raised `NoMethodError`, and you had to write `with(query: hash_including({}))`.
50
+
51
+ Use a URL without query parameters when passing `query: :any`. Otherwise, WebMock raises `ArgumentError`.
52
+
53
+ Thanks to [nightpool](https://github.com/nightpool)
54
+
55
+ * Raise a clear `ArgumentError` for an unsupported `query` option
56
+
57
+ Previously, a value such as `with(query: :all)` raised `NoMethodError` (undefined method `split`). The new error lists the supported values.
58
+
59
+ * Show `anything` for keys given without a value to `hash_including` or `hash_excluding` outside RSpec
60
+
61
+ Previously, with Minitest or Test::Unit, messages showed `#<WebMock::Matchers::AnyArgMatcher:0x...>`, for example for `with(query: hash_including(:a))`. RSpec's own matchers already showed `anything`.
62
+
63
+ * Provide `HTTP_HOST` to `to_rack` apps when the client has not supplied a Host header (#1084)
64
+
65
+ Previously, requests from some HTTP clients reached the Rack app without `HTTP_HOST`. This caused host checks such as Sinatra's `HostAuthorization` to see an empty host.
66
+
67
+ WebMock now fills in the host from the request URL, including the port when it is not the default. An explicit Host header is preserved.
68
+
69
+ Thanks to [Benoit Tigeot](https://github.com/benoittgt)
70
+
71
+ * Fix Rack compatibility for `to_rack` apps
72
+
73
+ The request environment now passes `Rack::Lint`.
74
+
75
+ `SERVER_PORT` and `CONTENT_LENGTH` are now strings. If your app or tests read them directly, use string comparisons, such as `env['SERVER_PORT'] == "80"`. The environment also includes `SERVER_PROTOCOL`.
76
+
77
+ `CONTENT_TYPE` is omitted when the request has no content type. The `Content-Length` header is passed only as `CONTENT_LENGTH`, without an extra `HTTP_CONTENT_LENGTH` entry. `Transfer-Encoding` is removed because the body has already been decoded.
78
+
79
+ Repeated header values are joined into one string. Header values and the body read from `rack.input` now use binary (`ASCII-8BIT`) strings.
80
+
3
81
  # 3.33.0
4
82
 
5
83
  * Return `effective_url` as a String for stubbed Typhoeus responses (#869)
data/README.md CHANGED
@@ -299,6 +299,15 @@ Net::HTTP.get('www.example.com',
299
299
  '/thing/5.json?x=1&y=2&z=3&anyParam=4') # ===> Success
300
300
  ```
301
301
 
302
+ ### Matching any query params
303
+
304
+ ```ruby
305
+ stub_request(:get, "www.example.com").with(query: :any)
306
+
307
+ RestClient.get("http://www.example.com/?q=example") # ===> Success
308
+ RestClient.get("http://www.example.com/") # ===> Success
309
+ ```
310
+
302
311
  ### Matching query params using hash
303
312
 
304
313
  ```ruby
@@ -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
@@ -10,6 +10,10 @@ module WebMock
10
10
  def ==(other)
11
11
  true
12
12
  end
13
+
14
+ def inspect
15
+ "anything"
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -27,37 +27,56 @@ module WebMock
27
27
 
28
28
  def build_rack_env(request)
29
29
  uri = request.uri
30
+ port = uri.inferred_port
30
31
  headers = (request.headers || {}).dup
31
- body = request.body || ''
32
+ body = (request.body || '').b
33
+ content_type = headers.delete('Content-Type')
34
+ headers.delete('Content-Length')
35
+ # The body is already decoded, as a server passes it on.
36
+ headers.delete('Transfer-Encoding')
32
37
 
33
38
  env = {
34
39
  # CGI variables specified by Rack
35
- 'REQUEST_METHOD' => request.method.to_s.upcase,
36
- 'CONTENT_TYPE' => headers.delete('Content-Type'),
37
- 'CONTENT_LENGTH' => body.bytesize,
38
- 'PATH_INFO' => uri.path,
39
- 'QUERY_STRING' => uri.query || '',
40
- 'SERVER_NAME' => uri.host,
41
- 'SERVER_PORT' => uri.port,
42
- 'SCRIPT_NAME' => ""
40
+ 'REQUEST_METHOD' => request.method.to_s.upcase,
41
+ 'CONTENT_LENGTH' => body.bytesize.to_s,
42
+ 'PATH_INFO' => uri.path,
43
+ 'QUERY_STRING' => uri.query || '',
44
+ 'SERVER_NAME' => uri.host,
45
+ 'SERVER_PORT' => port.to_s,
46
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
47
+ 'SCRIPT_NAME' => ""
43
48
  }
49
+ env['CONTENT_TYPE'] = header_value('Content-Type', content_type) if content_type
44
50
 
45
51
  env['HTTP_AUTHORIZATION'] = 'Basic ' + [uri.userinfo].pack('m').delete("\r\n") if uri.userinfo
46
52
 
47
53
  # Rack-specific variables
48
54
  env['rack.input'] = StringIO.new(body)
49
55
  env['rack.errors'] = $stderr
50
- if !Rack.const_defined?(:RELEASE) || Rack::RELEASE < "3"
51
- env['rack.version'] = Rack::VERSION
56
+ if !Rack.const_defined?(:RELEASE) || Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3")
57
+ env['rack.version'] = Rack::VERSION
58
+ env['rack.multithread'] = false
59
+ env['rack.multiprocess'] = false
52
60
  end
53
61
  env['rack.url_scheme'] = uri.scheme
54
62
  env['rack.run_once'] = true
55
63
 
56
64
  headers.each do |k, v|
57
- env["HTTP_#{k.tr('-','_').upcase}"] = v
65
+ env["HTTP_#{k.tr('-','_').upcase}"] = header_value(k, v)
58
66
  end
59
67
 
68
+ # Many clients add Host only when sending, so fill it in as they would.
69
+ env['HTTP_HOST'] ||= port == uri.default_port ? uri.host : "#{uri.host}:#{port}"
70
+
60
71
  env
61
72
  end
73
+
74
+ private
75
+
76
+ # Like a server, pass header values as bytes and join repeated ones. Rack
77
+ # splits cookies on "; ", so join those that way.
78
+ def header_value(name, value)
79
+ Array(value).map { |v| v.to_s.b }.join(name == 'Cookie' ? '; ' : WebMock::Util::Headers::JOIN)
80
+ end
62
81
  end
63
82
  end
@@ -10,6 +10,10 @@ module WebMock
10
10
  def rSpecHashExcludingMatcher?(matcher)
11
11
  matcher.class.name =~ /R?Spec::Mocks::ArgumentMatchers::HashExcludingMatcher/
12
12
  end
13
+
14
+ def rSpecAnyArgMatcher?(matcher)
15
+ matcher.class.name =~ /R?Spec::Mocks::ArgumentMatchers::AnyArgMatcher/
16
+ end
13
17
  end
14
18
 
15
19
  class RequestPattern
@@ -129,6 +133,10 @@ module WebMock
129
133
  def add_query_params(query_params)
130
134
  @query_params = if query_params.is_a?(Hash)
131
135
  query_params
136
+ elsif query_params.equal?(:any) \
137
+ || query_params.is_a?(WebMock::Matchers::AnyArgMatcher) \
138
+ || rSpecAnyArgMatcher?(query_params)
139
+ WebMock::Matchers::AnyArgMatcher.new(nil)
132
140
  elsif query_params.is_a?(WebMock::Matchers::HashIncludingMatcher) \
133
141
  || query_params.is_a?(WebMock::Matchers::HashExcludingMatcher)
134
142
  query_params
@@ -136,8 +144,10 @@ module WebMock
136
144
  WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(query_params)
137
145
  elsif rSpecHashExcludingMatcher?(query_params)
138
146
  WebMock::Matchers::HashExcludingMatcher.from_rspec_matcher(query_params)
139
- else
147
+ elsif query_params.nil? || query_params.is_a?(String)
140
148
  WebMock::Util::QueryMapper.query_to_values(query_params, notation: Config.instance.query_values_notation)
149
+ else
150
+ raise ArgumentError, "Query should be a String, Hash, :any, nil, or a hash_including or hash_excluding matcher. Got: #{query_params.class}"
141
151
  end
142
152
  end
143
153
 
@@ -153,6 +163,10 @@ module WebMock
153
163
 
154
164
  private
155
165
 
166
+ def raise_any_query_with_uri_query
167
+ raise ArgumentError, "query: :any can't be combined with a query in the URI #{pattern_inspect}, because no request would match. Remove the query from the URI."
168
+ end
169
+
156
170
  def pattern_inspect
157
171
  @pattern.inspect
158
172
  end
@@ -180,12 +194,17 @@ module WebMock
180
194
 
181
195
  class URIAddressablePattern < URIPattern
182
196
  def add_query_params(query_params)
197
+ super(query_params)
198
+ # With query params, the template is matched against the URI without its
199
+ # query, so a literal query in the template can never match.
200
+ if @query_params.is_a?(WebMock::Matchers::AnyArgMatcher) && @pattern.pattern.gsub(/\{[^}]*\}/, '').include?('?')
201
+ raise_any_query_with_uri_query
202
+ end
183
203
  @@add_query_params_warned ||= false
184
204
  if not @@add_query_params_warned
185
205
  @@add_query_params_warned = true
186
206
  warn "WebMock warning: ignoring query params in RFC 6570 template and checking them with WebMock"
187
207
  end
188
- super(query_params)
189
208
  end
190
209
 
191
210
  private
@@ -230,6 +249,9 @@ module WebMock
230
249
  query_hash = (WebMock::Util::QueryMapper.query_to_values(@pattern.query, notation: Config.instance.query_values_notation) || {}).merge(@query_params)
231
250
  @pattern.query = WebMock::Util::QueryMapper.values_to_query(query_hash, notation: WebMock::Config.instance.query_values_notation)
232
251
  @query_params = nil
252
+ elsif @query_params.is_a?(WebMock::Matchers::AnyArgMatcher) && @pattern.query
253
+ # The URI is then compared without its query, so it could never match.
254
+ raise_any_query_with_uri_query
233
255
  end
234
256
  end
235
257
 
@@ -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.33.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.33.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.33.0/CHANGELOG.md
364
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.33.0
365
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.33.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: