webmock 3.8.0 → 3.9.1
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 +4 -4
- data/.travis.yml +11 -6
- data/CHANGELOG.md +60 -0
- data/README.md +22 -3
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +2 -2
- data/lib/webmock/http_lib_adapters/http_rb/response.rb +11 -1
- data/lib/webmock/http_lib_adapters/net_http.rb +22 -12
- data/lib/webmock/request_pattern.rb +75 -46
- data/lib/webmock/rspec.rb +2 -1
- data/lib/webmock/stub_registry.rb +1 -1
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +3 -1
- data/spec/acceptance/async_http_client/async_http_client_spec.rb +5 -1
- data/spec/acceptance/http_rb/http_rb_spec.rb +2 -2
- data/spec/acceptance/net_http/net_http_spec.rb +12 -0
- data/spec/acceptance/shared/stubbing_requests.rb +18 -0
- data/spec/unit/request_pattern_spec.rb +113 -2
- data/spec/unit/webmock_spec.rb +52 -11
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57c2c81fa3eb74334f703d9f92eb11cea8b06f1c70b713fdee1a071a90e99744
|
4
|
+
data.tar.gz: 96798ebd031599d59d06ff8d2457af877ba6d4a7e9d20ef9e464396c9104bc2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6f0e72a1ecc925a006b9f781cb2e873a8dcafef0a6b80be74df2ad34bdb3eba0a9d02571f72fa825321da86d2a0afdbbdfb4b56e7c3b280bc1643b2acd76100
|
7
|
+
data.tar.gz: 123fdca940fe7ea90b015cb6b085a063a00becef933df84335d1cf762780c12066590ab943c4cd5eba91bba4603f3663d4e8532ca374c036f5c0f306de03e28f
|
data/.travis.yml
CHANGED
@@ -1,19 +1,24 @@
|
|
1
1
|
before_install:
|
2
2
|
- gem update --system -N
|
3
3
|
rvm:
|
4
|
-
- 2.3.8
|
5
|
-
- 2.4.6
|
6
4
|
- 2.5.5
|
7
5
|
- 2.6.3
|
8
6
|
- 2.7.0
|
9
7
|
- rbx-2
|
10
8
|
- ruby-head
|
11
|
-
- jruby-9.1.17.0
|
12
|
-
- jruby-9.2.7.0
|
13
|
-
- jruby-head
|
14
|
-
jdk: openjdk8
|
15
9
|
matrix:
|
10
|
+
include:
|
11
|
+
- rvm: jruby-9.1.17.0
|
12
|
+
jdk: openjdk8
|
13
|
+
- rvm: jruby-9.2.11.1
|
14
|
+
jdk: openjdk11
|
15
|
+
- rvm: jruby-head
|
16
|
+
jdk: openjdk11
|
16
17
|
allow_failures:
|
17
18
|
- rvm: jruby-head
|
18
19
|
- rvm: ruby-head
|
19
20
|
- rvm: rbx-2
|
21
|
+
env:
|
22
|
+
global:
|
23
|
+
JRUBY_OPTS: "--debug"
|
24
|
+
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,65 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
# 3.9.1
|
4
|
+
|
5
|
+
Fixed support for passing `URI` objects as second argument of `stub_request`
|
6
|
+
|
7
|
+
Thanks to [Ryan Kerr](https://github.com/leboshi)
|
8
|
+
|
9
|
+
## 3.9.0
|
10
|
+
|
11
|
+
* Allow using a "callable" (like a proc) as URI pattern
|
12
|
+
|
13
|
+
stub_request(:any, ->(uri) { true })
|
14
|
+
|
15
|
+
Thanks to [John Hawthorn](https://github.com/jhawthorn)
|
16
|
+
|
17
|
+
* Added stubbed IO on stubbed socket in Net::HTTP adapter.
|
18
|
+
|
19
|
+
Thanks to [Thilo Rusche](https://github.com/trusche)
|
20
|
+
|
21
|
+
* When 'webmock/rspec' is required, reset WebMock after all after(:each/example) hooks
|
22
|
+
|
23
|
+
Thanks to [Andrew Stuntz](https://github.com/drews256)
|
24
|
+
|
25
|
+
* Fixed `net_connect_allowed?` when invoked with no arguments, when there were any allowed URIs passed to `disable_net_connect?`.
|
26
|
+
|
27
|
+
Thanks to [Lucas Uyezu](https://github.com/lucasuyezu)
|
28
|
+
|
29
|
+
* Fixed async-http adapter which caused Async::HTTP::Client or Async::HTTP::Internet to hang and never return a response.
|
30
|
+
|
31
|
+
Thanks to (Bruno Sutic)[https://github.com/bruno-] and [Samuel Williams](https://github.com/ioquatix)
|
32
|
+
|
33
|
+
* Fixed warning when using async-http adapter
|
34
|
+
|
35
|
+
Thanks to (Bruno Sutic)[https://github.com/bruno-]
|
36
|
+
|
37
|
+
* Dropped support for Ruby 2.3 - EOL date: 2019-03-31
|
38
|
+
|
39
|
+
* Dropped support for Ruby 2.4 - EOL date: 2020-03-31
|
40
|
+
|
41
|
+
* Handling matching of Addressable::Template patterns that have an ip address without port and patterns that have ip address and don’t have schema and path.
|
42
|
+
|
43
|
+
Thanks to (Rafael França)[https://github.com/rafaelfranca] and (guppy0356)[https://github.com/guppy0356]
|
44
|
+
|
45
|
+
## 3.8.3
|
46
|
+
|
47
|
+
* Fixed problem introduced in version 3.4.2, which caused matching against Addressable::Template representing host part of the URI to raise an error.
|
48
|
+
|
49
|
+
Thanks to [Vesa Laakso](https://github.com/valscion)
|
50
|
+
|
51
|
+
## 3.8.2
|
52
|
+
|
53
|
+
* Support correct encoding parameter for HTTP.rb 2.x and earlier
|
54
|
+
|
55
|
+
Thanks to [Alex Coomans](https://github.com/drcapulet)
|
56
|
+
|
57
|
+
## 3.8.1
|
58
|
+
|
59
|
+
* Added support for mocking non-ASCII bodies when making requests with HTTP.rb
|
60
|
+
|
61
|
+
Thanks to [Patrik Ragnarsson](https://github.com/dentarg)
|
62
|
+
|
3
63
|
## 3.8.0
|
4
64
|
|
5
65
|
* Fixed options handling when initialising Async::HTTP::Client
|
data/README.md
CHANGED
@@ -38,9 +38,6 @@ Supported HTTP libraries
|
|
38
38
|
Supported Ruby Interpreters
|
39
39
|
---------------------------
|
40
40
|
|
41
|
-
* MRI 2.2
|
42
|
-
* MRI 2.3
|
43
|
-
* MRI 2.4
|
44
41
|
* MRI 2.5
|
45
42
|
* MRI 2.6
|
46
43
|
* MRI 2.7
|
@@ -243,6 +240,12 @@ stub_request(:any, /example/)
|
|
243
240
|
Net::HTTP.get('www.example.com', '/') # ===> Success
|
244
241
|
```
|
245
242
|
|
243
|
+
### Matching uris using lambda
|
244
|
+
|
245
|
+
```ruby
|
246
|
+
stub_request(:any, ->(uri) { true })
|
247
|
+
```
|
248
|
+
|
246
249
|
### Matching uris using RFC 6570 - Basic Example
|
247
250
|
|
248
251
|
```ruby
|
@@ -300,6 +303,12 @@ stub_request(:any, "www.example.com").
|
|
300
303
|
Net::HTTP.get("www.example.com", '/') # ===> "abc"
|
301
304
|
```
|
302
305
|
|
306
|
+
Set appropriate Content-Type for HTTParty's `parsed_response`.
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
stub_request(:any, "www.example.com").to_return body: '{}', headers: {content_type: 'application/json'}
|
310
|
+
```
|
311
|
+
|
303
312
|
### Response with body specified as IO object
|
304
313
|
|
305
314
|
```ruby
|
@@ -1118,6 +1127,16 @@ People who submitted patches and new features or suggested improvements. Many th
|
|
1118
1127
|
* Ryan Davis
|
1119
1128
|
* Brandur
|
1120
1129
|
* Samuel Williams
|
1130
|
+
* Patrik Ragnarsson
|
1131
|
+
* Alex Coomans
|
1132
|
+
* Vesa Laakso
|
1133
|
+
* John Hawthorn
|
1134
|
+
* guppy0356
|
1135
|
+
* Thilo Rusche
|
1136
|
+
* Andrew Stuntz
|
1137
|
+
* Lucas Uyezu
|
1138
|
+
* Bruno Sutic
|
1139
|
+
* Ryan Kerr
|
1121
1140
|
|
1122
1141
|
For a full list of contributors you can visit the
|
1123
1142
|
[contributors](https://github.com/bblimke/webmock/contributors) page.
|
@@ -136,7 +136,7 @@ if defined?(Async::HTTP)
|
|
136
136
|
|
137
137
|
def connect
|
138
138
|
server_socket, client_socket = create_connected_sockets
|
139
|
-
Async do
|
139
|
+
Async(transient: true) do
|
140
140
|
accept_socket(server_socket)
|
141
141
|
end
|
142
142
|
client_socket
|
@@ -151,7 +151,7 @@ if defined?(Async::HTTP)
|
|
151
151
|
def create_connected_sockets
|
152
152
|
Async::IO::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM).tap do |sockets|
|
153
153
|
sockets.each do |socket|
|
154
|
-
socket.instance_variable_set :@alpn_protocol,
|
154
|
+
socket.instance_variable_set :@alpn_protocol, nil
|
155
155
|
socket.instance_eval do
|
156
156
|
def alpn_protocol
|
157
157
|
nil # means HTTP11 will be used for HTTPS
|
@@ -14,9 +14,19 @@ module HTTP
|
|
14
14
|
def from_webmock(webmock_response, request_signature = nil)
|
15
15
|
status = Status.new(webmock_response.status.first)
|
16
16
|
headers = webmock_response.headers || {}
|
17
|
-
body = Body.new Streamer.new webmock_response.body
|
18
17
|
uri = normalize_uri(request_signature && request_signature.uri)
|
19
18
|
|
19
|
+
# HTTP.rb 3.0+ uses a keyword argument to pass the encoding, but 1.x
|
20
|
+
# and 2.x use a positional argument, and 0.x don't support supplying
|
21
|
+
# the encoding.
|
22
|
+
body = if HTTP::VERSION < "1.0.0"
|
23
|
+
Body.new(Streamer.new(webmock_response.body))
|
24
|
+
elsif HTTP::VERSION < "3.0.0"
|
25
|
+
Body.new(Streamer.new(webmock_response.body), webmock_response.body.encoding)
|
26
|
+
else
|
27
|
+
Body.new(Streamer.new(webmock_response.body), encoding: webmock_response.body.encoding)
|
28
|
+
end
|
29
|
+
|
20
30
|
return new(status, "1.1", headers, body, uri) if HTTP::VERSION < "1.0.0"
|
21
31
|
|
22
32
|
new({
|
@@ -252,6 +252,13 @@ class StubSocket #:nodoc:
|
|
252
252
|
def readuntil(*args)
|
253
253
|
end
|
254
254
|
|
255
|
+
def io
|
256
|
+
@io ||= StubIO.new
|
257
|
+
end
|
258
|
+
|
259
|
+
class StubIO
|
260
|
+
def setsockopt(*args); end
|
261
|
+
end
|
255
262
|
end
|
256
263
|
|
257
264
|
module Net #:nodoc: all
|
@@ -279,19 +286,22 @@ module Net #:nodoc: all
|
|
279
286
|
end
|
280
287
|
|
281
288
|
if RUBY_VERSION >= '2.6.0'
|
289
|
+
# https://github.com/ruby/ruby/blob/7d02441f0d6e5c9d0a73a024519eba4f69e36dce/lib/net/protocol.rb#L208
|
290
|
+
# Modified version of method from ruby, so that nil is always passed into orig_read_nonblock to avoid timeout
|
282
291
|
def rbuf_fill
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
292
|
+
case rv = @io.read_nonblock(BUFSIZE, nil, exception: false)
|
293
|
+
when String
|
294
|
+
return if rv.nil?
|
295
|
+
@rbuf << rv
|
296
|
+
rv.clear
|
297
|
+
return
|
298
|
+
when :wait_readable
|
299
|
+
@io.to_io.wait_readable(@read_timeout) or raise Net::ReadTimeout
|
300
|
+
when :wait_writable
|
301
|
+
@io.to_io.wait_writable(@read_timeout) or raise Net::ReadTimeout
|
302
|
+
when nil
|
303
|
+
raise EOFError, 'end of file reached'
|
304
|
+
end while true
|
295
305
|
end
|
296
306
|
end
|
297
307
|
end
|
@@ -80,6 +80,8 @@ module WebMock
|
|
80
80
|
URIRegexpPattern.new(uri)
|
81
81
|
elsif uri.is_a?(Addressable::Template)
|
82
82
|
URIAddressablePattern.new(uri)
|
83
|
+
elsif uri.respond_to?(:call)
|
84
|
+
URICallablePattern.new(uri)
|
83
85
|
else
|
84
86
|
URIStringPattern.new(uri)
|
85
87
|
end
|
@@ -107,11 +109,13 @@ module WebMock
|
|
107
109
|
include RSpecMatcherDetector
|
108
110
|
|
109
111
|
def initialize(pattern)
|
110
|
-
@pattern =
|
111
|
-
|
112
|
+
@pattern = if pattern.is_a?(Addressable::URI) \
|
113
|
+
|| pattern.is_a?(Addressable::Template)
|
114
|
+
pattern
|
115
|
+
elsif pattern.respond_to?(:call)
|
112
116
|
pattern
|
113
117
|
else
|
114
|
-
|
118
|
+
WebMock::Util::URI.normalize_uri(pattern)
|
115
119
|
end
|
116
120
|
@query_params = nil
|
117
121
|
end
|
@@ -131,38 +135,44 @@ module WebMock
|
|
131
135
|
end
|
132
136
|
end
|
133
137
|
|
138
|
+
def matches?(uri)
|
139
|
+
pattern_matches?(uri) && query_params_matches?(uri)
|
140
|
+
end
|
141
|
+
|
134
142
|
def to_s
|
135
|
-
str =
|
143
|
+
str = pattern_inspect
|
136
144
|
str += " with query params #{@query_params.inspect}" if @query_params
|
137
145
|
str
|
138
146
|
end
|
139
|
-
end
|
140
147
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
148
|
+
private
|
149
|
+
|
150
|
+
def pattern_inspect
|
151
|
+
@pattern.inspect
|
145
152
|
end
|
146
153
|
|
147
|
-
def
|
148
|
-
|
149
|
-
str += " with query params #{@query_params.inspect}" if @query_params
|
150
|
-
str
|
154
|
+
def query_params_matches?(uri)
|
155
|
+
@query_params.nil? || @query_params == WebMock::Util::QueryMapper.query_to_values(uri.query, notation: Config.instance.query_values_notation)
|
151
156
|
end
|
152
157
|
end
|
153
158
|
|
154
|
-
class
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
else
|
160
|
-
# WebMock checks the query, Addressable checks everything else
|
161
|
-
matches_with_variations?(uri.omit(:query)) &&
|
162
|
-
@query_params == WebMock::Util::QueryMapper.query_to_values(uri.query)
|
163
|
-
end
|
159
|
+
class URICallablePattern < URIPattern
|
160
|
+
private
|
161
|
+
|
162
|
+
def pattern_matches?(uri)
|
163
|
+
@pattern.call(uri)
|
164
164
|
end
|
165
|
+
end
|
166
|
+
|
167
|
+
class URIRegexpPattern < URIPattern
|
168
|
+
private
|
165
169
|
|
170
|
+
def pattern_matches?(uri)
|
171
|
+
WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| u.match(@pattern) }
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
class URIAddressablePattern < URIPattern
|
166
176
|
def add_query_params(query_params)
|
167
177
|
@@add_query_params_warned ||= false
|
168
178
|
if not @@add_query_params_warned
|
@@ -172,27 +182,57 @@ module WebMock
|
|
172
182
|
super(query_params)
|
173
183
|
end
|
174
184
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
185
|
+
private
|
186
|
+
|
187
|
+
def pattern_matches?(uri)
|
188
|
+
if @query_params.nil?
|
189
|
+
# Let Addressable check the whole URI
|
190
|
+
matches_with_variations?(uri)
|
191
|
+
else
|
192
|
+
# WebMock checks the query, Addressable checks everything else
|
193
|
+
matches_with_variations?(uri.omit(:query))
|
194
|
+
end
|
179
195
|
end
|
180
196
|
|
181
|
-
|
197
|
+
def pattern_inspect
|
198
|
+
@pattern.pattern.inspect
|
199
|
+
end
|
182
200
|
|
183
201
|
def matches_with_variations?(uri)
|
184
|
-
|
202
|
+
template =
|
203
|
+
begin
|
204
|
+
Addressable::Template.new(WebMock::Util::URI.heuristic_parse(@pattern.pattern))
|
205
|
+
rescue Addressable::URI::InvalidURIError
|
206
|
+
Addressable::Template.new(@pattern.pattern)
|
207
|
+
end
|
208
|
+
WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u|
|
209
|
+
template_matches_uri?(template, u)
|
210
|
+
}
|
211
|
+
end
|
185
212
|
|
186
|
-
|
213
|
+
def template_matches_uri?(template, uri)
|
214
|
+
template.match(uri)
|
215
|
+
rescue Addressable::URI::InvalidURIError
|
216
|
+
false
|
187
217
|
end
|
188
218
|
end
|
189
219
|
|
190
220
|
class URIStringPattern < URIPattern
|
191
|
-
def
|
221
|
+
def add_query_params(query_params)
|
222
|
+
super
|
223
|
+
if @query_params.is_a?(Hash) || @query_params.is_a?(String)
|
224
|
+
query_hash = (WebMock::Util::QueryMapper.query_to_values(@pattern.query, notation: Config.instance.query_values_notation) || {}).merge(@query_params)
|
225
|
+
@pattern.query = WebMock::Util::QueryMapper.values_to_query(query_hash, notation: WebMock::Config.instance.query_values_notation)
|
226
|
+
@query_params = nil
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
private
|
231
|
+
|
232
|
+
def pattern_matches?(uri)
|
192
233
|
if @pattern.is_a?(Addressable::URI)
|
193
234
|
if @query_params
|
194
|
-
uri.omit(:query) === @pattern
|
195
|
-
(@query_params.nil? || @query_params == WebMock::Util::QueryMapper.query_to_values(uri.query, notation: Config.instance.query_values_notation))
|
235
|
+
uri.omit(:query) === @pattern
|
196
236
|
else
|
197
237
|
uri === @pattern
|
198
238
|
end
|
@@ -201,19 +241,8 @@ module WebMock
|
|
201
241
|
end
|
202
242
|
end
|
203
243
|
|
204
|
-
def
|
205
|
-
|
206
|
-
if @query_params.is_a?(Hash) || @query_params.is_a?(String)
|
207
|
-
query_hash = (WebMock::Util::QueryMapper.query_to_values(@pattern.query, notation: Config.instance.query_values_notation) || {}).merge(@query_params)
|
208
|
-
@pattern.query = WebMock::Util::QueryMapper.values_to_query(query_hash, notation: WebMock::Config.instance.query_values_notation)
|
209
|
-
@query_params = nil
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
def to_s
|
214
|
-
str = WebMock::Util::URI.strip_default_port_from_uri_string(@pattern.to_s)
|
215
|
-
str += " with query params #{@query_params.inspect}" if @query_params
|
216
|
-
str
|
244
|
+
def pattern_inspect
|
245
|
+
WebMock::Util::URI.strip_default_port_from_uri_string(@pattern.to_s)
|
217
246
|
end
|
218
247
|
end
|
219
248
|
|
data/lib/webmock/rspec.rb
CHANGED
@@ -24,7 +24,7 @@ module WebMock
|
|
24
24
|
# doesn't run immediately after stub.with.
|
25
25
|
responses = {}
|
26
26
|
|
27
|
-
stub = ::WebMock::RequestStub.new(:any,
|
27
|
+
stub = ::WebMock::RequestStub.new(:any, ->(uri) { true }).with { |request|
|
28
28
|
responses[request.object_id] = yield(request)
|
29
29
|
}.to_return(lambda { |request| responses.delete(request.object_id) })
|
30
30
|
|
data/lib/webmock/version.rb
CHANGED
data/lib/webmock/webmock.rb
CHANGED
@@ -59,11 +59,13 @@ module WebMock
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def self.net_connect_allowed?(uri = nil)
|
62
|
+
return !!Config.instance.allow_net_connect if uri.nil?
|
63
|
+
|
62
64
|
if uri.is_a?(String)
|
63
65
|
uri = WebMock::Util::URI.normalize_uri(uri)
|
64
66
|
end
|
65
67
|
|
66
|
-
Config.instance.allow_net_connect ||
|
68
|
+
!!Config.instance.allow_net_connect ||
|
67
69
|
( Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri) ||
|
68
70
|
Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri) )
|
69
71
|
end
|
@@ -136,6 +136,8 @@ unless RUBY_PLATFORM =~ /java/
|
|
136
136
|
end
|
137
137
|
|
138
138
|
context 'scheme and protocol' do
|
139
|
+
let(:default_response_headers) { {} }
|
140
|
+
|
139
141
|
before do
|
140
142
|
stub_request(
|
141
143
|
:get, "#{scheme}://www.example.com"
|
@@ -152,7 +154,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
152
154
|
specify do
|
153
155
|
expect(subject).to eq(
|
154
156
|
status: 200,
|
155
|
-
headers:
|
157
|
+
headers: default_response_headers,
|
156
158
|
body: 'BODY'
|
157
159
|
)
|
158
160
|
end
|
@@ -169,6 +171,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
169
171
|
|
170
172
|
context 'HTTP10 protocol' do
|
171
173
|
let(:protocol) { Async::HTTP::Protocol::HTTP10 }
|
174
|
+
let(:default_response_headers) { {"connection"=>["keep-alive"]} }
|
172
175
|
|
173
176
|
include_examples :common
|
174
177
|
end
|
@@ -197,6 +200,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
197
200
|
|
198
201
|
context 'HTTP10 protocol' do
|
199
202
|
let(:protocol) { Async::HTTP::Protocol::HTTP10 }
|
203
|
+
let(:default_response_headers) { {"connection"=>["keep-alive"]} }
|
200
204
|
|
201
205
|
include_examples :common
|
202
206
|
end
|
@@ -74,13 +74,13 @@ describe "HTTP.rb" do
|
|
74
74
|
context "streamer" do
|
75
75
|
it "can be read to a provided buffer" do
|
76
76
|
stub_request(:get, "example.com/foo")
|
77
|
-
.to_return(status: 200, body: "Hello world!")
|
77
|
+
.to_return(status: 200, body: "Hello world! ")
|
78
78
|
response = HTTP.get "http://example.com/foo"
|
79
79
|
|
80
80
|
buffer = ""
|
81
81
|
response.body.readpartial(1024, buffer)
|
82
82
|
|
83
|
-
expect(buffer).to eq "Hello world!"
|
83
|
+
expect(buffer).to eq "Hello world! "
|
84
84
|
end
|
85
85
|
|
86
86
|
it "can be closed" do
|
@@ -201,6 +201,18 @@ describe "Net:HTTP" do
|
|
201
201
|
expect(Net::HTTP.get_response(Addressable::URI.parse('http://www.example.com/hello?a=1')).body).to eq("abc")
|
202
202
|
end
|
203
203
|
|
204
|
+
it "should support method calls on stubbed socket" do
|
205
|
+
WebMock.allow_net_connect!
|
206
|
+
stub_request(:get, 'www.google.com')#.with(headers: {"My-Header" => 99})
|
207
|
+
req = Net::HTTP::Get.new('/')
|
208
|
+
Net::HTTP.start('www.google.com') do |http|
|
209
|
+
http.request(req, '')
|
210
|
+
socket = http.instance_variable_get(:@socket)
|
211
|
+
expect(socket).to be_a(StubSocket)
|
212
|
+
expect { socket.io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) }.to_not raise_error
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
204
216
|
describe "connecting on Net::HTTP.start" do
|
205
217
|
before(:each) do
|
206
218
|
@http = Net::HTTP.new('www.google.com', 443)
|
@@ -640,4 +640,22 @@ shared_examples_for "stubbing requests" do |*adapter_info|
|
|
640
640
|
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
|
641
641
|
end
|
642
642
|
end
|
643
|
+
|
644
|
+
describe "in Rspec around(:each) hook" do
|
645
|
+
# order goes
|
646
|
+
# around(:each)
|
647
|
+
# before(:each)
|
648
|
+
# after(:each)
|
649
|
+
# anything after example.run in around(:each)
|
650
|
+
around(:each) do |example|
|
651
|
+
example.run
|
652
|
+
expect {
|
653
|
+
http_request(:get, "http://www.example.com/")
|
654
|
+
}.to_not raise_error # WebMock::NetConnectNotAllowedError
|
655
|
+
end
|
656
|
+
|
657
|
+
it "should still allow me to make a mocked request" do
|
658
|
+
stub_request(:get, "www.example.com")
|
659
|
+
end
|
660
|
+
end
|
643
661
|
end
|
@@ -111,6 +111,21 @@ describe WebMock::RequestPattern do
|
|
111
111
|
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
112
112
|
end
|
113
113
|
|
114
|
+
it "should match if uri matches requesst uri as URI object" do
|
115
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"))).
|
116
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should match if uri proc pattern returning true" do
|
120
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true })).
|
121
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should not match if uri proc pattern returns false" do
|
125
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { false })).
|
126
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
127
|
+
end
|
128
|
+
|
114
129
|
it "should match if uri Addressable::Template pattern matches unescaped form of request uri" do
|
115
130
|
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{any_path}"))).
|
116
131
|
to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
|
@@ -132,6 +147,30 @@ describe WebMock::RequestPattern do
|
|
132
147
|
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
133
148
|
end
|
134
149
|
|
150
|
+
it "should match if Addressable::Template pattern that has ip address host without port matches request uri" do
|
151
|
+
signature = WebMock::RequestSignature.new(:get, "127.0.0.1/1234")
|
152
|
+
uri = Addressable::Template.new("127.0.0.1/{id}")
|
153
|
+
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should match if Addressable::Template pattern host matches request uri" do
|
157
|
+
signature = WebMock::RequestSignature.new(:get, "www.example.com")
|
158
|
+
uri = Addressable::Template.new("{subdomain}.example.com")
|
159
|
+
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should not match if Addressable::Template pattern host does not match request uri" do
|
163
|
+
signature = WebMock::RequestSignature.new(:get, "www.bad-example.com")
|
164
|
+
uri = Addressable::Template.new("{subdomain}.example.com")
|
165
|
+
expect(WebMock::RequestPattern.new(:get, uri)).not_to match(signature)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should match if uri Addressable::Template pattern matches request uri without a schema and a path " do
|
169
|
+
signature = WebMock::RequestSignature.new(:get, "127.0.0.1:3000")
|
170
|
+
uri = Addressable::Template.new("127.0.0.1:3000")
|
171
|
+
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
172
|
+
end
|
173
|
+
|
135
174
|
it "should match for uris with same parameters as pattern" do
|
136
175
|
expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")).
|
137
176
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))
|
@@ -199,7 +238,7 @@ describe WebMock::RequestPattern do
|
|
199
238
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
200
239
|
end
|
201
240
|
|
202
|
-
it "should match request query params if params don't match" do
|
241
|
+
it "should not match request query params if params don't match" do
|
203
242
|
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"x" => ["b", "c"]})).
|
204
243
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
205
244
|
end
|
@@ -229,13 +268,85 @@ describe WebMock::RequestPattern do
|
|
229
268
|
end
|
230
269
|
end
|
231
270
|
|
271
|
+
describe "when uri is described as URI" do
|
272
|
+
it "should match request query params" do
|
273
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"), query: {"a" => ["b", "c"]})).
|
274
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should not match request query params if params don't match" do
|
278
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"), query: {"x" => ["b", "c"]})).
|
279
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should match when query params are declared as HashIncluding matcher matching params" do
|
283
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
284
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
285
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
286
|
+
end
|
287
|
+
|
288
|
+
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
289
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
290
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
291
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
292
|
+
end
|
293
|
+
|
294
|
+
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
295
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
296
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
297
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
298
|
+
end
|
299
|
+
|
300
|
+
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
301
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
302
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
303
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
describe "when uri is described as a proc" do
|
308
|
+
it "should match request query params" do
|
309
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: {"a" => ["b", "c"]})).
|
310
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
311
|
+
end
|
312
|
+
|
313
|
+
it "should not match request query params if params don't match" do
|
314
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: {"x" => ["b", "c"]})).
|
315
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should match when query params are declared as HashIncluding matcher matching params" do
|
319
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
320
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
321
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
322
|
+
end
|
323
|
+
|
324
|
+
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
325
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
326
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
327
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
328
|
+
end
|
329
|
+
|
330
|
+
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
331
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
332
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
333
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
334
|
+
end
|
335
|
+
|
336
|
+
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
337
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
338
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
339
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
232
343
|
describe "when uri is described as Addressable::Template" do
|
233
344
|
it "should raise error if query params are specified" do
|
234
345
|
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"a" => ["b", "c"]})).
|
235
346
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
236
347
|
end
|
237
348
|
|
238
|
-
it "should match request query params if params don't match" do
|
349
|
+
it "should not match request query params if params don't match" do
|
239
350
|
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"x" => ["b", "c"]})).
|
240
351
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
241
352
|
end
|
data/spec/unit/webmock_spec.rb
CHANGED
@@ -1,19 +1,60 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "WebMock
|
4
|
-
it "should report version" do
|
5
|
-
expect(WebMock.version).to eq(WebMock::VERSION)
|
6
|
-
end
|
3
|
+
describe "WebMock" do
|
7
4
|
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
describe ".version" do
|
6
|
+
it "should report version" do
|
7
|
+
expect(WebMock.version).to eq(WebMock::VERSION)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should not require safe_yaml" do
|
11
|
+
expect(defined?SafeYAML).to eq(nil)
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
+
it "should alias enable_net_connect! to allow_net_connect!" do
|
15
|
+
expect(WebMock.method(:enable_net_connect!)).to eq(WebMock.method(:allow_net_connect!))
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should alias disallow_net_connect! to disable_net_connect!" do
|
19
|
+
expect(WebMock.method(:disallow_net_connect!)).to eq(WebMock.method(:disable_net_connect!))
|
20
|
+
end
|
14
21
|
end
|
15
22
|
|
16
|
-
|
17
|
-
|
23
|
+
describe ".net_connect_allowed?" do
|
24
|
+
context 'enabled globally' do
|
25
|
+
before do
|
26
|
+
WebMock.enable_net_connect!
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'without arguments' do
|
30
|
+
it 'returns WebMock::Config.instance.allow_net_connect' do
|
31
|
+
expect(WebMock.net_connect_allowed?).to eql(true)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'disabled with allowed remote string' do
|
37
|
+
before do
|
38
|
+
WebMock.disable_net_connect!(allow: "http://192.168.64.2:20031")
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'without arguments' do
|
42
|
+
it 'returns WebMock::Config.instance.allow_net_connect' do
|
43
|
+
expect(WebMock.net_connect_allowed?).to eql(false)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'disabled globally' do
|
49
|
+
before do
|
50
|
+
WebMock.disable_net_connect!
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'without arguments' do
|
54
|
+
it 'returns WebMock::Config.instance.allow_net_connect' do
|
55
|
+
expect(WebMock.net_connect_allowed?).to eql(false)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
18
59
|
end
|
19
60
|
end
|
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.
|
4
|
+
version: 3.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bartosz Blimke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
@@ -407,9 +407,9 @@ licenses:
|
|
407
407
|
- MIT
|
408
408
|
metadata:
|
409
409
|
bug_tracker_uri: https://github.com/bblimke/webmock/issues
|
410
|
-
changelog_uri: https://github.com/bblimke/webmock/blob/v3.
|
411
|
-
documentation_uri: https://www.rubydoc.info/gems/webmock/3.
|
412
|
-
source_code_uri: https://github.com/bblimke/webmock/tree/v3.
|
410
|
+
changelog_uri: https://github.com/bblimke/webmock/blob/v3.9.1/CHANGELOG.md
|
411
|
+
documentation_uri: https://www.rubydoc.info/gems/webmock/3.9.1
|
412
|
+
source_code_uri: https://github.com/bblimke/webmock/tree/v3.9.1
|
413
413
|
wiki_uri: https://github.com/bblimke/webmock/wiki
|
414
414
|
post_install_message:
|
415
415
|
rdoc_options: []
|
@@ -426,7 +426,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
426
426
|
- !ruby/object:Gem::Version
|
427
427
|
version: '0'
|
428
428
|
requirements: []
|
429
|
-
rubygems_version: 3.
|
429
|
+
rubygems_version: 3.1.2
|
430
430
|
signing_key:
|
431
431
|
specification_version: 4
|
432
432
|
summary: Library for stubbing HTTP requests in Ruby.
|