webmock 3.14.0 → 3.24.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +207 -3
  3. data/README.md +65 -20
  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 +16 -4
  12. data/lib/webmock/http_lib_adapters/curb_adapter.rb +4 -2
  13. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +17 -7
  14. data/lib/webmock/http_lib_adapters/excon_adapter.rb +2 -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 +3 -3
  18. data/lib/webmock/http_lib_adapters/http_rb/request.rb +17 -5
  19. data/lib/webmock/http_lib_adapters/http_rb/response.rb +32 -9
  20. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +10 -2
  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 +26 -25
  24. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +2 -0
  25. data/lib/webmock/http_lib_adapters/net_http.rb +28 -117
  26. data/lib/webmock/http_lib_adapters/net_http_response.rb +2 -0
  27. data/lib/webmock/http_lib_adapters/patron_adapter.rb +3 -1
  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 +2 -0
  36. data/lib/webmock/request_execution_verifier.rb +2 -0
  37. data/lib/webmock/request_pattern.rb +35 -12
  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 +15 -13
  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 +2 -0
  48. data/lib/webmock/stub_registry.rb +2 -0
  49. data/lib/webmock/stub_request_snippet.rb +2 -0
  50. data/lib/webmock/test_unit.rb +2 -0
  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 +2 -0
  59. data/lib/webmock/util/uri.rb +3 -1
  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 +12 -0
  64. data/lib/webmock.rb +4 -2
  65. metadata +66 -182
  66. data/.gemtest +0 -0
  67. data/.github/workflows/CI.yml +0 -37
  68. data/.gitignore +0 -34
  69. data/.rspec-tm +0 -2
  70. data/Gemfile +0 -9
  71. data/Rakefile +0 -38
  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/async_http_client/async_http_client_spec.rb +0 -375
  77. data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +0 -73
  78. data/spec/acceptance/curb/curb_spec.rb +0 -499
  79. data/spec/acceptance/curb/curb_spec_helper.rb +0 -147
  80. data/spec/acceptance/em_http_request/em_http_request_spec.rb +0 -462
  81. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +0 -77
  82. data/spec/acceptance/excon/excon_spec.rb +0 -77
  83. data/spec/acceptance/excon/excon_spec_helper.rb +0 -52
  84. data/spec/acceptance/http_rb/http_rb_spec.rb +0 -93
  85. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +0 -54
  86. data/spec/acceptance/httpclient/httpclient_spec.rb +0 -217
  87. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +0 -57
  88. data/spec/acceptance/manticore/manticore_spec.rb +0 -107
  89. data/spec/acceptance/manticore/manticore_spec_helper.rb +0 -35
  90. data/spec/acceptance/net_http/net_http_shared.rb +0 -153
  91. data/spec/acceptance/net_http/net_http_spec.rb +0 -369
  92. data/spec/acceptance/net_http/net_http_spec_helper.rb +0 -64
  93. data/spec/acceptance/net_http/real_net_http_spec.rb +0 -20
  94. data/spec/acceptance/patron/patron_spec.rb +0 -125
  95. data/spec/acceptance/patron/patron_spec_helper.rb +0 -54
  96. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +0 -313
  97. data/spec/acceptance/shared/callbacks.rb +0 -148
  98. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +0 -36
  99. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +0 -95
  100. data/spec/acceptance/shared/precedence_of_stubs.rb +0 -15
  101. data/spec/acceptance/shared/request_expectations.rb +0 -930
  102. data/spec/acceptance/shared/returning_declared_responses.rb +0 -409
  103. data/spec/acceptance/shared/stubbing_requests.rb +0 -678
  104. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +0 -135
  105. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +0 -60
  106. data/spec/acceptance/webmock_shared.rb +0 -41
  107. data/spec/fixtures/test.txt +0 -1
  108. data/spec/quality_spec.rb +0 -84
  109. data/spec/spec_helper.rb +0 -48
  110. data/spec/support/example_curl_output.txt +0 -22
  111. data/spec/support/failures.rb +0 -9
  112. data/spec/support/my_rack_app.rb +0 -53
  113. data/spec/support/network_connection.rb +0 -19
  114. data/spec/support/webmock_server.rb +0 -70
  115. data/spec/unit/api_spec.rb +0 -175
  116. data/spec/unit/errors_spec.rb +0 -129
  117. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +0 -17
  118. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +0 -12
  119. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +0 -61
  120. data/spec/unit/matchers/hash_including_matcher_spec.rb +0 -87
  121. data/spec/unit/rack_response_spec.rb +0 -112
  122. data/spec/unit/request_body_diff_spec.rb +0 -90
  123. data/spec/unit/request_execution_verifier_spec.rb +0 -208
  124. data/spec/unit/request_pattern_spec.rb +0 -736
  125. data/spec/unit/request_registry_spec.rb +0 -95
  126. data/spec/unit/request_signature_snippet_spec.rb +0 -89
  127. data/spec/unit/request_signature_spec.rb +0 -155
  128. data/spec/unit/request_stub_spec.rb +0 -199
  129. data/spec/unit/response_spec.rb +0 -286
  130. data/spec/unit/stub_registry_spec.rb +0 -103
  131. data/spec/unit/stub_request_snippet_spec.rb +0 -115
  132. data/spec/unit/util/hash_counter_spec.rb +0 -39
  133. data/spec/unit/util/hash_keys_stringifier_spec.rb +0 -27
  134. data/spec/unit/util/headers_spec.rb +0 -28
  135. data/spec/unit/util/json_spec.rb +0 -33
  136. data/spec/unit/util/query_mapper_spec.rb +0 -157
  137. data/spec/unit/util/uri_spec.rb +0 -371
  138. data/spec/unit/util/version_checker_spec.rb +0 -65
  139. data/spec/unit/webmock_spec.rb +0 -60
  140. data/test/http_request.rb +0 -24
  141. data/test/shared_test.rb +0 -108
  142. data/test/test_helper.rb +0 -23
  143. data/test/test_webmock.rb +0 -12
  144. data/webmock.gemspec +0 -54
