webmock 1.8.6 → 3.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -5,16 +5,16 @@ describe WebMock::ResponseFactory do
5
5
  describe "response_for" do
6
6
 
7
7
  it "should create response with options passed as arguments" do
8
- options = {:body => "abc", :headers => {:a => :b}}
9
- WebMock::Response.should_receive(:new).with(options).and_return(@response = mock(WebMock::Response))
10
- WebMock::ResponseFactory.response_for(options).should == @response
8
+ options = {body: "abc", headers: {a: :b}}
9
+ expect(WebMock::Response).to receive(:new).with(options).and_return(@response = double(WebMock::Response))
10
+ expect(WebMock::ResponseFactory.response_for(options)).to eq(@response)
11
11
  end
12
12
 
13
13
 
14
14
  it "should create dynamic response for argument responding to call" do
15
- callable = mock(:call => {:body => "abc"})
16
- WebMock::DynamicResponse.should_receive(:new).with(callable).and_return(@response = mock(WebMock::Response))
17
- WebMock::ResponseFactory.response_for(callable).should == @response
15
+ callable = double(call: {body: "abc"})
16
+ expect(WebMock::DynamicResponse).to receive(:new).with(callable).and_return(@response = double(WebMock::Response))
17
+ expect(WebMock::ResponseFactory.response_for(callable)).to eq(@response)
18
18
  end
19
19
 
20
20
  end
@@ -23,54 +23,56 @@ end
23
23
 
24
24
  describe WebMock::Response do
25
25
  before(:each) do
26
- @response = WebMock::Response.new(:headers => {'A' => 'a'})
26
+ @response = WebMock::Response.new(headers: {'A' => 'a'})
27
+ end
28
+
29
+ it "should raise an error when initialized with unknown option" do
30
+ expect { WebMock::Response.new(foo: "bar") }.to raise_error('Unknown key: "foo". Valid keys are: "headers", "status", "body", "exception", "should_timeout"')
27
31
  end
28
32
 
29
33
  it "should report normalized headers" do
30
- WebMock::Util::Headers.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
31
- @response = WebMock::Response.new(:headers => {'A' => 'a'})
32
- @response.headers.should == {'B' => 'b'}
34
+ expect(WebMock::Util::Headers).to receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
35
+ @response = WebMock::Response.new(headers: {'A' => 'a'})
36
+ expect(@response.headers).to eq({'B' => 'b'})
33
37
  end
34
38
 
35
39
  describe "status" do
36
-
37
40
  it "should have 200 code and empty message by default" do
38
- @response.status.should == [200, ""]
41
+ expect(@response.status).to eq([200, ""])
39
42
  end
40
43
 
41
44
  it "should return assigned status" do
42
- @response = WebMock::Response.new(:status => 500)
43
- @response.status.should == [500, ""]
45
+ @response = WebMock::Response.new(status: 500)
46
+ expect(@response.status).to eq([500, ""])
44
47
  end
45
48
 
46
49
  it "should return assigned message" do
47
- @response = WebMock::Response.new(:status => [500, "Internal Server Error"])
48
- @response.status.should == [500, "Internal Server Error"]
50
+ @response = WebMock::Response.new(status: [500, "Internal Server Error"])
51
+ expect(@response.status).to eq([500, "Internal Server Error"])
49
52
  end
50
-
51
53
  end
52
54
 
53
55
  describe "raising error" do
54
56
 
55
57
  it "should raise error if any assigned" do
56
- @response = WebMock::Response.new(:exception => ArgumentError)
57
- lambda {
58
+ @response = WebMock::Response.new(exception: ArgumentError)
59
+ expect {
58
60
  @response.raise_error_if_any
59
- }.should raise_error(ArgumentError, "Exception from WebMock")
61
+ }.to raise_error(ArgumentError, "Exception from WebMock")
60
62
  end
61
63
 
62
64
  it "should raise error if any assigned as instance" do
