webmock 4.2.0 → 4.3.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: c360e82812395f3fd6a367abc7417457db31fd3d203b3876c4dab5ce27c8ea37
4
- data.tar.gz: a45f10756d7751e6d51511be1c3967843de2875acd10af18f1d0dffc2717014c
3
+ metadata.gz: c60034709239ce73b68da8da092c4e7e0ed8d0bef698f629ce6dc2a5ac6ceca8
4
+ data.tar.gz: c233fec73699cf7f7b7680207d7858afd09de17990115a1496d6286b1ab24526
5
5
  SHA512:
6
- metadata.gz: ca430a13614968b9e380b561e4271b9601c08531f4fc2cecb3a2f9b1a42602a953f12b7b01e3409b1ba42ff561b1ff9914f3f3976d65aaf1f25531f2e096e240
7
- data.tar.gz: 25512d9a0a614f92cf6c92661d8ce87f3786fa34cf71bb83062ec048dac0704cad1480776f8351e419c18872ae6d3f1cc4bfcf5a5f59b2ef2b8299c7914cb043
6
+ metadata.gz: b4777a9e9c4fe36fc342ba9589d1c43b514d0c72898c1a4baf91ed0bb572debaaca1b07d82cf4e4bb7aae6915625edecffb26e9ac89572a1362046d9a6af22b6
7
+ data.tar.gz: e13f72478e7a02da4e4f3305f4ea96cdce6a27f6330fc2c0978ce4e2c89b7a45babbde2f77ccef385f31b18c9946f699e3967f5fd7ec682ce19a14dd3804fa83
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ # 4.3.0
4
+
5
+ * Allow real requests except to destinations listed in `disallow:` (#1058)
6
+
7
+ `WebMock.allow_net_connect!(disallow: ["billing.example.com"])` lets tests reach other hosts. When WebMock intercepts a request to the billing host, it raises `WebMock::NetConnectNotAllowedError` unless a stub matches. `disallow:` accepts the same matchers as `allow:`.
8
+
9
+ Thanks to [sandfortw](https://github.com/sandfortw) for the suggestion
10
+
11
+ * Fix `RequestStub#then` silently ignoring its block (#1037)
12
+
13
+ Previously, `stub.then { |s| s.with(query: {page: "1"}) }` ignored the block. The stub now requires `page=1`, so a request without it no longer matches. With a block, `then` returns the block's result. Without a block, it still chains responses as before.
14
+
15
+ * Fix Excon splitting stubbed chunks when `Transfer-Encoding` is `["gzip", "chunked"]`
16
+
17
+ WebMock now keeps transfer encodings in the order you specify. With `body_chunks: ["ab", "cd"]` and Excon's `chunk_size: 1`, the streaming callback now receives `"ab"` and `"cd"`. Previously it received four single bytes.
18
+
3
19
  # 4.2.0
4
20
 
5
21
  * Add support for `body_chunks:` to specify which chunks a streaming HTTP client receives from a stub (#629)
data/README.md CHANGED
@@ -512,7 +512,7 @@ stub_request(:get, "www.example.com").to_return(raw_response_file.read)
512
512
  stub_request(:any, 'www.example.net').
513
513
  to_return { |request| {body: request.body} }
514
514
 
515
- RestClient.post('www.example.net', 'abc') # ===> "abc\n"
515
+ RestClient.post('www.example.net', 'abc') # ===> "abc"
516
516
  ```
517
517
 
518
518
  ### Responses dynamically evaluated from lambda
@@ -521,7 +521,7 @@ RestClient.post('www.example.net', 'abc') # ===> "abc\n"
521
521
  stub_request(:any, 'www.example.net').
522
522
  to_return(lambda { |request| {body: request.body} })
523
523
 
524
- RestClient.post('www.example.net', 'abc') # ===> "abc\n"
524
+ RestClient.post('www.example.net', 'abc') # ===> "abc"
525
525
  ```
526
526
 
527
527
  ### Dynamically evaluated raw responses recorded with `curl -is`
@@ -539,7 +539,7 @@ stub_request(:get, "www.example.com").
539
539
  stub_request(:any, 'www.example.net').
540
540
  to_return(body: lambda { |request| request.body })
541
541
 
542
- RestClient.post('www.example.net', 'abc') # ===> "abc\n"
542
+ RestClient.post('www.example.net', 'abc') # ===> "abc"
543
543
  ```
544
544
 
545
545
  ### Rack responses
@@ -607,27 +607,29 @@ WebMock is enabled, as with `Curl::Easy` requests.
607
607
  ```ruby
608
608
  stub_request(:get, "www.example.com").
609
609
  to_return({body: "abc"}, {body: "def"})
610
- Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
611
- Net::HTTP.get('www.example.com', '/') # ===> "def\n"
610
+ Net::HTTP.get('www.example.com', '/') # ===> "abc"
611
+ Net::HTTP.get('www.example.com', '/') # ===> "def"
612
612
 
613
613
  #after all responses are used the last response will be returned infinitely
614
614
 
615
- Net::HTTP.get('www.example.com', '/') # ===> "def\n"
615
+ Net::HTTP.get('www.example.com', '/') # ===> "def"
616
616
  ```
617
617
 
618
618
  ### Multiple responses using chained `to_return()`, `to_raise()` or `to_timeout` declarations
619
619
 
620
620
  ```ruby
621
621
  stub_request(:get, "www.example.com").
622
- to_return({body: "abc"}).then. #then() is just a syntactic sugar
622
+ to_return({body: "abc"}).then.
623
623
  to_return({body: "def"}).then.
624
624
  to_raise(MyException)
625
625
 
626
- Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
627
- Net::HTTP.get('www.example.com', '/') # ===> "def\n"
626
+ Net::HTTP.get('www.example.com', '/') # ===> "abc"
627
+ Net::HTTP.get('www.example.com', '/') # ===> "def"
628
628
  Net::HTTP.get('www.example.com', '/') # ===> MyException raised
629
629
  ```
630
630
 
631
+ Without a block, `.then` is optional between responses. With a block, `stub.then { |s| ... }` yields the stub and returns the block's result, just like Ruby's `then`.
632
+
631
633
  ### Specifying number of times given response should be returned
632
634
 
633
635
  ```ruby
@@ -635,9 +637,9 @@ stub_request(:get, "www.example.com").
635
637
  to_return({body: "abc"}).times(2).then.
636
638
  to_return({body: "def"})
637
639
 
638
- Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
639
- Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
640
- Net::HTTP.get('www.example.com', '/') # ===> "def\n"
640
+ Net::HTTP.get('www.example.com', '/') # ===> "abc"
641
+ Net::HTTP.get('www.example.com', '/') # ===> "abc"
642
+ Net::HTTP.get('www.example.com', '/') # ===> "def"
641
643
  ```
642
644
 
643
645
  ### Removing unused stubs
@@ -663,6 +665,18 @@ WebMock.disable_net_connect!
663
665
  Net::HTTP.get('www.something.com', '/') # ===> Failure
664
666
  ```
665
667
 
668
+ ### Block real requests to selected hosts or URLs
669
+
670
+ ```ruby
671
+ WebMock.allow_net_connect!(disallow: ["billing.example.com"])
672
+ ```
673
+
674
+ This allows real requests to other hosts. A request to `billing.example.com` needs a matching stub or it raises `WebMock::NetConnectNotAllowedError`. Each call to `allow_net_connect!` replaces the previous list.
675
+
676
+ `disallow:` accepts the same hosts, URLs, regular expressions, and callables as `allow:`. A URL with a path matches that exact path and query, so blocking `https://api.example/charge` still allows `https://api.example/charge?id=1`. Use a host name to block every path on that host.
677
+
678
+ When Patron, Curb, or Typhoeus follows redirects inside libcurl, WebMock checks only the initial request. For direct access to an async-http connection pool, use a host rule because WebMock sees only the pool endpoint.
679
+
666
680
  ### External requests can be disabled while allowing localhost
667
681
 
668
682
  ```ruby
@@ -790,13 +804,13 @@ which can be passed to `WebMock.allow_net_connect!` and `WebMock.disable_net_con
790
804
  WebMock.allow_net_connect!(net_http_connect_on_start: true)
791
805
  ```
792
806
 
793
- This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`. At the time of connection being made there is no information about the request or URL yet, therefore WebMock is not able to decide whether to stub a request or not and all connections are allowed. To enable connections only to a specific domain (e.g. your test server) use:
807
+ With this setting, Net::HTTP opens a connection on `start`, even if a stub later handles the request or `disable_net_connect!` blocks it. To connect on start only for your test server, give its host name.
794
808
 
795
809
  ```ruby
796
810
  WebMock.allow_net_connect!(net_http_connect_on_start: "www.example.com")
797
811
  ```
798
812
 
799
- The path is not known when the connection is made, so a URL with a path given here never matches.
813
+ A host listed in `disallow:` prevents the connection on start. The request path is unknown at that point, so a URL with a path in `net_http_connect_on_start:` never matches. WebMock applies path-specific `disallow:` rules when the request is made.
800
814
 
801
815
  ## Setting Expectations
802
816
 
@@ -12,6 +12,7 @@ module WebMock
12
12
  attr_accessor :allow_net_connect
13
13
  attr_accessor :allow_localhost
14
14
  attr_accessor :allow
15
+ attr_accessor :disallow
15
16
  attr_accessor :net_http_connect_on_start
16
17
  attr_accessor :show_stubbing_instructions
17
18
  attr_accessor :query_values_notation
@@ -5,8 +5,13 @@ module WebMock
5
5
  class NetConnectNotAllowedError < Exception
6
6
  def initialize(request_signature)
7
7
  request_signature_snippet = RequestSignatureSnippet.new(request_signature)
8
+ reason = if Config.instance.disallow
9
+ "Real HTTP connections to this destination are disabled."
10
+ else
11
+ "Real HTTP connections are disabled."
12
+ end
8
13
  text = [
9
- "Real HTTP connections are disabled. Unregistered request: #{request_signature}",
14
+ "#{reason} Unregistered request: #{request_signature}",
10
15
  request_signature_snippet.stubbing_instructions,
11
16
  request_signature_snippet.request_stubs,
12
17
  "="*60
@@ -68,7 +68,6 @@ if defined?(Async::HTTP)
68
68
  request_signature = build_request_signature(request)
69
69
  WebMock::RequestRegistry.instance.put(request_signature)
70
70
  webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
71
- net_connect_allowed = WebMock.net_connect_allowed?(request_signature.uri)
72
71
  real_request = false
73
72
 
74
73
  if webmock_response
@@ -76,11 +75,11 @@ if defined?(Async::HTTP)
76
75
  raise Async::TimeoutError, 'WebMock timeout error' if webmock_response.should_timeout
77
76
  WebMockApplication.add_webmock_response(request, webmock_response)
78
77
  response = @webmock_client.call(request)
79
- elsif net_connect_allowed
78
+ elsif WebMock.net_connect_allowed?(request_signature.uri)
80
79
  response = @network_client.call(request)
81
80
  real_request = true
82
81
  else
83
- raise WebMock::NetConnectNotAllowedError.new(request_signature) unless webmock_response
82
+ raise WebMock::NetConnectNotAllowedError.new(request_signature)
84
83
  end
85
84
 
86
85
  if WebMock::CallbackRegistry.any_callbacks?
@@ -108,7 +108,7 @@ module WebMock
108
108
  end
109
109
 
110
110
  def then
111
- self
111
+ block_given? ? super : self
112
112
  end
113
113
 
114
114
  def times(number)
@@ -23,21 +23,23 @@ module WebMock
23
23
  end
24
24
  end
25
25
 
26
- # The headers of a response. normalize_headers sorts repeated values, so
27
- # the order they are given in doesn't matter for matching, but of two
28
- # cookies with the same name a client keeps the last one, so Set-Cookie
29
- # values are put back in the order given. normalize_headers is called
30
- # with the headers alone, as before, since some test suites replace it
31
- # with a method that takes one argument.
26
+ # Request matching sorts repeated values. In responses the last cookie
27
+ # of the same name wins, and transfer codings apply in the order given.
28
+ # Some suites replace normalize_headers with a one-argument method.
29
+ ORDERED_RESPONSE_HEADERS = %w[Set-Cookie Transfer-Encoding].freeze
30
+ private_constant :ORDERED_RESPONSE_HEADERS
31
+
32
32
  def self.normalize_response_headers(headers)
33
33
  normalized = normalize_headers(headers)
34
- return normalized unless normalized.is_a?(Hash) && normalized["Set-Cookie"].is_a?(Array)
34
+ return normalized unless normalized.is_a?(Hash) && ORDERED_RESPONSE_HEADERS.any? { |name| normalized[name].is_a?(Array) }
35
35
 
36
- cookies = nil
37
- headers.each do |name, value|
38
- cookies = value if value.is_a?(Array) && value.size > 1 && normalize_name(name) == "Set-Cookie"
36
+ ordered = {}
37
+ headers.each do |name, values|
38
+ next unless values.is_a?(Array)
39
+ name = normalize_name(name)
40
+ ordered[name] = values.map(&:to_s) if ORDERED_RESPONSE_HEADERS.include?(name) && normalized[name].is_a?(Array)
39
41
  end
40
- cookies ? normalized.merge("Set-Cookie" => cookies.map(&:to_s)) : normalized
42
+ normalized.merge(ordered)
41
43
  end
42
44
 
43
45
  def self.sorted_headers_string(headers)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebMock
4
- VERSION = '4.2.0' unless defined?(::WebMock::VERSION)
4
+ VERSION = '4.3.0' unless defined?(::WebMock::VERSION)
5
5
  end
@@ -47,6 +47,7 @@ module WebMock
47
47
  return restoring_net_connect_settings { allow_net_connect!(options); block.call } if block
48
48
 
49
49
  Config.instance.allow_net_connect = true
50
+ Config.instance.disallow = options[:disallow]
50
51
  Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
51
52
  end
52
53
 
@@ -54,12 +55,13 @@ module WebMock
54
55
  return restoring_net_connect_settings { disable_net_connect!(options); block.call } if block
55
56
 
56
57
  Config.instance.allow_net_connect = false
58
+ Config.instance.disallow = nil
57
59
  Config.instance.allow_localhost = options[:allow_localhost]
58
60
  Config.instance.allow = options[:allow]
59
61
  Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
60
62
  end
61
63
 
62
- NET_CONNECT_SETTINGS = %i[allow_net_connect allow_localhost allow net_http_connect_on_start].freeze
64
+ NET_CONNECT_SETTINGS = %i[allow_net_connect allow_localhost allow disallow net_http_connect_on_start].freeze
63
65
  private_constant :NET_CONNECT_SETTINGS
64
66
 
65
67
  # Runs the block, then puts back the net connect settings from before it,
@@ -92,23 +94,21 @@ module WebMock
92
94
  def self.net_connect_allowed_by_settings?(uri = nil)
93
95
  return !!Config.instance.allow_net_connect if uri.nil?
94
96
 
95
- if uri.is_a?(String)
96
- uri = WebMock::Util::URI.normalize_uri(uri)
97
+ if Config.instance.allow_net_connect
98
+ disallow = Config.instance.disallow
99
+ !disallow || !net_connect_explicit_allowed?(disallow, WebMock::Util::URI.normalize_uri(uri))
100
+ else
101
+ uri = WebMock::Util::URI.normalize_uri(uri) if uri.is_a?(String)
102
+ Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri) ||
103
+ Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri)
97
104
  end
98
-
99
- !!Config.instance.allow_net_connect ||
100
- ( Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri) ||
101
- Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri) )
102
105
  end
103
106
 
104
107
  def self.net_http_connect_on_start?(uri)
105
- allowed = Config.instance.net_http_connect_on_start || false
106
-
107
- if [true, false].include?(allowed)
108
- allowed
109
- else
110
- net_connect_explicit_allowed?(allowed, uri)
111
- end
108
+ allowed = Config.instance.net_http_connect_on_start
109
+ return false unless allowed
110
+ return false if net_connect_explicit_allowed?(Config.instance.disallow, uri)
111
+ allowed.equal?(true) || net_connect_explicit_allowed?(allowed, uri)
112
112
  end
113
113
 
114
114
  def self.net_connect_explicit_allowed?(allowed, uri=nil)
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: 4.2.0
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
@@ -363,9 +363,9 @@ licenses:
363
363
  - MIT
364
364
  metadata:
365
365
  bug_tracker_uri: https://github.com/bblimke/webmock/issues
366
- changelog_uri: https://github.com/bblimke/webmock/blob/v4.2.0/CHANGELOG.md
367
- documentation_uri: https://www.rubydoc.info/gems/webmock/4.2.0
368
- source_code_uri: https://github.com/bblimke/webmock/tree/v4.2.0
366
+ changelog_uri: https://github.com/bblimke/webmock/blob/v4.3.0/CHANGELOG.md
367
+ documentation_uri: https://www.rubydoc.info/gems/webmock/4.3.0
368
+ source_code_uri: https://github.com/bblimke/webmock/tree/v4.3.0
369
369
  wiki_uri: https://github.com/bblimke/webmock/wiki
370
370
  rdoc_options: []
371
371
  require_paths: