webmock 1.8.6 → 3.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/CI.yml +37 -0
  3. data/.gitignore +6 -0
  4. data/CHANGELOG.md +1198 -0
  5. data/Gemfile +3 -15
  6. data/README.md +761 -305
  7. data/Rakefile +13 -40
  8. data/lib/webmock/api.rb +63 -17
  9. data/lib/webmock/callback_registry.rb +1 -1
  10. data/lib/webmock/config.rb +8 -0
  11. data/lib/webmock/cucumber.rb +2 -0
  12. data/lib/webmock/errors.rb +8 -24
  13. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +216 -0
  14. data/lib/webmock/http_lib_adapters/curb_adapter.rb +148 -84
  15. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +224 -4
  16. data/lib/webmock/http_lib_adapters/excon_adapter.rb +104 -34
  17. data/lib/webmock/http_lib_adapters/http_rb/client.rb +17 -0
  18. data/lib/webmock/http_lib_adapters/http_rb/request.rb +16 -0
  19. data/lib/webmock/http_lib_adapters/http_rb/response.rb +64 -0
  20. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +29 -0
  21. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +68 -0
  22. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +37 -0
  23. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +152 -86
  24. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +145 -0
  25. data/lib/webmock/http_lib_adapters/net_http.rb +155 -46
  26. data/lib/webmock/http_lib_adapters/net_http_response.rb +1 -1
  27. data/lib/webmock/http_lib_adapters/patron_adapter.rb +16 -15
  28. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +76 -82
  29. data/lib/webmock/matchers/any_arg_matcher.rb +13 -0
  30. data/lib/webmock/matchers/hash_argument_matcher.rb +21 -0
  31. data/lib/webmock/matchers/hash_excluding_matcher.rb +15 -0
  32. data/lib/webmock/matchers/hash_including_matcher.rb +4 -12
  33. data/lib/webmock/minitest.rb +29 -3
  34. data/lib/webmock/rack_response.rb +14 -7
  35. data/lib/webmock/request_body_diff.rb +64 -0
  36. data/lib/webmock/request_execution_verifier.rb +38 -17
  37. data/lib/webmock/request_pattern.rb +158 -38
  38. data/lib/webmock/request_registry.rb +3 -3
  39. data/lib/webmock/request_signature.rb +7 -3
  40. data/lib/webmock/request_signature_snippet.rb +61 -0
  41. data/lib/webmock/request_stub.rb +9 -6
  42. data/lib/webmock/response.rb +30 -15
  43. data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +38 -2
  44. data/lib/webmock/rspec/matchers/webmock_matcher.rb +23 -2
  45. data/lib/webmock/rspec/matchers.rb +0 -1
  46. data/lib/webmock/rspec.rb +11 -2
  47. data/lib/webmock/stub_registry.rb +31 -10
  48. data/lib/webmock/stub_request_snippet.rb +14 -6
  49. data/lib/webmock/test_unit.rb +4 -4
  50. data/lib/webmock/util/hash_counter.rb +20 -6
  51. data/lib/webmock/util/hash_keys_stringifier.rb +5 -3
  52. data/lib/webmock/util/hash_validator.rb +17 -0
  53. data/lib/webmock/util/headers.rb +23 -2
  54. data/lib/webmock/util/json.rb +20 -7
  55. data/lib/webmock/util/query_mapper.rb +281 -0
  56. data/lib/webmock/util/uri.rb +29 -19
  57. data/lib/webmock/util/values_stringifier.rb +20 -0
  58. data/lib/webmock/util/version_checker.rb +40 -2
  59. data/lib/webmock/version.rb +1 -1
  60. data/lib/webmock/webmock.rb +56 -17
  61. data/lib/webmock.rb +56 -46
  62. data/minitest/test_helper.rb +8 -3
  63. data/minitest/test_webmock.rb +4 -1
  64. data/minitest/webmock_spec.rb +16 -6
  65. data/spec/acceptance/async_http_client/async_http_client_spec.rb +375 -0
  66. data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +73 -0
  67. data/spec/acceptance/curb/curb_spec.rb +227 -68
  68. data/spec/acceptance/curb/curb_spec_helper.rb +11 -8
  69. data/spec/acceptance/em_http_request/em_http_request_spec.rb +322 -28
  70. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +15 -10
  71. data/spec/acceptance/excon/excon_spec.rb +66 -4
  72. data/spec/acceptance/excon/excon_spec_helper.rb +21 -7
  73. data/spec/acceptance/http_rb/http_rb_spec.rb +93 -0
  74. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +54 -0
  75. data/spec/acceptance/httpclient/httpclient_spec.rb +152 -11
  76. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +25 -16
  77. data/spec/acceptance/manticore/manticore_spec.rb +107 -0
  78. data/spec/acceptance/manticore/manticore_spec_helper.rb +35 -0
  79. data/spec/acceptance/net_http/net_http_shared.rb +52 -24
  80. data/spec/acceptance/net_http/net_http_spec.rb +164 -50
  81. data/spec/acceptance/net_http/net_http_spec_helper.rb +19 -10
  82. data/spec/acceptance/net_http/real_net_http_spec.rb +1 -1
  83. data/spec/acceptance/patron/patron_spec.rb +29 -40
  84. data/spec/acceptance/patron/patron_spec_helper.rb +15 -11
  85. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +229 -58
  86. data/spec/acceptance/shared/callbacks.rb +32 -30
  87. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +20 -5
  88. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +14 -14
  89. data/spec/acceptance/shared/precedence_of_stubs.rb +6 -6
  90. data/spec/acceptance/shared/request_expectations.rb +560 -296
  91. data/spec/acceptance/shared/returning_declared_responses.rb +180 -138
  92. data/spec/acceptance/shared/stubbing_requests.rb +385 -154
  93. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +78 -17
  94. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +19 -15
  95. data/spec/acceptance/webmock_shared.rb +2 -2
  96. data/spec/fixtures/test.txt +1 -0
  97. data/spec/quality_spec.rb +27 -3
  98. data/spec/spec_helper.rb +11 -20
  99. data/spec/support/failures.rb +9 -0
  100. data/spec/support/my_rack_app.rb +8 -3
  101. data/spec/support/network_connection.rb +7 -13
  102. data/spec/support/webmock_server.rb +8 -3
  103. data/spec/unit/api_spec.rb +175 -0
  104. data/spec/unit/errors_spec.rb +116 -19
  105. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +1 -1
  106. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +2 -2
  107. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +61 -0
  108. data/spec/unit/matchers/hash_including_matcher_spec.rb +87 -0
  109. data/spec/unit/rack_response_spec.rb +54 -16
  110. data/spec/unit/request_body_diff_spec.rb +90 -0
  111. data/spec/unit/request_execution_verifier_spec.rb +147 -39
  112. data/spec/unit/request_pattern_spec.rb +462 -198
  113. data/spec/unit/request_registry_spec.rb +29 -9
  114. data/spec/unit/request_signature_snippet_spec.rb +89 -0
  115. data/spec/unit/request_signature_spec.rb +91 -49
  116. data/spec/unit/request_stub_spec.rb +71 -70
  117. data/spec/unit/response_spec.rb +100 -81
  118. data/spec/unit/stub_registry_spec.rb +37 -20
  119. data/spec/unit/stub_request_snippet_spec.rb +51 -31
  120. data/spec/unit/util/hash_counter_spec.rb +6 -6
  121. data/spec/unit/util/hash_keys_stringifier_spec.rb +4 -4
  122. data/spec/unit/util/headers_spec.rb +4 -4
  123. data/spec/unit/util/json_spec.rb +29 -3
  124. data/spec/unit/util/query_mapper_spec.rb +157 -0
  125. data/spec/unit/util/uri_spec.rb +150 -36
  126. data/spec/unit/util/version_checker_spec.rb +15 -9
  127. data/spec/unit/webmock_spec.rb +57 -4
  128. data/test/http_request.rb +3 -3
  129. data/test/shared_test.rb +45 -13
  130. data/test/test_helper.rb +1 -1
  131. data/test/test_webmock.rb +6 -0
  132. data/webmock.gemspec +30 -11
  133. metadata +308 -199
  134. data/.rvmrc +0 -1
  135. data/.travis.yml +0 -11
  136. data/Guardfile +0 -24
  137. data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_0_x.rb +0 -151
  138. data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb +0 -210
