webmock 3.23.1 → 3.24.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4cfe9b7372b311f25b6b531efc50e11297e19cb325306132b889bf2d67ab3fec
4
- data.tar.gz: 4448d921dc29e08024392f7fe0c8ab5a832920a6e749801e1ca2257a1c050e0b
3
+ metadata.gz: 9283efd8ecb49795fe14fc5903295b0d143fbdd17f5cc1611e89d918f917c33f
4
+ data.tar.gz: ea556e59d242667816568c75f7c42be613e09f688c3ada07c65f0c9de0e3d05d
5
5
  SHA512:
6
- metadata.gz: b6afe5a627d5c3c1498e5b4f35d07f9118977af57f4b7915b04bdeb67f2eb676bbbf5bfc28a895681c6635fe74d736d9a83d50916c2f80b1adb4b68d455de283
7
- data.tar.gz: 8843478e3cbcd78b3e4cc6d6a09c757ff02328549ea37768f23d721adb3721ab0daf49a000fcc8fa600382964538d6bc5aec7a6d1fdde4668a608b3b2b6fda08
6
+ metadata.gz: bcd310894ba47584609ffafcd332cbd824d3921ec938a7ca63e84e983436188dbf7f44a13137e407b3c5798fd9a02ced95e5797ac9ee3e923adf588cf04c4015
7
+ data.tar.gz: 617bcf11b564bf6cc2faf4a973e9882dbb9639e685ed989fe6bc11b65c1ea7e66d12a106484e2e641ff4fda4c8c7336fe83509852b2a858f94fe0a657b5aae6c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ # 3.24.0
4
+
5
+ * Ignore parsing errors when parsing invalid JSON or XML body to match against body pattern #1066
6
+
7
+ Thanks to [Christian Schmidt](https://github.com/c960657)
8
+
9
+ * Added support for mocked HTTP::Connection#finished_request? method #1065
10
+
11
+ Thanks to [Christian Schmidt](https://github.com/c960657)
12
+
13
+ * Detect if Patron is loaded by checking if Patron::Session constant is defined #1068
14
+
15
+ Thanks to [Rodrigo Argumedo](https://github.com/rodrigoargumedo)
16
+
17
+ * Raising an ArgumentError when uri is passed as a Pathname object to stub_request or request expectation declaration.
18
+
19
+ * Dropped support for em-http-request on Ruby 3.4. The current version of em-http-request (1.1.7) is incompatible with Ruby 3.4 due to an unresolved issue (https://github.com/igrigorik/em-http-request/issues/365). Support for em-http-request will be re-enabled once the compatibility issue is resolved.
20
+
21
+ Thanks to [Christian Schmidt](https://github.com/c960657)
22
+
3
23
  # 3.23.1
4
24
 
5
25
  * Added support for async-http version >= 0.65.0 [PR](https://github.com/bblimke/webmock/pull/1056)
data/README.md CHANGED
@@ -1204,6 +1204,8 @@ People who submitted patches and new features or suggested improvements. Many th
1204
1204
  * Gio Lodi
1205
1205
  * Ryan Brooks
1206
1206
  * Jacob Frautschi
1207
+ * Christian Schmidt
1208
+ * Rodrigo Argumedo
1207
1209
 
1208
1210
  For a full list of contributors you can visit the
1209
1211
  [contributors](https://github.com/bblimke/webmock/contributors) page.
@@ -35,9 +35,9 @@ if defined?(Async::HTTP)
35
35
  class WebMockClientWrapper < Client
36
36
  def initialize(
37
37
  endpoint,
38
- protocol = endpoint.protocol,
39
- scheme = endpoint.scheme,
40
- authority = endpoint.authority,
38
+ protocol: endpoint.protocol,
39
+ scheme: endpoint.scheme,
40
+ authority: endpoint.authority,
41
41
  **options
42
42
  )
43
43
  webmock_endpoint = WebMockEndpoint.new(scheme, authority, protocol)
@@ -45,6 +45,7 @@ if defined?(Async::HTTP)
45
45
  @network_client = WebMockClient.new(endpoint, **options)
46
46
  @webmock_client = WebMockClient.new(webmock_endpoint, **options)
47
47
 
48
+ @endpoint = endpoint
48
49
  @scheme = scheme
49
50
  @authority = authority
50
51
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ return if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.4.0')
4
+
3
5
  begin
4
6
  require 'em-http-request'
5
7
  rescue LoadError
@@ -25,6 +25,10 @@ module HTTP
25
25
  @io.close
26
26
  end
27
27
 
28
+ def finished_request?
29
+ @io.eof?
30
+ end
31
+
28
32
  def sequence_id
29
33
  -1
30
34
  end
@@ -6,7 +6,7 @@ rescue LoadError
6
6
  # patron not found
7
7
  end
8
8
 
9
- if defined?(::Patron)
9
+ if defined?(::Patron::Session)
10
10
  module WebMock
11
11
  module HttpLibAdapters
12
12
  class PatronAdapter < ::WebMock::HttpLibAdapter
@@ -84,11 +84,14 @@ module WebMock
84
84
  URIAddressablePattern.new(uri)
85
85
  elsif uri.respond_to?(:call)
86
86
  URICallablePattern.new(uri)
87
- else
87
+ elsif uri.is_a?(::URI::Generic)
88
+ URIStringPattern.new(uri.to_s)
89
+ elsif uri.is_a?(String)
88
90
  URIStringPattern.new(uri)
91
+ else
92
+ raise ArgumentError.new("URI should be a String, Regexp, Addressable::Template or a callable object. Got: #{uri.class}")
89
93
  end
90
94
  end
91
-
92
95
  end
93
96
 
94
97
 
@@ -303,12 +306,14 @@ module WebMock
303
306
  def body_as_hash(body, content_type)
304
307
  case body_format(content_type)
305
308
  when :json then
306
- WebMock::Util::JSON.parse(body)
309
+ WebMock::Util::Parsers::JSON.parse(body)
307
310
  when :xml then
308
- Crack::XML.parse(body)
311
+ WebMock::Util::Parsers::XML.parse(body)
309
312
  else
310
313
  WebMock::Util::QueryMapper.query_to_values(body, notation: Config.instance.query_values_notation)
311
314
  end
315
+ rescue WebMock::Util::Parsers::ParseError
316
+ nil
312
317
  end
313
318
 
314
319
  def body_format(content_type)
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This is a copy of https://github.com/jnunemaker/crack/blob/master/lib/crack/json.rb
4
+ # with date parsing removed
5
+ # Copyright (c) 2004-2008 David Heinemeier Hansson
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9
+
10
+ require_relative "parse_error"
11
+
12
+ module WebMock
13
+ module Util
14
+ module Parsers
15
+ class JSON
16
+ def self.parse(json)
17
+ yaml = unescape(convert_json_to_yaml(json))
18
+ YAML.load(yaml)
19
+ rescue ArgumentError, Psych::SyntaxError => e
20
+ raise ParseError, "Invalid JSON string: #{yaml}, Error: #{e.inspect}"
21
+ end
22
+
23
+ protected
24
+
25
+ def self.unescape(str)
26
+ str.gsub(/\\u([0-9a-f]{4})/) { [$1.hex].pack("U") }
27
+ end
28
+
29
+ # Ensure that ":" and "," are always followed by a space
30
+ def self.convert_json_to_yaml(json) #:nodoc:
31
+ scanner, quoting, marks, times = StringScanner.new(json), false, [], []
32
+ while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/)
33
+ case char = scanner[1]
34
+ when '"', "'"
35
+ if !quoting
36
+ quoting = char
37
+ elsif quoting == char
38
+ quoting = false
39
+ end
40
+ when ":",","
41
+ marks << scanner.pos - 1 unless quoting
42
+ when "\\"
43
+ scanner.skip(/\\/)
44
+ end
45
+ end
46
+
47
+ if marks.empty?
48
+ json.gsub(/\\\//, '/')
49
+ else
50
+ left_pos = [-1].push(*marks)
51
+ right_pos = marks << json.bytesize
52
+ output = []
53
+
54
+ left_pos.each_with_index do |left, i|
55
+ if json.respond_to?(:byteslice)
56
+ output << json.byteslice(left.succ..right_pos[i])
57
+ else
58
+ output << json[left.succ..right_pos[i]]
59
+ end
60
+ end
61
+
62
+ output = output * " "
63
+
64
+ times.each { |i| output[i-1] = ' ' }
65
+ output.gsub!(/\\\//, '/')
66
+ output
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,7 @@
1
+ module WebMock
2
+ module Util
3
+ module Parsers
4
+ class ParseError < StandardError; end
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ require_relative "parse_error"
2
+ require "crack/xml"
3
+
4
+ module WebMock
5
+ module Util
6
+ module Parsers
7
+ class XML
8
+ def self.parse(xml)
9
+ ::Crack::XML.parse(xml)
10
+ rescue ::REXML::ParseException => e
11
+ raise ParseError, "Invalid XML string: #{xml}, Error: #{e.inspect}"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebMock
4
- VERSION = '3.23.1' unless defined?(::WebMock::VERSION)
4
+ VERSION = '3.24.0' unless defined?(::WebMock::VERSION)
5
5
  end
data/lib/webmock.rb CHANGED
@@ -4,7 +4,6 @@ require 'singleton'
4
4
 
5
5
  require 'addressable/uri'
6
6
  require 'addressable/template'
7
- require 'crack/xml'
8
7
 
9
8
  require_relative 'webmock/deprecation'
10
9
  require_relative 'webmock/version'
@@ -17,7 +16,8 @@ require_relative 'webmock/util/headers'
17
16
  require_relative 'webmock/util/hash_counter'
18
17
  require_relative 'webmock/util/hash_keys_stringifier'
19
18
  require_relative 'webmock/util/values_stringifier'
20
- require_relative 'webmock/util/json'
19
+ require_relative 'webmock/util/parsers/json'
20
+ require_relative 'webmock/util/parsers/xml'
21
21
  require_relative 'webmock/util/version_checker'
22
22
  require_relative 'webmock/util/hash_validator'
23
23
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.23.1
4
+ version: 3.24.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-24 00:00:00.000000000 Z
11
+ date: 2024-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -359,7 +359,9 @@ files:
359
359
  - lib/webmock/util/hash_keys_stringifier.rb
360
360
  - lib/webmock/util/hash_validator.rb
361
361
  - lib/webmock/util/headers.rb
362
- - lib/webmock/util/json.rb
362
+ - lib/webmock/util/parsers/json.rb
363
+ - lib/webmock/util/parsers/parse_error.rb
364
+ - lib/webmock/util/parsers/xml.rb
363
365
  - lib/webmock/util/query_mapper.rb
364
366
  - lib/webmock/util/uri.rb
365
367
  - lib/webmock/util/values_stringifier.rb
@@ -371,9 +373,9 @@ licenses:
371
373
  - MIT
372
374
  metadata:
373
375
  bug_tracker_uri: https://github.com/bblimke/webmock/issues
374
- changelog_uri: https://github.com/bblimke/webmock/blob/v3.23.1/CHANGELOG.md
375
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.23.1
376
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.23.1
376
+ changelog_uri: https://github.com/bblimke/webmock/blob/v3.24.0/CHANGELOG.md
377
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.24.0
378
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.24.0
377
379
  wiki_uri: https://github.com/bblimke/webmock/wiki
378
380
  post_install_message:
379
381
  rdoc_options: []
@@ -390,7 +392,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
390
392
  - !ruby/object:Gem::Version
391
393
  version: '0'
392
394
  requirements: []
393
- rubygems_version: 3.5.7
395
+ rubygems_version: 3.5.16
394
396
  signing_key:
395
397
  specification_version: 4
396
398
  summary: Library for stubbing HTTP requests in Ruby.
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # This is a copy of https://github.com/jnunemaker/crack/blob/master/lib/crack/json.rb
4
- # with date parsing removed
5
- # Copyright (c) 2004-2008 David Heinemeier Hansson
6
- # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
- # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9
-
10
- module WebMock
11
- module Util
12
- class JSON
13
- class ParseError < StandardError; end
14
-
15
- def self.parse(json)
16
- yaml = unescape(convert_json_to_yaml(json))
17
- YAML.load(yaml)
18
- rescue ArgumentError => e
19
- raise ParseError, "Invalid JSON string: #{yaml}, Error: #{e.inspect}"
20
- end
21
-
22
- protected
23
- def self.unescape(str)
24
- str.gsub(/\\u([0-9a-f]{4})/) { [$1.hex].pack("U") }
25
- end
26
-
27
- # Ensure that ":" and "," are always followed by a space
28
- def self.convert_json_to_yaml(json) #:nodoc:
29
- scanner, quoting, marks, times = StringScanner.new(json), false, [], []
30
- while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/)
31
- case char = scanner[1]
32
- when '"', "'"
33
- if !quoting
34
- quoting = char
35
- elsif quoting == char
36
- quoting = false
37
- end
38
- when ":",","
39
- marks << scanner.pos - 1 unless quoting
40
- when "\\"
41
- scanner.skip(/\\/)
42
- end
43
- end
44
-
45
- if marks.empty?
46
- json.gsub(/\\\//, '/')
47
- else
48
- left_pos = [-1].push(*marks)
49
- right_pos = marks << json.bytesize
50
- output = []
51
-
52
- left_pos.each_with_index do |left, i|
53
- if json.respond_to?(:byteslice)
54
- output << json.byteslice(left.succ..right_pos[i])
55
- else
56
- output << json[left.succ..right_pos[i]]
57
- end
58
- end
59
-
60
- output = output * " "
61
-
62
- times.each { |i| output[i-1] = ' ' }
63
- output.gsub!(/\\\//, '/')
64
- output
65
- end
66
- end
67
- end
68
- end
69
- end