@@ -1,678 +0,0 @@
1
- # encoding: utf-8
2
-
3
- shared_examples_for "stubbing requests" do |*adapter_info|
4
- describe "when requests are stubbed" do
5
- describe "based on uri" do
6
- it "should return stubbed response even if request have escaped parameters" do
7
- stub_request(:get, "www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}").to_return(body: "abc")
8
- expect(http_request(:get, "http://www.example.com/hello%2B/?#{ESCAPED_PARAMS}").body).to eq("abc")
9
- end
10
-
11
- it "should return stubbed response even if query params have integer values" do
12
- stub_request(:get, "www.example.com").with(query: {"a" => 1}).to_return(body: "abc")
13
- expect(http_request(:get, "http://www.example.com/?a=1").body).to eq("abc")
14
- end
15
-
16
- it "should return stubbed response even if request has non escaped params" do
17
- stub_request(:get, "www.example.com/hello%2B/?#{ESCAPED_PARAMS}").to_return(body: "abc")
18
- expect(http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}").body).to eq("abc")
19
- end
20
-
21
- it "should return stubbed response for url with non utf query params" do
22
- param = 'aäoöuü'.encode('iso-8859-1')
23
- param = CGI.escape(param)
24
- stub_request(:get, "www.example.com/?#{param}").to_return(body: "abc")
25
- expect(http_request(:get, "http://www.example.com/?#{param}").body).to eq("abc")
26
- end
27
-
28
- it "should return stubbed response even if stub uri is declared as regexp and request params are escaped" do
29
- stub_request(:get, /.*x=ab c.*/).to_return(body: "abc")
30
- expect(http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body).to eq("abc")
31
- end
32
-
33
- it "should raise error specifying stubbing instructions with escaped characters in params if there is no matching stub" do
34
- begin
35
- http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}")
36
- rescue WebMock::NetConnectNotAllowedError => e
37
- expect(e.message).to match(/Unregistered request: GET http:\/\/www\.example\.com\/hello\+\/\?x=ab%20c&z='Stop!'%20said%20Fred%20m/m)
38
- expect(e.message).to match(/stub_request\(:get, "http:\/\/www\.example\.com\/hello\+\/\?x=ab%20c&z='Stop!'%20said%20Fred%20m"\)/m)
39
- end
40
-
41
- stub_request(:get, "http://www.example.com/hello+/?x=ab%20c&z='Stop!'%20said%20Fred%20m")
42
- http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}")
43
- end
44
- end
45
-
46
- describe "based on query params" do
47
- it "should return stubbed response when stub declares query params as a hash" do
48
- stub_request(:get, "www.example.com").with(query: {"a" => ["b x", "c d"]}).to_return(body: "abc")
49
- expect(http_request(:get, "http://www.example.com/?a[]=b+x&a[]=c%20d").body).to eq("abc")
50
- end
51
-
52
- it "should return stubbed response when stub declares query params as a hash" do
53
- stub_request(:get, "www.example.com").with(query: "a[]=b&a[]=c").to_return(body: "abc")
54
- expect(http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body).to eq("abc")
55
- end
56
-
57
- it "should return stubbed response when stub declares query params both in uri and as a hash" do
58
- stub_request(:get, "www.example.com/?x=3").with(query: {"a" => ["b", "c"]}).to_return(body: "abc")
59
- expect(http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c").body).to eq("abc")
60
- end
61
-
62
- it "should return stubbed response when stub expects only part of query params" do
63
- stub_request(:get, "www.example.com").with(query: hash_including({"a" => ["b", "c"]})).to_return(body: "abc")
64
- expect(http_request(:get, "http://www.example.com/?a[]=b&a[]=c&b=1").body).to eq("abc")
65
- end
66
-
67
- it 'should return stubbed response when stub expects exclude part of query params' do
68
- stub_request(:get, 'www.example.com').with(query: hash_excluding(a: ['b', 'c'])).to_return(body: 'abc')
69
- expect(http_request(:get, 'http://www.example.com/?a[]=c&a[]=d&b=1').body).to eq('abc')
70
- end
71
-
72
- it "should return stubbed response when stub expects an empty array" do
73
- stub_request(:get, 'www.example.com').with(query: { a: [] }).to_return(body: 'abc')
74
- expect(http_request(:get, 'http://www.example.com/?a[]').body).to eq('abc')
75
- end
76
- end
77
-
78
- describe "based on method" do
79
- it "should return stubbed response" do
80
- stub_request(:get, "www.example.com")
81
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
82
- end
83
-
84
- it "should match stubbed request when http request was made with method given as string" do
85
- stub_request(:get, "www.example.com")
86
- expect(http_request('get', "http://www.example.com/").status).to eq("200")
87
- end
88
-
89
- it "should raise error if stubbed request has different method" do
90
- stub_request(:get, "www.example.com")
91
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
92
- expect {
93
- http_request(:delete, "http://www.example.com/")
94
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: DELETE http://www.example.com/)
95
- )
96
- end
97
- end
98
-
99
- describe "based on body" do
100
- it "should match requests if body is the same" do
101
- stub_request(:post, "www.example.com").with(body: "abc")
102
- expect(http_request(
103
- :post, "http://www.example.com/",
104
- body: "abc").status).to eq("200")
105
- end
106
-
107
- it "should match requests if body is not set in the stub" do
108
- stub_request(:post, "www.example.com")
109
- expect(http_request(
110
- :post, "http://www.example.com/",
111
- body: "abc").status).to eq("200")
112
- end
113
-
114
- it "should not match requests if body is different" do
115
- stub_request(:post, "www.example.com").with(body: "abc")
116
- expect {
117
- http_request(:post, "http://www.example.com/", body: "def")
118
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'def'))
119
- end
120
-
121
- describe "with regular expressions" do
122
- it "should match requests if body matches regexp" do
123
- stub_request(:post, "www.example.com").with(body: /\d+abc$/)
124
- expect(http_request(
125
- :post, "http://www.example.com/",
126
- body: "123abc").status).to eq("200")
127
- end
128
-
129
- it "should not match requests if body doesn't match regexp" do
130
- stub_request(:post, "www.example.com").with(body: /^abc/)
131
- expect {
132
- http_request(:post, "http://www.example.com/", body: "xabc")
133
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'xabc'))
134
- end
135
- end
136
-
137
- describe "when body is declared as a hash" do
138
- before(:each) do
139
- stub_request(:post, "www.example.com").
140
- with(body: {:a => '1', :b => 'five x', 'c' => {'d' => ['e', 'f']} })
141
- end
142
-
143
- describe "for request with url encoded body" do
144
- it "should match request if hash matches body" do
145
- expect(http_request(
146
- :post, "http://www.example.com/",
147
- body: 'a=1&c[d][]=e&c[d][]=f&b=five+x').status).to eq("200")
148
- end
149
-
150
- it "should match request if hash matches body in different order of params" do
151
- expect(http_request(
152
- :post, "http://www.example.com/",
153
- body: 'a=1&c[d][]=e&b=five+x&c[d][]=f').status).to eq("200")
154
- end
155
-
156
- it "should not match if hash doesn't match url encoded body" do
157
- expect {
158
- http_request(
159
- :post, "http://www.example.com/",
160
- body: 'c[d][]=f&a=1&c[d][]=e')
161
- }.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'))
162
- end
163
-
164
- describe "for request with form url encoded body and content type" do
165
- it "should match if stubbed request body hash has string values matching string values in request body" do
166
- WebMock.reset!
167
- stub_request(:post, "www.example.com").with(body: {"foo" => '1'})
168
- expect(http_request(
169
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
170
- body: "foo=1").status).to eq("200")
171
- end
172
-
173
- it "should match if stubbed request body hash has NON string values matching string values in request body" do
174
- WebMock.reset!
175
- stub_request(:post, "www.example.com").with(body: {"foo" => 1})
176
- expect(http_request(
177
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
178
- body: "foo=1").status).to eq("200")
179
- end
180
-
181
- it "should match if stubbed request body is hash_included" do
182
- WebMock.reset!
183
- stub_request(:post, "www.example.com").with(body: {"foo" => hash_including("bar" => '1')})
184
- expect(http_request(
185
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
186
- body: "foo[bar]=1").status).to eq("200")
187
- end
188
- end
189
- end
190
-
191
-
192
- describe "for request with json body and content type is set to json" do
193
- it "should match if hash matches body" do
194
- expect(http_request(
195
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
196
- body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five x\"}").status).to eq("200")
197
- end
198
-
199
- it "should match if hash matches body in different form" do
200
- expect(http_request(
201
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
202
- body: "{\"a\":\"1\",\"b\":\"five x\",\"c\":{\"d\":[\"e\",\"f\"]}}").status).to eq("200")
203
- end
204
-
205
- it "should match if hash contains date string" do #Crack creates date object
206
- WebMock.reset!
207
- stub_request(:post, "www.example.com").
208
- with(body: {"foo" => "2010-01-01"})
209
- expect(http_request(
210
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
211
- body: "{\"foo\":\"2010-01-01\"}").status).to eq("200")
212
- end
213
-
214
- it "should match if any of the strings have spaces" do
215
- WebMock.reset!
216
- stub_request(:post, "www.example.com").with(body: {"foo" => "a b c"})
217
- expect(http_request(
218
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
219
- body: "{\"foo\":\"a b c\"}").status).to eq("200")
220
- end
221
-
222
- it "should match if stubbed request body hash has NON string values matching NON string values in request body" do
223
- WebMock.reset!
224
- stub_request(:post, "www.example.com").with(body: {"foo" => 1})
225
- expect(http_request(
226
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
227
- body: "{\"foo\":1}").status).to eq("200")
228
- end
229
-
230
- it "should not match if stubbed request body hash has string values matching NON string values in request body" do
231
- WebMock.reset!
232
- stub_request(:post, "www.example.com").with(body: {"foo" => '1'})
233
- expect{http_request(
234
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
235
- body: "{\"foo\":1}") }.to raise_error(WebMock::NetConnectNotAllowedError)
236
- end
237
-
238
- it "should not match if stubbed request body hash has NON string values matching string values in request body" do
239
- WebMock.reset!
240
- stub_request(:post, "www.example.com").with(body: {"foo" => 1})
241
- expect{http_request(
242
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
243
- body: "{\"foo\":\"1\"}") }.to raise_error(WebMock::NetConnectNotAllowedError)
244
- end
245
- end
246
-
247
- describe "for request with xml body and content type is set to xml" do
248
- before(:each) do
249
- WebMock.reset!
250
- stub_request(:post, "www.example.com").
251
- with(body: { "opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} }})
252
- end
253
-
254
- it "should match if hash matches body" do
255
- expect(http_request(
256
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
257
- body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status).to eq("200")
258
- end
259
-
260
- it "should match if hash matches body in different form" do
261
- expect(http_request(
262
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
263
- body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status).to eq("200")
264
- end
265
-
266
- it "should match if hash contains date string" do #Crack creates date object
267
- WebMock.reset!
268
- stub_request(:post, "www.example.com").
269
- with(body: {"opt" => {"foo" => "2010-01-01"}})
270
- expect(http_request(
271
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
272
- body: "<opt foo=\"2010-01-01\">\n</opt>\n").status).to eq("200")
273
- end
274
- end
275
- end
276
-
277
- describe "when body is declared as partial hash matcher" do
278
- subject(:request) { http_request( :post, "http://www.example.com/",
279
- body: 'a=1&c[d][]=e&c[d][]=f&b=five') }
280
-
281
- subject(:wrong_request) { http_request(:post, "http://www.example.com/",
282
- body: 'c[d][]=f&a=1&c[d][]=e').status }
283
-
284
- describe "when using 'RSpec:Mocks::ArgumentMatchers#hash_including'" do
285
- before(:each) do
286
- stub_request(:post, "www.example.com").
287
- with(body: hash_including(:a, c: {'d' => ['e', 'f']} ))
288
- end
289
-
290
- describe "for request with url encoded body" do
291
- it "should match request if hash matches body" do
292
- expect(request.status).to eq("200")
293
- end
294
-
295
- it "should not match if hash doesn't match url encoded body" do
296
- expect { wrong_request }.to raise_error(WebMock::NetConnectNotAllowedError)
297
- end
298
- end
299
-
300
- describe "for request with json body and content type is set to json" do
301
- it "should match if hash matches body" do
302
- expect(http_request(
303
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
304
- body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status).to eq("200")
305
- end
306
- end
307
- end
308
-
309
- describe "when using 'WebMock::API#hash_including'" do
310
- before(:each) do
311
- stub_request(:post, "www.example.com").
312
- with(body: WebMock::API.hash_including(:a, c: {'d' => ['e', 'f']} ))
313
- end
314
-
315
- describe "for request with url encoded body" do
316
- it "should match request if hash matches body" do
317
- expect(request.status).to eq("200")
318
- end
319
-
320
- it "should not match if hash doesn't match url encoded body" do
321
- expect { wrong_request }.to raise_error(WebMock::NetConnectNotAllowedError)
322
- end
323
- end
324
-
325
- describe "for request with json body and content type is set to json" do
326
- it "should match if hash matches body" do
327
- expect(http_request(
328
- :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
329
- body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status).to eq("200")
330
- end
331
- end
332
- end
333
-
334
- end
335
- end
336
-
337
- describe "based on headers" do
338
- it "should match requests if headers are the same" do
339
- stub_request(:get, "www.example.com").with(headers: SAMPLE_HEADERS )
340
- expect(http_request(
341
- :get, "http://www.example.com/",
342
- headers: SAMPLE_HEADERS).status).to eq("200")
343
- end
344
-
345
- it "should match requests if headers are the same and declared as array" do
346
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b"]} )
347
- expect(http_request(
348
- :get, "http://www.example.com/",
349
- headers: {"a" => "b"}).status).to eq("200")
350
- end
351
-
352
- describe "when multiple headers with the same key are used" do
353
- it "should match requests if headers are the same" do
354
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]} )
355
- expect(http_request(
356
- :get, "http://www.example.com/",
357
- headers: {"a" => ["b", "c"]}).status).to eq("200")
358
- end
359
-
360
- it "should match requests if headers are the same but in different order" do
361
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]} )
362
- expect(http_request(
363
- :get, "http://www.example.com/",
364
- headers: {"a" => ["c", "b"]}).status).to eq("200")
365
- end
366
-
367
- it "should not match requests if headers are different" do
368
- stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]})
369
-
370
- expect {
371
- http_request(
372
- :get, "http://www.example.com/",
373
- headers: {"a" => ["b", "d"]})
374
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
375
- end
376
- end
377
-
378
- it "should match requests if request headers are not stubbed" do
379
- stub_request(:get, "www.example.com")
380
- expect(http_request(
381
- :get, "http://www.example.com/",
382
- headers: SAMPLE_HEADERS).status).to eq("200")
383
- end
384
-
385
- it "should not match requests if headers are different" do
386
- stub_request(:get, "www.example.com").with(headers: SAMPLE_HEADERS)
387
-
388
- expect {
389
- http_request(
390
- :get, "http://www.example.com/",
391
- headers: { 'Content-Length' => '9999'})
392
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
393
- end
394
-
395
- it "should not match if accept header is different" do
396
- stub_request(:get, "www.example.com").
397
- with(headers: { 'Accept' => 'application/json'})
398
- expect {
399
- http_request(
400
- :get, "http://www.example.com/",
401
- headers: { 'Accept' => 'application/xml'})
402
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
403
- end
404
-
405
- describe "declared as regular expressions" do
406
- it "should match requests if header values match regular expression" do
407
- stub_request(:get, "www.example.com").with(headers: { some_header: /^MyAppName$/ })
408
- expect(http_request(
409
- :get, "http://www.example.com/",
410
- headers: { 'some-header' => 'MyAppName' }).status).to eq("200")
411
- end
412
-
413
- it "should not match requests if headers values do not match regular expression" do
414
- stub_request(:get, "www.example.com").with(headers: { some_header: /^MyAppName$/ })
415
-
416
- expect {
417
- http_request(
418
- :get, "http://www.example.com/",
419
- headers: { 'some-header' => 'xMyAppName' })
420
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
421
- end
422
- end
423
- end
424
-
425
- describe "when stubbing request with userinfo in url", unless: (adapter_info.include?(:no_url_auth)) do
426
- it "should match if credentials are the same" do
427
- stub_request(:get, "user:pass@www.example.com")
428
- expect(http_request(:get, "http://user:pass@www.example.com/").status).to eq("200")
429
- end
430
-
431
- it "should not match if credentials are different" do
432
- stub_request(:get, "user:pass@www.example.com")
433
- expect {
434
- expect(http_request(:get, "http://user:pazz@www.example.com/").status).to eq("200")
435
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
436
- end
437
-
438
- it "should not match if credentials are stubbed but not provided in the request" do
439
- stub_request(:get, "user:pass@www.example.com")
440
- expect {
441
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
442
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
443
- end
444
-
445
- it "should not match if credentials are not stubbed but exist in the request" do
446
- stub_request(:get, "www.example.com")
447
- expect {
448
- expect(http_request(:get, "http://user:pazz@www.example.com/").status).to eq("200")
449
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
450
- end
451
-
452
- it "should not match if request has credentials provided in basic authentication herader" do
453
- stub_request(:get, "user:pass@www.example.com")
454
- expect {
455
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
456
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
457
- end
458
- end
459
-
460
- describe "when stubbing request with basic authentication header" do
461
- it "should match if credentials are the same" do
462
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
463
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
464
- end
465
-
466
- it "should match if credentials are the same and encode to more than one line" do
467
- stub_request(:get, "www.example.com").with(basic_auth: ['user' * 5, 'pass' * 5])
468
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user' * 5, 'pass' * 5]).status).to eq("200")
469
- end
470
-
471
- it "should match if credentials are the same and were provided directly as authentication headers in request" do
472
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
473
- expect(http_request(:get, "http://www.example.com/", headers: {'Authorization'=>'Basic dXNlcjpwYXNz'}).status).to eq("200")
474
- end
475
-
476
- it "should match if credentials are the same and have been declared in the stub as encoded header" do
477
- stub_request(:get, "www.example.com").with(headers: {'Authorization'=>'Basic dXNlcjpwYXNz'})
478
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
479
- end
480
-
481
- it "should not match if credentials are different" do
482
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
483
- expect {
484
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pazz']).status).to eq("200")
485
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
486
- end
487
-
488
- it "should not match if credentials are stubbed but not provided in the request" do
489
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
490
- expect {
491
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
492
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
493
- end
494
-
495
- it "should match if credentials are not stubbed but exist in the request" do
496
- stub_request(:get, "www.example.com")
497
- expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
498
- end
499
-
500
- it "should not match if request has credentials provides in userinfo", unless: (adapter_info.include?(:no_url_auth)) do
501
- stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
502
- expect {
503
- expect(http_request(:get, "http://user:pass@www.example.com/").status).to eq("200")
504
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pass@www.example.com/))
505
- end
506
- end
507
-
508
- describe "when stubbing request with a global hook" do
509
- after(:each) do
510
- WebMock::StubRegistry.instance.global_stubs.clear
511
- end
512
-
513
- it 'returns the response returned by the hook' do
514
- WebMock.globally_stub_request do |request|
515
- { body: "global stub body" }
516
- end
517
-
518
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
519
- end
520
-
521
- it 'does not get cleared when a user calls WebMock.reset!' do
522
- WebMock.globally_stub_request do |request|
523
- { body: "global stub body" }
524
- end
525
- WebMock.reset!
526
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
527
- end
528
-
529
- it "does not stub the request if the hook does not return anything" do
530
- WebMock.globally_stub_request { |r| }
531
- expect {
532
- http_request(:get, "http://www.example.com/")
533
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
534
- end
535
-
536
- it "passes the request to the block" do
537
- passed_request = nil
538
- WebMock.globally_stub_request do |request|
539
- passed_request = request
540
- { body: "global stub body" }
541
- end
542
-
543
- http_request(:get, "http://www.example.com:456/bar")
544
- expect(passed_request.uri.to_s).to eq("http://www.example.com:456/bar")
545
- end
546
-
547
- it "should call the block only once per request" do
548
- call_count = 0
549
- WebMock.globally_stub_request do |request|
550
- call_count += 1
551
- { body: "global stub body" }
552
- end
553
- http_request(:get, "http://www.example.com/")
554
- expect(call_count).to eq(1)
555
- end
556
-
557
- it 'supports multiple global stubs; the first registered one that returns a non-nil value determines the stub' do
558
- stub_invocation_order = []
559
- WebMock.globally_stub_request do |request|
560
- stub_invocation_order << :nil_stub
561
- nil
562
- end
563
-
564
- WebMock.globally_stub_request do |request|
565
- stub_invocation_order << :hash_stub
566
- { body: "global stub body" }
567
- end
568
-
569
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
570
- expect(stub_invocation_order).to eq([:nil_stub, :hash_stub])
571
- end
572
-
573
- [:before, :after].each do |before_or_after|
574
- context "when there is also a non-global registered stub #{before_or_after} the global stub" do
575
- def stub_non_globally
576
- stub_request(:get, "www.example.com").to_return(body: 'non-global stub body')
577
- end
578
-
579
- define_method :register_stubs do |block|
580
- stub_non_globally if before_or_after == :before
581
- WebMock.globally_stub_request(&block)
582
- stub_non_globally if before_or_after == :after
583
- end
584
-
585
- it 'uses the response from the global stub if the block returns a non-nil value' do
586
- register_stubs(lambda { |req| { body: 'global stub body' } })
587
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
588
- end
589
-
590
- it 'uses the response from the non-global stub if the block returns a nil value' do
591
- register_stubs(lambda { |req| nil })
592
- expect(http_request(:get, "http://www.example.com/").body).to eq("non-global stub body")
593
- end
594
- end
595
- end
596
-
597
- context "when global stub should be invoked last" do
598
- before do
599
- WebMock.globally_stub_request(:after_local_stubs) do
600
- { body: "global stub body" }
601
- end
602
- end
603
-
604
- it "uses global stub when non-global stub is not defined" do
605
- expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
606
- end
607
-
608
- it "uses non-global stub first" do
609
- stub_request(:get, "www.example.com").to_return(body: 'non-global stub body')
610
- expect(http_request(:get, "http://www.example.com/").body).to eq("non-global stub body")
611
- end
612
- end
613
- end
614
-
615
- describe "when stubbing request with a block evaluated on request" do
616
- it "should match if block returns true" do
617
- stub_request(:get, "www.example.com").with { |request| true }
618
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
619
- end
620
-
621
- it "should not match if block returns false" do
622
- stub_request(:get, "www.example.com").with { |request| false }
623
- expect {
624
- http_request(:get, "http://www.example.com/")
625
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
626
- end
627
-
628
- it "should pass the request to the block" do
629
- stub_request(:post, "www.example.com").with { |request| request.body == "wadus" }
630
- expect(http_request(
631
- :post, "http://www.example.com/",
632
- body: "wadus").status).to eq("200")
633
- expect {
634
- http_request(:post, "http://www.example.com/", body: "jander")
635
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'jander'))
636
- end
637
-
638
- it "should call the block only once per request" do
639
- call_count = 0
640
- stub_request(:get, "www.example.com").with { |request| call_count += 1; true }
641
- expect(http_request(:get, "http://www.example.com/").status).to eq("200")
642
- expect(call_count).to eq(1)
643
- end
644
- end
645
- end
646
-
647
- describe "when request stub was removed" do
648
- it "should raise an error on request" do
649
- stub = stub_request(:get, "www.example.com")
650
-
651
- http_request(:get, "http://www.example.com/")
652
-
653
- remove_request_stub(stub)
654
-
655
- expect {
656
- http_request(:get, "http://www.example.com/")
657
- }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
658
- end
659
- end
660
-
661
- describe "in Rspec around(:each) hook" do
662
- # order goes
663
- # around(:each)
664
- # before(:each)
665
- # after(:each)
666
- # anything after example.run in around(:each)
667
- around(:each) do |example|
668
- example.run
669
- expect {
670
- http_request(:get, "http://www.example.com/")
671
- }.to_not raise_error # WebMock::NetConnectNotAllowedError
672
- end
673
-
674
- it "should still allow me to make a mocked request" do
675
- stub_request(:get, "www.example.com")
676
- end
677
- end
678
- end