webmock 3.3.0 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +38 -2
- data/README.md +3 -2
- data/lib/webmock.rb +1 -0
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +5 -0
- data/lib/webmock/http_lib_adapters/net_http.rb +14 -0
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +1 -1
- data/lib/webmock/request_pattern.rb +7 -3
- data/lib/webmock/stub_request_snippet.rb +7 -3
- data/lib/webmock/util/hash_counter.rb +1 -1
- data/lib/webmock/util/query_mapper.rb +1 -1
- data/lib/webmock/util/values_stringifier.rb +20 -0
- data/lib/webmock/version.rb +1 -1
- data/spec/acceptance/curb/curb_spec.rb +15 -0
- data/spec/acceptance/patron/patron_spec.rb +7 -0
- data/spec/acceptance/shared/stubbing_requests.rb +42 -0
- data/spec/unit/stub_request_snippet_spec.rb +16 -2
- data/spec/unit/util/query_mapper_spec.rb +6 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: babd32444fb5057e9e1945acefbef6601b5b9e2c78a3670ff929a027b89529eb
|
4
|
+
data.tar.gz: 3016884b0119a6cf8e2685628744f661b7d0e0b59462b925b98598732d5cd1a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15e986cdda0dc84d50d35e0e596e92ae18edac6d19c3dcf16930256fc518ddeecab5a5d7e0f5c404bf285f877627707872992547a35f4e4dee2fb31dc4af19f0
|
7
|
+
data.tar.gz: 3ac9e572b8785c1059c1c1388822854a7600cfc28017e48f6dbe2ee3934c29bab214f54b29cd73ee605e74abcac35a0d2dc7cb132bdc54bbfe6853f547626c55
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,46 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 3.4.0
|
4
|
+
|
5
|
+
* Ruby 2.6 support. Prevent `Net/ReadTimeout` error in Ruby 2.6
|
6
|
+
|
7
|
+
Thanks to [Koichi ITO](https://github.com/koic)
|
8
|
+
|
9
|
+
* Handling query params, which represent nested hashes with keys starting with non word characters.
|
10
|
+
|
11
|
+
Thanks to [rapides](https://github.com/rapides) for reporting the issue.
|
12
|
+
|
13
|
+
* Patron adapter handles PATCH requests with body.
|
14
|
+
|
15
|
+
Thanks to [Mattia](https://github.com/iMacTia) for reporting the issue.
|
16
|
+
|
17
|
+
* Allowing requests with url encoded body to be matched by request stubs declared with hash body with non-string values.
|
18
|
+
|
19
|
+
stub_request(:post, "www.example.com").with(body: {"a" => 1, "b" => false})
|
20
|
+
|
21
|
+
RestClient.post('www.example.com', 'a=1&b=false', :content_type => 'application/x-www-form-urlencoded') # ===> Success
|
22
|
+
|
23
|
+
Thanks to [Kenny Ortmann](https://github.com/yairgo) for suggesting this feature.
|
24
|
+
|
25
|
+
* When request headers contain 'Accept'=>'application/json' and no registered stub matches the request, WebMock prints a suggested stub code with to_return body set to '{}'.
|
26
|
+
|
27
|
+
Thanks to [redbar0n](https://github.com/redbar0n)
|
28
|
+
|
29
|
+
* Improved suggested stub output when the request stub only contains headers declaration.
|
30
|
+
|
31
|
+
Thanks to [Olia Kremmyda](https://github.com/oliakremmyda)
|
32
|
+
|
33
|
+
* Fixed Curb adapter to handle `reset` method.
|
34
|
+
|
35
|
+
Thanks tp [dinhhuydh](https://github.com/dinhhuydh) for reporting the issue.
|
36
|
+
Thanks to [Olia Kremmyda](https://github.com/oliakremmyda) for fixing it.
|
37
|
+
|
38
|
+
|
3
39
|
## 3.3.0
|
4
40
|
|
5
41
|
* Better formatting of outputted request stub snippets, displayed on failures due to unstubbed requests.
|
6
42
|
|
7
|
-
Thanks to [Olia Kremmyda](https://github.com/
|
43
|
+
Thanks to [Olia Kremmyda](https://github.com/oliakremmyda)
|
8
44
|
|
9
45
|
|
10
46
|
## 3.2.1
|
@@ -22,7 +58,7 @@
|
|
22
58
|
|
23
59
|
* Fixed bug when handling redirection using Curb.
|
24
60
|
|
25
|
-
Thanks to [Olia Kremmyda](https://github.com/
|
61
|
+
Thanks to [Olia Kremmyda](https://github.com/oliakremmyda)
|
26
62
|
|
27
63
|
|
28
64
|
## 3.1.1
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
WebMock
|
2
2
|
=======
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/webmock.svg)](http://badge.fury.io/rb/webmock) [![Build Status](https://secure.travis-ci.org/bblimke/webmock.svg?branch=master)](http://travis-ci.org/bblimke/webmock) [![Dependency Status](https://gemnasium.com/bblimke/webmock.svg)](http://gemnasium.com/bblimke/webmock) [![Code Climate](https://codeclimate.com/github/bblimke/webmock/badges/gpa.svg)](https://codeclimate.com/github/bblimke/webmock) [![Inline docs](http://inch-ci.org/github/bblimke/webmock.svg?branch=master)](http://inch-ci.org/github/bblimke/webmock)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/webmock.svg)](http://badge.fury.io/rb/webmock) [![Build Status](https://secure.travis-ci.org/bblimke/webmock.svg?branch=master)](http://travis-ci.org/bblimke/webmock) [![Dependency Status](https://gemnasium.com/bblimke/webmock.svg)](http://gemnasium.com/bblimke/webmock) [![Code Climate](https://codeclimate.com/github/bblimke/webmock/badges/gpa.svg)](https://codeclimate.com/github/bblimke/webmock) [![Mentioned in Awesome Ruby](https://awesome.re/mentioned-badge.svg)](https://github.com/markets/awesome-ruby) [![Inline docs](http://inch-ci.org/github/bblimke/webmock.svg?branch=master)](http://inch-ci.org/github/bblimke/webmock)
|
4
4
|
|
5
5
|
Library for stubbing and setting expectations on HTTP requests in Ruby.
|
6
6
|
|
@@ -1053,7 +1053,8 @@ People who submitted patches and new features or suggested improvements. Many th
|
|
1053
1053
|
* Olia Kremmyda
|
1054
1054
|
* Michał Matyas
|
1055
1055
|
* Matt Brictson
|
1056
|
-
*
|
1056
|
+
* Kenny Ortmann
|
1057
|
+
* redbar0n
|
1057
1058
|
|
1058
1059
|
For a full list of contributors you can visit the
|
1059
1060
|
[contributors](https://github.com/bblimke/webmock/contributors) page.
|
data/lib/webmock.rb
CHANGED
@@ -14,6 +14,7 @@ require 'webmock/util/uri'
|
|
14
14
|
require 'webmock/util/headers'
|
15
15
|
require 'webmock/util/hash_counter'
|
16
16
|
require 'webmock/util/hash_keys_stringifier'
|
17
|
+
require 'webmock/util/values_stringifier'
|
17
18
|
require 'webmock/util/json'
|
18
19
|
require 'webmock/util/version_checker'
|
19
20
|
require 'webmock/util/hash_validator'
|
@@ -271,6 +271,20 @@ module Net #:nodoc: all
|
|
271
271
|
end
|
272
272
|
raise "Unable to create local socket" unless @io
|
273
273
|
end
|
274
|
+
|
275
|
+
if RUBY_VERSION >= '2.6.0'
|
276
|
+
def rbuf_fill
|
277
|
+
trace = TracePoint.trace(:line) do |tp|
|
278
|
+
if tp.binding.local_variable_defined?(:tmp)
|
279
|
+
tp.binding.local_variable_set(:tmp, nil)
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
super
|
284
|
+
|
285
|
+
trace.disable
|
286
|
+
end
|
287
|
+
end
|
274
288
|
end
|
275
289
|
|
276
290
|
end
|
@@ -69,7 +69,7 @@ if defined?(::Patron)
|
|
69
69
|
uri = WebMock::Util::URI.heuristic_parse(req.url)
|
70
70
|
uri.path = uri.normalized_path.gsub("[^:]//","/")
|
71
71
|
|
72
|
-
if [:put, :post].include?(req.action)
|
72
|
+
if [:put, :post, :patch].include?(req.action)
|
73
73
|
if req.file_name
|
74
74
|
if !File.exist?(req.file_name) || !File.readable?(req.file_name)
|
75
75
|
raise ArgumentError.new("Unable to open specified file.")
|
@@ -243,7 +243,7 @@ module WebMock
|
|
243
243
|
|
244
244
|
if (@pattern).is_a?(Hash)
|
245
245
|
return true if @pattern.empty?
|
246
|
-
|
246
|
+
matching_body_hashes?(body_as_hash(body, content_type), @pattern, content_type)
|
247
247
|
elsif (@pattern).is_a?(WebMock::Matchers::HashIncludingMatcher)
|
248
248
|
@pattern == body_as_hash(body, content_type)
|
249
249
|
else
|
@@ -298,15 +298,16 @@ module WebMock
|
|
298
298
|
#
|
299
299
|
# @return [Boolean] true if the paramaters match the comparison
|
300
300
|
# hash, false if not.
|
301
|
-
def
|
301
|
+
def matching_body_hashes?(query_parameters, pattern, content_type)
|
302
302
|
return false unless query_parameters.is_a?(Hash)
|
303
303
|
return false unless query_parameters.keys.sort == pattern.keys.sort
|
304
304
|
query_parameters.each do |key, actual|
|
305
305
|
expected = pattern[key]
|
306
306
|
|
307
307
|
if actual.is_a?(Hash) && expected.is_a?(Hash)
|
308
|
-
return false unless
|
308
|
+
return false unless matching_body_hashes?(actual, expected, content_type)
|
309
309
|
else
|
310
|
+
expected = WebMock::Util::ValuesStringifier.stringify_values(expected) if url_encoded_body?(content_type)
|
310
311
|
return false unless expected === actual
|
311
312
|
end
|
312
313
|
end
|
@@ -321,6 +322,9 @@ module WebMock
|
|
321
322
|
Hash[WebMock::Util::HashKeysStringifier.stringify_keys!(hash, deep: true).sort]
|
322
323
|
end
|
323
324
|
|
325
|
+
def url_encoded_body?(content_type)
|
326
|
+
content_type =~ %r{^application/x-www-form-urlencoded}
|
327
|
+
end
|
324
328
|
end
|
325
329
|
|
326
330
|
class HeadersPattern
|
@@ -20,13 +20,17 @@ module WebMock
|
|
20
20
|
end
|
21
21
|
|
22
22
|
if (request_pattern.headers_pattern)
|
23
|
-
with << "
|
23
|
+
with << "," unless with.empty?
|
24
24
|
|
25
|
-
with << "
|
25
|
+
with << "\n headers: #{request_pattern.headers_pattern.pp_to_s}"
|
26
26
|
end
|
27
27
|
string << ".\n with(#{with})" unless with.empty?
|
28
28
|
if with_response
|
29
|
-
|
29
|
+
if request_pattern.headers_pattern && request_pattern.headers_pattern.matches?({ 'Accept' => "application/json" })
|
30
|
+
string << ".\n to_return(status: 200, body: \"{}\", headers: {})"
|
31
|
+
else
|
32
|
+
string << ".\n to_return(status: 200, body: \"\", headers: {})"
|
33
|
+
end
|
30
34
|
end
|
31
35
|
string
|
32
36
|
end
|
@@ -122,7 +122,7 @@ module WebMock::Util
|
|
122
122
|
|
123
123
|
def fill_accumulator_for_subscript(accumulator, key, value)
|
124
124
|
current_node = accumulator
|
125
|
-
subkeys = key.split(/(?=\[\
|
125
|
+
subkeys = key.split(/(?=\[[^\[\]]+)/)
|
126
126
|
subkeys[0..-2].each do |subkey|
|
127
127
|
node = subkey =~ /\[\]\z/ ? [] : {}
|
128
128
|
subkey = subkey.gsub(/[\[\]]/, '')
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class WebMock::Util::ValuesStringifier
|
2
|
+
def self.stringify_values(value)
|
3
|
+
case value
|
4
|
+
when nil
|
5
|
+
value
|
6
|
+
when Hash
|
7
|
+
Hash[
|
8
|
+
value.map do |k, v|
|
9
|
+
[k, stringify_values(v)]
|
10
|
+
end
|
11
|
+
]
|
12
|
+
when Array
|
13
|
+
value.map do |v|
|
14
|
+
stringify_values(v)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
value.to_s
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/webmock/version.rb
CHANGED
@@ -462,5 +462,20 @@ unless RUBY_PLATFORM =~ /java/
|
|
462
462
|
it_should_behave_like "Curb"
|
463
463
|
include CurbSpecHelper::ClassPerform
|
464
464
|
end
|
465
|
+
|
466
|
+
describe "using .reset" do
|
467
|
+
before do
|
468
|
+
@curl = Curl::Easy.new
|
469
|
+
@curl.url = "http://example.com"
|
470
|
+
body = "on_success fired"
|
471
|
+
stub_request(:any, "example.com").to_return(body: body)
|
472
|
+
@curl.http_get
|
473
|
+
end
|
474
|
+
|
475
|
+
it "should clear the body_str" do
|
476
|
+
@curl.reset
|
477
|
+
expect(@curl.body_str).to eq(nil)
|
478
|
+
end
|
479
|
+
end
|
465
480
|
end
|
466
481
|
end
|
@@ -19,6 +19,13 @@ unless RUBY_PLATFORM =~ /java/
|
|
19
19
|
@sess.base_url = "http://www.example.com"
|
20
20
|
end
|
21
21
|
|
22
|
+
it "should allow stubbing PATCH request with body" do
|
23
|
+
stub_request(:patch, "http://www.example.com/")
|
24
|
+
.with(body: "abc")
|
25
|
+
|
26
|
+
@sess.patch('/', "abc")
|
27
|
+
end
|
28
|
+
|
22
29
|
describe "file requests" do
|
23
30
|
|
24
31
|
before(:each) do
|
@@ -155,6 +155,24 @@ shared_examples_for "stubbing requests" do |*adapter_info|
|
|
155
155
|
body: 'c[d][]=f&a=1&c[d][]=e')
|
156
156
|
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'c\[d\]\[\]=f&a=1&c\[d\]\[\]=e'))
|
157
157
|
end
|
158
|
+
|
159
|
+
describe "for request with form url encoded body and content type" do
|
160
|
+
it "should match if stubbed request body hash has string values matching string values in request body" do
|
161
|
+
WebMock.reset!
|
162
|
+
stub_request(:post, "www.example.com").with(body: {"foo" => '1'})
|
163
|
+
expect(http_request(
|
164
|
+
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
|
165
|
+
body: "foo=1").status).to eq("200")
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should match if stubbed request body hash has NON string values matching string values in request body" do
|
169
|
+
WebMock.reset!
|
170
|
+
stub_request(:post, "www.example.com").with(body: {"foo" => 1})
|
171
|
+
expect(http_request(
|
172
|
+
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
|
173
|
+
body: "foo=1").status).to eq("200")
|
174
|
+
end
|
175
|
+
end
|
158
176
|
end
|
159
177
|
|
160
178
|
|
@@ -187,6 +205,30 @@ shared_examples_for "stubbing requests" do |*adapter_info|
|
|
187
205
|
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
|
188
206
|
body: "{\"foo\":\"a b c\"}").status).to eq("200")
|
189
207
|
end
|
208
|
+
|
209
|
+
it "should match if stubbed request body hash has NON string values matching NON string values in request body" do
|
210
|
+
WebMock.reset!
|
211
|
+
stub_request(:post, "www.example.com").with(body: {"foo" => 1})
|
212
|
+
expect(http_request(
|
213
|
+
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
|
214
|
+
body: "{\"foo\":1}").status).to eq("200")
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should not match if stubbed request body hash has string values matching NON string values in request body" do
|
218
|
+
WebMock.reset!
|
219
|
+
stub_request(:post, "www.example.com").with(body: {"foo" => '1'})
|
220
|
+
expect{http_request(
|
221
|
+
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
|
222
|
+
body: "{\"foo\":1}") }.to raise_error(WebMock::NetConnectNotAllowedError)
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should not match if stubbed request body hash has NON string values matching string values in request body" do
|
226
|
+
WebMock.reset!
|
227
|
+
stub_request(:post, "www.example.com").with(body: {"foo" => 1})
|
228
|
+
expect{http_request(
|
229
|
+
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
|
230
|
+
body: "{\"foo\":\"1\"}") }.to raise_error(WebMock::NetConnectNotAllowedError)
|
231
|
+
end
|
190
232
|
end
|
191
233
|
|
192
234
|
describe "for request with xml body and content type is set to xml" do
|
@@ -34,7 +34,7 @@ describe WebMock::StubRequestSnippet do
|
|
34
34
|
it "should print stub request snippet with headers if any" do
|
35
35
|
@request_signature.headers = {'B' => 'b', 'A' => 'a'}
|
36
36
|
expected = 'stub_request(:get, "http://www.example.com/?a=b&c=d").'+
|
37
|
-
"\n with(
|
37
|
+
"\n with(\n headers: {\n\t\ 'A\'=>\'a\',\n\t \'B\'=>\'b\'\n })." +
|
38
38
|
"\n to_return(status: 200, body: \"\", headers: {})"
|
39
39
|
@request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
|
40
40
|
expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
|
@@ -94,8 +94,22 @@ stub_request(:post, "http://www.example.com/").
|
|
94
94
|
STUB
|
95
95
|
expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected.strip)
|
96
96
|
end
|
97
|
-
end
|
98
97
|
|
98
|
+
it "should print stub request snippet with valid JSON body when request header contains 'Accept'=>'application/json' " do
|
99
|
+
@request_signature = WebMock::RequestSignature.new(:post, "www.example.com",
|
100
|
+
headers: {'Accept' => 'application/json'})
|
101
|
+
@request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
|
102
|
+
expected = <<-STUB
|
103
|
+
stub_request(:post, "http://www.example.com/").
|
104
|
+
with(
|
105
|
+
headers: {
|
106
|
+
\t 'Accept'=>'application/json'
|
107
|
+
}).
|
108
|
+
to_return(status: 200, body: \"{}\", headers: {})
|
109
|
+
STUB
|
110
|
+
expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected.strip)
|
111
|
+
end
|
112
|
+
end
|
99
113
|
|
100
114
|
end
|
101
115
|
end
|
@@ -70,6 +70,12 @@ describe WebMock::Util::QueryMapper do
|
|
70
70
|
hsh = subject.query_to_values(query)
|
71
71
|
expect(hsh['a']).to eq('foo')
|
72
72
|
end
|
73
|
+
|
74
|
+
it "should parse hash query with key starting with non word character" do
|
75
|
+
query = "a[$in]=1".freeze
|
76
|
+
hsh = subject.query_to_values(query)
|
77
|
+
expect(hsh).to eql({'a' => {'$in' => '1'}})
|
78
|
+
end
|
73
79
|
end
|
74
80
|
|
75
81
|
context '#to_query' 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.
|
4
|
+
version: 3.4.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: 2018-
|
11
|
+
date: 2018-04-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
@@ -305,6 +305,7 @@ files:
|
|
305
305
|
- lib/webmock/util/json.rb
|
306
306
|
- lib/webmock/util/query_mapper.rb
|
307
307
|
- lib/webmock/util/uri.rb
|
308
|
+
- lib/webmock/util/values_stringifier.rb
|
308
309
|
- lib/webmock/util/version_checker.rb
|
309
310
|
- lib/webmock/version.rb
|
310
311
|
- lib/webmock/webmock.rb
|
@@ -398,7 +399,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
398
399
|
version: '0'
|
399
400
|
requirements: []
|
400
401
|
rubyforge_project: webmock
|
401
|
-
rubygems_version: 2.6
|
402
|
+
rubygems_version: 2.7.6
|
402
403
|
signing_key:
|
403
404
|
specification_version: 4
|
404
405
|
summary: Library for stubbing HTTP requests in Ruby.
|