@@ -8,92 +8,100 @@ shared_context "request expectations" do |*adapter_info|
8
8
  end
9
9
 
10
10
  it "should satisfy expectation if request was executed with the same uri and method" do
11
- lambda {
11
+ expect {
12
12
  http_request(:get, "http://www.example.com/")
13
- a_request(:get, "http://www.example.com").should have_been_made.once
14
- }.should_not raise_error
13
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.once
14
+ }.not_to raise_error
15
15
  end
16
16
 
17
17
  it "should satisfy expectation declared on WebMock.resuest" do
18
- lambda {
18
+ expect {
19
19
  http_request(:get, "http://www.example.com/")
20
- WebMock.request(:get, "http://www.example.com").should have_been_made.once
21
- }.should_not raise_error
20
+ expect(WebMock.request(:get, "http://www.example.com")).to have_been_made.once
21
+ }.not_to raise_error
22
22
  end
23
23
 
24
24
  it "should satisfy expectation if request was not expected and not executed" do
25
- lambda {
26
- a_request(:get, "http://www.example.com").should_not have_been_made
27
- }.should_not raise_error
25
+ expect {
26
+ expect(a_request(:get, "http://www.example.com")).not_to have_been_made
27
+ }.not_to raise_error
28
28
  end
29
29
 
30
30
  it "should fail if request was not expected but executed" do
