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
@@ -11,9 +11,9 @@ unless RUBY_PLATFORM =~ /java/
11
11
 
12
12
  describe "when doing PUTs" do
13
13
  it "should stub them" do
14
- stub_request(:put, "www.example.com").with(:body => "01234")
15
- http_request(:put, "http://www.example.com", :body => "01234").
16
- status.should == "200"
14
+ stub_request(:put, "www.example.com").with(body: "01234")
15
+ expect(http_request(:put, "http://www.example.com", body: "01234").
16
+ status).to eq("200")
17
17
  end
18
18
  end
19
19
  end
@@ -30,98 +30,132 @@ unless RUBY_PLATFORM =~ /java/
30
30
  @curl.url = "http://example.com"
31
31
  end
32
32
 
33
+ describe 'on_debug' do
34
+ it "should call on_debug" do
35
+ stub_request(:any, "example.com").
36
+ to_return(status: 200, headers: { 'Server' => 'nginx' }, body: { hello: :world }.to_json)
37
+
38
+ test = []
39
+
40
+ @curl.on_debug do |message, operation|
41
+ test << "#{operation} -> #{message}"
42
+ end
43
+ @curl.headers['Content-Type'] = 'application/json'
44
+ @curl.http_post({ hello: :world }.to_json)
45
+
46
+ expect(test).to_not be_empty
47
+ end
48
+ end
49
+
33
50
  it "should call on_success with 2xx response" do
34
51
  body = "on_success fired"
35
- stub_request(:any, "example.com").to_return(:body => body)
52
+ stub_request(:any, "example.com").to_return(body: body)
36
53
 
37
54
  test = nil
38
55
  @curl.on_success do |c|
39
56
  test = c.body_str
40
57
  end
41
58
  @curl.http_get
42
- test.should == body
59
+ expect(test).to eq(body)
43
60
  end
44
61
 
45
- it "should call on_failure with 4xx response" do
62
+ it "should call on_missing with 4xx response" do
46
63
  response_code = 403
47
64
  stub_request(:any, "example.com").
48
- to_return(:status => [response_code, "None shall pass"])
65
+ to_return(status: [response_code, "None shall pass"])
49
66
 
50
67
  test = nil
51
- @curl.on_failure do |c, code|
68
+ @curl.on_missing do |c, code|
52
69
  test = code
53
70
  end
54
71
  @curl.http_get
55
- test.should == response_code
72
+ expect(test).to eq(response_code)
56
73
  end
57
74
 
58
75
  it "should call on_failure with 5xx response" do
59
76
  response_code = 599
60
77
  stub_request(:any, "example.com").
61
- to_return(:status => [response_code, "Server On Fire"])
78
+ to_return(status: [response_code, "Server On Fire"])
62
79
 
63
80
  test = nil
64
81
  @curl.on_failure do |c, code|
65
82
  test = code
66
83
  end
67
84
  @curl.http_get
68
- test.should == response_code
85
+ expect(test).to eq(response_code)
69
86
  end
70
87
 
71
88
  it "should call on_body when response body is read" do
72
89
  body = "on_body fired"
73
90
  stub_request(:any, "example.com").
74
- to_return(:body => body)
91
+ to_return(body: body)
75
92
 
76
93
  test = nil
77
94
  @curl.on_body do |data|
78
95
  test = data
79
96
  end
80
97
  @curl.http_get
81
- test.should == body
98
+ expect(test).to eq(body)
99
+ end
100
+
101
+ it "should call on_body for each chunk with chunked response" do
102
+ stub_request(:any, "example.com").
103
+ to_return(body: ["first_chunk", "second_chunk"],
104
+ headers: {"Transfer-Encoding" => "chunked"})
105
+
106
+ test = []
107
+ @curl.on_body do |data|
108
+ test << data
109
+ end
110
+ @curl.http_get
111
+ expect(test).to eq(["first_chunk", "second_chunk"])
82
112
  end
83
113
 
84
114
  it "should call on_header when response headers are read" do
85
115
  stub_request(:any, "example.com").
86
- to_return(:headers => {:one => 1})
116
+ to_return(headers: {one: 1})
87
117
 
88
- test = nil
118
+ test = []
89
119
  @curl.on_header do |data|
90
- test = data
120
+ test << data
91
121
  end
92
122
  @curl.http_get
93
- test.should match(/One: 1/)
123
+ expect(test).to eq([
124
+ "HTTP/1.1 200 \r\n",
125
+ 'One: 1'
126
+ ])
94
127
  end
95
128
 
96
129
  it "should call on_complete when request is complete" do
