webmock 1.8.6 → 3.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/CI.yml +37 -0
- data/.gitignore +6 -0
- data/CHANGELOG.md +1198 -0
- data/Gemfile +3 -15
- data/README.md +761 -305
- data/Rakefile +13 -40
- data/lib/webmock/api.rb +63 -17
- data/lib/webmock/callback_registry.rb +1 -1
- data/lib/webmock/config.rb +8 -0
- data/lib/webmock/cucumber.rb +2 -0
- data/lib/webmock/errors.rb +8 -24
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +216 -0
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +148 -84
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +224 -4
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +104 -34
- data/lib/webmock/http_lib_adapters/http_rb/client.rb +17 -0
- data/lib/webmock/http_lib_adapters/http_rb/request.rb +16 -0
- data/lib/webmock/http_lib_adapters/http_rb/response.rb +64 -0
- data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +29 -0
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +68 -0
- data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +37 -0
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +152 -86
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +145 -0
- data/lib/webmock/http_lib_adapters/net_http.rb +155 -46
- data/lib/webmock/http_lib_adapters/net_http_response.rb +1 -1
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +16 -15
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +76 -82
- data/lib/webmock/matchers/any_arg_matcher.rb +13 -0
- data/lib/webmock/matchers/hash_argument_matcher.rb +21 -0
- data/lib/webmock/matchers/hash_excluding_matcher.rb +15 -0
- data/lib/webmock/matchers/hash_including_matcher.rb +4 -12
- data/lib/webmock/minitest.rb +29 -3
- data/lib/webmock/rack_response.rb +14 -7
- data/lib/webmock/request_body_diff.rb +64 -0
- data/lib/webmock/request_execution_verifier.rb +38 -17
- data/lib/webmock/request_pattern.rb +158 -38
- data/lib/webmock/request_registry.rb +3 -3
- data/lib/webmock/request_signature.rb +7 -3
- data/lib/webmock/request_signature_snippet.rb +61 -0
- data/lib/webmock/request_stub.rb +9 -6
- data/lib/webmock/response.rb +30 -15
- data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +38 -2
- data/lib/webmock/rspec/matchers/webmock_matcher.rb +23 -2
- data/lib/webmock/rspec/matchers.rb +0 -1
- data/lib/webmock/rspec.rb +11 -2
- data/lib/webmock/stub_registry.rb +31 -10
- data/lib/webmock/stub_request_snippet.rb +14 -6
- data/lib/webmock/test_unit.rb +4 -4
- data/lib/webmock/util/hash_counter.rb +20 -6
- data/lib/webmock/util/hash_keys_stringifier.rb +5 -3
- data/lib/webmock/util/hash_validator.rb +17 -0
- data/lib/webmock/util/headers.rb +23 -2
- data/lib/webmock/util/json.rb +20 -7
- data/lib/webmock/util/query_mapper.rb +281 -0
- data/lib/webmock/util/uri.rb +29 -19
- data/lib/webmock/util/values_stringifier.rb +20 -0
- data/lib/webmock/util/version_checker.rb +40 -2
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +56 -17
- data/lib/webmock.rb +56 -46
- data/minitest/test_helper.rb +8 -3
- data/minitest/test_webmock.rb +4 -1
- data/minitest/webmock_spec.rb +16 -6
- data/spec/acceptance/async_http_client/async_http_client_spec.rb +375 -0
- data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +73 -0
- data/spec/acceptance/curb/curb_spec.rb +227 -68
- data/spec/acceptance/curb/curb_spec_helper.rb +11 -8
- data/spec/acceptance/em_http_request/em_http_request_spec.rb +322 -28
- data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +15 -10
- data/spec/acceptance/excon/excon_spec.rb +66 -4
- data/spec/acceptance/excon/excon_spec_helper.rb +21 -7
- data/spec/acceptance/http_rb/http_rb_spec.rb +93 -0
- data/spec/acceptance/http_rb/http_rb_spec_helper.rb +54 -0
- data/spec/acceptance/httpclient/httpclient_spec.rb +152 -11
- data/spec/acceptance/httpclient/httpclient_spec_helper.rb +25 -16
- data/spec/acceptance/manticore/manticore_spec.rb +107 -0
- data/spec/acceptance/manticore/manticore_spec_helper.rb +35 -0
- data/spec/acceptance/net_http/net_http_shared.rb +52 -24
- data/spec/acceptance/net_http/net_http_spec.rb +164 -50
- data/spec/acceptance/net_http/net_http_spec_helper.rb +19 -10
- data/spec/acceptance/net_http/real_net_http_spec.rb +1 -1
- data/spec/acceptance/patron/patron_spec.rb +29 -40
- data/spec/acceptance/patron/patron_spec_helper.rb +15 -11
- data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +229 -58
- data/spec/acceptance/shared/callbacks.rb +32 -30
- data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +20 -5
- data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +14 -14
- data/spec/acceptance/shared/precedence_of_stubs.rb +6 -6
- data/spec/acceptance/shared/request_expectations.rb +560 -296
- data/spec/acceptance/shared/returning_declared_responses.rb +180 -138
- data/spec/acceptance/shared/stubbing_requests.rb +385 -154
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +78 -17
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +19 -15
- data/spec/acceptance/webmock_shared.rb +2 -2
- data/spec/fixtures/test.txt +1 -0
- data/spec/quality_spec.rb +27 -3
- data/spec/spec_helper.rb +11 -20
- data/spec/support/failures.rb +9 -0
- data/spec/support/my_rack_app.rb +8 -3
- data/spec/support/network_connection.rb +7 -13
- data/spec/support/webmock_server.rb +8 -3
- data/spec/unit/api_spec.rb +175 -0
- data/spec/unit/errors_spec.rb +116 -19
- data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +1 -1
- data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +2 -2
- data/spec/unit/matchers/hash_excluding_matcher_spec.rb +61 -0
- data/spec/unit/matchers/hash_including_matcher_spec.rb +87 -0
- data/spec/unit/rack_response_spec.rb +54 -16
- data/spec/unit/request_body_diff_spec.rb +90 -0
- data/spec/unit/request_execution_verifier_spec.rb +147 -39
- data/spec/unit/request_pattern_spec.rb +462 -198
- data/spec/unit/request_registry_spec.rb +29 -9
- data/spec/unit/request_signature_snippet_spec.rb +89 -0
- data/spec/unit/request_signature_spec.rb +91 -49
- data/spec/unit/request_stub_spec.rb +71 -70
- data/spec/unit/response_spec.rb +100 -81
- data/spec/unit/stub_registry_spec.rb +37 -20
- data/spec/unit/stub_request_snippet_spec.rb +51 -31
- data/spec/unit/util/hash_counter_spec.rb +6 -6
- data/spec/unit/util/hash_keys_stringifier_spec.rb +4 -4
- data/spec/unit/util/headers_spec.rb +4 -4
- data/spec/unit/util/json_spec.rb +29 -3
- data/spec/unit/util/query_mapper_spec.rb +157 -0
- data/spec/unit/util/uri_spec.rb +150 -36
- data/spec/unit/util/version_checker_spec.rb +15 -9
- data/spec/unit/webmock_spec.rb +57 -4
- data/test/http_request.rb +3 -3
- data/test/shared_test.rb +45 -13
- data/test/test_helper.rb +1 -1
- data/test/test_webmock.rb +6 -0
- data/webmock.gemspec +30 -11
- metadata +308 -199
- data/.rvmrc +0 -1
- data/.travis.yml +0 -11
- data/Guardfile +0 -24
- data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_0_x.rb +0 -151
- data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb +0 -210
@@ -1,6 +1,7 @@
|
|
1
1
|
shared_context "callbacks" do |*adapter_info|
|
2
2
|
describe "when after_request callback is declared" do
|
3
3
|
before(:each) do
|
4
|
+
@called = nil
|
4
5
|
WebMock.reset_callbacks
|
5
6
|
stub_request(:get, "http://www.example.com")
|
6
7
|
end
|
@@ -9,7 +10,7 @@ shared_context "callbacks" do |*adapter_info|
|
|
9
10
|
WebMock.after_request {
|
10
11
|
@called = true
|
11
12
|
}
|
12
|
-
@called.
|
13
|
+
expect(@called).to eq(nil)
|
13
14
|
end
|
14
15
|
|
15
16
|
it "should invoke a callback after request is made" do
|
@@ -17,31 +18,31 @@ shared_context "callbacks" do |*adapter_info|
|
|
17
18
|
@called = true
|
18
19
|
}
|
19
20
|
http_request(:get, "http://www.example.com/")
|
20
|
-
@called.
|
21
|
+
expect(@called).to eq(true)
|
21
22
|
end
|
22
23
|
|
23
24
|
it "should not invoke a callback if this http library should be ignored" do
|
24
|
-
WebMock.after_request(:
|
25
|
+
WebMock.after_request(except: [http_library()]) {
|
25
26
|
@called = true
|
26
27
|
}
|
27
28
|
http_request(:get, "http://www.example.com/")
|
28
|
-
@called.
|
29
|
+
expect(@called).to eq(nil)
|
29
30
|
end
|
30
31
|
|
31
32
|
it "should invoke a callback even if other http libraries should be ignored" do
|
32
|
-
WebMock.after_request(:
|
33
|
+
WebMock.after_request(except: [:other_lib]) {
|
33
34
|
@called = true
|
34
35
|
}
|
35
36
|
http_request(:get, "http://www.example.com/")
|
36
|
-
@called.
|
37
|
+
expect(@called).to eq(true)
|
37
38
|
end
|
38
39
|
|
39
40
|
it "should pass request signature to the callback" do
|
40
|
-
WebMock.after_request(:
|
41
|
+
WebMock.after_request(except: [:other_lib]) do |request_signature, _|
|
41
42
|
@request_signature = request_signature
|
42
43
|
end
|
43
44
|
http_request(:get, "http://www.example.com/")
|
44
|
-
@request_signature.uri.to_s.
|
45
|
+
expect(@request_signature.uri.to_s).to eq("http://www.example.com:80/")
|
45
46
|
end
|
46
47
|
|
47
48
|
after(:each) do
|
@@ -60,7 +61,7 @@ shared_context "callbacks" do |*adapter_info|
|
|
60
61
|
end
|
61
62
|
|
62
63
|
http_request(:get, "http://www.example.com/")
|
63
|
-
global_stub_request_sig.
|
64
|
+
expect(global_stub_request_sig).to be(after_request_request_sig)
|
64
65
|
end
|
65
66
|
|
66
67
|
context "passing response to callback" do
|
@@ -68,53 +69,54 @@ shared_context "callbacks" do |*adapter_info|
|
|
68
69
|
before(:each) do
|
69
70
|
stub_request(:get, "http://www.example.com").
|
70
71
|
to_return(
|
71
|
-
:
|
72
|
-
:
|
73
|
-
:
|
72
|
+
status: [200, "hello"],
|
73
|
+
headers: {'Content-Length' => '666', 'Hello' => 'World'},
|
74
|
+
body: "foo bar"
|
74
75
|
)
|
75
|
-
WebMock.after_request(:
|
76
|
+
WebMock.after_request(except: [:other_lib]) do |_, response|
|
76
77
|
@response = response
|
77
78
|
end
|
78
79
|
http_request(:get, "http://www.example.com/")
|
79
80
|
end
|
80
81
|
|
81
82
|
it "should pass response to callback with the status and message" do
|
82
|
-
@response.status.
|
83
|
+
expect(@response.status).to eq([200, "hello"])
|
83
84
|
end
|
84
85
|
|
85
86
|
it "should pass response to callback with headers" do
|
86
|
-
@response.headers.
|
87
|
+
expect(@response.headers).to eq({
|
87
88
|
'Content-Length' => '666',
|
88
89
|
'Hello' => 'World'
|
89
|
-
}
|
90
|
+
})
|
90
91
|
end
|
91
92
|
|
92
93
|
it "should pass response to callback with body" do
|
93
|
-
@response.body.
|
94
|
+
expect(@response.body).to eq("foo bar")
|
94
95
|
end
|
95
96
|
end
|
96
97
|
|
97
|
-
describe "when request is not stubbed", :
|
98
|
+
describe "when request is not stubbed", net_connect: true do
|
98
99
|
before(:each) do
|
99
100
|
WebMock.reset!
|
100
101
|
WebMock.allow_net_connect!
|
101
|
-
WebMock.after_request(:
|
102
|
+
WebMock.after_request(except: [:other_lib]) do |_, response|
|
102
103
|
@response = response
|
103
104
|
end
|
104
|
-
http_request(:get, "http://
|
105
|
+
http_request(:get, "http://httpstat.us/201", headers: { "Accept" => "*" })
|
105
106
|
end
|
106
107
|
|
107
108
|
it "should pass real response to callback with status and message" do
|
108
|
-
@response.status[0].
|
109
|
-
@response.status[1].
|
109
|
+
expect(@response.status[0]).to eq(201)
|
110
|
+
expect(@response.status[1]).to eq("Created") unless adapter_info.include?(:no_status_message)
|
110
111
|
end
|
111
112
|
|
112
113
|
it "should pass real response to callback with headers" do
|
113
|
-
@response.headers["
|
114
|
+
expect(@response.headers["X-Powered-By"]).to eq( "ASP.NET")
|
115
|
+
expect(@response.headers["Content-Length"]).to eq("11") unless adapter_info.include?(:no_content_length_header)
|
114
116
|
end
|
115
117
|
|
116
118
|
it "should pass response to callback with body" do
|
117
|
-
@response.body.size.
|
119
|
+
expect(@response.body.size).to eq(11)
|
118
120
|
end
|
119
121
|
end
|
120
122
|
end
|
@@ -123,16 +125,16 @@ shared_context "callbacks" do |*adapter_info|
|
|
123
125
|
WebMock.after_request { @called = 1 }
|
124
126
|
WebMock.after_request { @called += 1 }
|
125
127
|
http_request(:get, "http://www.example.com/")
|
126
|
-
@called.
|
128
|
+
expect(@called).to eq(2)
|
127
129
|
end
|
128
130
|
|
129
|
-
it "should invoke callbacks only for real requests if requested", :
|
130
|
-
WebMock.after_request(:
|
131
|
+
it "should invoke callbacks only for real requests if requested", net_connect: true do
|
132
|
+
WebMock.after_request(real_requests_only: true) { @called = true }
|
131
133
|
http_request(:get, "http://www.example.com/")
|
132
|
-
@called.
|
134
|
+
expect(@called).to eq(nil)
|
133
135
|
WebMock.allow_net_connect!
|
134
136
|
http_request(:get, "http://www.example.net/")
|
135
|
-
@called.
|
137
|
+
expect(@called).to eq(true)
|
136
138
|
end
|
137
139
|
|
138
140
|
it "should not invoke any callbacks after callbacks were reset" do
|
@@ -140,7 +142,7 @@ shared_context "callbacks" do |*adapter_info|
|
|
140
142
|
WebMock.reset_callbacks
|
141
143
|
stub_request(:get, "http://www.example.com/")
|
142
144
|
http_request(:get, "http://www.example.com/")
|
143
|
-
@called.
|
145
|
+
expect(@called).to eq(nil)
|
144
146
|
end
|
145
147
|
end
|
146
148
|
end
|
@@ -7,15 +7,30 @@ shared_context "complex cross-concern behaviors" do |*adapter_info|
|
|
7
7
|
real_response = http_request(:get, webmock_server_url)
|
8
8
|
|
9
9
|
stub_request(:get, webmock_server_url).to_return(
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
10
|
+
status: recorded_response.status,
|
11
|
+
body: recorded_response.body,
|
12
|
+
headers: recorded_response.headers
|
13
13
|
)
|
14
14
|
|
15
15
|
played_back_response = http_request(:get, webmock_server_url)
|
16
16
|
|
17
|
-
played_back_response.headers.keys.
|
18
|
-
played_back_response.
|
17
|
+
expect(played_back_response.headers.keys).to include('Set-Cookie')
|
18
|
+
expect(played_back_response).to eq(real_response)
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:no_content_url) { 'http://httpstat.us/204' }
|
22
|
+
[nil, ''].each do |stub_val|
|
23
|
+
it "returns the same value (nil or "") for a request stubbed as #{stub_val.inspect} that a real empty response has", net_connect: true do
|
24
|
+
unless http_library == :curb
|
25
|
+
WebMock.allow_net_connect!
|
26
|
+
|
27
|
+
real_response = http_request(:get, no_content_url)
|
28
|
+
stub_request(:get, no_content_url).to_return(status: 204, body: stub_val)
|
29
|
+
stubbed_response = http_request(:get, no_content_url)
|
30
|
+
|
31
|
+
expect(stubbed_response.body).to eq(real_response.body)
|
32
|
+
end
|
33
|
+
end
|
19
34
|
end
|
20
35
|
end
|
21
36
|
|
@@ -19,7 +19,7 @@ shared_context "enabled and disabled webmock" do |*adapter_info|
|
|
19
19
|
|
20
20
|
describe "when webmock is disabled except this lib" do
|
21
21
|
before(:each) do
|
22
|
-
WebMock.disable!(:
|
22
|
+
WebMock.disable!(except: [http_library])
|
23
23
|
end
|
24
24
|
after(:each) do
|
25
25
|
WebMock.enable!
|
@@ -30,7 +30,7 @@ shared_context "enabled and disabled webmock" do |*adapter_info|
|
|
30
30
|
describe "when webmock is enabled except this lib" do
|
31
31
|
before(:each) do
|
32
32
|
WebMock.disable!
|
33
|
-
WebMock.enable!(:
|
33
|
+
WebMock.enable!(except: [http_library])
|
34
34
|
end
|
35
35
|
after(:each) do
|
36
36
|
WebMock.enable!
|
@@ -42,18 +42,18 @@ end
|
|
42
42
|
shared_context "disabled WebMock" do
|
43
43
|
it "should not register executed requests" do
|
44
44
|
http_request(:get, webmock_server_url)
|
45
|
-
a_request(:get, webmock_server_url).
|
45
|
+
expect(a_request(:get, webmock_server_url)).not_to have_been_made
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should not block unstubbed requests" do
|
49
|
-
|
49
|
+
expect {
|
50
50
|
http_request(:get, webmock_server_url)
|
51
|
-
}.
|
51
|
+
}.not_to raise_error
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should return real response even if there are stubs" do
|
55
|
-
stub_request(:get, /.*/).to_return(:
|
56
|
-
http_request(:get, webmock_server_url).body.
|
55
|
+
stub_request(:get, /.*/).to_return(body: "x")
|
56
|
+
expect(http_request(:get, webmock_server_url).body).to eq("hello world")
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should not invoke any callbacks" do
|
@@ -62,7 +62,7 @@ shared_context "disabled WebMock" do
|
|
62
62
|
@called = nil
|
63
63
|
WebMock.after_request { @called = 1 }
|
64
64
|
http_request(:get, webmock_server_url)
|
65
|
-
@called.
|
65
|
+
expect(@called).to eq(nil)
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
@@ -70,18 +70,18 @@ shared_context "enabled WebMock" do
|
|
70
70
|
it "should register executed requests" do
|
71
71
|
WebMock.allow_net_connect!
|
72
72
|
http_request(:get, webmock_server_url)
|
73
|
-
a_request(:get, webmock_server_url).
|
73
|
+
expect(a_request(:get, webmock_server_url)).to have_been_made
|
74
74
|
end
|
75
75
|
|
76
76
|
it "should block unstubbed requests" do
|
77
|
-
|
77
|
+
expect {
|
78
78
|
http_request(:get, "http://www.example.com/")
|
79
|
-
}.
|
79
|
+
}.to raise_error(WebMock::NetConnectNotAllowedError)
|
80
80
|
end
|
81
81
|
|
82
82
|
it "should return stubbed response" do
|
83
|
-
stub_request(:get, /.*/).to_return(:
|
84
|
-
http_request(:get, "http://www.example.com/").body.
|
83
|
+
stub_request(:get, /.*/).to_return(body: "x")
|
84
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("x")
|
85
85
|
end
|
86
86
|
|
87
87
|
it "should invoke callbacks" do
|
@@ -90,6 +90,6 @@ shared_context "enabled WebMock" do
|
|
90
90
|
@called = nil
|
91
91
|
WebMock.after_request { @called = 1 }
|
92
92
|
http_request(:get, webmock_server_url)
|
93
|
-
@called.
|
93
|
+
expect(@called).to eq(1)
|
94
94
|
end
|
95
95
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
shared_context "precedence of stubs" do |*adapter_info|
|
2
2
|
describe "when choosing a matching request stub" do
|
3
3
|
it "should use the last declared matching request stub" do
|
4
|
-
stub_request(:get, "www.example.com").to_return(:
|
5
|
-
stub_request(:get, "www.example.com").to_return(:
|
6
|
-
http_request(:get, "http://www.example.com/").body.
|
4
|
+
stub_request(:get, "www.example.com").to_return(body: "abc")
|
5
|
+
stub_request(:get, "www.example.com").to_return(body: "def")
|
6
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("def")
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should not be affected by the type of uri or request method" do
|
10
|
-
stub_request(:get, "www.example.com").to_return(:
|
11
|
-
stub_request(:any, /.*example.*/).to_return(:
|
12
|
-
http_request(:get, "http://www.example.com/").body.
|
10
|
+
stub_request(:get, "www.example.com").to_return(body: "abc")
|
11
|
+
stub_request(:any, /.*example.*/).to_return(body: "def")
|
12
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("def")
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|