63
- @response = WebMock::Response.new(:exception => ArgumentError.new("hello world"))
64
- lambda {
65
+ @response = WebMock::Response.new(exception: ArgumentError.new("hello world"))
66
+ expect {
65
67
  @response.raise_error_if_any
66
- }.should raise_error(ArgumentError, "hello world")
68
+ }.to raise_error(ArgumentError, "hello world")
67
69
  end
68
70
 
69
71
  it "should raise error if any assigned as string" do
70
- @response = WebMock::Response.new(:exception => "hello world")
71
- lambda {
72
+ @response = WebMock::Response.new(exception: "hello world")
73
+ expect {
72
74
  @response.raise_error_if_any
73
- }.should raise_error("hello world")
75
+ }.to raise_error("hello world")
74
76
  end
75
77
 
76
78
  it "should not raise error if no error assigned" do
@@ -82,13 +84,13 @@ describe WebMock::Response do
82
84
  describe "timeout" do
83
85
 
84
86
  it "should know if it should timeout" do
85
- @response = WebMock::Response.new(:should_timeout => true)
86
- @response.should_timeout.should be_true
87
+ @response = WebMock::Response.new(should_timeout: true)
88
+ expect(@response.should_timeout).to be_truthy
87
89
  end
88
90
 
89
91
  it "should not timeout by default" do
90
92
  @response = WebMock::Response.new
91
- @response.should_timeout.should be_false
93
+ expect(@response.should_timeout).to be_falsey
92
94
  end
93
95
 
94
96
  end
@@ -96,30 +98,47 @@ describe WebMock::Response do
96
98
  describe "body" do
97
99
 
98
100
  it "should return empty body by default" do
99
- @response.body.should == ''
101
+ expect(@response.body).to eq('')
100
102
  end
101
103
 
102
104
  it "should report body if assigned" do
103
- @response = WebMock::Response.new(:body => "abc")
104
- @response.body.should == "abc"
105
+ @response = WebMock::Response.new(body: "abc")
106
+ expect(@response.body).to eq("abc")
105
107
  end
106
108
 
107
109
  it "should report string even if existing file path was provided" do
108
- @response = WebMock::Response.new(:body => __FILE__)
109
- @response.body.should == __FILE__
110
+ @response = WebMock::Response.new(body: __FILE__)
111
+ expect(@response.body).to eq(__FILE__)
110
112
  end
111
113
 
112
114
  it "should report content of a IO object if provided" do
113
- @response = WebMock::Response.new(:body => File.new(__FILE__))
114
- @response.body.should == File.new(__FILE__).read
115
+ @response = WebMock::Response.new(body: File.new(__FILE__))
116
+ expect(@response.body).to eq(File.read(__FILE__))
115
117
  end
116
118
 
117
119
  it "should report many times content of a IO object if provided" do
118
- @response = WebMock::Response.new(:body => File.new(__FILE__))
119
- @response.body.should == File.new(__FILE__).read
120
- @response.body.should == File.new(__FILE__).read
120
+ @response = WebMock::Response.new(body: File.new(__FILE__))
121
+ expect(@response.body).to eq(File.read(__FILE__))
122
+ expect(@response.body).to eq(File.read(__FILE__))
123
+ end
124
+
125
+ it "should work with Pathnames" do
126
+ @response = WebMock::Response.new(body: Pathname.new(__FILE__))
127
+ expect(@response.body).to eq(File.read(__FILE__))
128
+ end
129
+
130
+ # Users of webmock commonly make the mistake of stubbing the response
131
+ # body to return a hash, to prevent this:
132
+ #
133
+ it "should error if given a non-allowed type: a hash" do
134
+ expect { WebMock::Response.new(body: Hash.new) }.to \
135
+ raise_error(WebMock::Response::InvalidBody)
121
136
  end
122
137
 