97
130
  body = "on_complete fired"
98
- stub_request(:any, "example.com").to_return(:body => body)
131
+ stub_request(:any, "example.com").to_return(body: body)
99
132
 
100
133
  test = nil
101
134
  @curl.on_complete do |curl|
102
135
  test = curl.body_str
103
136
  end
104
137
  @curl.http_get
105
- test.should == body
138
+ expect(test).to eq(body)
106
139
  end
107
140
 
108
141
  it "should call on_progress when portion of response body is read" do
109
- stub_request(:any, "example.com").to_return(:body => "01234")
142
+ stub_request(:any, "example.com").to_return(body: "01234")
110
143
 
111
144
  test = nil
112
145
  @curl.on_progress do |*args|
113
- args.length.should == 4
114
- args.each {|arg| arg.is_a?(Float).should == true }
146
+ expect(args.length).to eq(4)
147
+ args.each {|arg| expect(arg.is_a?(Float)).to eq(true) }
115
148
  test = true
116
149
  end
117
150
  @curl.http_get
118
- test.should == true
151
+ expect(test).to eq(true)
119
152
  end
120
153
 
121
154
  it "should call callbacks in correct order on successful request" do
122
155
  stub_request(:any, "example.com")
123
156
  order = []
124
157
  @curl.on_success {|*args| order << :on_success }
158
+ @curl.on_missing {|*args| order << :on_missing }
125
159
  @curl.on_failure {|*args| order << :on_failure }
126
160
  @curl.on_header {|*args| order << :on_header }
127
161
  @curl.on_body {|*args| order << :on_body }
@@ -129,13 +163,29 @@ unless RUBY_PLATFORM =~ /java/
129
163
  @curl.on_progress {|*args| order << :on_progress }
130
164
  @curl.http_get
131
165
 
132
- order.should == [:on_progress,:on_header,:on_body,:on_complete,:on_success]
166
+ expect(order).to eq([:on_progress,:on_header,:on_body,:on_complete,:on_success])
133
167
  end
134
168
 
135
169
  it "should call callbacks in correct order on failed request" do
136
- stub_request(:any, "example.com").to_return(:status => [500, ""])
170
+ stub_request(:any, "example.com").to_return(status: [500, ""])
171
+ order = []
172
+ @curl.on_success {|*args| order << :on_success }
173
+ @curl.on_missing {|*args| order << :on_missing }
174
+ @curl.on_failure {|*args| order << :on_failure }
175
+ @curl.on_header {|*args| order << :on_header }
176
+ @curl.on_body {|*args| order << :on_body }
177
+ @curl.on_complete {|*args| order << :on_complete }
178
+ @curl.on_progress {|*args| order << :on_progress }
179
+ @curl.http_get
180
+
181
+ expect(order).to eq([:on_progress,:on_header,:on_body,:on_complete,:on_failure])
182
+ end
183
+
184
+ it "should call callbacks in correct order on missing request" do
185
+ stub_request(:any, "example.com").to_return(status: [403, ""])
137
186
  order = []
138
187
  @curl.on_success {|*args| order << :on_success }
188
+ @curl.on_missing {|*args| order << :on_missing }
139
189
  @curl.on_failure {|*args| order << :on_failure }
140
190
  @curl.on_header {|*args| order << :on_header }
141
191
  @curl.on_body {|*args| order << :on_body }
@@ -143,7 +193,7 @@ unless RUBY_PLATFORM =~ /java/
143
193
  @curl.on_progress {|*args| order << :on_progress }
144
194
  @curl.http_get
145
195
 
146
- order.should == [:on_progress,:on_header,:on_body,:on_complete,:on_failure]
196
+ expect(order).to eq([:on_progress,:on_header,:on_body,:on_complete,:on_missing])
147
197
  end
148
198
  end
149
199
 
@@ -158,12 +208,12 @@ unless RUBY_PLATFORM =~ /java/
158
208
 
159
209
  it 'should be the same as #url even with a location header' do
160
210
  stub_request(:any, 'example.com').
161
- to_return(:body => "abc",
162
- :status => 302,
163
- :headers => { 'Location' => 'http://www.example.com' })
211
+ to_return(body: "abc",
212
+ status: 302,
213
+ headers: { 'Location' => 'http://www.example.com' })
164
214
 
165
215
  @curl.http_get
166
- @curl.last_effective_url.should == 'http://example.com'
216
+ expect(@curl.last_effective_url).to eq('http://example.com')
167
217
  end
168
218
  end
169
219
 
@@ -173,60 +223,60 @@ unless RUBY_PLATFORM =~ /java/
173
223
  it 'should be the same as #url when no location header is present' do
