webmock 3.5.0 → 3.25.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.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +476 -2
  3. data/README.md +166 -42
  4. data/lib/webmock/api.rb +2 -0
  5. data/lib/webmock/assertion_failure.rb +2 -0
  6. data/lib/webmock/callback_registry.rb +2 -0
  7. data/lib/webmock/config.rb +2 -0
  8. data/lib/webmock/cucumber.rb +2 -0
  9. data/lib/webmock/deprecation.rb +2 -0
  10. data/lib/webmock/errors.rb +2 -0
  11. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +228 -0
  12. data/lib/webmock/http_lib_adapters/curb_adapter.rb +14 -3
  13. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +22 -9
  14. data/lib/webmock/http_lib_adapters/excon_adapter.rb +5 -0
  15. data/lib/webmock/http_lib_adapters/http_lib_adapter.rb +2 -0
  16. data/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +2 -0
  17. data/lib/webmock/http_lib_adapters/http_rb/client.rb +4 -1
  18. data/lib/webmock/http_lib_adapters/http_rb/request.rb +17 -5
  19. data/lib/webmock/http_lib_adapters/http_rb/response.rb +49 -5
  20. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +11 -3
  21. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +8 -2
  22. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +7 -5
  23. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +25 -7
  24. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +35 -15
  25. data/lib/webmock/http_lib_adapters/net_http.rb +59 -112
  26. data/lib/webmock/http_lib_adapters/net_http_response.rb +3 -1
  27. data/lib/webmock/http_lib_adapters/patron_adapter.rb +4 -2
  28. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +18 -2
  29. data/lib/webmock/matchers/any_arg_matcher.rb +2 -0
  30. data/lib/webmock/matchers/hash_argument_matcher.rb +2 -0
  31. data/lib/webmock/matchers/hash_excluding_matcher.rb +2 -0
  32. data/lib/webmock/matchers/hash_including_matcher.rb +2 -0
  33. data/lib/webmock/minitest.rb +2 -0
  34. data/lib/webmock/rack_response.rb +5 -1
  35. data/lib/webmock/request_body_diff.rb +3 -1
  36. data/lib/webmock/request_execution_verifier.rb +2 -0
  37. data/lib/webmock/request_pattern.rb +118 -61
  38. data/lib/webmock/request_registry.rb +2 -0
  39. data/lib/webmock/request_signature.rb +4 -2
  40. data/lib/webmock/request_signature_snippet.rb +2 -0
  41. data/lib/webmock/request_stub.rb +34 -0
  42. data/lib/webmock/response.rb +22 -14
  43. data/lib/webmock/responses_sequence.rb +2 -0
  44. data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +2 -0
  45. data/lib/webmock/rspec/matchers/webmock_matcher.rb +2 -0
  46. data/lib/webmock/rspec/matchers.rb +2 -0
  47. data/lib/webmock/rspec.rb +4 -1
  48. data/lib/webmock/stub_registry.rb +28 -11
  49. data/lib/webmock/stub_request_snippet.rb +2 -0
  50. data/lib/webmock/test_unit.rb +3 -3
  51. data/lib/webmock/util/hash_counter.rb +12 -6
  52. data/lib/webmock/util/hash_keys_stringifier.rb +2 -0
  53. data/lib/webmock/util/hash_validator.rb +2 -0
  54. data/lib/webmock/util/headers.rb +23 -10
  55. data/lib/webmock/util/parsers/json.rb +72 -0
  56. data/lib/webmock/util/parsers/parse_error.rb +7 -0
  57. data/lib/webmock/util/parsers/xml.rb +16 -0
  58. data/lib/webmock/util/query_mapper.rb +6 -2
  59. data/lib/webmock/util/uri.rb +11 -9
  60. data/lib/webmock/util/values_stringifier.rb +2 -0
  61. data/lib/webmock/util/version_checker.rb +7 -5
  62. data/lib/webmock/version.rb +3 -1
  63. data/lib/webmock/webmock.rb +22 -3
  64. data/lib/webmock.rb +5 -2
  65. metadata +99 -175
  66. data/.gemtest +0 -0
  67. data/.gitignore +0 -34
  68. data/.rspec-tm +0 -2
  69. data/.travis.yml +0 -21
  70. data/Gemfile +0 -9
  71. data/Rakefile +0 -30
  72. data/lib/webmock/util/json.rb +0 -67
  73. data/minitest/test_helper.rb +0 -34
  74. data/minitest/test_webmock.rb +0 -9
  75. data/minitest/webmock_spec.rb +0 -60
  76. data/spec/acceptance/curb/curb_spec.rb +0 -481
  77. data/spec/acceptance/curb/curb_spec_helper.rb +0 -147
  78. data/spec/acceptance/em_http_request/em_http_request_spec.rb +0 -406
  79. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +0 -77
  80. data/spec/acceptance/excon/excon_spec.rb +0 -77
  81. data/spec/acceptance/excon/excon_spec_helper.rb +0 -50
  82. data/spec/acceptance/http_rb/http_rb_spec.rb +0 -82
  83. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +0 -54
  84. data/spec/acceptance/httpclient/httpclient_spec.rb +0 -217
  85. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +0 -57
  86. data/spec/acceptance/manticore/manticore_spec.rb +0 -56
  87. data/spec/acceptance/manticore/manticore_spec_helper.rb +0 -35
  88. data/spec/acceptance/net_http/net_http_shared.rb +0 -153
  89. data/spec/acceptance/net_http/net_http_spec.rb +0 -331
  90. data/spec/acceptance/net_http/net_http_spec_helper.rb +0 -64
  91. data/spec/acceptance/net_http/real_net_http_spec.rb +0 -20
  92. data/spec/acceptance/patron/patron_spec.rb +0 -125
  93. data/spec/acceptance/patron/patron_spec_helper.rb +0 -54
  94. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +0 -313
  95. data/spec/acceptance/shared/callbacks.rb +0 -147
  96. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +0 -36
  97. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +0 -95
  98. data/spec/acceptance/shared/precedence_of_stubs.rb +0 -15
  99. data/spec/acceptance/shared/request_expectations.rb +0 -923
  100. data/spec/acceptance/shared/returning_declared_responses.rb +0 -388
  101. data/spec/acceptance/shared/stubbing_requests.rb +0 -638
  102. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +0 -135
  103. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +0 -60
  104. data/spec/acceptance/webmock_shared.rb +0 -41
  105. data/spec/fixtures/test.txt +0 -1
  106. data/spec/quality_spec.rb +0 -84
  107. data/spec/spec_helper.rb +0 -48
  108. data/spec/support/example_curl_output.txt +0 -22
  109. data/spec/support/failures.rb +0 -9
  110. data/spec/support/my_rack_app.rb +0 -53
  111. data/spec/support/network_connection.rb +0 -19
  112. data/spec/support/webmock_server.rb +0 -69
  113. data/spec/unit/api_spec.rb +0 -175
  114. data/spec/unit/errors_spec.rb +0 -129
  115. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +0 -17
  116. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +0 -12
  117. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +0 -61
  118. data/spec/unit/matchers/hash_including_matcher_spec.rb +0 -87
  119. data/spec/unit/rack_response_spec.rb +0 -112
  120. data/spec/unit/request_body_diff_spec.rb +0 -90
  121. data/spec/unit/request_execution_verifier_spec.rb +0 -208
  122. data/spec/unit/request_pattern_spec.rb +0 -596
  123. data/spec/unit/request_registry_spec.rb +0 -95
  124. data/spec/unit/request_signature_snippet_spec.rb +0 -89
  125. data/spec/unit/request_signature_spec.rb +0 -155
  126. data/spec/unit/request_stub_spec.rb +0 -199
  127. data/spec/unit/response_spec.rb +0 -282
  128. data/spec/unit/stub_registry_spec.rb +0 -103
  129. data/spec/unit/stub_request_snippet_spec.rb +0 -115
  130. data/spec/unit/util/hash_counter_spec.rb +0 -39
  131. data/spec/unit/util/hash_keys_stringifier_spec.rb +0 -27
  132. data/spec/unit/util/headers_spec.rb +0 -28
  133. data/spec/unit/util/json_spec.rb +0 -33
  134. data/spec/unit/util/query_mapper_spec.rb +0 -150
  135. data/spec/unit/util/uri_spec.rb +0 -299
  136. data/spec/unit/util/version_checker_spec.rb +0 -65
  137. data/spec/unit/webmock_spec.rb +0 -11
  138. data/test/http_request.rb +0 -24
  139. data/test/shared_test.rb +0 -108
  140. data/test/test_helper.rb +0 -23
  141. data/test/test_webmock.rb +0 -6
  142. data/webmock.gemspec +0 -46