138
+ it "should error if given a non-allowed type: something that is not a hash" do
139
+ expect { WebMock::Response.new(body: 123) }.to \
140
+ raise_error(WebMock::Response::InvalidBody)
141
+ end
123
142
  end
124
143
 
125
144
  describe "from raw response" do
@@ -132,57 +151,57 @@ describe WebMock::Response do
132
151
 
133
152
 
134
153
  it "should read status" do
135
- @response.status.should == [202, "OK"]
154
+ expect(@response.status).to eq([202, "OK"])
136
155
  end
137
156
 
138
157
  it "should read headers" do
139
- @response.headers.should == {
140
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
141
- "Content-Type"=>"text/html; charset=UTF-8",
142
- "Content-Length"=>"419",
143
- "Connection"=>"Keep-Alive",
144
- "Accept"=>"image/jpeg, image/png"
145
- }
158
+ expect(@response.headers).to eq({
159
+ "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
160
+ "Content-Type"=>"text/html; charset=UTF-8",
161
+ "Content-Length"=>"419",
162
+ "Connection"=>"Keep-Alive",
163
+ "Accept"=>"image/jpeg, image/png"
164
+ })
146
165
  end
147
166
 
148
167
  it "should read body" do
149
- @response.body.size.should == 419
168
+ expect(@response.body.size).to eq(419)
150
169
  end
151
170
 
152
171
  it "should close IO" do
153
- @file.should be_closed
172
+ expect(@file).to be_closed
154
173
  end
155
174
 
156
175
  end
157
176
 
158
177
  describe "when input is String" do
159
178
  before(:each) do
160
- @input = File.new(CURL_EXAMPLE_OUTPUT_PATH).read
179
+ @input = File.read(CURL_EXAMPLE_OUTPUT_PATH)
161
180
  @response = WebMock::Response.new(@input)
162
181
  end
163
182
 
164
183
  it "should read status" do
165
- @response.status.should == [202, "OK"]
184
+ expect(@response.status).to eq([202, "OK"])
166
185
  end
167
186
 
168
187
  it "should read headers" do
169
- @response.headers.should == {
170
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
171
- "Content-Type"=>"text/html; charset=UTF-8",
172
- "Content-Length"=>"419",
173
- "Connection"=>"Keep-Alive",
174
- "Accept"=>"image/jpeg, image/png"
175
- }
188
+ expect(@response.headers).to eq({
189
+ "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
190
+ "Content-Type"=>"text/html; charset=UTF-8",
191
+ "Content-Length"=>"419",
192
+ "Connection"=>"Keep-Alive",
193
+ "Accept"=>"image/jpeg, image/png"
194
+ })
176
195
  end
177
196
 
178
197
  it "should read body" do
179
- @response.body.size.should == 419
198
+ expect(@response.body.size).to eq(419)
180
199
  end
181
200
 
182
201
  it "should work with transfer-encoding set to chunked" do
183
202
  @input.gsub!("Content-Length: 419", "Transfer-Encoding: chunked")
184
203
  @response = WebMock::Response.new(@input)
185
- @response.body.size.should == 419
204
+ expect(@response.body.size).to eq(419)
186
205
  end
187
206
 
188
207
  end
@@ -190,22 +209,22 @@ describe WebMock::Response do
190
209
  describe "with dynamically evaluated options" do
191
210
 
192
211
  before(:each) do
193
- @request_signature = WebMock::RequestSignature.new(:post, "www.example.com", :body => "abc", :headers => {'A' => 'a'})
212
+ @request_signature = WebMock::RequestSignature.new(:post, "www.example.com", body: "abc", headers: {'A' => 'a'})
194
213
  end
195
214
 
196
215
  it "should have evaluated body" do
197
- @response = WebMock::Response.new(:body => lambda {|request| request.body})
198
- @response.evaluate(@request_signature).body.should == "abc"
216
+ @response = WebMock::Response.new(body: lambda {|request| request.body})
217
+ expect(@response.evaluate(@request_signature).body).to eq("abc")
199
218
  end
