webmock 3.17.1 → 3.18.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: e4cbe1f0f2f191bea7fda094073e265aeff44e1669d1222da5cb724e5124eb55
4
- data.tar.gz: e631266b6f082b93800e815a9dc0aa071a60a89c1f803bf3eee739e6b97820a2
3
+ metadata.gz: 70c0ad15601d48c3f413012ced7fde245d03fcc5d5bb7b609fd2ce89084b2589
4
+ data.tar.gz: 4575309d868350685b573d8929ce1836eb044b00fb862c1b9b266a73a1992e9f
5
5
  SHA512:
6
- metadata.gz: 4911f51bd82520812df827796996ab8cf9975f1bee625a158f4644f8c121bbf34172266cd3097ef0609750d51d0c987ea7a645df077f936924f2ea0915f1c0a1
7
- data.tar.gz: bc053c6d94f668d75c1e97629dd9ba38f018ffb2355f5cd97e1f5e54015f6adf02c6f9cac2abf6b5c13d7d4fcd69d1bae77b45cffa2b2b8488fa4d0a1c53e1b3
6
+ metadata.gz: acba1ac0ec3d97c5eb244a84d1b8e895f13e9b3cfb1ca5ac4c0d5109eb7fcc508a3d1e51faa426bd31cb2509d61effed7c134119b5c05d128b3466ec3ee397c7
7
+ data.tar.gz: e8eb5385dd3d4fc9f02c541fbec1bc2c825e07445046b3bde50c471925728c08b1bb9aaf963d61fb44f5a38041f54964ab9fa89caef4cd16fd06fe6bb1ceca44
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ # 3.18.0
4
+
5
+ * Net::BufferedIO is not replaced anymore.
6
+
7
+ Thanks to [Ray Zane](https://github.com/rzane)
8
+
9
+ * Simplified connection handing in Net::HTTP adapter.
10
+
11
+ Thanks to [Ray Zane](https://github.com/rzane)
12
+
3
13
  # 3.17.1
4
14
 
5
15
  * Fixed Syntax Error
@@ -30,7 +40,11 @@
30
40
 
31
41
  Thanks to [Cedric Sohrauer](https://github.com/cedrics)
32
42
 
33
- # 3.16.0
43
+ # 3.16.2
44
+
45
+ * Minimum required Ruby version is 2.0.
46
+
47
+ # 3.16.0 (yanked)
34
48
 
35
49
  * Fix leaky file descriptors and reuse socket for persistent connections.
36
50
 
@@ -40,7 +54,11 @@
40
54
 
41
55
  Thanks to [Ray Zane](https://github.com/rzane)
42
56
 
43
- # 3.15.0
57
+ # 3.15.2
58
+
59
+ * Minimum required Ruby version is 2.0.
60
+
61
+ # 3.15.0 (yanked)
44
62
 
45
63
  * fixed async-http adapter on Windows
46
64
 
data/README.md CHANGED
@@ -1176,7 +1176,7 @@ For a full list of contributors you can visit the
1176
1176
 
1177
1177
  ## Background
1178
1178
 
1179
- Thank you Fakeweb! This library was inspired by [FakeWeb](http://fakeweb.rubyforge.org).
1179
+ Thank you Fakeweb! This library was inspired by [FakeWeb](https://github.com/chrisk/fakeweb).
1180
1180
  I imported some solutions from that project to WebMock. I also copied some code i.e Net:HTTP adapter.
1181
1181
  Fakeweb architecture unfortunately didn't allow me to extend it easily with the features I needed.
1182
1182
  I also preferred some things to work differently i.e request stub precedence.
@@ -10,24 +10,19 @@ module WebMock
10
10
  adapter_for :net_http
11
11
 
12
12
  OriginalNetHTTP = Net::HTTP unless const_defined?(:OriginalNetHTTP)
13
- OriginalNetBufferedIO = Net::BufferedIO unless const_defined?(:OriginalNetBufferedIO)
14
13
 
15
14
  def self.enable!
16
- Net.send(:remove_const, :BufferedIO)
17
15
  Net.send(:remove_const, :HTTP)
18
16
  Net.send(:remove_const, :HTTPSession)
19
17
  Net.send(:const_set, :HTTP, @webMockNetHTTP)
20
18
  Net.send(:const_set, :HTTPSession, @webMockNetHTTP)
21
- Net.send(:const_set, :BufferedIO, Net::WebMockNetBufferedIO)
22
19
  end
23
20
 
24
21
  def self.disable!
25
- Net.send(:remove_const, :BufferedIO)
26
22
  Net.send(:remove_const, :HTTP)
27
23
  Net.send(:remove_const, :HTTPSession)
28
24
  Net.send(:const_set, :HTTP, OriginalNetHTTP)
29
25
  Net.send(:const_set, :HTTPSession, OriginalNetHTTP)
30
- Net.send(:const_set, :BufferedIO, OriginalNetBufferedIO)
31
26
 
32
27
  #copy all constants from @webMockNetHTTP to original Net::HTTP
33
28
  #in case any constants were added to @webMockNetHTTP instead of Net::HTTP
@@ -72,78 +67,57 @@ module WebMock
72
67
  end
73
68
 
74
69
  def request(request, body = nil, &block)
70
+ return super unless started?
71
+
75
72
  request_signature = WebMock::NetHTTPUtility.request_signature_from_request(self, request, body)
76
73
 
77
74
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
78
75
 
79
76
  if webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
80
- @socket = Net::HTTP.socket_type.new
81
77
  WebMock::CallbackRegistry.invoke_callbacks(
82
78
  {lib: :net_http}, request_signature, webmock_response)
83
79
  build_net_http_response(webmock_response, &block)
84
80
  elsif WebMock.net_connect_allowed?(request_signature.uri)
85
81
  check_right_http_connection
86
- after_request = lambda do |response|
87
- if WebMock::CallbackRegistry.any_callbacks?
88
- webmock_response = build_webmock_response(response)
89
- WebMock::CallbackRegistry.invoke_callbacks(
90
- {lib: :net_http, real_request: true}, request_signature, webmock_response)
91
- end
92
- response.extend Net::WebMockHTTPResponse
93
- block.call response if block
94
- response
95
- end
96
- super_with_after_request = lambda {
97
- response = super(request, nil, &nil)
98
- after_request.call(response)
99
- }
100
- if started?
101
- ensure_actual_connection
102
- super_with_after_request.call
103
- else
104
- start_with_connect {
105
- super_with_after_request.call
106
- }
82
+ ensure_actually_connected
83
+
84
+ response = super(request, nil, &nil)
85
+
86
+ if WebMock::CallbackRegistry.any_callbacks?
87
+ WebMock::CallbackRegistry.invoke_callbacks(
88
+ {lib: :net_http, real_request: true},
89
+ request_signature,
90
+ build_webmock_response(response)
91
+ )
107
92
  end
93
+
94
+ response.extend Net::WebMockHTTPResponse
95
+ block.call response if block
96
+ response
108
97
  else
109
98
  raise WebMock::NetConnectNotAllowedError.new(request_signature)
110
99
  end
111
100
  end
112
101
 
113
- def start_without_connect
114
- raise IOError, 'HTTP session already opened' if @started
115
- if block_given?
116
- begin
117
- @socket = Net::HTTP.socket_type.new
118
- @started = true
119
- return yield(self)
120
- ensure
121
- do_finish
122
- end
123
- end
124
- @socket = Net::HTTP.socket_type.new
125
- @started = true
126
- self
127
- end
128
-
129
-
130
- def ensure_actual_connection
131
- if @socket.is_a?(StubSocket)
132
- @socket&.close
133
- @socket = nil
134
- do_start
135
- end
136
- end
102
+ private
137
103
 
138
- alias_method :start_with_connect, :start
104
+ alias_method :actually_connect, :connect
139
105
 
140
- def start(&block)
106
+ def connect
141
107
  uri = Addressable::URI.parse(WebMock::NetHTTPUtility.get_uri(self))
142
108
 
143
109
  if WebMock.net_http_connect_on_start?(uri)
144
- super(&block)
110
+ super
145
111
  else
146
- start_without_connect(&block)
112
+ @socket = StubSocket.new
113
+ end
114
+ end
115
+
116
+ def ensure_actually_connected
117
+ if @socket.is_a?(StubSocket)
118
+ @socket&.close
119
+ @socket = nil
120
+ actually_connect
147
121
  end
148
122
  end
149
123
 
@@ -208,19 +182,6 @@ module WebMock
208
182
  end
209
183
  end
210
184
 
211
- # patch for StringIO behavior in Ruby 2.2.3
212
- # https://github.com/bblimke/webmock/issues/558
213
- class PatchedStringIO < StringIO #:nodoc:
214
-
215
- alias_method :orig_read_nonblock, :read_nonblock
216
-
217
- def read_nonblock(size, *args, **kwargs)
218
- args.reject! {|arg| !arg.is_a?(Hash)}
219
- orig_read_nonblock(size, *args, **kwargs)
220
- end
221
-
222
- end
223
-
224
185
  class StubSocket #:nodoc:
225
186
 
226
187
  attr_accessor :read_timeout, :continue_timeout, :write_timeout
@@ -254,54 +215,6 @@ class StubSocket #:nodoc:
254
215
  end
255
216
  end
256
217
 
257
- module Net #:nodoc: all
258
-
259
- class WebMockNetBufferedIO < BufferedIO
260
- def initialize(io, *args, **kwargs)
261
- io = case io
262
- when Socket, OpenSSL::SSL::SSLSocket, IO
263
- io
264
- when StringIO
265
- PatchedStringIO.new(io.string)
266
- when String
267
- PatchedStringIO.new(io)
268
- end
269
- raise "Unable to create local socket" unless io
270
-
271
- # Prior to 2.4.0 `BufferedIO` only takes a single argument (`io`) with no
272
- # options. Here we pass through our full set of arguments only if we're
273
- # on 2.4.0 or later, and use a simplified invocation otherwise.
274
- if RUBY_VERSION >= '2.4.0'
275
- super
276
- else
277
- super(io)
278
- end
279
- end
280
-
281
- if RUBY_VERSION >= '2.6.0'
282
- # https://github.com/ruby/ruby/blob/7d02441f0d6e5c9d0a73a024519eba4f69e36dce/lib/net/protocol.rb#L208
283
- # Modified version of method from ruby, so that nil is always passed into orig_read_nonblock to avoid timeout
284
- def rbuf_fill
285
- case rv = @io.read_nonblock(BUFSIZE, nil, exception: false)
286
- when String
287
- return if rv.nil?
288
- @rbuf << rv
289
- rv.clear
290
- return
291
- when :wait_readable
292
- @io.to_io.wait_readable(@read_timeout) or raise Net::ReadTimeout
293
- when :wait_writable
294
- @io.to_io.wait_writable(@read_timeout) or raise Net::ReadTimeout
295
- when nil
296
- raise EOFError, 'end of file reached'
297
- end while true
298
- end
299
- end
300
- end
301
-
302
- end
303
-
304
-
305
218
  module WebMock
306
219
  module NetHTTPUtility
307
220
 
@@ -14,8 +14,11 @@ module WebMock
14
14
 
15
15
  class Response
16
16
  def initialize(options = {})
17
- if options.is_a?(IO) || options.is_a?(String)
17
+ case options
18
+ when IO, StringIO
18
19
  self.options = read_raw_response(options)
20
+ when String
21
+ self.options = read_raw_response(StringIO.new(options))
19
22
  else
20
23
  self.options = options
21
24
  end
@@ -120,13 +123,8 @@ module WebMock
120
123
  end
121
124
  end
122
125
 
123
- def read_raw_response(raw_response)
124
- if raw_response.is_a?(IO)
125
- string = raw_response.read
126
- raw_response.close
127
- raw_response = string
128
- end
129
- socket = ::Net::BufferedIO.new(raw_response)
126
+ def read_raw_response(io)
127
+ socket = ::Net::BufferedIO.new(io)
130
128
  response = ::Net::HTTPResponse.read_new(socket)
131
129
  transfer_encoding = response.delete('transfer-encoding') #chunks were already read by curl
132
130
  response.reading_body(socket, true) {}
@@ -138,6 +136,8 @@ module WebMock
138
136
  options[:body] = response.read_body
139
137
  options[:status] = [response.code.to_i, response.message]
140
138
  options
139
+ ensure
140
+ socket.close
141
141
  end
142
142
 
143
143
  InvalidBody = Class.new(StandardError)
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '3.17.1' unless defined?(::WebMock::VERSION)
2
+ VERSION = '3.18.0' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -198,14 +198,14 @@ describe "Net:HTTP" do
198
198
 
199
199
  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
200
200
  it "uses the StubSocket to provide IP address" do
201
- Net::HTTP.start("http://example.com") do |http|
201
+ Net::HTTP.start("example.com") do |http|
202
202
  expect(http.ipaddr).to eq("127.0.0.1")
203
203
  end
204
204
  end
205
205
  end
206
206
 
207
207
  it "defines common socket methods" do
208
- Net::HTTP.start("http://example.com") do |http|
208
+ Net::HTTP.start("example.com") do |http|
209
209
  socket = http.instance_variable_get(:@socket)
210
210
  expect(socket.io.ssl_version).to eq("TLSv1.3")
211
211
  expect(socket.io.cipher).to eq(["TLS_AES_128_GCM_SHA256", "TLSv1.3", 128, 128])
@@ -142,6 +142,34 @@ describe WebMock::Response do
142
142
  end
143
143
 
144
144
  describe "from raw response" do
145
+ describe "when input is a StringIO" do
146
+ before(:each) do
147
+ @io = StringIO.new(File.read(CURL_EXAMPLE_OUTPUT_PATH))
148
+ @response = WebMock::Response.new(@io)
149
+ end
150
+
151
+ it "should read status" do
152
+ expect(@response.status).to eq([202, "OK"])
153
+ end
154
+
155
+ it "should read headers" do
156
+ expect(@response.headers).to eq(
157
+ "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
158
+ "Content-Type"=>"text/html; charset=UTF-8",
159
+ "Content-Length"=>"419",
160
+ "Connection"=>"Keep-Alive",
161
+ "Accept"=>"image/jpeg, image/png"
162
+ )
163
+ end
164
+
165
+ it "should read body" do
166
+ expect(@response.body.size).to eq(419)
167
+ end
168
+
169
+ it "should close IO" do
170
+ expect(@io).to be_closed
171
+ end
172
+ end
145
173
 
146
174
  describe "when input is IO" do
147
175
  before(:each) do
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.17.1
4
+ version: 3.18.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: 2022-08-09 00:00:00.000000000 Z
11
+ date: 2022-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -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.17.1/CHANGELOG.md
425
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.17.1
426
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.17.1
424
+ changelog_uri: https://github.com/bblimke/webmock/blob/v3.18.0/CHANGELOG.md
425
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.18.0
426
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.18.0
427
427
  wiki_uri: https://github.com/bblimke/webmock/wiki
428
428
  post_install_message:
429
429
  rdoc_options: []
@@ -440,7 +440,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
440
440
  - !ruby/object:Gem::Version
441
441
  version: '0'
442
442
  requirements: []
443
- rubygems_version: 3.1.2
443
+ rubygems_version: 3.0.3
444
444
  signing_key:
445
445
  specification_version: 4
446
446
  summary: Library for stubbing HTTP requests in Ruby.