@@ -1,313 +0,0 @@
1
- shared_context "allowing and disabling net connect" do |*adapter_info|
2
- describe "when net connect" do
3
- describe "is allowed", net_connect: true do
4
- before(:each) do
5
- WebMock.allow_net_connect!
6
- end
7
-
8
- it "should make a real web request if request is not stubbed" do
9
- expect(http_request(:get, webmock_server_url).status).to eq("200")
10
- end
11
-
12
- it "should make a real https request if request is not stubbed" do
13
- unless http_library == :httpclient
14
- result = http_request(:get, "https://www.google.com/").body
15
- if result.respond_to? :encode
16
- result = result.encode(
17
- 'UTF-8',
18
- 'binary',
19
- invalid: :replace,
20
- undef: :replace,
21
- replace: ''
22
- )
23
- end
24
- expect(result).to match(/.*google.*/)
25
- end
26
- end
27
-
28
- it "should return stubbed response if request was stubbed" do
29
- stub_request(:get, "www.example.com").to_return(body: "abc")
30
- expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
31
- end
32
- end
33
-
34
- describe "is not allowed" do
35
- before(:each) do
36
- WebMock.disable_net_connect!
37
- end
38
-
39
- it "should return stubbed response if request was stubbed" do
40
- stub_request(:get, "www.example.com").to_return(body: "abc")
41
- expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
42
- end
43
-
44
- it "should return stubbed response if request with path was stubbed" do
45
- stub_request(:get, "www.example.com/hello_world").to_return(body: "abc")
46
- expect(http_request(:get, "http://www.example.com/hello_world").body).to eq("abc")
47
- end
48
-
49
- it "should raise exception if request was not stubbed" do
50
- expect {
51
- http_request(:get, "http://www.example.com/")
52
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
53
- end
54
- end
55
-
56
- describe "is not allowed with exception for localhost" do
57
- before(:each) do
58
- WebMock.disable_net_connect!(allow_localhost: true)
59
- end
60
-
61
- it "should return stubbed response if request was stubbed" do
62
- stub_request(:get, "www.example.com").to_return(body: "abc")
63
- expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
64
- end
65
-
66
- it "should raise exception if request was not stubbed" do
67
- expect {
68
- http_request(:get, "http://www.example.com/")
69
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
70
- end
71
-
72
- it "should make a real request to localhost" do
73
- expect {
74
- http_request(:get, "http://localhost:12345/")
75
- }.to raise_error(connection_refused_exception_class)
76
- end
77
-
78
- it "should make a real request to 127.0.0.1" do
79
- expect {
80
- http_request(:get, "http://127.0.0.1:12345/")
81
- }.to raise_error(connection_refused_exception_class)
82
- end
83
-
84
- it "should make a real request to 0.0.0.0" do
85
- expect {
86
- http_request(:get, "http://0.0.0.0:12345/")
87
- }.to raise_error(connection_refused_exception_class)
88
- end
89
- end
90
-
91
- describe "is not allowed, with exceptions" do
92
- describe "allowing by host string" do
93
- before :each do
94
- WebMock.disable_net_connect!(allow: 'httpstat.us')
95
- end
96
-
97
- context "when the host is not allowed" do
98
- it "should return stubbed response if request was stubbed" do
99
- stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
100
- expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
101
- end
102
-
103
- it "should raise exception if request was not stubbed" do
104
- expect {
105
- http_request(:get, 'http://disallowed.example.com/')
106
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
107
- end
108
- end
109
-
110
- context "when the host is allowed" do
111
- it "should return stubbed response if request was stubbed" do
112
- stub_request(:get, 'httpstat.us/200').to_return(body: "abc")
113
- expect(http_request(:get, "http://httpstat.us/200").body).to eq("abc")
114
- end
115
-
116
- # WARNING: this makes a real HTTP request!
117
- it "should make a real request to allowed host", net_connect: true do
118
- expect(http_request(:get, "http://httpstat.us/200").status).to eq('200')
119
- end
120
- end
121
- end
122
-
123
- describe "allowing by host:port string" do
124
- def replace_with_different_port(uri)
125
- uri.sub(%r{:(\d+)}){|m0, m1| ':' + ($~[1].to_i + 1).to_s }
126
- end
127
-
128
- let(:allowed_host_with_port) { WebMockServer.instance.host_with_port }
129
- let(:disallowed_host_with_port) { replace_with_different_port(allowed_host_with_port) }
130
-
131
- before :each do
132
- WebMock.disable_net_connect!(allow: allowed_host_with_port)
133
- end
134
-
135
- context "when the host is not allowed" do
136
- it "should return stubbed response if request was stubbed" do
137
- request_url = "http://#{disallowed_host_with_port}/foo"
138
- stub_request(:get, request_url).to_return(body: "abc")
139
- expect(http_request(:get, request_url).body).to eq("abc")
140
- end
141
-
142
- it "should raise exception if request was not stubbed" do
143
- request_url = "http://#{disallowed_host_with_port}/foo"
144
- expect {
145
- http_request(:get, request_url)
146
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET #{request_url}))
147
- end
148
- end
149
-
150
- context "when the host is allowed" do
151
- it "should return stubbed response if request was stubbed" do
152
- request_url = "http://#{allowed_host_with_port}/foo"
153
- stub_request(:get, request_url).to_return(body: "abc")
154
- expect(http_request(:get, request_url).body).to eq('abc')
155
- end
156
-
157
- it "should make a real request to allowed host", net_connect: true do
158
- request_url = "http://#{allowed_host_with_port}/foo"
159
- expect(http_request(:get, request_url).status).to eq('200')
160
- end
161
- end
162
- end
163
-
164
- describe "allowing by scheme:host string" do
165
- before :each do
166
- WebMock.disable_net_connect!(allow: 'https://www.google.pl')
167
- end
168
-
169
- context "when the host and scheme is not allowed" do
170
- it "should return stubbed response if request was stubbed" do
171
- stub_request(:get, 'https://disallowed.example.com/foo').to_return(body: "abc")
172
- expect(http_request(:get, 'https://disallowed.example.com/foo').body).to eq("abc")
173
- end
174
-
175
- it "should raise exception if request was not stubbed" do
176
- expect {
177
- http_request(:get, 'https://disallowed.example.com/')
178
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET https://disallowed.example.com))
179
- end
180
-
181
- it "should raise exception if request was made to different port" do
182
- expect {
183
- http_request(:get, 'https://www.google.pl:80/')
184
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET https://www.google.pl:80))
185
- end
186
-
187
- it "should raise exception if request was made to different scheme" do
188
- expect {
189
- http_request(:get, 'http://www.google.pl/')
190
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.google.pl))
191
- end
192
- end
193
-
194
- context "when the host is allowed" do
195
- it "should return stubbed response if request was stubbed" do
196
- stub_request(:get, 'https://www.google.pl').to_return(body: "abc")
197
- expect(http_request(:get, "https://www.google.pl/").body).to eq("abc")
198
- end
199
-
200
- it "should make a real request to allowed host with scheme", net_connect: true do
201
- method = http_library == :httpclient ? :head : :get #https://github.com/nahi/httpclient/issues/299
202
- expect(http_request(method, "https://www.google.pl/").status).to eq('200')
203
- end
204
-
205
- it "should make a real request to allowed host with scheme and port", net_connect: true do
206
- method = http_library == :httpclient ? :head : :get
207
- expect(http_request(method, "https://www.google.pl:443/").status).to eq('200')
208
- end
209
- end
210
- end
211
-
212
- describe "allowing by regular expression" do
213
- before :each do
214
- WebMock.disable_net_connect!(allow: %r{httpstat})
215
- end
216
-
217
- context "when the host is not allowed" do
218
- it "should return stubbed response if request was stubbed" do
219
- stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
220
- expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
221
- end
222
-
223
- it "should raise exception if request was not stubbed" do
224
- expect {
225
- http_request(:get, 'http://disallowed.example.com/')
226
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
227
- end
228
- end
229
-
230
- context "when the host is allowed" do
231
- it "should return stubbed response if request was stubbed" do
232
- stub_request(:get, 'httpstat.us/200').to_return(body: "abc")
233
- expect(http_request(:get, "http://httpstat.us/200").body).to eq("abc")
234
- end
235
-
236
- # WARNING: this makes a real HTTP request!
237
- it "should make a real request to allowed host", net_connect: true do
238
- expect(http_request(:get, "http://httpstat.us/200").status).to eq('200')
239
- end
240
-
241
- it "should make a real request if request is allowed by path regexp and url contains default port", net_connect: true do
242
- WebMock.disable_net_connect!(allow: %r{www.google.pl/webhp})
243
- method = http_library == :httpclient ? :head : :get
244
- expect(http_request(method, 'https://www.google.pl:443/webhp').status).to eq('200')
245
- end
246
- end
247
- end
248
-
249
- describe "allowing by a callable" do
250
- before :each do
251
- WebMock.disable_net_connect!(allow: lambda{|url| url.to_str.include?('httpstat') })
252
- end
253
-
254
- context "when the host is not allowed" do
255
- it "should return stubbed response if request was stubbed" do
256
- stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
257
- expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
258
- end
259
-
260
- it "should raise exception if request was not stubbed" do
261
- expect {
262
- http_request(:get, 'http://disallowed.example.com/')
263
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
264
- end
265
- end
266
-
267
- context "when the host is allowed" do
268
- it "should return stubbed response if request was stubbed" do
269
- stub_request(:get, 'httpstat.us/200').to_return(body: "abc")
270
- expect(http_request(:get, "http://httpstat.us/200").body).to eq("abc")
271
- end
272
-
273
- # WARNING: this makes a real HTTP request!
274
- it "should make a real request to allowed host", net_connect: true do
275
- expect(http_request(:get, "http://httpstat.us/200").status).to eq('200')
276
- end
277
- end
278
- end
279
-
280
- describe "allowing by a list of the above" do
281
- before :each do
282
- WebMock.disable_net_connect!(allow: [lambda{|_| false }, %r{foobar}, 'httpstat.us'])
283
- end
284
-
285
- context "when the host is not allowed" do
286
- it "should return stubbed response if request was stubbed" do
287
- stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
288
- expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
289
- end
290
-
291
- it "should raise exception if request was not stubbed" do
292
- expect {
293
- http_request(:get, 'http://disallowed.example.com/')
294
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
295
- end
296
- end
297
-
298
- context "when the host is allowed" do
299
- it "should return stubbed response if request was stubbed" do
300
- stub_request(:get, 'httpstat.us/200').to_return(body: "abc")
301
- expect(http_request(:get, "http://httpstat.us/200").body).to eq("abc")
302
- end
303
-
304
- # WARNING: this makes a real HTTP request!
305
- it "should make a real request to allowed host", net_connect: true do
306
- expect(http_request(:get, "http://httpstat.us/200").status).to eq('200')
307
- end
308
- end
309
- end
310
-
311
- end
312
- end
313
- end
@@ -1,147 +0,0 @@
1
- shared_context "callbacks" do |*adapter_info|
2
- describe "when after_request callback is declared" do
3
- before(:each) do
4
- @called = nil
5
- WebMock.reset_callbacks
6
- stub_request(:get, "http://www.example.com")
7
- end
8
-
9
- it "should not invoke callback unless request is made" do
10
- WebMock.after_request {
11
- @called = true
12
- }
13
- expect(@called).to eq(nil)
14
- end
15
-
16
- it "should invoke a callback after request is made" do
17
- WebMock.after_request {
18
- @called = true
19
- }
20
- http_request(:get, "http://www.example.com/")
21
- expect(@called).to eq(true)
22
- end
23
-
24
- it "should not invoke a callback if this http library should be ignored" do
25
- WebMock.after_request(except: [http_library()]) {
26
- @called = true
27
- }
28
- http_request(:get, "http://www.example.com/")
29
- expect(@called).to eq(nil)
30
- end
31
-
32
- it "should invoke a callback even if other http libraries should be ignored" do
33
- WebMock.after_request(except: [:other_lib]) {
34
- @called = true
35
- }
36
- http_request(:get, "http://www.example.com/")
37
- expect(@called).to eq(true)
38
- end
39
-
40
- it "should pass request signature to the callback" do
41
- WebMock.after_request(except: [:other_lib]) do |request_signature, _|
42
- @request_signature = request_signature
43
- end
44
- http_request(:get, "http://www.example.com/")
45
- expect(@request_signature.uri.to_s).to eq("http://www.example.com:80/")
46
- end
47
-
48
- after(:each) do
49
- WebMock::StubRegistry.instance.global_stubs.clear
50
- end
51
-
52
- it 'passes the same request signature instance to the callback that was passed to the global stub callback' do
53
- global_stub_request_sig = after_request_request_sig = nil
54
- WebMock.globally_stub_request do |request_sig|
55
- global_stub_request_sig = request_sig
56
- nil
57
- end
58
-
59
- WebMock.after_request do |request_sig, _|
60
- after_request_request_sig = request_sig
61
- end
62
-
63
- http_request(:get, "http://www.example.com/")
64
- expect(global_stub_request_sig).to be(after_request_request_sig)
65
- end
66
-
67
- context "passing response to callback" do
68
- context "when request is stubbed" do
69
- before(:each) do
70
- stub_request(:get, "http://www.example.com").
71
- to_return(
72
- status: [200, "hello"],
73
- headers: {'Content-Length' => '666', 'Hello' => 'World'},
74
- body: "foo bar"
75
- )
76
- WebMock.after_request(except: [:other_lib]) do |_, response|
77
- @response = response
78
- end
79
- http_request(:get, "http://www.example.com/")
80
- end
81
-
82
- it "should pass response to callback with the status and message" do
83
- expect(@response.status).to eq([200, "hello"])
84
- end
85
-
86
- it "should pass response to callback with headers" do
87
- expect(@response.headers).to eq({
88
- 'Content-Length' => '666',
89
- 'Hello' => 'World'
90
- })
91
- end
92
-
93
- it "should pass response to callback with body" do
94
- expect(@response.body).to eq("foo bar")
95
- end
96
- end
97
-
98
- describe "when request is not stubbed", net_connect: true do
99
- before(:each) do
100
- WebMock.reset!
101
- WebMock.allow_net_connect!
102
- WebMock.after_request(except: [:other_lib]) do |_, response|
103
- @response = response
104
- end
105
- http_request(:get, "http://httpstat.us/201", headers: { "Accept" => "*" })
106
- end
107
-
108
- it "should pass real response to callback with status and message" do
109
- expect(@response.status[0]).to eq(201)
110
- expect(@response.status[1]).to eq("Created") unless adapter_info.include?(:no_status_message)
111
- end
112
-
113
- it "should pass real response to callback with headers" do
114
- expect(@response.headers["Content-Length"]).to eq("11")
115
- end
116
-
117
- it "should pass response to callback with body" do
118
- expect(@response.body.size).to eq(11)
119
- end
120
- end
121
- end
122
-
123
- it "should invoke multiple callbacks in order of their declarations" do
124
- WebMock.after_request { @called = 1 }
125
- WebMock.after_request { @called += 1 }
126
- http_request(:get, "http://www.example.com/")
127
- expect(@called).to eq(2)
128
- end
129
-
130
- it "should invoke callbacks only for real requests if requested", net_connect: true do
131
- WebMock.after_request(real_requests_only: true) { @called = true }
132
- http_request(:get, "http://www.example.com/")
133
- expect(@called).to eq(nil)
134
- WebMock.allow_net_connect!
135
- http_request(:get, "http://www.example.net/")
136
- expect(@called).to eq(true)
137
- end
138
-
139
- it "should not invoke any callbacks after callbacks were reset" do
140
- WebMock.after_request { @called = 1 }
141
- WebMock.reset_callbacks
142
- stub_request(:get, "http://www.example.com/")
143
- http_request(:get, "http://www.example.com/")
144
- expect(@called).to eq(nil)
145
- end
146
- end
147
- end
@@ -1,36 +0,0 @@
1
- shared_context "complex cross-concern behaviors" do |*adapter_info|
2
- it 'allows a response with multiple values for the same header to be recorded and played back exactly as-is' do
3
- WebMock.allow_net_connect!
4
-
5
- recorded_response = nil
6
- WebMock.after_request { |_,r| recorded_response = r }
7
- real_response = http_request(:get, webmock_server_url)
8
-
9
- stub_request(:get, webmock_server_url).to_return(
10
- status: recorded_response.status,
11
- body: recorded_response.body,
12
- headers: recorded_response.headers
13
- )
14
-
15
- played_back_response = http_request(:get, webmock_server_url)
16
-
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
34
- end
35
- end
36
-
@@ -1,95 +0,0 @@
1
- shared_context "enabled and disabled webmock" do |*adapter_info|
2
- describe "when webmock is disabled" do
3
- before(:each) do
4
- WebMock.disable!
5
- end
6
- after(:each) do
7
- WebMock.enable!
8
- end
9
- include_context "disabled WebMock"
10
- end
11
-
12
- describe "when webmock is enabled again" do
13
- before(:each) do
14
- WebMock.disable!
15
- WebMock.enable!
16
- end
17
- include_context "enabled WebMock"
18
- end
19
-
20
- describe "when webmock is disabled except this lib" do
21
- before(:each) do
22
- WebMock.disable!(except: [http_library])
23
- end
24
- after(:each) do
25
- WebMock.enable!
26
- end
27
- include_context "enabled WebMock"
28
- end
29
-
30
- describe "when webmock is enabled except this lib" do
31
- before(:each) do
32
- WebMock.disable!
33
- WebMock.enable!(except: [http_library])
34
- end
35
- after(:each) do
36
- WebMock.enable!
37
- end
38
- include_context "disabled WebMock"
39
- end
40
- end
41
-
42
- shared_context "disabled WebMock" do
43
- it "should not register executed requests" do
44
- http_request(:get, webmock_server_url)
45
- expect(a_request(:get, webmock_server_url)).not_to have_been_made
46
- end
47
-
48
- it "should not block unstubbed requests" do
49
- expect {
50
- http_request(:get, webmock_server_url)
51
- }.not_to raise_error
52
- end
53
-
54
- it "should return real response even if there are stubs" do
55
- stub_request(:get, /.*/).to_return(body: "x")
56
- expect(http_request(:get, webmock_server_url).body).to eq("hello world")
57
- end
58
-
59
- it "should not invoke any callbacks" do
60
- WebMock.reset_callbacks
61
- stub_request(:get, webmock_server_url)
62
- @called = nil
63
- WebMock.after_request { @called = 1 }
64
- http_request(:get, webmock_server_url)
65
- expect(@called).to eq(nil)
66
- end
67
- end
68
-
69
- shared_context "enabled WebMock" do
70
- it "should register executed requests" do
71
- WebMock.allow_net_connect!
72
- http_request(:get, webmock_server_url)
73
- expect(a_request(:get, webmock_server_url)).to have_been_made
74
- end
75
-
76
- it "should block unstubbed requests" do
77
- expect {
78
- http_request(:get, "http://www.example.com/")
79
- }.to raise_error(WebMock::NetConnectNotAllowedError)
80
- end
81
-
82
- it "should return stubbed response" do
83
- stub_request(:get, /.*/).to_return(body: "x")
84
- expect(http_request(:get, "http://www.example.com/").body).to eq("x")
85
- end
86
-
87
- it "should invoke callbacks" do
88
- WebMock.allow_net_connect!
89
- WebMock.reset_callbacks
90
- @called = nil
91
- WebMock.after_request { @called = 1 }
92
- http_request(:get, webmock_server_url)
93
- expect(@called).to eq(1)
94
- end
95
- end
@@ -1,15 +0,0 @@
1
- shared_context "precedence of stubs" do |*adapter_info|
2
- describe "when choosing a matching request stub" do
3
- it "should use the last declared matching request stub" do
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
- end
8
-
9
- it "should not be affected by the type of uri or request method" do
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
- end
14
- end
15
- end