200
219
 
201
220
  it "should have evaluated headers" do
202
- @response = WebMock::Response.new(:headers => lambda {|request| request.headers})
203
- @response.evaluate(@request_signature).headers.should == {'A' => 'a'}
221
+ @response = WebMock::Response.new(headers: lambda {|request| request.headers})
222
+ expect(@response.evaluate(@request_signature).headers).to eq({'A' => 'a'})
204
223
  end
205
224
 
206
225
  it "should have evaluated status" do
207
- @response = WebMock::Response.new(:status => lambda {|request| 302})
208
- @response.evaluate(@request_signature).status.should == [302, ""]
226
+ @response = WebMock::Response.new(status: lambda {|request| 302})
227
+ expect(@response.evaluate(@request_signature).status).to eq([302, ""])
209
228
  end
210
229
 
211
230
  end
@@ -217,25 +236,25 @@ describe WebMock::Response do
217
236
  describe "evaluating response options" do
218
237
 
219
238
  it "should evaluate new response with evaluated options" do
220
- request_signature = WebMock::RequestSignature.new(:post, "www.example.com", :body => "abc", :headers => {'A' => 'a'})
239
+ request_signature = WebMock::RequestSignature.new(:post, "www.example.com", body: "abc", headers: {'A' => 'a'})
221
240
  response = WebMock::DynamicResponse.new(lambda {|request|
222
- {
223
- :body => request.body,
224
- :headers => request.headers,
225
- :status => 302
226
- }
241
+ {
242
+ body: request.body,
243
+ headers: request.headers,
244
+ status: 302
245
+ }
227
246
  })
228
247
  evaluated_response = response.evaluate(request_signature)
229
- evaluated_response.body.should == "abc"
230
- evaluated_response.headers.should == {'A' => 'a'}
231
- evaluated_response.status.should == [302, ""]
248
+ expect(evaluated_response.body).to eq("abc")
249
+ expect(evaluated_response.headers).to eq({'A' => 'a'})
250
+ expect(evaluated_response.status).to eq([302, ""])
232
251
  end
233
252
 
234
253
  it "should be equal to static response after evaluation" do
235
- request_signature = WebMock::RequestSignature.new(:post, "www.example.com", :body => "abc")
236
- response = WebMock::DynamicResponse.new(lambda {|request| {:body => request.body}})
254
+ request_signature = WebMock::RequestSignature.new(:post, "www.example.com", body: "abc")
255
+ response = WebMock::DynamicResponse.new(lambda {|request| {body: request.body}})
237
256
  evaluated_response = response.evaluate(request_signature)
238
- evaluated_response.should == WebMock::Response.new(:body => "abc")
257
+ expect(evaluated_response).to eq(WebMock::Response.new(body: "abc"))
239
258
  end
240
259
 
241
260
  describe "when raw response is evaluated" do
@@ -249,14 +268,14 @@ describe WebMock::Response do
249
268
  describe "as a file" do
250
269
  it "should return response" do
251
270
  response = WebMock::DynamicResponse.new(lambda {|request| @files[request.uri.host.to_s] })
252
- response.evaluate(@request_signature).body.size.should == 419
271
+ expect(response.evaluate(@request_signature).body.size).to eq(419)
253
272
  end
254
273
  end
255
274
 
256
275
  describe "as a string" do
257
276
  it "should return response" do
258
277
  response = WebMock::DynamicResponse.new(lambda {|request| @files[request.uri.host.to_s].read })
259
- response.evaluate(@request_signature).body.size.should == 419
278
+ expect(response.evaluate(@request_signature).body.size).to eq(419)
260
279
  end
261
280
  end
262
281
  end
@@ -9,31 +9,40 @@ describe WebMock::StubRegistry do
9
9
  @request_stub = WebMock::RequestStub.new(:get, "www.example.com")
10
10
  end
11
11
 
