webmock 3.15.2 → 3.16.1

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: 16b4c7cbd605852940cec98061211c0e61be7fe3508def1800076febade08cdd
4
- data.tar.gz: 43a33f7993589931deea7860b11c4562a0c76ac9a0dda8cba3bc320c796e36ce
3
+ metadata.gz: 263012f9e44fcac8ea946df8447eab008528eb83677b83a63ba4558c9f66fbf7
4
+ data.tar.gz: d5dbeaaba04501fbeeac6a10a95d8d5c9eca1824fec719d26b2ea769630bbce7
5
5
  SHA512:
6
- metadata.gz: a6d5f9bc87ef66d6700b9b989ea3c30cadd74e62cd67ef9477ab581cbfcc21444d66aa3570b7f9e62949e1be834233c302a887e9a4ae7682009c8f4ccea582c2
7
- data.tar.gz: 137e6d7e95d18539d85b1a1aa53f290aaef74f50a08dcbc330e15bbc26446dadab03cae7cef7cb8c8cd586f942ff6e54123ab13b03d919dfb554663e4b74bb29
6
+ metadata.gz: bbaa6dfb6563bf64a3ddf902977345cac5e909503db5eda9cd4f39a799a81f8f23d740bb113d860596752ad27ac8cd0ee2f1d3a9f91d535d611e7bba1c192547
7
+ data.tar.gz: 7732c64762933e689c2f5cbe147cc430580be9bfe5cc6b1480ccefdcf683a3849438090070e02f4f9ea5b4bfaa59e8c68028092e305c01147567b04357585211
data/CHANGELOG.md CHANGED
@@ -1,18 +1,24 @@
1
1
  # Changelog
2
2
 
3
- # 3.15.2
3
+ # 3.16.1
4
4
 
5
- * Minimum required Ruby version is 2.0 again.
5
+ * Minimum required Ruby version is 2.3
6
6
 
7
- # 3.15.1
7
+ # 3.16.0
8
8
 
