webmock 1.8.6 → 3.14.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 +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,156 +1,167 @@
|
|
1
1
|
class MyException < StandardError; end;
|
2
2
|
|
3
|
+
class Responder
|
4
|
+
def call(request)
|
5
|
+
{body: request.body}
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
3
9
|
shared_context "declared responses" do |*adapter_info|
|
4
10
|
describe "when request stub declares that request should raise exception" do
|
5
11
|
it "should raise exception" do
|
6
12
|
stub_request(:get, "www.example.com").to_raise(MyException)
|
7
|
-
|
13
|
+
expect {
|
8
14
|
http_request(:get, "http://www.example.com/")
|
9
|
-
}.
|
15
|
+
}.to raise_error(MyException, "Exception from WebMock")
|
10
16
|
end
|
11
17
|
|
12
18
|
it "should raise exception if declared as and exception instance" do
|
13
19
|
stub_request(:get, "www.example.com").to_raise(MyException.new("hello world"))
|
14
|
-
|
20
|
+
expect {
|
15
21
|
http_request(:get, "http://www.example.com/")
|
16
|
-
}.
|
22
|
+
}.to raise_error(MyException, "hello world")
|
17
23
|
end
|
18
24
|
|
19
25
|
it "should raise exception if declared as an exception instance" do
|
20
26
|
stub_request(:get, "www.example.com").to_raise("hello world")
|
21
|
-
|
27
|
+
expect {
|
22
28
|
http_request(:get, "http://www.example.com/")
|
23
|
-
}.
|
29
|
+
}.to raise_error("hello world")
|
24
30
|
end
|
25
31
|
|
26
32
|
it "should raise exception after returning declared successful response first" do
|
27
|
-
stub_request(:get, "www.example.com").to_return(:
|
28
|
-
http_request(:get, "http://www.example.com/").body.
|
29
|
-
|
33
|
+
stub_request(:get, "www.example.com").to_return(body: "abc").then.to_raise(MyException)
|
34
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
|
35
|
+
expect {
|
30
36
|
http_request(:get, "http://www.example.com/")
|
31
|
-
}.
|
37
|
+
}.to raise_error(MyException, "Exception from WebMock")
|
32
38
|
end
|
33
39
|
end
|
34
40
|
|
35
41
|
describe "when request stub declares that request should timeout" do
|
36
42
|
it "should timeout" do
|
37
43
|
stub_request(:get, "www.example.com").to_timeout
|
38
|
-
|
44
|
+
expect {
|
39
45
|
http_request(:get, "http://www.example.com/")
|
40
|
-
}.
|
46
|
+
}.to raise_error(client_timeout_exception_class)
|
41
47
|
end
|
42
48
|
|
43
49
|
it "should timeout after returning declared successful response" do
|
44
|
-
stub_request(:get, "www.example.com").to_return(:
|
45
|
-
http_request(:get, "http://www.example.com/").body.
|
46
|
-
|
50
|
+
stub_request(:get, "www.example.com").to_return(body: "abc").then.to_timeout
|
51
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
|
52
|
+
expect {
|
47
53
|
http_request(:get, "http://www.example.com/")
|
48
|
-
}.
|
54
|
+
}.to raise_error(client_timeout_exception_class)
|
49
55
|
end
|
50
56
|
end
|
51
57
|
|
52
58
|
describe "when request stub declares that request should return a response" do
|
53
59
|
it "should return response with declared body" do
|
54
|
-
stub_request(:get, "www.example.com").to_return(:
|
55
|
-
http_request(:get, "http://www.example.com/").body.
|
60
|
+
stub_request(:get, "www.example.com").to_return(body: "abc")
|
61
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
|
56
62
|
end
|
57
63
|
|
58
64
|
it "should return response with declared headers" do
|
59
|
-
stub_request(:get, "www.example.com").to_return(:
|
65
|
+
stub_request(:get, "www.example.com").to_return(headers: SAMPLE_HEADERS)
|
60
66
|
response = http_request(:get, "http://www.example.com/")
|
61
|
-
response.headers["
|
67
|
+
expect(response.headers["Accept"]).to eq("application/json")
|
68
|
+
unless adapter_info.include?(:no_content_length_header)
|
69
|
+
expect(response.headers["Content-Length"]).to eq("8888")
|
70
|
+
end
|
62
71
|
end
|
63
72
|
|
64
73
|
it "should return response with declared headers even if there are multiple headers with the same key" do
|
65
|
-
stub_request(:get, "www.example.com").to_return(:
|
74
|
+
stub_request(:get, "www.example.com").to_return(headers: {"a" => ["b", "c"]})
|
66
75
|
response = http_request(:get, "http://www.example.com/")
|
67
|
-
response.headers["A"].
|
76
|
+
expect(response.headers["A"]).to eq("b, c")
|
68
77
|
end
|
69
78
|
|
70
79
|
it "should return response with declared status code" do
|
71
|
-
stub_request(:get, "www.example.com").to_return(:
|
72
|
-
http_request(:get, "http://www.example.com/").status.
|
80
|
+
stub_request(:get, "www.example.com").to_return(status: 500)
|
81
|
+
expect(http_request(:get, "http://www.example.com/").status).to eq("500")
|
73
82
|
end
|
74
83
|
|
75
|
-
it "should return response with declared status message", :
|
76
|
-
stub_request(:get, "www.example.com").to_return(:
|
84
|
+
it "should return response with declared status message", unless: (adapter_info.include?(:no_status_message)) do
|
85
|
+
stub_request(:get, "www.example.com").to_return(status: [500, "Internal Server Error"])
|
77
86
|
response = http_request(:get, "http://www.example.com/")
|
78
|
-
response.message.
|
87
|
+
expect(response.message).to eq("Internal Server Error")
|
79
88
|
end
|
80
89
|
|
81
90
|
it "should return response with a default status code" do
|
82
91
|
stub_request(:get, "www.example.com")
|
83
|
-
http_request(:get, "http://www.example.com/").status.
|
92
|
+
expect(http_request(:get, "http://www.example.com/").status).to eq("200")
|
84
93
|
end
|
85
94
|
|
86
|
-
it "should return default response with empty message if response was not declared", :
|
95
|
+
it "should return default response with empty message if response was not declared", unless: (adapter_info.include?(:no_status_message)) do
|
87
96
|
stub_request(:get, "www.example.com")
|
88
97
|
response = http_request(:get, "http://www.example.com/")
|
89
|
-
response.message.
|
98
|
+
expect(response.message).to eq("")
|
90
99
|
end
|
91
100
|
|
92
101
|
describe "when response body was declared as IO" do
|
93
102
|
it "should return response body" do
|
94
|
-
stub_request(:get, "www.example.com").to_return(:
|
95
|
-
http_request(:get, "http://www.example.com/").body.
|
103
|
+
stub_request(:get, "www.example.com").to_return(body: File.new(__FILE__))
|
104
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq(File.read(__FILE__))
|
96
105
|
end
|
97
106
|
|
98
107
|
it "should return response body if requested many times" do
|
99
|
-
stub_request(:get, "www.example.com").to_return(:
|
108
|
+
stub_request(:get, "www.example.com").to_return(body: File.new(__FILE__))
|
100
109
|
2.times do
|
101
|
-
http_request(:get, "http://www.example.com/").body.
|
110
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq(File.read(__FILE__))
|
102
111
|
end
|
103
112
|
end
|
104
113
|
|
105
114
|
it "should close IO after request" do
|
106
|
-
stub_request(:get, "www.example.com").to_return(:
|
107
|
-
@file.
|
115
|
+
stub_request(:get, "www.example.com").to_return(body: @file = File.new(__FILE__))
|
116
|
+
expect(@file).to be_closed
|
108
117
|
end
|
109
118
|
end
|
110
119
|
|
111
120
|
describe "when response parts were declared as lambdas" do
|
112
121
|
it "should return evaluated response body" do
|
113
|
-
stub_request(:post, "www.example.com").to_return(:
|
114
|
-
http_request(:post, "http://www.example.com/", :
|
122
|
+
stub_request(:post, "www.example.com").to_return(body: lambda { |request| request.body })
|
123
|
+
expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
|
115
124
|
end
|
116
125
|
|
117
126
|
it "should return evaluated response headers" do
|
118
|
-
stub_request(:post, "www.example.com").to_return(:
|
119
|
-
http_request(:post, "http://www.example.com/", :
|
127
|
+
stub_request(:post, "www.example.com").to_return(headers: lambda { |request| request.headers })
|
128
|
+
expect(http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'B'}).headers['A']).to eq('B')
|
129
|
+
expect(http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'C'}).headers['A']).to eq('C')
|
120
130
|
end
|
121
|
-
end
|
122
131
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
end
|
132
|
+
it "should evaluate response body for each request" do
|
133
|
+
stub_request(:post, "www.example.com").to_return(body: lambda { |request| request.body })
|
134
|
+
expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
|
135
|
+
expect(http_request(:post, "http://www.example.com/", body: "foxtrot").body).to eq("foxtrot")
|
128
136
|
end
|
137
|
+
end
|
129
138
|
|
139
|
+
describe "when response was declared as lambda" do
|
130
140
|
it "should return evaluated response body" do
|
131
141
|
stub_request(:post, "www.example.com").to_return(lambda {|request|
|
132
|
-
{:
|
142
|
+
{body: request.body}
|
133
143
|
})
|
134
|
-
http_request(:post, "http://www.example.com/", :
|
144
|
+
expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
|
145
|
+
expect(http_request(:post, "http://www.example.com/", body: "foxtrot").body).to eq("foxtrot")
|
135
146
|
end
|
136
147
|
|
137
148
|
it "should return evaluated response headers" do
|
138
149
|
stub_request(:get, "www.example.com").to_return(lambda { |request|
|
139
|
-
{:
|
150
|
+
{headers: request.headers}
|
140
151
|
})
|
141
|
-
http_request(:get, "http://www.example.com/", :
|
152
|
+
expect(http_request(:get, "http://www.example.com/", headers: {'A' => 'B'}).headers['A']).to eq('B')
|
142
153
|
end
|
143
154
|
|
144
155
|
it "should return dynamic response declared as a block" do
|
145
156
|
stub_request(:post, "www.example.com").to_return do |request|
|
146
|
-
{:
|
157
|
+
{body: request.body}
|
147
158
|
end
|
148
|
-
http_request(:post, "http://www.example.com/", :
|
159
|
+
expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
|
149
160
|
end
|
150
161
|
|
151
162
|
it "should return dynamic response declared as an object responding to call" do
|
152
163
|
stub_request(:post, "www.example.com").to_return(Responder.new)
|
153
|
-
http_request(:post, "http://www.example.com/", :
|
164
|
+
expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
|
154
165
|
end
|
155
166
|
end
|
156
167
|
|
@@ -163,59 +174,77 @@ shared_context "declared responses" do |*adapter_info|
|
|
163
174
|
end
|
164
175
|
|
165
176
|
it "should return recorded headers" do
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
177
|
+
if adapter_info.include?(:no_content_length_header)
|
178
|
+
expect(@response.headers).to eq({
|
179
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
180
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
181
|
+
"Connection"=>"Keep-Alive",
|
182
|
+
"Accept"=>"image/jpeg, image/png"
|
183
|
+
})
|
184
|
+
else
|
185
|
+
expect(@response.headers).to eq({
|
186
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
187
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
188
|
+
"Content-Length"=>"419",
|
189
|
+
"Connection"=>"Keep-Alive",
|
190
|
+
"Accept"=>"image/jpeg, image/png"
|
191
|
+
})
|
192
|
+
end
|
173
193
|
end
|
174
194
|
|
175
195
|
it "should return recorded body" do
|
176
|
-
@response.body.size.
|
196
|
+
expect(@response.body.size).to eq(419)
|
177
197
|
end
|
178
198
|
|
179
199
|
it "should return recorded status" do
|
180
|
-
@response.status.
|
200
|
+
expect(@response.status).to eq("202")
|
181
201
|
end
|
182
202
|
|
183
|
-
it "should return recorded status message", :
|
184
|
-
@response.message.
|
203
|
+
it "should return recorded status message", unless: (adapter_info.include?(:no_status_message)) do
|
204
|
+
expect(@response.message).to eq("OK")
|
185
205
|
end
|
186
206
|
|
187
207
|
it "should ensure file is closed" do
|
188
|
-
@file.
|
208
|
+
expect(@file).to be_closed
|
189
209
|
end
|
190
210
|
end
|
191
211
|
|
192
212
|
describe "when response was declared as a string with a raw response" do
|
193
213
|
before(:each) do
|
194
|
-
@input = File.
|
214
|
+
@input = File.read(CURL_EXAMPLE_OUTPUT_PATH)
|
195
215
|
stub_request(:get, "www.example.com").to_return(@input)
|
196
216
|
@response = http_request(:get, "http://www.example.com/")
|
197
217
|
end
|
198
218
|
|
199
219
|
it "should return recorded headers" do
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
220
|
+
if adapter_info.include?(:no_content_length_header)
|
221
|
+
expect(@response.headers).to eq({
|
222
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
223
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
224
|
+
"Connection"=>"Keep-Alive",
|
225
|
+
"Accept"=>"image/jpeg, image/png"
|
226
|
+
})
|
227
|
+
else
|
228
|
+
expect(@response.headers).to eq({
|
229
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
230
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
231
|
+
"Content-Length"=>"419",
|
232
|
+
"Connection"=>"Keep-Alive",
|
233
|
+
"Accept"=>"image/jpeg, image/png"
|
234
|
+
})
|
235
|
+
end
|
207
236
|
end
|
208
237
|
|
209
238
|
it "should return recorded body" do
|
210
|
-
@response.body.size.
|
239
|
+
expect(@response.body.size).to eq(419)
|
211
240
|
end
|
212
241
|
|
213
242
|
it "should return recorded status" do
|
214
|
-
@response.status.
|
243
|
+
expect(@response.status).to eq("202")
|
215
244
|
end
|
216
245
|
|
217
|
-
it "should return recorded status message", :
|
218
|
-
@response.message.
|
246
|
+
it "should return recorded status message", unless: (adapter_info.include?(:no_status_message)) do
|
247
|
+
expect(@response.message).to eq("OK")
|
219
248
|
end
|
220
249
|
end
|
221
250
|
|
@@ -228,65 +257,78 @@ shared_context "declared responses" do |*adapter_info|
|
|
228
257
|
|
229
258
|
it "should return response from evaluated file" do
|
230
259
|
stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
|
231
|
-
http_request(:get, "http://www.example.com/").body.size.
|
260
|
+
expect(http_request(:get, "http://www.example.com/").body.size).to eq(419)
|
232
261
|
end
|
233
262
|
|
234
263
|
it "should return response from evaluated string" do
|
235
264
|
stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
|
236
|
-
http_request(:get, "http://www.example.com/").body.size.
|
265
|
+
expect(http_request(:get, "http://www.example.com/").body.size).to eq(419)
|
237
266
|
end
|
238
267
|
end
|
239
268
|
|
240
269
|
describe "when response is declared as an Rack app" do
|
241
270
|
it "should return response returned by the rack app" do
|
242
271
|
stub_request(:any, "http://www.example.com/greet").to_rack(MyRackApp)
|
243
|
-
http_request(:post, 'http://www.example.com/greet', :
|
272
|
+
expect(http_request(:post, 'http://www.example.com/greet', body: 'name=Jimmy').body).to eq('Good to meet you, Jimmy!')
|
244
273
|
end
|
245
274
|
|
246
275
|
it "should pass along the port number to the rack app" do
|
247
276
|
stub_request(:get, "http://www.example.com/compute").to_rack(MyRackApp)
|
248
|
-
http_request(:get, "http://www.example.com/compute").status.
|
277
|
+
expect(http_request(:get, "http://www.example.com/compute").status).to eq("200")
|
278
|
+
end
|
279
|
+
|
280
|
+
it "preserves content-type header when proxying to a rack app" do
|
281
|
+
stub_request(:any, //).to_rack(lambda {|req| [200, {}, ["OK"]] })
|
282
|
+
|
283
|
+
url = "https://google.com/hi/there"
|
284
|
+
headers = {
|
285
|
+
"Accept" => "application/json",
|
286
|
+
"Content-Type" => "application/json"
|
287
|
+
}
|
288
|
+
|
289
|
+
http_request(:get, url, headers: headers)
|
290
|
+
expect(WebMock).to have_requested(:get, url).with(headers: headers)
|
249
291
|
end
|
250
292
|
end
|
251
293
|
|
252
294
|
describe "when sequences of responses are declared" do
|
253
295
|
it "should return responses one by one if declared in array" do
|
254
|
-
stub_request(:get, "www.example.com").to_return([ {:
|
255
|
-
http_request(:get, "http://www.example.com/").body.
|
256
|
-
http_request(:get, "http://www.example.com/").body.
|
257
|
-
http_request(:get, "http://www.example.com/").body.
|
296
|
+
stub_request(:get, "www.example.com").to_return([ {body: "1"}, {body: "2"}, {body: "3"} ])
|
297
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
298
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
299
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("3")
|
258
300
|
end
|
259
301
|
|
260
302
|
it "should repeat returning last declared response from a sequence after all responses were returned" do
|
261
|
-
stub_request(:get, "www.example.com").to_return([ {:
|
262
|
-
http_request(:get, "http://www.example.com/").body.
|
263
|
-
http_request(:get, "http://www.example.com/").body.
|
264
|
-
http_request(:get, "http://www.example.com/").body.
|
303
|
+
stub_request(:get, "www.example.com").to_return([ {body: "1"}, {body: "2"} ])
|
304
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
305
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
306
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
265
307
|
end
|
266
308
|
|
267
309
|
it "should return responses one by one if declared as comma separated params" do
|
268
|
-
stub_request(:get, "www.example.com").to_return({:
|
269
|
-
http_request(:get, "http://www.example.com/").body.
|
270
|
-
http_request(:get, "http://www.example.com/").body.
|
271
|
-
http_request(:get, "http://www.example.com/").body.
|
310
|
+
stub_request(:get, "www.example.com").to_return({body: "1"}, {body: "2"}, {body: "3"})
|
311
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
312
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
313
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("3")
|
272
314
|
end
|
273
315
|
|
274
316
|
it "should return responses one by one if declared with several to_return invokations" do
|
275
317
|
stub_request(:get, "www.example.com").
|
276
|
-
to_return({:
|
277
|
-
to_return({:
|
278
|
-
to_return({:
|
279
|
-
http_request(:get, "http://www.example.com/").body.
|
280
|
-
http_request(:get, "http://www.example.com/").body.
|
281
|
-
http_request(:get, "http://www.example.com/").body.
|
318
|
+
to_return({body: "1"}).
|
319
|
+
to_return({body: "2"}).
|
320
|
+
to_return({body: "3"})
|
321
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
322
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
323
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("3")
|
282
324
|
end
|
283
325
|
|
284
326
|
it "should return responses one by one if declared with to_return invocations separated with then syntactic sugar" do
|
285
|
-
stub_request(:get, "www.example.com").to_return({:
|
286
|
-
to_return({:
|
287
|
-
http_request(:get, "http://www.example.com/").body.
|
288
|
-
http_request(:get, "http://www.example.com/").body.
|
289
|
-
http_request(:get, "http://www.example.com/").body.
|
327
|
+
stub_request(:get, "www.example.com").to_return({body: "1"}).then.
|
328
|
+
to_return({body: "2"}).then.to_return({body: "3"})
|
329
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
330
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
331
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("3")
|
290
332
|
end
|
291
333
|
|
292
334
|
end
|
@@ -294,38 +336,38 @@ shared_context "declared responses" do |*adapter_info|
|
|
294
336
|
describe "when responses are declared to return more than once" do
|
295
337
|
it "should repeat one response declared number of times" do
|
296
338
|
stub_request(:get, "www.example.com").
|
297
|
-
to_return({:
|
298
|
-
to_return({:
|
299
|
-
http_request(:get, "http://www.example.com/").body.
|
300
|
-
http_request(:get, "http://www.example.com/").body.
|
301
|
-
http_request(:get, "http://www.example.com/").body.
|
339
|
+
to_return({body: "1"}).times(2).
|
340
|
+
to_return({body: "2"})
|
341
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
342
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
343
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
302
344
|
end
|
303
345
|
|
304
346
|
|
305
347
|
it "should repeat sequence of response declared number of times" do
|
306
348
|
stub_request(:get, "www.example.com").
|
307
|
-
to_return({:
|
308
|
-
to_return({:
|
309
|
-
http_request(:get, "http://www.example.com/").body.
|
310
|
-
http_request(:get, "http://www.example.com/").body.
|
311
|
-
http_request(:get, "http://www.example.com/").body.
|
312
|
-
http_request(:get, "http://www.example.com/").body.
|
313
|
-
http_request(:get, "http://www.example.com/").body.
|
349
|
+
to_return({body: "1"}, {body: "2"}).times(2).
|
350
|
+
to_return({body: "3"})
|
351
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
352
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
353
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
354
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
355
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("3")
|
314
356
|
end
|
315
357
|
|
316
358
|
|
317
359
|
it "should repeat infinitely last response even if number of declared times is lower" do
|
318
360
|
stub_request(:get, "www.example.com").
|
319
|
-
to_return({:
|
320
|
-
http_request(:get, "http://www.example.com/").body.
|
321
|
-
http_request(:get, "http://www.example.com/").body.
|
322
|
-
http_request(:get, "http://www.example.com/").body.
|
361
|
+
to_return({body: "1"}).times(2)
|
362
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
363
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
364
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
|
323
365
|
end
|
324
366
|
|
325
367
|
it "should give error if times is declared without specifying response" do
|
326
|
-
|
368
|
+
expect {
|
327
369
|
stub_request(:get, "www.example.com").times(3)
|
328
|
-
}.
|
370
|
+
}.to raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
|
329
371
|
end
|
330
372
|
|
331
373
|
end
|
@@ -334,33 +376,33 @@ shared_context "declared responses" do |*adapter_info|
|
|
334
376
|
it "should repeat raising exception declared number of times" do
|
335
377
|
stub_request(:get, "www.example.com").
|
336
378
|
to_raise(MyException).times(2).
|
337
|
-
to_return({:
|
338
|
-
|
379
|
+
to_return({body: "2"})
|
380
|
+
expect {
|
339
381
|
http_request(:get, "http://www.example.com/")
|
340
|
-
}.
|
341
|
-
|
382
|
+
}.to raise_error(MyException, "Exception from WebMock")
|
383
|
+
expect {
|
342
384
|
http_request(:get, "http://www.example.com/")
|
343
|
-
}.
|
344
|
-
http_request(:get, "http://www.example.com/").body.
|
385
|
+
}.to raise_error(MyException, "Exception from WebMock")
|
386
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
345
387
|
end
|
346
388
|
|
347
389
|
it "should repeat raising sequence of exceptions declared number of times" do
|
348
390
|
stub_request(:get, "www.example.com").
|
349
391
|
to_raise(MyException, ArgumentError).times(2).
|
350
|
-
to_return({:
|
351
|
-
|
392
|
+
to_return({body: "2"})
|
393
|
+
expect {
|
352
394
|
http_request(:get, "http://www.example.com/")
|
353
|
-
}.
|
354
|
-
|
395
|
+
}.to raise_error(MyException, "Exception from WebMock")
|
396
|
+
expect {
|
355
397
|
http_request(:get, "http://www.example.com/")
|
356
|
-
}.
|
357
|
-
|
398
|
+
}.to raise_error(ArgumentError)
|
399
|
+
expect {
|
358
400
|
http_request(:get, "http://www.example.com/")
|
359
|
-
}.
|
360
|
-
|
401
|
+
}.to raise_error(MyException, "Exception from WebMock")
|
402
|
+
expect {
|
361
403
|
http_request(:get, "http://www.example.com/")
|
362
|
-
}.
|
363
|
-
http_request(:get, "http://www.example.com/").body.
|
404
|
+
}.to raise_error(ArgumentError)
|
405
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
|
364
406
|
end
|
365
407
|
end
|
366
408
|
end
|