12
+ describe "remove_request_stub" do
13
+ it "should remove stub from registry" do
14
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub)
15
+ expect(WebMock::StubRegistry.instance.registered_request?(@request_signature)).to eq(@request_stub)
16
+ WebMock::StubRegistry.instance.remove_request_stub(@request_stub)
17
+ expect(WebMock::StubRegistry.instance.registered_request?(@request_signature)).to eq(nil)
18
+ end
19
+ end
20
+
12
21
  describe "reset!" do
13
22
  before(:each) do
14
23
  WebMock::StubRegistry.instance.register_request_stub(@request_stub)
15
24
  end
16
25
 
17
26
  it "should clean request stubs" do
18
- WebMock::StubRegistry.instance.registered_request?(@request_signature).should == @request_stub
27
+ expect(WebMock::StubRegistry.instance.registered_request?(@request_signature)).to eq(@request_stub)
19
28
  WebMock::StubRegistry.instance.reset!
20
- WebMock::StubRegistry.instance.registered_request?(@request_signature).should == nil
29
+ expect(WebMock::StubRegistry.instance.registered_request?(@request_signature)).to eq(nil)
21
30
  end
22
31
  end
23
32
 
24
33
  describe "registering and reporting registered requests" do
25
34
 
26
35
  it "should return registered stub" do
27
- WebMock::StubRegistry.instance.register_request_stub(@request_stub).should == @request_stub
36
+ expect(WebMock::StubRegistry.instance.register_request_stub(@request_stub)).to eq(@request_stub)
28
37
  end
29
38
 
30
39
  it "should report if request stub is not registered" do
31
- WebMock::StubRegistry.instance.registered_request?(@request_signature).should == nil
40
+ expect(WebMock::StubRegistry.instance.registered_request?(@request_signature)).to eq(nil)
32
41
  end
33
42
 
34
- it "should register and report registered stib" do
43
+ it "should register and report registered stub" do
35
44
  WebMock::StubRegistry.instance.register_request_stub(@request_stub)
36
- WebMock::StubRegistry.instance.registered_request?(@request_signature).should == @request_stub
45
+ expect(WebMock::StubRegistry.instance.registered_request?(@request_signature)).to eq(@request_stub)
37
46
  end
38
47
 
39
48
 
@@ -42,43 +51,51 @@ describe WebMock::StubRegistry do
42
51
  describe "response for request" do
43
52
 
44
53
  it "should report registered evaluated response for request pattern" do
45
- @request_stub.to_return(:body => "abc")
54
+ @request_stub.to_return(body: "abc")
46
55
  WebMock::StubRegistry.instance.register_request_stub(@request_stub)
47
- WebMock::StubRegistry.instance.response_for_request(@request_signature).
48
- should == WebMock::Response.new(:body => "abc")
56
+ expect(WebMock::StubRegistry.instance.response_for_request(@request_signature)).
57
+ to eq(WebMock::Response.new(body: "abc"))
49
58
  end
50
59
 
51
60
  it "should report evaluated response" do
52
- @request_stub.to_return {|request| {:body => request.method.to_s} }
61
+ @request_stub.to_return {|request| {body: request.method.to_s} }
53
62
  WebMock::StubRegistry.instance.register_request_stub(@request_stub)
54
63
  response1 = WebMock::StubRegistry.instance.response_for_request(@request_signature)
55
- response1.should == WebMock::Response.new(:body => "get")
64
+ expect(response1).to eq(WebMock::Response.new(body: "get"))
65
+ end
66
+
67
+ it "should report clone of the response" do
68
+ @request_stub.to_return(body: lambda{|r| r.method.to_s})
69
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub)
70
+ response1 = WebMock::StubRegistry.instance.response_for_request(@request_signature)
71
+ response2 = WebMock::StubRegistry.instance.response_for_request(@request_signature)
72
+ expect(response1).not_to be(response2)
56
73
  end
57
74
 