174
224
  stub_request(:any, "example.com")
175
225
  @curl.http_get
176
- @curl.last_effective_url.should == 'http://example.com'
226
+ expect(@curl.last_effective_url).to eq('http://example.com')
177
227
  end
178
228
 
179
229
  it 'should be the value of the location header when present' do
180
230
  stub_request(:any, 'example.com').
181
- to_return(:headers => { 'Location' => 'http://www.example.com' })
231
+ to_return(headers: { 'Location' => 'http://www.example.com' })
182
232
  stub_request(:any, 'www.example.com')
183
233
 
184
234
  @curl.http_get
185
- @curl.last_effective_url.should == 'http://www.example.com'
235
+ expect(@curl.last_effective_url).to eq('http://www.example.com')
186
236
  end
187
237
 
188
238
  it 'should work with more than one redirect' do
189
239
  stub_request(:any, 'example.com').
190
- to_return(:headers => { 'Location' => 'http://www.example.com' })
240
+ to_return(headers: { 'Location' => 'http://www.example.com' })
191
241
  stub_request(:any, 'www.example.com').
192
- to_return(:headers => { 'Location' => 'http://blog.example.com' })
242
+ to_return(headers: { 'Location' => 'http://blog.example.com' })
193
243
  stub_request(:any, 'blog.example.com')
194
244
 
195
245
  @curl.http_get
196
- @curl.last_effective_url.should == 'http://blog.example.com'
246
+ expect(@curl.last_effective_url).to eq('http://blog.example.com')
197
247
  end
198
248
 
199
249
  it 'should maintain the original url' do
200
250
  stub_request(:any, 'example.com').
201
- to_return(:headers => { 'Location' => 'http://www.example.com' })
251
+ to_return(headers: { 'Location' => 'http://www.example.com' })
202
252
  stub_request(:any, 'www.example.com')
203
253
 
204
254
  @curl.http_get
205
- @curl.url.should == 'http://example.com'
255
+ expect(@curl.url).to eq('http://example.com')
206
256
  end
207
257
 
208
258
  it 'should have the redirected-to attrs (body, response code)' do
209
259
  stub_request(:any, 'example.com').
210
- to_return(:body => 'request A',
211
- :status => 302,
212
- :headers => { 'Location' => 'http://www.example.com' })
213
- stub_request(:any, 'www.example.com').to_return(:body => 'request B')
260
+ to_return(body: 'request A',
261
+ status: 302,
262
+ headers: { 'Location' => 'http://www.example.com' })
263
+ stub_request(:any, 'www.example.com').to_return(body: 'request B')
214
264
 
215
265
  @curl.http_get
216
- @curl.body_str.should == 'request B'
217
- @curl.response_code.should == 200
266
+ expect(@curl.body_str).to eq('request B')
267
+ expect(@curl.response_code).to eq(200)
218
268
  end
219
269
 
220
270
  it 'should follow more than one redirect' do
221
271
  stub_request(:any, 'example.com').
222
- to_return(:headers => { 'Location' => 'http://www.example.com' })
272
+ to_return(headers: { 'Location' => 'http://www.example.com' })
223
273
  stub_request(:any, 'www.example.com').
224
- to_return(:headers => { 'Location' => 'http://blog.example.com' })
225
- stub_request(:any, 'blog.example.com').to_return(:body => 'blog post')
274
+ to_return(headers: { 'Location' => 'http://blog.example.com' })
275
+ stub_request(:any, 'blog.example.com').to_return(body: 'blog post')
226
276
 
227
277
  @curl.http_get
228
- @curl.url.should == 'http://example.com'
229
- @curl.body_str.should == 'blog post'
278
+ expect(@curl.url).to eq('http://example.com')
279
+ expect(@curl.body_str).to eq('blog post')
230
280
  end
231
281
  end
232
282
  end
@@ -242,28 +292,62 @@ unless RUBY_PLATFORM =~ /java/
242
292
  content_type = "application/json"
243
293
 
244
294
  stub_request(:any, 'example.com').
245
- to_return(:body => "abc",
246
- :status => 200,
247
- :headers => { 'Content-Type' => content_type })
295
+ to_return(body: "abc",
296
+ status: 200,
297
+ headers: { 'Content-Type' => content_type })
248
298
 
249
299
  @curl.http_get
250
- @curl.content_type.should == content_type
300
+ expect(@curl.content_type).to eq(content_type)
251
301
  end
252
302
  end
253
303
 
254
304
  context "when response does not include Content-Type header" do
255
305
  it "returns nil for content_type" do