31
- lambda {
31
+ expect {
32
32
  http_request(:get, "http://www.example.com/")
33
- a_request(:get, "http://www.example.com").should_not have_been_made
34
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
33
+ expect(a_request(:get, "http://www.example.com")).not_to have_been_made
34
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
35
35
  end
36
36
 
37
37
  it "should fail resulting with failure with a message and executed requests listed" do
38
- lambda {
38
+ expect {
39
39
  http_request(:get, "http://www.example.com/")
40
- a_request(:get, "http://www.example.com").should_not have_been_made
41
- }.should fail_with(%r{The following requests were made:\n\nGET http://www.example.com/.+was made 1 time})
40
+ expect(a_request(:get, "http://www.example.com")).not_to have_been_made
41
+ }.to fail_with(%r{The following requests were made:\n\nGET http://www.example.com/.+was made 1 time})
42
42
  end
43
43
 
44
44
  it "should fail if request was not executed" do
45
- lambda {
46
- a_request(:get, "http://www.example.com").should have_been_made
47
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
45
+ expect {
46
+ expect(a_request(:get, "http://www.example.com")).to have_been_made
47
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
48
48
  end
49
49
 
50
50
  it "should fail if request was executed to different uri" do
51
- lambda {
51
+ expect {
52
52
  http_request(:get, "http://www.example.com/")
53
- a_request(:get, "http://www.example.org").should have_been_made
54
- }.should fail_with(%r(The request GET http://www.example.org/ was expected to execute 1 time but it executed 0 times))
53
+ expect(a_request(:get, "http://www.example.org")).to have_been_made
54
+ }.to fail_with(%r(The request GET http://www.example.org/ was expected to execute 1 time but it executed 0 times))
55
55
  end
56
56
 
57
57
  it "should fail if request was executed with different method" do
58
- lambda {
59
- http_request(:post, "http://www.example.com/", :body => "abc")
60
- a_request(:get, "http://www.example.com").should have_been_made
61
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
58
+ expect {
59
+ http_request(:post, "http://www.example.com/", body: "abc")
60
+ expect(a_request(:get, "http://www.example.com")).to have_been_made
61
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
62
62
  end
63
63
 
64
64
  it "should satisfy expectation if request was executed with different form of uri" do
65
- lambda {
65
+ expect {
66
66
  http_request(:get, "http://www.example.com/")
67
- a_request(:get, "www.example.com").should have_been_made
68
- }.should_not raise_error
67
+ expect(a_request(:get, "www.example.com")).to have_been_made
68
+ }.not_to raise_error
69
69
  end
70
70
 
71
71
  it "should satisfy expectation if request was executed with different form of uri without port " do
72
- lambda {
72
+ expect {
73
73
  http_request(:get, "http://www.example.com/")
74
- a_request(:get, "www.example.com:80").should have_been_made
75
- }.should_not raise_error
74
+ expect(a_request(:get, "www.example.com:80")).to have_been_made
75
+ }.not_to raise_error
76
76
  end
77
77
 
78
78
  it "should satisfy expectation if request was executed with different form of uri with port" do
79
- lambda {
79
+ expect {
80
80
  http_request(:get, "http://www.example.com/")
81
- a_request(:get, "www.example.com:80").should have_been_made
82
- }.should_not raise_error
81
+ expect(a_request(:get, "www.example.com:80")).to have_been_made
82
+ }.not_to raise_error
83
83
  end
84
84
 
85
85
  it "should fail if request was executed to a different port" do
86
- lambda {
86
+ expect {
87
87
  http_request(:get, "http://www.example.com:80/")
88
- a_request(:get, "www.example.com:90").should have_been_made
89
- }.should fail_with(%r(The request GET http://www.example.com:90/ was expected to execute 1 time but it executed 0 times))
88
+ expect(a_request(:get, "www.example.com:90")).to have_been_made
89
+ }.to fail_with(%r(The request GET http://www.example.com:90/ was expected to execute 1 time but it executed 0 times))
90
90
  end
91
91
 
92
92
  it "should satisfy expectation if request was executed with different form of uri with https port" do
93
- lambda {
93
+ expect {
94
94
  http_request(:get, "https://www.example.com/")
95
- a_request(:get, "https://www.example.com:443/").should have_been_made
96
- }.should_not raise_error
95
+ expect(a_request(:get, "https://www.example.com:443/")).to have_been_made
96
+ }.not_to raise_error
97
+ end
98
+
99
+ it "should satisfy expectations even if requests were executed in different order than expectations were declared" do
100
+ stub_request(:post, "http://www.example.com")
101
+ http_request(:post, "http://www.example.com/", body: "def")
102
+ http_request(:post, "http://www.example.com/", body: "abc")
103
+ expect(WebMock).to have_requested(:post, "www.example.com").with(body: "abc")
104
+ expect(WebMock).to have_requested(:post, "www.example.com").with(body: "def")
97
105
  end
98
106
 
99
107
  describe "when matching requests with escaped or unescaped uris" do
@@ -103,24 +111,24 @@ shared_context "request expectations" do |*adapter_info|
103
111
  end
104
112
 
105
113
  it "should satisfy expectation if request was executed with escaped params" do
106
- lambda {
114
+ expect {
107
115
  http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
108
- a_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}").should have_been_made
109
- }.should_not raise_error
116
+ expect(a_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")).to have_been_made
117
+ }.not_to raise_error
110
118
  end
111
119
 
112
120
  it "should satisfy expectation if request was executed with non escaped params" do
113
- lambda {
121
+ expect {
114
122
  http_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
115
- a_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}").should have_been_made
116
- }.should_not raise_error
123
+ expect(a_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")).to have_been_made
124
+ }.not_to raise_error
117
125
  end
118
126
 
119
127
  it "should satisfy expectation if request was executed with escaped params and uri matching regexp was expected" do
120
- lambda {
128
+ expect {
121
129
  http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
122
- a_request(:get, /.*example.*/).should have_been_made
123
- }.should_not raise_error
130
+ expect(a_request(:get, /.*example.*/)).to have_been_made
131
+ }.not_to raise_error
124
132
  end
125
133
 
126
134
  end
@@ -131,85 +139,292 @@ shared_context "request expectations" do |*adapter_info|
131
139
  end
132
140
 
133
141
  it "should satisfy expectation if the request was executed with query params declared as a hash in a query option" do
134
- lambda {
142
+ expect {
135
143
  http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
136
- a_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).should have_been_made
137
- }.should_not raise_error
144
+ expect(a_request(:get, "www.example.com").with(query: {"a" => ["b", "c"]})).to have_been_made
145
+ }.not_to raise_error
138
146
  end
139
147
 
140
148
  it "should satisfy expectation if the request was executed with query params declared as string in query option" do
141
- lambda {
149
+ expect {
142
150
  http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
143
- a_request(:get, "www.example.com").with(:query => "a[]=b&a[]=c").should have_been_made
144
- }.should_not raise_error
151
+ expect(a_request(:get, "www.example.com").with(query: "a[]=b&a[]=c")).to have_been_made
152
+ }.not_to raise_error
145
153
  end
146
154
 
147
155
  it "should satisfy expectation if the request was executed with query params both in uri and in query option" do
148
- lambda {
156
+ expect {
149
157
  http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c")
150
- a_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).should have_been_made
151
- }.should_not raise_error
158
+ expect(a_request(:get, "www.example.com/?x=3").with(query: {"a" => ["b", "c"]})).to have_been_made
159
+ }.not_to raise_error
152
160
  end
153
161
 
154
162
  it "should satisfy expectation if the request was executed with only part query params declared as a hash in a query option" do
155
- lambda {
163
+ expect {
156
164
  http_request(:get, "http://www.example.com/?a[]=b&a[]=c&b=1")
157
- a_request(:get, "www.example.com").with(:query => hash_including({"a" => ["b", "c"]})).should have_been_made
158
- }.should_not raise_error
165
+ expect(a_request(:get, "www.example.com").with(query: hash_including({"a" => ["b", "c"]}))).to have_been_made
166
+ }.not_to raise_error
167
+ end
168
+
169
+ it 'should satisfy expectation if the request was executed with excluding part of query params declared as a hash in a query option' do
170
+ expect {
171
+ http_request(:get, "http://www.example.com/?a[]=d&b[]=e&b=1")
172
+ expect(a_request(:get, "www.example.com").with(query: hash_excluding(a: ['b', 'c']))).to have_been_made
173
+ }.not_to raise_error
174
+ end
175
+
176
+ it 'should satisfy expectation if the request was executed with an empty array in the query params' do
177
+ expect {
178
+ http_request(:get, "http://www.example.com/?a[]")
179
+ expect(a_request(:get, "www.example.com").with(query: hash_including(a: []))).to have_been_made
180
+ }.not_to raise_error
181
+ end
182
+ end
183
+
184
+ context "when using flat array notation" do
185
+ before :all do
186
+ WebMock::Config.instance.query_values_notation = :flat_array
187
+ end
188
+
189
+ it "should satisfy expectation if request includes different repeated query params in flat array notation" do
190
+ expect {
191
+ stub_request(:get, "http://www.example.com/?a=1&a=2")
192
+ http_request(:get, "http://www.example.com/?a=1&a=2")
193
+ expect(a_request(:get, "http://www.example.com/?a=1&a=2")).to have_been_made
194
+ }.not_to raise_error
195
+ end
196
+
197
+ after :all do
198
+ WebMock::Config.instance.query_values_notation = nil
199
+ end
200
+ end
201
+
202
+
203
+
204
+ describe "at_most_times" do
205
+ it "fails if request was made more times than maximum" do
206
+ expect {
207
+ http_request(:get, "http://www.example.com/")
208
+ http_request(:get, "http://www.example.com/")
209
+ http_request(:get, "http://www.example.com/")
210
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
211
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute at most 2 times but it executed 3 times))
212
+ end
213
+
214
+ it "passes if request was made the maximum number of times" do
215
+ expect {
216
+ http_request(:get, "http://www.example.com/")
217
+ http_request(:get, "http://www.example.com/")
218
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
219
+ }.not_to raise_error
220
+ end
221
+
222
+ it "passes if request was made fewer than the maximum number of times" do
223
+ expect {
224
+ http_request(:get, "http://www.example.com/")
225
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
226
+ }.not_to raise_error
227
+ end
228
+
229
+ it "passes if request was not made at all" do
230
+ expect {
231
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
232
+ }.not_to raise_error
233
+ end
234
+ end
235
+
236
+
237
+ describe "at_least_times" do
238
+ it "fails if request was made fewer times than minimum" do
239
+ expect {
240
+ http_request(:get, "http://www.example.com/")
241
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_times(2)
242
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute at least 2 times but it executed 1 time))
243
+ end
244
+
245
+ it "passes if request was made the minimum number of times" do
246
+ expect {
247
+ http_request(:get, "http://www.example.com/")
248
+ http_request(:get, "http://www.example.com/")
249
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_times(2)
250
+ }.not_to raise_error
251
+ end
252
+
253
+ it "passes if request was made more than the minimum number of times" do
254
+ expect {
255
+ http_request(:get, "http://www.example.com/")
256
+ http_request(:get, "http://www.example.com/")
257
+ http_request(:get, "http://www.example.com/")
258
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_times(2)
259
+ }.not_to raise_error
260
+ end
261
+
262
+ context "descriptive at_most_ matcher" do
263
+ context "at_most_once" do
264
+ it "succeeds if no request was executed" do
265
+ expect {
266
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_once
267
+ }.not_to raise_error
268
+ end
269
+
270
+ it "satisfies expectation if request was executed with the same uri and method once" do
271
+ expect {
272
+ http_request(:get, "http://www.example.com/")
273
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_once
274
+ }.not_to raise_error
275
+ end
276
+
277
+ it "fails if request was executed with the same uri and method twice" do
278
+ expect {
279
+ http_request(:get, "http://www.example.com/")
280
+ http_request(:get, "http://www.example.com/")
281
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_once
282
+ }.to fail
283
+ end
284
+ end
285
+
286
+ context "at_most_twice" do
287
+ it "succeeds if no request was executed" do
288
+ expect {
289
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
290
+ }.not_to raise_error
291
+ end
292
+
293
+ it "succeeds if too few requests were executed" do
294
+ expect {
295
+ http_request(:get, "http://www.example.com/")
296
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
297
+ }.not_to raise_error
298
+ end
299
+
300
+ it "satisfies expectation if request was executed with the same uri and method twice" do
301
+ expect {
302
+ http_request(:get, "http://www.example.com/")
303
+ http_request(:get, "http://www.example.com/")
304
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
305
+ }.not_to raise_error
306
+ end
307
+
308
+ it "fails if request was executed with the same uri and method three times" do
309
+ expect {
310
+ http_request(:get, "http://www.example.com/")
311
+ http_request(:get, "http://www.example.com/")
312
+ http_request(:get, "http://www.example.com/")
313
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
314
+ }.to fail
315
+ end
316
+ end
317
+ end
318
+
319
+ context "descriptive at_least_ matcher" do
320
+ context "at_least_once" do
321
+ it "fails if no request was executed" do
322
+ expect {
323
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_once
324
+ }.to fail
325
+ end
326
+
327
+ it "satisfies expectation if request was executed with the same uri and method once" do
328
+ expect {
329
+ http_request(:get, "http://www.example.com/")
330
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_once
331
+ }.not_to raise_error
332
+ end
333
+
334
+ it "satisfies expectation if request was executed with the same uri and method twice" do
335
+ expect {
336
+ http_request(:get, "http://www.example.com/")
337
+ http_request(:get, "http://www.example.com/")
338
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_once
339
+ }.not_to raise_error
340
+ end
341
+ end
342
+
343
+ context "at_least_twice" do
344
+ it "fails if no request was executed" do
345
+ expect {
346
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
347
+ }.to fail
348
+ end
349
+
350
+ it "fails if too few requests were executed" do
351
+ expect {
352
+ http_request(:get, "http://www.example.com/")
353
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
354
+ }.to fail
355
+ end
356
+
357
+ it "satisfies expectation if request was executed with the same uri and method twice" do
358
+ expect {
359
+ http_request(:get, "http://www.example.com/")
360
+ http_request(:get, "http://www.example.com/")
361
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
362
+ }.not_to raise_error
363
+ end
364
+
365
+ it "satisfies expectation if request was executed with the same uri and method three times" do
366
+ expect {
367
+ http_request(:get, "http://www.example.com/")
368
+ http_request(:get, "http://www.example.com/")
369
+ http_request(:get, "http://www.example.com/")
370
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
371
+ }.not_to raise_error
372
+ end
373
+ end
159
374
  end
160
375
  end
161
376
 
162
377
  it "should fail if request was made more times than expected" do
163
- lambda {
378
+ expect {
164
379
  http_request(:get, "http://www.example.com/")
165
380
  http_request(:get, "http://www.example.com/")
166
- a_request(:get, "http://www.example.com").should have_been_made
167
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 2 times))
381
+ expect(a_request(:get, "http://www.example.com")).to have_been_made
382
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 2 times))
168
383
  end
169
384
 
170
385
  it "should fail if request was made less times than expected" do
171
- lambda {
386
+ expect {
172
387
  http_request(:get, "http://www.example.com/")
173
- a_request(:get, "http://www.example.com").should have_been_made.twice
174
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 2 times but it executed 1 time))
388
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.twice
389
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 2 times but it executed 1 time))
175
390
  end
176
391
 
177
392
  it "should fail if request was made less times than expected when 3 times expected" do
178
- lambda {
393
+ expect {
179
394
  http_request(:get, "http://www.example.com/")
180
- a_request(:get, "http://www.example.com").should have_been_made.times(3)
181
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 3 times but it executed 1 time))
395
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.times(3)
396
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 3 times but it executed 1 time))
182
397
  end
183
398
 
184
399
  it "should satisfy expectation if request was executed with the same body" do
185
- lambda {
186
- http_request(:post, "http://www.example.com/", :body => "abc")
187
- a_request(:post, "www.example.com").with(:body => "abc").should have_been_made
188
- }.should_not raise_error
400
+ expect {
401
+ http_request(:post, "http://www.example.com/", body: "abc")
402
+ expect(a_request(:post, "www.example.com").with(body: "abc")).to have_been_made
403
+ }.not_to raise_error
189
404
  end
190
405
 
191
406
  it "should fail if request was executed with different body" do
192
- lambda {
193
- http_request(:get, "http://www.example.com/", :body => "abc")
194
- a_request(:get, "www.example.com").
195
- with(:body => "def").should have_been_made
196
- }.should fail_with(%r(The request GET http://www.example.com/ with body "def" was expected to execute 1 time but it executed 0 times))
407
+ expect {
408
+ http_request(:post, "http://www.example.com/", body: "abc")
409
+ expect(a_request(:post, "www.example.com").
410
+ with(body: "def")).to have_been_made
411
+ }.to fail_with(%r(The request POST http://www.example.com/ with body "def" was expected to execute 1 time but it executed 0 times))
197
412
  end
198
413
 
199
414
  describe "when expected request body is declared as a regexp" do
200
415
  it "should satisfy expectation if request was executed with body matching regexp" do
201
- lambda {
202
- http_request(:post, "http://www.example.com/", :body => "abc")
203
- a_request(:post, "www.example.com").with(:body => /^abc$/).should have_been_made
204
- }.should_not raise_error
416
+ expect {
417
+ http_request(:post, "http://www.example.com/", body: "abc")
418
+ expect(a_request(:post, "www.example.com").with(body: /^abc$/)).to have_been_made
419
+ }.not_to raise_error
205
420
  end
206
421
 
207
422
  it "should fail if request was executed with body not matching regexp" do
208
- lambda {
209
- http_request(:get, "http://www.example.com/", :body => "abc")
210
- a_request(:get, "www.example.com").
211
- with(:body => /^xabc/).should have_been_made
212
- }.should fail_with(%r(The request GET http://www.example.com/ with body /\^xabc/ was expected to execute 1 time but it executed 0 times))
423
+ expect {
424
+ http_request(:post, "http://www.example.com/", body: "abc")
425
+ expect(a_request(:post, "www.example.com").
426
+ with(body: /^xabc/)).to have_been_made
427
+ }.to fail_with(%r(The request POST http://www.example.com/ with body /\^xabc/ was expected to execute 1 time but it executed 0 times))
213
428
  end
214
429
 
215
430
  end
@@ -220,50 +435,50 @@ shared_context "request expectations" do |*adapter_info|
220
435
 
221
436
  describe "when request is made with url encoded body matching hash" do
222
437
  it "should satisfy expectation" do
223
- lambda {
224
- http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&c[d][]=f&b=five')
225
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
226
- }.should_not raise_error
438
+ expect {
439
+ http_request(:post, "http://www.example.com/", body: 'a=1&c[d][]=e&c[d][]=f&b=five')
440
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
441
+ }.not_to raise_error
227
442
  end
228
443
 
229
444
  it "should satisfy expectation even if url encoded params have different order" do
230
- lambda {
231
- http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&b=five&c[d][]=f')
232
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
233
- }.should_not raise_error
445
+ expect {
446
+ http_request(:post, "http://www.example.com/", body: 'a=1&c[d][]=e&b=five&c[d][]=f')
447
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
448
+ }.not_to raise_error
234
449
  end
235
450
 
236
451
  it "should fail if request is executed with url encoded body not matching hash" do
237
- lambda {
238
- http_request(:post, "http://www.example.com/", :body => 'c[d][]=f&a=1&c[d][]=e')
239
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
240
- }.should fail_with(fail_message)
452
+ expect {
453
+ http_request(:post, "http://www.example.com/", body: 'c[d][]=f&a=1&c[d][]=e')
454
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
455
+ }.to fail_with(fail_message)
241
456
  end
242
457
  end
243
458
 
244
459
  describe "when request is executed with json body matching hash and Content-Type is set to json" do
245
460
  it "should satisfy expectation" do
246
- lambda {
247
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
248
- :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}")
249
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
250
- }.should_not raise_error
461
+ expect {
462
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
463
+ body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}")
464
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
465
+ }.not_to raise_error
251
466
  end
252
467
 
253
468
  it "should satisfy expectation even if json body is in different form" do
254
- lambda {
255
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
256
- :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}")
257
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
258
- }.should_not raise_error
469
+ expect {
470
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
471
+ body: "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}")
472
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
473
+ }.not_to raise_error
259
474
  end
260
475
 
261
476
  it "should satisfy expectation even if json body contains date string" do
262
- lambda {
263
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
264
- :body => "{\"foo\":\"2010-01-01\"}")
265
- a_request(:post, "www.example.com").with(:body => {"foo" => "2010-01-01"}).should have_been_made
266
- }.should_not raise_error
477
+ expect {
478
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
479
+ body: "{\"foo\":\"2010-01-01\"}")
480
+ expect(a_request(:post, "www.example.com").with(body: {"foo" => "2010-01-01"})).to have_been_made
481
+ }.not_to raise_error
267
482
  end
268
483
  end
269
484
 
@@ -272,27 +487,27 @@ shared_context "request expectations" do |*adapter_info|
272
487
  let(:body_hash) { { "opt" => {:a => "1", :b => 'five', 'c' => {'d' => ['e', 'f']}} }}
273
488
 
274
489
  it "should satisfy expectation" do
275
- lambda {
276
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
277
- :body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n")
278
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
279
- }.should_not raise_error
490
+ expect {
491
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
492
+ body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n")
493
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
494
+ }.not_to raise_error
280
495
  end
281
496
 
282
497
  it "should satisfy expectation even if xml body is in different form" do
283
- lambda {
284
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
285
- :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n")
286
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
287
- }.should_not raise_error
498
+ expect {
499
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
500
+ body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n")
501
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
502
+ }.not_to raise_error
288
503
  end
289
504
 
290
505
  it "should satisfy expectation even if xml body contains date string" do
291
- lambda {
292
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
293
- :body => "<opt foo=\"2010-01-01\">\n</opt>\n")
294
- a_request(:post, "www.example.com").with(:body => {"opt" => {"foo" => "2010-01-01"}}).should have_been_made
295
- }.should_not raise_error
506
+ expect {
507
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
508
+ body: "<opt foo=\"2010-01-01\">\n</opt>\n")
509
+ expect(a_request(:post, "www.example.com").with(body: {"opt" => {"foo" => "2010-01-01"}})).to have_been_made
510
+ }.not_to raise_error
296
511
  end
297
512
  end
298
513
  end
@@ -303,288 +518,337 @@ shared_context "request expectations" do |*adapter_info|
303
518
 
304
519
  describe "when request is made with url encoded body matching hash" do
305
520
  it "should satisfy expectation" do
306
- lambda {
307
- http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&c[d][]=f&b=five')
308
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
309
- }.should_not raise_error
521
+ expect {
522
+ http_request(:post, "http://www.example.com/", body: 'a=1&c[d][]=e&c[d][]=f&b=five')
523
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
524
+ }.not_to raise_error
310
525
  end
311
526
 
312
527
  it "should fail if request is executed with url encoded body not matching hash" do
313
- lambda {
314
- http_request(:post, "http://www.example.com/", :body => 'c[d][]=f&a=1&c[d][]=e')
315
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
316
- }.should fail_with(fail_message)
528
+ expect {
529
+ http_request(:post, "http://www.example.com/", body: 'c[d][]=f&a=1&c[d][]=e')
530
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
531
+ }.to fail_with(fail_message)
317
532
  end
318
533
  end
319
534
  end
320
535
 
321
536
  describe "when request with headers is expected" do
322
537
  it "should satisfy expectation if request was executed with the same headers" do
323
- lambda {
324
- http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
325
- a_request(:get, "www.example.com").
326
- with(:headers => SAMPLE_HEADERS).should have_been_made
327
- }.should_not raise_error
538
+ expect {
539
+ http_request(:get, "http://www.example.com/", headers: SAMPLE_HEADERS)
540
+ expect(a_request(:get, "www.example.com").
541
+ with(headers: SAMPLE_HEADERS)).to have_been_made
542
+ }.not_to raise_error
328
543
  end
329
544
 
330
545
  it "should satisfy expectation if request was executed with the same headers but with header value declared as array" do
331
- lambda {
332
- http_request(:get, "http://www.example.com/", :headers => {"a" => "b"})
333
- a_request(:get, "www.example.com").
334
- with(:headers => {"a" => ["b"]}).should have_been_made
335
- }.should_not raise_error
546
+ expect {
547
+ http_request(:get, "http://www.example.com/", headers: {"a" => "b"})
548
+ expect(a_request(:get, "www.example.com").
549
+ with(headers: {"a" => ["b"]})).to have_been_made
550
+ }.not_to raise_error
336
551
  end
337
552
 
338
553
  describe "when multiple headers with the same key are passed" do
339
554
  it "should satisfy expectation" do
340
- lambda {
341
- http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
342
- a_request(:get, "www.example.com").
343
- with(:headers => {"a" => ["b", "c"]}).should have_been_made
344
- }.should_not raise_error
555
+ expect {
556
+ http_request(:get, "http://www.example.com/", headers: {"a" => ["b", "c"]})
557
+ expect(a_request(:get, "www.example.com").
558
+ with(headers: {"a" => ["b", "c"]})).to have_been_made
559
+ }.not_to raise_error
345
560
  end
346
561
 
347
562
  it "should satisfy expectation even if request was executed with the same headers but different order" do
348
- lambda {
349
- http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
350
- a_request(:get, "www.example.com").
351
- with(:headers => {"a" => ["c", "b"]}).should have_been_made
352
- }.should_not raise_error
563
+ expect {
564
+ http_request(:get, "http://www.example.com/", headers: {"a" => ["b", "c"]})
565
+ expect(a_request(:get, "www.example.com").
566
+ with(headers: {"a" => ["c", "b"]})).to have_been_made
567
+ }.not_to raise_error
353
568
  end
354
569
 
355
570
  it "should fail if request was executed with different headers" do
356
- lambda {
357
- http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
358
- a_request(:get, "www.example.com").
359
- with(:headers => {"a" => ["b", "d"]}).should have_been_made
360
- }.should fail_with(%r(The request GET http://www.example.com/ with headers \{'A'=>\['b', 'd'\]\} was expected to execute 1 time but it executed 0 times))
571
+ expect {
572
+ http_request(:get, "http://www.example.com/", headers: {"a" => ["b", "c"]})
573
+ expect(a_request(:get, "www.example.com").
574
+ with(headers: {"a" => ["b", "d"]})).to have_been_made
575
+ }.to fail_with(%r(The request GET http://www.example.com/ with headers \{'A'=>\['b', 'd'\]\} was expected to execute 1 time but it executed 0 times))
361
576
  end
362
577
  end
363
578
 
364
579
  it "should fail if request was executed with different headers" do
365
- lambda {
366
- http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
367
- a_request(:get, "www.example.com").
368
- with(:headers => { 'Content-Length' => '9999'}).should have_been_made
369
- }.should fail_with(%r(The request GET http://www.example.com/ with headers \{'Content-Length'=>'9999'\} was expected to execute 1 time but it executed 0 times))
580
+ expect {
581
+ http_request(:get, "http://www.example.com/", headers: SAMPLE_HEADERS)
582
+ expect(a_request(:get, "www.example.com").
583
+ with(headers: { 'Content-Length' => '9999'})).to have_been_made
584
+ }.to fail_with(%r(The request GET http://www.example.com/ with headers \{'Content-Length'=>'9999'\} was expected to execute 1 time but it executed 0 times))
370
585
  end
371
586
 
372
587
  it "should fail if request was executed with less headers" do
373
- lambda {
374
- http_request(:get, "http://www.example.com/", :headers => {'A' => 'a'})
375
- a_request(:get, "www.example.com").
376
- with(:headers => {'A' => 'a', 'B' => 'b'}).should have_been_made
377
- }.should fail_with(%r(The request GET http://www.example.com/ with headers \{'A'=>'a', 'B'=>'b'\} was expected to execute 1 time but it executed 0 times))
588
+ expect {
589
+ http_request(:get, "http://www.example.com/", headers: {'A' => 'a'})
590
+ expect(a_request(:get, "www.example.com").
591
+ with(headers: {'A' => 'a', 'B' => 'b'})).to have_been_made
592
+ }.to fail_with(%r(The request GET http://www.example.com/ with headers \{'A'=>'a', 'B'=>'b'\} was expected to execute 1 time but it executed 0 times))
378
593
  end
379
594
 
380
595
  it "should satisfy expectation if request was executed with more headers" do
381
- lambda {
596
+ expect {
382
597
  http_request(:get, "http://www.example.com/",
383
- :headers => {'A' => 'a', 'B' => 'b'}
598
+ headers: {'A' => 'a', 'B' => 'b'}
384
599
  )
385
- a_request(:get, "www.example.com").
386
- with(:headers => {'A' => 'a'}).should have_been_made
387
- }.should_not raise_error
600
+ expect(a_request(:get, "www.example.com").
601
+ with(headers: {'A' => 'a'})).to have_been_made
602
+ }.not_to raise_error
388
603
  end
389
604
 
390
605
  it "should satisfy expectation if request was executed with body and headers but they were not specified in expectantion" do
391
- lambda {
392
- http_request(:get, "http://www.example.com/",
393
- :body => "abc",
394
- :headers => SAMPLE_HEADERS
606
+ expect {
607
+ http_request(:post, "http://www.example.com/",
608
+ body: "abc",
609
+ headers: SAMPLE_HEADERS
395
610
  )
396
- a_request(:get, "www.example.com").should have_been_made
397
- }.should_not raise_error
611
+ expect(a_request(:post, "www.example.com")).to have_been_made
612
+ }.not_to raise_error
398
613
  end
399
614
 
400
615
  it "should satisfy expectation if request was executed with headers matching regular expressions" do
401
- lambda {
402
- http_request(:get, "http://www.example.com/", :headers => { 'some-header' => 'MyAppName' })
403
- a_request(:get, "www.example.com").
404
- with(:headers => { :some_header => /^MyAppName$/ }).should have_been_made
405
- }.should_not raise_error
616
+ expect {
617
+ http_request(:get, "http://www.example.com/", headers: { 'some-header' => 'MyAppName' })
618
+ expect(a_request(:get, "www.example.com").
619
+ with(headers: { some_header: /^MyAppName$/ })).to have_been_made
620
+ }.not_to raise_error
406
621
  end
407
622
 
408
623
  it "should fail if request was executed with headers not matching regular expression" do
409
- lambda {
410
- http_request(:get, "http://www.example.com/", :headers => { 'some-header' => 'xMyAppName' })
411
- a_request(:get, "www.example.com").
412
- with(:headers => { :some_header => /^MyAppName$/ }).should have_been_made
413
- }.should fail_with(%r(The request GET http://www.example.com/ with headers \{'Some-Header'=>/\^MyAppName\$/\} was expected to execute 1 time but it executed 0 times))
624
+ expect {
625
+ http_request(:get, "http://www.example.com/", headers: { 'some-header' => 'xMyAppName' })
626
+ expect(a_request(:get, "www.example.com").
627
+ with(headers: { some_header: /^MyAppName$/ })).to have_been_made
628
+ }.to fail_with(%r(The request GET http://www.example.com/ with headers \{'Some-Header'=>/\^MyAppName\$/\} was expected to execute 1 time but it executed 0 times))
414
629
  end
415
630
  end
416
631
 
417
632
  describe "when expectation contains a request matching block" do
418
633
  it "should satisfy expectation if request was executed and block evaluated to true" do
419
- lambda {
420
- http_request(:post, "http://www.example.com/", :body => "wadus")
421
- a_request(:post, "www.example.com").with { |req| req.body == "wadus" }.should have_been_made
422
- }.should_not raise_error
634
+ expect {
635
+ http_request(:post, "http://www.example.com/", body: "wadus")
636
+ expect(a_request(:post, "www.example.com").with { |req| req.body == "wadus" }).to have_been_made
637
+ }.not_to raise_error
423
638
  end
424
639
 
425
640
  it "should fail if request was executed and block evaluated to false" do
426
- lambda {
427
- http_request(:post, "http://www.example.com/", :body => "abc")
428
- a_request(:post, "www.example.com").with { |req| req.body == "wadus" }.should have_been_made
429
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
641
+ expect {
642
+ http_request(:post, "http://www.example.com/", body: "abc")
643
+ expect(a_request(:post, "www.example.com").with { |req| req.body == "wadus" }).to have_been_made
644
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
430
645
  end
431
646
 
432
647
  it "should fail if request was not expected but it executed and block matched request" do
433
- lambda {
434
- http_request(:post, "http://www.example.com/", :body => "wadus")
435
- a_request(:post, "www.example.com").with { |req| req.body == "wadus" }.should_not have_been_made
436
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
648
+ expect {
649
+ http_request(:post, "http://www.example.com/", body: "wadus")
650
+ expect(a_request(:post, "www.example.com").with { |req| req.body == "wadus" }).not_to have_been_made
651
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
437
652
  end
438
653
  end
439
654
 
440
- describe "with authentication", :unless => (adapter_info.include?(:no_url_auth)) do
655
+ describe "with userinfo", unless: (adapter_info.include?(:no_url_auth)) do
441
656
  before(:each) do
442
657
  stub_request(:any, "http://user:pass@www.example.com")
443
658
  stub_request(:any, "http://user:pazz@www.example.com")
444
659
  end
445
660
 
446
661
  it "should satisfy expectation if request was executed with expected credentials" do
447
- lambda {
662
+ expect {
448
663
  http_request(:get, "http://user:pass@www.example.com/")
449
- a_request(:get, "http://user:pass@www.example.com").should have_been_made.once
450
- }.should_not raise_error
664
+ expect(a_request(:get, "http://user:pass@www.example.com")).to have_been_made.once
665
+ }.not_to raise_error
451
666
  end
452
667
 
453
668
  it "should fail if request was executed with different credentials than expected" do
454
- lambda {
669
+ expect {
455
670
  http_request(:get, "http://user:pass@www.example.com/")
456
- a_request(:get, "http://user:pazz@www.example.com").should have_been_made.once
457
- }.should fail_with(%r(The request GET http://user:pazz@www.example.com/ was expected to execute 1 time but it executed 0 times))
671
+ expect(a_request(:get, "http://user:pazz@www.example.com")).to have_been_made.once
672
+ }.to fail_with(%r(The request GET http://user:pazz@www.example.com/ was expected to execute 1 time but it executed 0 times))
458
673
  end
459
674
 
460
675
  it "should fail if request was executed without credentials and credentials were expected" do
461
- lambda {
676
+ expect {
462
677
  http_request(:get, "http://www.example.com/")
463
- a_request(:get, "http://user:pass@www.example.com").should have_been_made.once
464
- }.should fail_with(%r(The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times))
678
+ expect(a_request(:get, "http://user:pass@www.example.com")).to have_been_made.once
679
+ }.to fail_with(%r(The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times))
465
680
  end
466
681
 
467
682
  it "should fail if request was executed with credentials but expected without credentials" do
468
- lambda {
683
+ expect {
469
684
  http_request(:get, "http://user:pass@www.example.com/")
470
- a_request(:get, "http://www.example.com").should have_been_made.once
471
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
685
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.once
686
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
687
+ end
688
+
689
+ it "should fail if request was executed with basic auth header but expected with credentials in userinfo" do
690
+ expect {
691
+ http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
692
+ expect(a_request(:get, "http://user:pass@www.example.com")).to have_been_made.once
693
+ }.to fail_with(%r(The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times))
472
694
  end
695
+ end
473
696
 
474
- it "should satisfy expectations even if requests were executed in different order than expectations were declared" do
475
- stub_request(:post, "http://www.example.com")
476
- http_request(:post, "http://www.example.com/", :body => "def")
477
- http_request(:post, "http://www.example.com/", :body => "abc")
478
- WebMock.should have_requested(:post, "www.example.com").with(:body => "abc")
479
- WebMock.should have_requested(:post, "www.example.com").with(:body => "def")
697
+ describe "with basic authentication header" do
698
+ before(:each) do
699
+ stub_request(:any, "http://www.example.com").with(basic_auth: ['user', 'pass'])
700
+ stub_request(:any, "http://www.example.com").with(basic_auth: ['user', 'pazz'])
701
+ end
702
+
703
+ it "should satisfy expectation if request was executed with expected credentials" do
704
+ expect {
705
+ http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
706
+ expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
707
+ }.not_to raise_error
708
+ end
709
+
710
+ it "should satisfy expectation if request was executed with expected credentials passed directly as header" do
711
+ expect {
712
+ http_request(:get, "http://www.example.com/", headers: {'Authorization'=>'Basic dXNlcjpwYXNz'})
713
+ expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
714
+ }.not_to raise_error
715
+ end
716
+
717
+ it "should fail if request was executed with different credentials than expected" do
718
+ expect {
719
+ http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
720
+ expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pazz'])).to have_been_made.once
721
+ }.to fail_with(%r(The request GET http://www.example.com/ with headers {'Authorization'=>'Basic dXNlcjpwYXp6'} was expected to execute 1 time but it executed 0 times))
722
+ end
723
+
724
+ it "should fail if request was executed without credentials and credentials were expected" do
725
+ expect {
726
+ http_request(:get, "http://www.example.com/")
727
+ expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
728
+ }.to fail_with(%r(The request GET http://www.example.com/ with headers {'Authorization'=>'Basic dXNlcjpwYXNz'} was expected to execute 1 time but it executed 0 times))
729
+ end
730
+
731
+ it "should not fail if request was executed with credentials but expected despite credentials" do
732
+ expect {
733
+ http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
734
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.once
735
+ }.not_to raise_error
736
+ end
737
+
738
+ it "should fail if request was executed with basic auth header and credentials were provided in url", unless: (adapter_info.include?(:no_url_auth)) do
739
+ expect {
740
+ stub_request(:any, "http://user:pass@www.example.com")
741
+ http_request(:get, "http://user:pass@www.example.com/")
742
+ expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
743
+ }.to fail_with(%r(The request GET http://www.example.com/ with headers {'Authorization'=>'Basic dXNlcjpwYXNz'} was expected to execute 1 time but it executed 0 times))
480
744
  end
481
745
  end
482
746
 
483
747
  describe "when expectations were set on WebMock object" do
484
748
  it "should satisfy expectation if expected request was made" do
485
- lambda {
749
+ expect {
486
750
  http_request(:get, "http://www.example.com/")
487
- WebMock.should have_requested(:get, "http://www.example.com").once
488
- }.should_not raise_error
751
+ expect(WebMock).to have_requested(:get, "http://www.example.com").once
752
+ }.not_to raise_error
489
753
  end
490
754
 
491
755
  it "should satisfy expectation if request with body and headers was expected and request was made" do
492
- lambda {
493
- http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
494
- WebMock.should have_requested(:post, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'}).once
495
- }.should_not raise_error
756
+ expect {
757
+ http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'a'})
758
+ expect(WebMock).to have_requested(:post, "http://www.example.com").with(body: "abc", headers: {'A' => 'a'}).once
759
+ }.not_to raise_error
496
760
  end
497
761
 
498
762
  it "should fail if request expected not to be made was made" do
499
- lambda {
763
+ expect {
500
764
  http_request(:get, "http://www.example.com/")
501
- WebMock.should_not have_requested(:get, "http://www.example.com")
502
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
765
+ expect(WebMock).not_to have_requested(:get, "http://www.example.com")
766
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
503
767
  end
504
768
 
505
769
  it "should satisfy expectation if request was executed and expectation had block which evaluated to true" do
506
- lambda {
507
- http_request(:post, "http://www.example.com/", :body => "wadus")
508
- WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
509
- }.should_not raise_error
770
+ expect {
771
+ http_request(:post, "http://www.example.com/", body: "wadus")
772
+ expect(WebMock).to have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
773
+ }.not_to raise_error
510
774
  end
511
775
 
512
776
  it "should fail if request was executed and expectation had block which evaluated to false" do
513
- lambda {
514
- http_request(:post, "http://www.example.com/", :body => "abc")
515
- WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
516
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
777
+ expect {
778
+ http_request(:post, "http://www.example.com/", body: "abc")
779
+ expect(WebMock).to have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
780
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
517
781
  end
518
782
 
519
783
  it "should fail if request was expected not to be made but was made and block matched request" do
520
- lambda {
521
- http_request(:post, "http://www.example.com/", :body => "wadus")
522
- WebMock.should_not have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
523
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
784
+ expect {
785
+ http_request(:post, "http://www.example.com/", body: "wadus")
786
+ expect(WebMock).not_to have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
787
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
524
788
  end
525
789
  end
526
790
 
527
791
  describe "when expectation is declared using assert_requested" do
528
792
  it "should satisfy expectation if requests was made" do
529
- lambda {
793
+ expect {
530
794
  http_request(:get, "http://www.example.com/")
531
- assert_requested(:get, "http://www.example.com", :times => 1)
795
+ assert_requested(:get, "http://www.example.com", times: 1)
532
796
  assert_requested(:get, "http://www.example.com")
533
- }.should_not raise_error
797
+ }.not_to raise_error
534
798
  end
535
799
 
536
800
  it "should satisfy expectation if request was made with body and headers" do
537
- lambda {
538
- http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
539
- assert_requested(:post, "http://www.example.com", :body => "abc", :headers => {'A' => 'a'})
540
- }.should_not raise_error
801
+ expect {
802
+ http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'a'})
803
+ assert_requested(:post, "http://www.example.com", body: "abc", headers: {'A' => 'a'})
804
+ }.not_to raise_error
541
805
  end
542
806
 
543
807
  it "should fail if request expected not to be made was not wade" do
544
- lambda {
808
+ expect {
545
809
  http_request(:get, "http://www.example.com/")
546
810
  assert_not_requested(:get, "http://www.example.com")
547
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
811
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
548
812
  end
549
813
 
550
814
  it "should fail if request expected not to be made was made and expectation block evaluated to true" do
551
- lambda {
552
- http_request(:post, "http://www.example.com/", :body => "wadus")
815
+ expect {
816
+ http_request(:post, "http://www.example.com/", body: "wadus")
553
817
  assert_not_requested(:post, "www.example.com") { |req| req.body == "wadus" }
554
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
818
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
555
819
  end
556
820
 
557
821
  it "should satisfy expectation if request was made and expectation block evaluated to true" do
558
- lambda {
559
- http_request(:post, "http://www.example.com/", :body => "wadus")
822
+ expect {
823
+ http_request(:post, "http://www.example.com/", body: "wadus")
560
824
  assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
561
- }.should_not raise_error
825
+ }.not_to raise_error
562
826
  end
563
827
 
564
828
  it "should fail if request was made and expectation block evaluated to false" do
565
- lambda {
566
- http_request(:post, "http://www.example.com/", :body => "abc")
829
+ expect {
830
+ http_request(:post, "http://www.example.com/", body: "abc")
567
831
  assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
568
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
832
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
569
833
  end
570
834
  end
571
835
 
572
836
  describe "when expectation is declared using assert_requested" do
573
837
  it "should satisfy expectation if requests was made" do
574
838
  stub_http = stub_http_request(:get, "http://www.example.com")
575
- lambda {
839
+ expect {
576
840
  http_request(:get, "http://www.example.com/")
577
- assert_requested(stub_http, :times => 1)
841
+ assert_requested(stub_http, times: 1)
578
842
  assert_requested(stub_http)
579
- }.should_not raise_error
843
+ }.not_to raise_error
580
844
  end
581
845
 
582
846
  it "should fail if request expected not to be made was not wade" do
583
847
  stub_http = stub_http_request(:get, "http://www.example.com")
584
- lambda {
848
+ expect {
585
849
  http_request(:get, "http://www.example.com/")
586
850
  assert_not_requested(stub_http)
587
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
851
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
588
852
  end
589
853
  end
590
854
  end
@@ -594,72 +858,72 @@ shared_context "request expectations" do |*adapter_info|
594
858
  it "should satisfy expectation if expected request was made" do
595
859
  stub = stub_request(:get, "http://www.example.com/")
596
860
  http_request(:get, "http://www.example.com/")
597
- stub.should have_been_requested.once
861
+ expect(stub).to have_been_requested.once
598
862
  end
599
863
 
600
864
  it "should satisfy expectations if subsequent requests were made" do
601
865
  stub = stub_request(:get, "http://www.example.com/")
602
866
  http_request(:get, "http://www.example.com/")
603
- stub.should have_been_requested.once
867
+ expect(stub).to have_been_requested.once
604
868
  http_request(:get, "http://www.example.com/")
605
- stub.should have_been_requested.twice
869
+ expect(stub).to have_been_requested.twice
606
870
  end
607
871
 
608
872
  it "should satisfy expectation if expected request with body and headers was made" do
609
- stub = stub_request(:post, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'})
610
- http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
611
- stub.should have_been_requested.once
873
+ stub = stub_request(:post, "http://www.example.com").with(body: "abc", headers: {'A' => 'a'})
874
+ http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'a'})
875
+ expect(stub).to have_been_requested.once
612
876
  end
613
877
 
614
878
  it "should fail if request not expected to be made was made" do
615
- lambda {
879
+ expect {
616
880
  stub = stub_request(:get, "http://www.example.com")
617
881
  http_request(:get, "http://www.example.com/")
618
- stub.should_not have_been_requested
619
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
882
+ expect(stub).not_to have_been_requested
883
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
620
884
  end
621
885
 
622
886
  it "should fail request not expected to be made was made and expectation block evaluated to true" do
623
- lambda {
887
+ expect {
624
888
  stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
625
- http_request(:post, "http://www.example.com/", :body => "wadus")
626
- stub.should_not have_been_requested
627
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
889
+ http_request(:post, "http://www.example.com/", body: "wadus")
890
+ expect(stub).not_to have_been_requested
891
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
628
892
  end
629
893
 
630
894
  it "should satisfy expectation if request was made and expectation block evaluated to true" do
631
895
  stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
632
- http_request(:post, "http://www.example.com/", :body => "wadus")
633
- stub.should have_been_requested
896
+ http_request(:post, "http://www.example.com/", body: "wadus")
897
+ expect(stub).to have_been_requested
634
898
  end
635
899
 
636
900
  it "should satisfy expectation if request was made and expectation block evaluated to false" do
637
- lambda {
901
+ expect {
638
902
  stub_request(:any, /.+/) #stub any request
639
903
  stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
640
- http_request(:post, "http://www.example.com/", :body => "abc")
641
- stub.should have_been_requested
642
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
904
+ http_request(:post, "http://www.example.com/", body: "abc")
905
+ expect(stub).to have_been_requested
906
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
643
907
  end
644
908
  end
645
909
 
646
- describe "when net connect is allowed", :net_connect => true do
910
+ describe "when net connect is allowed", net_connect: true do
647
911
  before(:each) do
648
912
  WebMock.allow_net_connect!
649
913
  end
650
914
 
651
915
  it "should satisfy expectation if expected requests was made" do
652
- lambda {
916
+ expect {
653
917
  http_request(:get, "http://www.example.com/")
654
- a_request(:get, "http://www.example.com").should have_been_made
655
- }.should_not raise_error
918
+ expect(a_request(:get, "http://www.example.com")).to have_been_made
919
+ }.not_to raise_error
656
920
  end
657
921
 
658
922
  it "should fail request expected not to be made, was made" do
659
- lambda {
923
+ expect {
660
924
  http_request(:get, "http://www.example.com/")
661
- a_request(:get, "http://www.example.com").should_not have_been_made
662
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
925
+ expect(a_request(:get, "http://www.example.com")).not_to have_been_made
926
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
663
927
  end
664
928
  end
665
929
  end