58
- it "should report clone of theresponse" do
59
- @request_stub.to_return {|request| {:body => request.method.to_s} }
75
+ it "should report clone of the dynamic response" do
76
+ @request_stub.to_return {|request| {body: request.method.to_s} }
60
77
  WebMock::StubRegistry.instance.register_request_stub(@request_stub)
61
78
  response1 = WebMock::StubRegistry.instance.response_for_request(@request_signature)
62
79
  response2 = WebMock::StubRegistry.instance.response_for_request(@request_signature)
63
- response1.should_not be(response2)
80
+ expect(response1).not_to be(response2)
64
81
  end
65
82
 
66
83
  it "should report nothing if no response for request is registered" do
67
- WebMock::StubRegistry.instance.response_for_request(@request_signature).should == nil
84
+ expect(WebMock::StubRegistry.instance.response_for_request(@request_signature)).to eq(nil)
68
85
  end
69
86
 
70
87
  it "should always return last registered matching response" do
71
88
  @request_stub1 = WebMock::RequestStub.new(:get, "www.example.com")
72
- @request_stub1.to_return(:body => "abc")
89
+ @request_stub1.to_return(body: "abc")
73
90
  @request_stub2 = WebMock::RequestStub.new(:get, "www.example.com")
74
- @request_stub2.to_return(:body => "def")
91
+ @request_stub2.to_return(body: "def")
75
92
  @request_stub3 = WebMock::RequestStub.new(:get, "www.example.org")
76
- @request_stub3.to_return(:body => "ghj")
93
+ @request_stub3.to_return(body: "ghj")
77
94
  WebMock::StubRegistry.instance.register_request_stub(@request_stub1)
78
95
  WebMock::StubRegistry.instance.register_request_stub(@request_stub2)
79
96
  WebMock::StubRegistry.instance.register_request_stub(@request_stub3)
80
- WebMock::StubRegistry.instance.response_for_request(@request_signature).
81
- should == WebMock::Response.new(:body => "def")
97
+ expect(WebMock::StubRegistry.instance.response_for_request(@request_signature)).
98
+ to eq(WebMock::Response.new(body: "def"))
82
99
  end
83
100
 
84
101
  end
@@ -4,57 +4,57 @@ describe WebMock::StubRequestSnippet do
4
4
  describe "to_s" do
5
5
  describe "GET" do
6
6
  before(:each) do
7
- @request_signature = WebMock::RequestSignature.new(:get, "www.example.com/?a=b&c=d", :headers => {})
7
+ @request_signature = WebMock::RequestSignature.new(:get, "www.example.com/?a=b&c=d", headers: {})
8
8
  end
9
9
 
10
10
  it "should print stub request snippet with url with params and method and empty successful response" do
11
- expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").\n to_return(:status => 200, :body => "", :headers => {}))
11
+ expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").\n to_return(status: 200, body: "", headers: {}))
12
12
  @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
13
- WebMock::StubRequestSnippet.new(@request_stub).to_s.should == expected
13
+ expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
14
14
  end
15
15
 
16
16
  it "should print stub request snippet with body if available" do
17
17
  @request_signature.body = "abcdef"
18
18
  expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").)+
19
- "\n with(:body => \"abcdef\")." +
20
- "\n to_return(:status => 200, :body => \"\", :headers => {})"
19
+ "\n with(\n body: \"abcdef\")." +
20
+ "\n to_return(status: 200, body: \"\", headers: {})"
21
21
  @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
22
- WebMock::StubRequestSnippet.new(@request_stub).to_s.should == expected
22
+ expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
23
23
  end
24
24
 
25
25
  it "should print stub request snippet with multiline body" do
26
26
  @request_signature.body = "abc\ndef"
27
27
  expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").)+
28
- "\n with(:body => \"abc\\ndef\")." +
29
- "\n to_return(:status => 200, :body => \"\", :headers => {})"
28
+ "\n with(\n body: \"abc\\ndef\")." +
29
+ "\n to_return(status: 200, body: \"\", headers: {})"
30
30
  @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