256
- content_type = "application/json"
257
306
 
258
307
  stub_request(:any, 'example.com').
259
- to_return(:body => "abc",
260
- :status => 200 )
308
+ to_return(body: "abc",
309
+ status: 200 )
261
310
 
262
311
  @curl.http_get
263
- @curl.content_type.should be_nil
312
+ expect(@curl.content_type).to be_nil
264
313
  end
265
314
  end
266
315
  end
316
+
317
+ describe "#chunked_response?" do
318
+ before(:each) do
319
+ @curl = Curl::Easy.new
320
+ @curl.url = "http://example.com"
321
+ end
322
+
323
+ it "is true when Transfer-Encoding is 'chunked' and body responds to each" do
324
+ stub_request(:any, 'example.com').
325
+ to_return(body: ["abc", "def"],
326
+ status: 200,
327
+ headers: { 'Transfer-Encoding' => 'chunked' })
328
+
329
+ @curl.http_get
330
+ expect(@curl).to be_chunked_response
331
+ end
332
+
333
+ it "is false when Transfer-Encoding is not 'chunked'" do
334
+ stub_request(:any, 'example.com').
335
+ to_return(body: ["abc", "def"],
336
+ status: 200)
337
+
338
+ @curl.http_get
339
+ expect(@curl).not_to be_chunked_response
340
+ end
341
+
342
+ it "is false when Transfer-Encoding is 'chunked' but body does not respond to each" do
343
+ stub_request(:any, 'example.com').
344
+ to_return(body: "abc",
345
+ status: 200)
346
+
347
+ @curl.http_get
348
+ expect(@curl).not_to be_chunked_response
349
+ end
350
+ end
267
351
  end
268
352
 
269
353
  describe "Webmock with Curb" do
@@ -272,12 +356,54 @@ unless RUBY_PLATFORM =~ /java/
272
356
  include CurbSpecHelper::DynamicHttp
273
357
 
274
358
  it "should work with uppercase arguments" do
275
- stub_request(:get, "www.example.com").to_return(:body => "abc")
359
+ stub_request(:get, "www.example.com").to_return(body: "abc")
276
360
 
277
361
  c = Curl::Easy.new
278
362
  c.url = "http://www.example.com"
279
363
  c.http(:GET)
280
- c.body_str.should == "abc"
364
+ expect(c.body_str).to eq("abc")
365
+ end
366
+
367
+ it "should alias body to body_str" do
368
+ stub_request(:get, "www.example.com").to_return(body: "abc")
369
+
370
+ c = Curl::Easy.new
371
+ c.url = "http://www.example.com"
372
+ c.http(:GET)
373
+ expect(c.body).to eq("abc")
374
+ end
375
+
376
+ it "supports array headers passed to Curl::Easy" do
377
+ stub_request(:get, "www.example.com").with(headers: {'X-One' => '1'}).to_return(body: "abc")
378
+
379
+ c = Curl::Easy.new
380
+ c.url = "http://www.example.com"
381
+ c.headers = ["X-One: 1"]
382
+ c.http(:GET)
383
+ expect(c.body).to eq("abc")
384
+ end
385
+
386
+ describe 'match request body' do
387
+ it 'for post' do
388
+ stub_request(:post, "www.example.com").with(body: 'foo=nhe').to_return(body: "abc")
389
+
390
+ response = Curl.post("http://www.example.com", {foo: :nhe})
391
+ expect(response.body_str).to eq("abc")
392
+ end
393
+
394
+ it 'for patch' do
395
+ stub_request(:patch, "www.example.com").with(body: 'foo=nhe').to_return(body: "abc")
396
+
397
+ response = Curl.patch("http://www.example.com", {foo: :nhe})
398
+ expect(response.body_str).to eq("abc")
399
+ end
400
+
401
+ it 'for put' do
402
+ stub_request(:put, "www.example.com").with(body: 'foo=nhe').to_return(body: "abc")
403
+
404
+ response = Curl.put("http://www.example.com", {foo: :nhe})
405
+ expect(response.body_str).to eq("abc")
406
+ end
281
407
  end
282
408
  end
283
409
 
@@ -285,39 +411,50 @@ unless RUBY_PLATFORM =~ /java/
285
411
  it_should_behave_like "Curb"
286
412
  include CurbSpecHelper::NamedHttp
287
413
 
414
+ it "should reset @webmock_method after each call" do
415
+ stub_request(:post, "www.example.com").with(body: "01234")
416
+ c = Curl::Easy.new
417
+ c.url = "http://www.example.com"
418
+ c.post_body = "01234"
419
+ c.http_post
420
+ expect {
421
+ c.perform
422
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com))
423
+ end
424
+
288
425
  it "should work with blank arguments for post" do