9
- * Minimum required Ruby version is 2.3
9
+ * Fix leaky file descriptors and reuse socket for persistent connections.
10
+
11
+ Thanks to [Ray Zane](https://github.com/rzane)
12
+
13
+ * Allow specifying for what URIs or hosts, Net::HTTP should connect on start.
14
+
15
+ Thanks to [Ray Zane](https://github.com/rzane)
10
16
 
11
17
  # 3.15.0
12
18
 
13
19
  * fixed async-http adapter on Windows
14
20
 
15
- Tyanks to [Pavel Rosický](https://github.com/ahorek)
21
+ Thanks to [Pavel Rosický](https://github.com/ahorek)
16
22
 
17
23
  * Support for http.rb >= 5.0.2
18
24
 
@@ -635,9 +641,9 @@
635
641
  * `WebMock.disable_net_connect` accepts `:allow` option with an object that responds to `#call`, receiving a `URI` object and returning a boolean:
636
642
 
637
643
 
638
- blacklist = ['google.com', 'facebook.com', 'apple.com']
644
+ denylist = ['google.com', 'facebook.com', 'apple.com']
639
645
  allowed_sites = lambda{|uri|
640
- blacklist.none?{|site| uri.host.include?(site) }
646
+ denylist.none?{|site| uri.host.include?(site) }
641
647
  }
642
648
  WebMock.disable_net_connect!(:allow => allowed_sites)
643
649
 
data/README.md CHANGED
@@ -550,9 +550,9 @@ RestClient.get('sample.org', '/bar') # ===> Failure
550
550
  With an object that responds to `#call`, receiving a `URI` object and returning a boolean:
551
551
 
552
552
  ```ruby
553
- blacklist = ['google.com', 'facebook.com', 'apple.com']
553
+ denylist = ['google.com', 'facebook.com', 'apple.com']
554
554
  allowed_sites = lambda{|uri|
555
- blacklist.none?{|site| uri.host.include?(site) }
555
+ denylist.none?{|site| uri.host.include?(site) }
556
556
  }
557
557
  WebMock.disable_net_connect!(allow: allowed_sites)
558
558
 
@@ -1165,6 +1165,7 @@ People who submitted patches and new features or suggested improvements. Many th
1165
1165
  * Giorgio Gambino
1166
1166
  * Timmitry
1167
1167
  * Michael Fairley
1168
+ * Ray Zane
1168
1169
 
1169
1170
  For a full list of contributors you can visit the
1170
1171
  [contributors](https://github.com/bblimke/webmock/contributors) page.
@@ -98,13 +98,8 @@ module WebMock
98
98
  after_request.call(response)
99
99
  }
100
100
  if started?
101
- if WebMock::Config.instance.net_http_connect_on_start
102
- super_with_after_request.call
103
- else
104
- start_with_connect_without_finish {
105
- super_with_after_request.call
106
- }
107
- end
101
+ ensure_actual_connection
102
+ super_with_after_request.call
108
103
  else
109
104
  start_with_connect {
110
105
  super_with_after_request.call
@@ -119,32 +114,29 @@ module WebMock
119
114
  raise IOError, 'HTTP session already opened' if @started
120
115
  if block_given?
121
116
  begin
117
+ @socket = Net::HTTP.socket_type.new
122
118
  @started = true
123
119
  return yield(self)
124
120
  ensure
125
121
  do_finish
126
122
  end
127
123
  end
124
+ @socket = Net::HTTP.socket_type.new
128
125
  @started = true
129
126
  self
130
127
  end
131
128
 
132
129
 
133
- def start_with_connect_without_finish # :yield: http
134
- if block_given?
135
- begin
136
- do_start
137
- return yield(self)
138
- end
139
- end
140
- do_start
141
- self
130
+ def ensure_actual_connection
131
+ do_start if @socket.is_a?(StubSocket)
142
132
  end
143
133
 
144
134
  alias_method :start_with_connect, :start
145
135
 
146
136
  def start(&block)
147
- if WebMock::Config.instance.net_http_connect_on_start
137
+ uri = Addressable::URI.parse(WebMock::NetHTTPUtility.get_uri(self))
138
+
139
+ if WebMock.net_http_connect_on_start?(uri)
148
140
  super(&block)
149
141
  else
150
142
  start_without_connect(&block)
@@ -258,6 +250,7 @@ class StubSocket #:nodoc:
258
250
 
259
251
  class StubIO
260
252
  def setsockopt(*args); end
253
+ def peer_cert; end
261
254
  end
262
255
  end
263
256
 
@@ -341,7 +334,7 @@ module WebMock
341
334
  WebMock::RequestSignature.new(method, uri, body: request.body, headers: headers)
342
335
  end
343
336
 
344
- def self.get_uri(net_http, path)
337
+ def self.get_uri(net_http, path = nil)
345
338
  protocol = net_http.use_ssl? ? "https" : "http"
346
339
 
347
340
  hostname = net_http.address
@@ -35,11 +35,11 @@ module WebMock
35
35
  alias == eql?
36
36
 
37
37
  def url_encoded?
38
- !!(headers && headers.fetch('Content-Type', "").start_with?('application/x-www-form-urlencoded'))
38
+ !!(headers&.fetch('Content-Type', nil)&.start_with?('application/x-www-form-urlencoded'))
39
39
  end
40
40
 
41
41
  def json_headers?
42
- !!(headers && headers.fetch('Content-Type', "").start_with?('application/json'))
42
+ !!(headers&.fetch('Content-Type', nil)&.start_with?('application/json'))
43
43
  end
44
44
 
45
45
  private
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '3.15.2' unless defined?(::WebMock::VERSION)
2
+ VERSION = '3.16.1' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -70,6 +70,16 @@ module WebMock
70
70
  Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri) )
71
71
  end
72
72
 
73
+ def self.net_http_connect_on_start?(uri)
74
+ allowed = Config.instance.net_http_connect_on_start || false
75
+
76
+ if [true, false].include?(allowed)
77
+ allowed
78
+ else
79
+ net_connect_explicit_allowed?(allowed, uri)
80
+ end
81
+ end
82
+
73
83
  def self.net_connect_explicit_allowed?(allowed, uri=nil)
74
84
  case allowed
75
85
  when Array
@@ -1,3 +1,5 @@
1
+ require 'set'
2
+
1
3
  shared_examples_for "Net::HTTP" do
2
4
  describe "when making real requests", net_connect: true do
3
5
  let(:port){ WebMockServer.instance.port }
@@ -26,21 +28,56 @@ shared_examples_for "Net::HTTP" do
26
28
 
27
29
  it "should connect only once when connected on start", net_connect: true do
28
30
  @http = Net::HTTP.new('localhost', port)
29
- socket_id_before_request = socket_id_after_request = nil
31
+ socket_before_request = socket_after_request = nil
30
32
  @http.start {|conn|
31
- socket_id_before_request = conn.instance_variable_get(:@socket).object_id
33
+ socket_before_request = conn.instance_variable_get(:@socket)
32
34
  conn.request(Net::HTTP::Get.new("/"))
33
- socket_id_after_request = conn.instance_variable_get(:@socket).object_id
35
+ socket_after_request = conn.instance_variable_get(:@socket)
34
36
  }
35
37
 
36
- if !defined?(WebMock::Config) || WebMock::Config.instance.net_http_connect_on_start
37
- expect(socket_id_before_request).not_to eq(nil.object_id)
38
- expect(socket_id_after_request).not_to eq(nil.object_id)
39
- expect(socket_id_after_request).to eq(socket_id_before_request)
38
+ if !defined?(WebMock::NetHTTPUtility) || WebMock::Config.instance.net_http_connect_on_start
39
+ expect(socket_before_request).to be_a(Net::BufferedIO)
40
+ expect(socket_after_request).to be_a(Net::BufferedIO)
41
+ expect(socket_after_request).to be(socket_before_request)
42
+ else
43
+ expect(socket_before_request).to be_a(StubSocket)
44
+ expect(socket_after_request).to be_a(Net::BufferedIO)
45
+ end
46
+ end
47
+
48
+ it "should allow sending multiple requests when persisted", net_connect: true do
49
+ @http = Net::HTTP.new('example.org')
50
+ @http.start
51
+ expect(@http.get("/")).to be_a(Net::HTTPSuccess)
52
+ expect(@http.get("/")).to be_a(Net::HTTPSuccess)
53
+ expect(@http.get("/")).to be_a(Net::HTTPSuccess)
54
+ @http.finish
55
+ end
56
+
57
+ it "should not leak file descriptors", net_connect: true do
58
+ sockets = Set.new
59
+
60
+ @http = Net::HTTP.new('example.org')
61
+ @http.start
62
+ sockets << @http.instance_variable_get(:@socket)
63
+ @http.get("/")
64
+ sockets << @http.instance_variable_get(:@socket)
65
+ @http.get("/")
66
+ sockets << @http.instance_variable_get(:@socket)
67
+ @http.get("/")
68
+ sockets << @http.instance_variable_get(:@socket)
69
+ @http.finish
70
+
71
+ if !defined?(WebMock::NetHTTPUtility) || WebMock.net_http_connect_on_start?(Addressable::URI.parse("http://example.com/"))
72
+ expect(sockets.length).to eq(1)
73
+ expect(sockets.to_a[0]).to be_a(Net::BufferedIO)
40
74
  else
41
- expect(socket_id_before_request).to eq(nil.object_id)
42
- expect(socket_id_after_request).not_to eq(nil.object_id)
75
+ expect(sockets.length).to eq(2)
76
+ expect(sockets.to_a[0]).to be_a(StubSocket)
77
+ expect(sockets.to_a[1]).to be_a(Net::BufferedIO)
43
78
  end
79
+
80
+ expect(sockets.all?(&:closed?)).to be(true)
44
81
  end
45
82
 
46
83
  it "should pass the read_timeout value on", net_connect: true do
@@ -254,6 +254,21 @@ describe "Net:HTTP" do
254
254
  }
255
255
  end
256
256
 
257
+ it "should connect to the server on start when allowlisted", net_connect: true do
258
+ WebMock.disable_net_connect!(allow: "www.google.com", net_http_connect_on_start: "www.google.com")
259
+ @http.start {|conn|
260
+ cert = OpenSSL::X509::Certificate.new conn.peer_cert
261
+ expect(cert).to be_a(OpenSSL::X509::Certificate)
262
+ }
263
+ end
264
+
265
+ it "should not connect to the server on start when not allowlisted", net_connect: true do
266
+ WebMock.disable_net_connect!(allow: "www.google.com", net_http_connect_on_start: "www.yahoo.com")
267
+ @http.start {|conn|
268
+ expect(conn.peer_cert).to be_nil
269
+ }
270
+ end
271
+
257
272
  it "should connect to the server if the URI matches an regex", net_connect: true do
258
273
  WebMock.disable_net_connect!(allow: /google.com/)
259
274
  Net::HTTP.get('www.google.com','/')
@@ -282,6 +297,13 @@ describe "Net:HTTP" do
282
297
  it_should_behave_like "Net::HTTP"
283
298
  end
284
299
 
300
+ describe "when net_http_connect_on_start is a specific host" do
301
+ before(:each) do
302
+ WebMock.allow_net_connect!(net_http_connect_on_start: "localhost")
303
+ end
304
+ it_should_behave_like "Net::HTTP"
305
+ end
306
+
285
307
  describe 'after_request callback support', net_connect: true do
286
308
  let(:expected_body_regex) { /hello world/ }
287
309
 
@@ -365,5 +387,10 @@ describe "Net:HTTP" do
365
387
  path = '/example.jpg'
366
388
  expect(WebMock::NetHTTPUtility.get_uri(net_http, path)).to eq('http://www.example.com:80/example.jpg')
367
389
  end
390
+
391
+ it "does not require a path" do
392
+ net_http = Net::HTTP.new('www.example.com', 80)
393
+ expect(WebMock::NetHTTPUtility.get_uri(net_http)).to eq('http://www.example.com:80')
394
+ end
368
395
  end
369
396
  end
@@ -17,4 +17,4 @@ describe "Real Net:HTTP without webmock", without_webmock: true do
17
17
  end
18
18
 
19
19
  it_should_behave_like "Net::HTTP"
20
- end
20
+ end
@@ -57,4 +57,58 @@ describe "WebMock" do
57
57
  end
58
58
  end
59
59
  end
60
+
61
+ describe ".net_http_connect_on_start?" do
62
+ let(:uri) { Addressable::URI.parse("http://example.org:5432") }
63
+
64
+ it "will not connect on start when false" do
65
+ WebMock.disable_net_connect!
66
+ expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
67
+ end
68
+
69
+ it "will connect on start when true" do
70
+ WebMock.disable_net_connect!(net_http_connect_on_start: true)
71
+ expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
72
+ end
73
+
74
+ it "will connect on start when regexp matches" do
75
+ WebMock.disable_net_connect!(net_http_connect_on_start: /example/)
76
+ expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
77
+ end
78
+
79
+ it "will not connect on start when regexp does not match" do
80
+ WebMock.disable_net_connect!(net_http_connect_on_start: /nope/)
81
+ expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
82
+ end
83
+
84
+ it "will connect on start when host matches" do
85
+ WebMock.disable_net_connect!(net_http_connect_on_start: "example.org")
86
+ expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
87
+ end
88
+
89
+ it "will not connect on start when host does not match" do
90
+ WebMock.disable_net_connect!(net_http_connect_on_start: "localhost")
91
+ expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
92
+ end
93
+
94
+ it "will connect on start when host + port matches" do
95
+ WebMock.disable_net_connect!(net_http_connect_on_start: "example.org:5432")
96
+ expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
97
+ end
98
+
99
+ it "will not connect on start when host + port does not match" do
100
+ WebMock.disable_net_connect!(net_http_connect_on_start: "example.org:80")
101
+ expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
102
+ end
103
+
104
+ it "will connect on start when scheme + host + port matches" do
105
+ WebMock.disable_net_connect!(net_http_connect_on_start: "http://example.org:5432")
106
+ expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
107
+ end
108
+
109
+ it "will not connect on start when scheme + host + port does not match" do
110
+ WebMock.disable_net_connect!(net_http_connect_on_start: "https://example.org:5432")
111
+ expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
112
+ end
113
+ end
60
114
  end
data/webmock.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
  'wiki_uri' => 'https://github.com/bblimke/webmock/wiki'
22
22
  }
23
23
 
24
- s.required_ruby_version = '>= 2.0'
24
+ s.required_ruby_version = '>= 2.3'
25
25
 
26
26
  s.add_dependency 'addressable', '>= 2.8.0'
27
27
  s.add_dependency 'crack', '>= 0.3.2'
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.15.2
4
+ version: 3.16.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
@@ -421,9 +421,9 @@ licenses:
421
421
  - MIT
422
422
  metadata:
423
423
  bug_tracker_uri: https://github.com/bblimke/webmock/issues
424
- changelog_uri: https://github.com/bblimke/webmock/blob/v3.15.2/CHANGELOG.md
425
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.15.2
426
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.15.2
424
+ changelog_uri: https://github.com/bblimke/webmock/blob/v3.16.1/CHANGELOG.md
425
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.16.1
426
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.16.1
427
427
  wiki_uri: https://github.com/bblimke/webmock/wiki
428
428
  post_install_message:
429
429
  rdoc_options: []
@@ -433,7 +433,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
433
433
  requirements:
434
434
  - - ">="
435
435
  - !ruby/object:Gem::Version
436
- version: '2.0'
436
+ version: '2.3'
437
437
  required_rubygems_version: !ruby/object:Gem::Requirement
438
438
  requirements:
439
439
  - - ">="