31
- WebMock::StubRequestSnippet.new(@request_stub).to_s.should == expected
31
+ expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
32
32
  end
33
33
 
34
34
  it "should print stub request snippet with headers if any" do
35
35
  @request_signature.headers = {'B' => 'b', 'A' => 'a'}
36
36
  expected = 'stub_request(:get, "http://www.example.com/?a=b&c=d").'+
37
- "\n with(:headers => {\'A\'=>\'a\', \'B\'=>\'b\'})." +
38
- "\n to_return(:status => 200, :body => \"\", :headers => {})"
37
+ "\n with(\n headers: {\n\t\ 'A\'=>\'a\',\n\t \'B\'=>\'b\'\n })." +
38
+ "\n to_return(status: 200, body: \"\", headers: {})"
39
39
  @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
40
- WebMock::StubRequestSnippet.new(@request_stub).to_s.should == expected
40
+ expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
41
41
  end
42
42
 
43
43
  it "should print stub request snippet with body and headers" do
44
44
  @request_signature.body = "abcdef"
45
45
  @request_signature.headers = {'B' => 'b', 'A' => 'a'}
46
46
  expected = 'stub_request(:get, "http://www.example.com/?a=b&c=d").'+
47
- "\n with(:body => \"abcdef\",\n :headers => {\'A\'=>\'a\', \'B\'=>\'b\'})." +
48
- "\n to_return(:status => 200, :body => \"\", :headers => {})"
47
+ "\n with(\n body: \"abcdef\",\n headers: {\n\t \'A\'=>\'a\',\n\t \'B\'=>\'b\'\n })." +
48
+ "\n to_return(status: 200, body: \"\", headers: {})"
49
49
  @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
50
- WebMock::StubRequestSnippet.new(@request_stub).to_s.should == expected
50
+ expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected)
51
51
  end
52
52
 
53
53
  it "should not print to_return part if not wanted" do
54
54
  expected = 'stub_request(:get, "http://www.example.com/").'+
55
- "\n with(:body => \"abcdef\")"
56
- stub = WebMock::RequestStub.new(:get, "www.example.com").with(:body => "abcdef").to_return(:body => "hello")
57
- WebMock::StubRequestSnippet.new(stub).to_s(false).should == expected
55
+ "\n with(\n body: \"abcdef\")"
56
+ stub = WebMock::RequestStub.new(:get, "www.example.com").with(body: "abcdef").to_return(body: "hello")
57
+ expect(WebMock::StubRequestSnippet.new(stub).to_s(false)).to eq(expected)
58
58
  end
59
59
  end
60
60
 
@@ -63,33 +63,53 @@ describe WebMock::StubRequestSnippet do
63
63
  let(:multipart_form_body) { 'complicated stuff--ABC123--goes here' }
64
64
  it "should print stub request snippet with body as a hash using rails conventions on form posts" do
65
65
  @request_signature = WebMock::RequestSignature.new(:post, "www.example.com",
66
- :headers => {'Content-Type' => 'application/x-www-form-urlencoded'},
67
- :body => form_body)
66
+ headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
67
+ body: form_body)
68
68
  @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
69
69
  expected = <<-STUB
70
70
  stub_request(:post, "http://www.example.com/").