289
- stub_request(:post, "www.example.com").with(:body => "01234")
426
+ stub_request(:post, "www.example.com").with(body: "01234")
290
427
  c = Curl::Easy.new
291
428
  c.url = "http://www.example.com"
292
429
  c.post_body = "01234"
293
430
  c.http_post
294
- c.response_code.should == 200
431
+ expect(c.response_code).to eq(200)
295
432
  end
296
433
 
297
434
  it "should work with several body arguments for post using the class method" do
298
- stub_request(:post, "www.example.com").with(:user => {:first_name=>'Bartosz', :last_name=>'Blimke'})
435
+ stub_request(:post, "www.example.com").with(body: {user: {first_name: 'Bartosz', last_name: 'Blimke'}})
299
436
  c = Curl::Easy.http_post "http://www.example.com", 'user[first_name]=Bartosz', 'user[last_name]=Blimke'
300
- c.response_code.should == 200
437
+ expect(c.response_code).to eq(200)
301
438
  end
302
439
 
303
440
  it "should work with blank arguments for put" do
304
- stub_request(:put, "www.example.com").with(:body => "01234")
441
+ stub_request(:put, "www.example.com").with(body: "01234")
305
442
  c = Curl::Easy.new
306
443
  c.url = "http://www.example.com"
307
444
  c.put_data = "01234"
308
445
  c.http_put
309
- c.response_code.should == 200
446
+ expect(c.response_code).to eq(200)
310
447
  end
311
448
 
312
449
  it "should work with multiple arguments for post" do
313
- data = { :name => "john", :address => "111 example ave" }
450
+ data = { name: "john", address: "111 example ave" }
314
451
 
315
- stub_request(:post, "www.example.com").with(:body => data)
452
+ stub_request(:post, "www.example.com").with(body: data)
316
453
  c = Curl::Easy.new
317
454
  c.url = "http://www.example.com"
318
455
  c.http_post Curl::PostField.content('name', data[:name]), Curl::PostField.content('address', data[:address])
319
456
 
320
- c.response_code.should == 200
457
+ expect(c.response_code).to eq(200)
321
458
  end
322
459
 
323
460
  end
@@ -336,5 +473,27 @@ unless RUBY_PLATFORM =~ /java/
336
473
  it_should_behave_like "Curb"
337
474
  include CurbSpecHelper::ClassPerform
338
475
  end
476
+
477
+ describe "using #reset" do
478
+ before do
479
+ @curl = Curl::Easy.new
480
+ @curl.url = "http://example.com"
481
+ stub_request(:any, "example.com").
482
+ to_return(body: "abc",
483
+ headers: { "Content-Type" => "application/json" })
484
+ @curl.http_get
485
+ end
486
+
487
+ it "should clear all memoized response fields" do
488
+ @curl.reset
489
+ expect(@curl).to have_attributes(
490
+ body_str: nil,
491
+ content_type: nil,
492
+ header_str: nil,
493
+ last_effective_url: nil,
494
+ response_code: 0,
495
+ )
496
+ end
497
+ end
339
498
  end
340
499
  end
@@ -21,18 +21,21 @@ module CurbSpecHelper
21
21
  end
22
22
 
23
23
  OpenStruct.new(
24
- :body => curl.body_str,
25
- :headers => WebMock::Util::Headers.normalize_headers(response_headers),
26
- :status => curl.response_code.to_s,
27
- :message => status
24
+ body: curl.body_str,
25
+ headers: WebMock::Util::Headers.normalize_headers(response_headers),
26
+ status: curl.response_code.to_s,
27
+ message: status
28
28
  )
29
29
  end
30
30
 
31
31
  def setup_request(uri, curl, options={})
32
32
  curl ||= Curl::Easy.new
33
- curl.url = uri.omit(:userinfo).to_s
34
- curl.username = uri.user
35
- curl.password = uri.password
33
+ curl.url = uri.to_s
34
+ if options[:basic_auth]
35
+ curl.http_auth_types = :basic
36
+ curl.username = options[:basic_auth][0]
37
+ curl.password = options[:basic_auth][1]
38
+ end
36
39
  curl.timeout = 30
37
40
  curl.connect_timeout = 30
38
41
 
@@ -66,7 +69,7 @@ module CurbSpecHelper
66
69
  curl.put_data = body
67
70
  end
68
71
 
69
- curl.http(method)
72
+ curl.http(method.to_s.upcase)
70
73
  curl
71
74
  end
72
75
  end