/
curb_spec.rb
392 lines (322 loc) · 12.1 KB
/
curb_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
require 'spec_helper'
require 'acceptance/webmock_shared'
unless RUBY_PLATFORM =~ /java/
require 'acceptance/curb/curb_spec_helper'
shared_examples_for "Curb" do
include CurbSpecHelper
include_examples "with WebMock"
describe "when doing PUTs" do
it "should stub them" do
stub_request(:put, "www.example.com").with(:body => "01234")
http_request(:put, "http://www.example.com", :body => "01234").
status.should == "200"
end
end
end
describe "Curb features" do
before(:each) do
WebMock.disable_net_connect!
WebMock.reset!
end
describe "callbacks" do
before(:each) do
@curl = Curl::Easy.new
@curl.url = "http://example.com"
end
it "should call on_success with 2xx response" do
body = "on_success fired"
stub_request(:any, "example.com").to_return(:body => body)
test = nil
@curl.on_success do |c|
test = c.body_str
end
@curl.http_get
test.should == body
end
it "should call on_failure with 4xx response" do
response_code = 403
stub_request(:any, "example.com").
to_return(:status => [response_code, "None shall pass"])
test = nil
@curl.on_failure do |c, code|
test = code
end
@curl.http_get
test.should == response_code
end
it "should call on_failure with 5xx response" do
response_code = 599
stub_request(:any, "example.com").
to_return(:status => [response_code, "Server On Fire"])
test = nil
@curl.on_failure do |c, code|
test = code
end
@curl.http_get
test.should == response_code
end
it "should call on_body when response body is read" do
body = "on_body fired"
stub_request(:any, "example.com").
to_return(:body => body)
test = nil
@curl.on_body do |data|
test = data
end
@curl.http_get
test.should == body
end
it "should call on_body for each chunk with chunked response" do
body = "on_body fired"
stub_request(:any, "example.com").
to_return(:body => ["first_chunk", "second_chunk"],
:headers => {"Transfer-Encoding" => "chunked"})
test = []
@curl.on_body do |data|
test << data
end
@curl.http_get
test.should == ["first_chunk", "second_chunk"]
end
it "should call on_header when response headers are read" do
stub_request(:any, "example.com").
to_return(:headers => {:one => 1})
test = []
@curl.on_header do |data|
test << data
end
@curl.http_get
test.should == [
"HTTP/1.1 200 \r\n",
'One: 1'
]
end
it "should call on_complete when request is complete" do
body = "on_complete fired"
stub_request(:any, "example.com").to_return(:body => body)
test = nil
@curl.on_complete do |curl|
test = curl.body_str
end
@curl.http_get
test.should == body
end
it "should call on_progress when portion of response body is read" do
stub_request(:any, "example.com").to_return(:body => "01234")
test = nil
@curl.on_progress do |*args|
args.length.should == 4
args.each {|arg| arg.is_a?(Float).should == true }
test = true
end
@curl.http_get
test.should == true
end
it "should call callbacks in correct order on successful request" do
stub_request(:any, "example.com")
order = []
@curl.on_success {|*args| order << :on_success }
@curl.on_failure {|*args| order << :on_failure }
@curl.on_header {|*args| order << :on_header }
@curl.on_body {|*args| order << :on_body }
@curl.on_complete {|*args| order << :on_complete }
@curl.on_progress {|*args| order << :on_progress }
@curl.http_get
order.should == [:on_progress,:on_header,:on_body,:on_complete,:on_success]
end
it "should call callbacks in correct order on failed request" do
stub_request(:any, "example.com").to_return(:status => [500, ""])
order = []
@curl.on_success {|*args| order << :on_success }
@curl.on_failure {|*args| order << :on_failure }
@curl.on_header {|*args| order << :on_header }
@curl.on_body {|*args| order << :on_body }
@curl.on_complete {|*args| order << :on_complete }
@curl.on_progress {|*args| order << :on_progress }
@curl.http_get
order.should == [:on_progress,:on_header,:on_body,:on_complete,:on_failure]
end
end
describe '#last_effective_url' do
before(:each) do
@curl = Curl::Easy.new
@curl.url = "http://example.com"
end
context 'when not following redirects' do
before { @curl.follow_location = false }
it 'should be the same as #url even with a location header' do
stub_request(:any, 'example.com').
to_return(:body => "abc",
:status => 302,
:headers => { 'Location' => 'http://www.example.com' })
@curl.http_get
@curl.last_effective_url.should == 'http://example.com'
end
end
context 'when following redirects' do
before { @curl.follow_location = true }
it 'should be the same as #url when no location header is present' do
stub_request(:any, "example.com")
@curl.http_get
@curl.last_effective_url.should == 'http://example.com'
end
it 'should be the value of the location header when present' do
stub_request(:any, 'example.com').
to_return(:headers => { 'Location' => 'http://www.example.com' })
stub_request(:any, 'www.example.com')
@curl.http_get
@curl.last_effective_url.should == 'http://www.example.com'
end
it 'should work with more than one redirect' do
stub_request(:any, 'example.com').
to_return(:headers => { 'Location' => 'http://www.example.com' })
stub_request(:any, 'www.example.com').
to_return(:headers => { 'Location' => 'http://blog.example.com' })
stub_request(:any, 'blog.example.com')
@curl.http_get
@curl.last_effective_url.should == 'http://blog.example.com'
end
it 'should maintain the original url' do
stub_request(:any, 'example.com').
to_return(:headers => { 'Location' => 'http://www.example.com' })
stub_request(:any, 'www.example.com')
@curl.http_get
@curl.url.should == 'http://example.com'
end
it 'should have the redirected-to attrs (body, response code)' do
stub_request(:any, 'example.com').
to_return(:body => 'request A',
:status => 302,
:headers => { 'Location' => 'http://www.example.com' })
stub_request(:any, 'www.example.com').to_return(:body => 'request B')
@curl.http_get
@curl.body_str.should == 'request B'
@curl.response_code.should == 200
end
it 'should follow more than one redirect' do
stub_request(:any, 'example.com').
to_return(:headers => { 'Location' => 'http://www.example.com' })
stub_request(:any, 'www.example.com').
to_return(:headers => { 'Location' => 'http://blog.example.com' })
stub_request(:any, 'blog.example.com').to_return(:body => 'blog post')
@curl.http_get
@curl.url.should == 'http://example.com'
@curl.body_str.should == 'blog post'
end
end
end
describe "#content_type" do
before(:each) do
@curl = Curl::Easy.new
@curl.url = "http://example.com"
end
context "when response includes Content-Type header" do
it "returns correct content_type" do
content_type = "application/json"
stub_request(:any, 'example.com').
to_return(:body => "abc",
:status => 200,
:headers => { 'Content-Type' => content_type })
@curl.http_get
@curl.content_type.should == content_type
end
end
context "when response does not include Content-Type header" do
it "returns nil for content_type" do
content_type = "application/json"
stub_request(:any, 'example.com').
to_return(:body => "abc",
:status => 200 )
@curl.http_get
@curl.content_type.should be_nil
end
end
end
describe "#chunked_response?" do
before(:each) do
@curl = Curl::Easy.new
@curl.url = "http://example.com"
end
it "is true when Transfer-Encoding is 'chunked' and body responds to each" do
stub_request(:any, 'example.com').
to_return(:body => ["abc", "def"],
:status => 200,
:headers => { 'Transfer-Encoding' => 'chunked' })
@curl.http_get
@curl.should be_chunked_response
end
it "is false when Transfer-Encoding is not 'chunked'" do
stub_request(:any, 'example.com').
to_return(:body => ["abc", "def"],
:status => 200)
@curl.http_get
@curl.should_not be_chunked_response
end
it "is false when Transfer-Encoding is 'chunked' but body does not respond to each" do
stub_request(:any, 'example.com').
to_return(:body => "abc",
:status => 200)
@curl.http_get
@curl.should_not be_chunked_response
end
end
end
describe "Webmock with Curb" do
describe "using #http for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::DynamicHttp
it "should work with uppercase arguments" do
stub_request(:get, "www.example.com").to_return(:body => "abc")
c = Curl::Easy.new
c.url = "http://www.example.com"
c.http(:GET)
c.body_str.should == "abc"
end
end
describe "using #http_* methods for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::NamedHttp
it "should work with blank arguments for post" do
stub_request(:post, "www.example.com").with(:body => "01234")
c = Curl::Easy.new
c.url = "http://www.example.com"
c.post_body = "01234"
c.http_post
c.response_code.should == 200
end
it "should work with several body arguments for post using the class method" do
stub_request(:post, "www.example.com").with(:user => {:first_name=>'Bartosz', :last_name=>'Blimke'})
c = Curl::Easy.http_post "http://www.example.com", 'user[first_name]=Bartosz', 'user[last_name]=Blimke'
c.response_code.should == 200
end
it "should work with blank arguments for put" do
stub_request(:put, "www.example.com").with(:body => "01234")
c = Curl::Easy.new
c.url = "http://www.example.com"
c.put_data = "01234"
c.http_put
c.response_code.should == 200
end
it "should work with multiple arguments for post" do
data = { :name => "john", :address => "111 example ave" }
stub_request(:post, "www.example.com").with(:body => data)
c = Curl::Easy.new
c.url = "http://www.example.com"
c.http_post Curl::PostField.content('name', data[:name]), Curl::PostField.content('address', data[:address])
c.response_code.should == 200
end
end
describe "using #perform for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::Perform
end
describe "using .http_* methods for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::ClassNamedHttp
end
describe "using .perform for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::ClassPerform
end
end
end