71
- with(:body => {"user"=>{"first_name"=>"Bartosz"}},
72
- :headers => {'Content-Type'=>'application/x-www-form-urlencoded'}).
73
- to_return(:status => 200, :body => \"\", :headers => {})
71
+ with(
72
+ body: {"user"=>{"first_name"=>"Bartosz"}},
73
+ headers: {
74
+ \t 'Content-Type'=>'application/x-www-form-urlencoded'
75
+ }).
76
+ to_return(status: 200, body: \"\", headers: {})
74
77
  STUB
75
- WebMock::StubRequestSnippet.new(@request_stub).to_s.should == expected.strip
78
+ expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected.strip)
76
79
  end
77
80
 
78
81
  it "should print stub request snippet leaving body as string when not a urlencoded form" do
79
82
  @request_signature = WebMock::RequestSignature.new(:post, "www.example.com",
80
- :headers => {'Content-Type' => 'multipart/form-data; boundary=ABC123'},
81
- :body => multipart_form_body)
83
+ headers: {'Content-Type' => 'multipart/form-data; boundary=ABC123'},
84
+ body: multipart_form_body)
82
85
  @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
83
86
  expected = <<-STUB
84
87
  stub_request(:post, "http://www.example.com/").
85
- with(:body => "#{multipart_form_body}",
86
- :headers => {'Content-Type'=>'multipart/form-data; boundary=ABC123'}).
87
- to_return(:status => 200, :body => \"\", :headers => {})
88
+ with(
89
+ body: "#{multipart_form_body}",
90
+ headers: {
91
+ \t 'Content-Type'=>'multipart/form-data; boundary=ABC123'
92
+ }).
93
+ to_return(status: 200, body: \"\", headers: {})
88
94
  STUB
89
- WebMock::StubRequestSnippet.new(@request_stub).to_s.should == expected.strip
95
+ expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected.strip)
90
96
  end
91
- end
92
97
 
98
+ it "should print stub request snippet with valid JSON body when request header contains 'Accept'=>'application/json' " do
99
+ @request_signature = WebMock::RequestSignature.new(:post, "www.example.com",
100
+ headers: {'Accept' => 'application/json'})
101
+ @request_stub = WebMock::RequestStub.from_request_signature(@request_signature)
102
+ expected = <<-STUB
103
+ stub_request(:post, "http://www.example.com/").
104
+ with(
105
+ headers: {
106
+ \t 'Accept'=>'application/json'
107
+ }).
108
+ to_return(status: 200, body: \"{}\", headers: {})
109
+ STUB
110
+ expect(WebMock::StubRequestSnippet.new(@request_stub).to_s).to eq(expected.strip)
111
+ end
112
+ end
93
113
 
94
114
  end
95
115
  end
@@ -3,22 +3,22 @@ require 'spec_helper'
3
3
  describe WebMock::Util::HashCounter do
4
4
 
5
5
  it "should return 0 for non existing key" do
6
- WebMock::Util::HashCounter.new.get(:abc).should == 0
6
+ expect(WebMock::Util::HashCounter.new.get(:abc)).to eq(0)
7
7
  end
8
8
 
9
9
  it "should increase the returned value on every put with the same key" do
10
10
  counter = WebMock::Util::HashCounter.new
11
11
  counter.put(:abc)
12
- counter.get(:abc).should == 1
12
+ expect(counter.get(:abc)).to eq(1)
13
13
  counter.put(:abc)
14
- counter.get(:abc).should == 2
14
+ expect(counter.get(:abc)).to eq(2)
15
15
  end
16
16
 
17
17
  it "should only increase value for given key provided to put" do
18
18
  counter = WebMock::Util::HashCounter.new
19
19
  counter.put(:abc)
20
- counter.get(:abc).should == 1
21
- counter.get(:def).should == 0
20
+ expect(counter.get(:abc)).to eq(1)
21
+ expect(counter.get(:def)).to eq(0)
22
22
  end
23
23
 
24
24
  describe "each" do
@@ -33,7 +33,7 @@ describe WebMock::Util::HashCounter do
33
33
 
34
34
  elements = []
35
35
  counter.each {|k,v| elements << [k,v]}
36
- elements.should == [[:c, 1], [:b, 2], [:a, 2], [:d, 1]]
36
+ expect(elements).to eq([[:c, 1], [:b, 2], [:a, 2], [:d, 1]])
37
37
  end
38
38
  end
39
39
  end