-
Notifications
You must be signed in to change notification settings - Fork 555
/
curb_spec.rb
510 lines (419 loc) · 16.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
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")
expect(http_request(:put, "http://www.example.com", body: "01234").
status).to eq("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
describe 'on_debug' do
it "should call on_debug" do
stub_request(:any, "example.com").
to_return(status: 200, headers: { 'Server' => 'nginx' }, body: { hello: :world }.to_json)
test = []
@curl.on_debug do |message, operation|
test << "#{operation} -> #{message}"
end
@curl.headers['Content-Type'] = 'application/json'
@curl.http_post({ hello: :world }.to_json)
expect(test).to_not be_empty
end
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
expect(test).to eq(body)
end
it "should call on_missing with 4xx response" do
response_code = 403
stub_request(:any, "example.com").
to_return(status: [response_code, "None shall pass"])
test = nil
@curl.on_missing do |c, code|
test = code
end
@curl.http_get
expect(test).to eq(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
expect(test).to eq(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
expect(test).to eq(body)
end
it "should call on_body for each chunk with chunked response" do
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
expect(test).to eq(["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
expect(test).to eq([
"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
expect(test).to eq(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|
expect(args.length).to eq(4)
args.each {|arg| expect(arg.is_a?(Float)).to eq(true) }
test = true
end
@curl.http_get
expect(test).to eq(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_missing {|*args| order << :on_missing }
@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
expect(order).to eq([: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_missing {|*args| order << :on_missing }
@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
expect(order).to eq([:on_progress,:on_header,:on_body,:on_complete,:on_failure])
end
it "should call callbacks in correct order on missing request" do
stub_request(:any, "example.com").to_return(status: [403, ""])
order = []
@curl.on_success {|*args| order << :on_success }
@curl.on_missing {|*args| order << :on_missing }
@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
expect(order).to eq([:on_progress,:on_header,:on_body,:on_complete,:on_missing])
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
expect(@curl.last_effective_url).to eq('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
expect(@curl.last_effective_url).to eq('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
expect(@curl.last_effective_url).to eq('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
expect(@curl.last_effective_url).to eq('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
expect(@curl.url).to eq('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
expect(@curl.body_str).to eq('request B')
expect(@curl.response_code).to eq(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
expect(@curl.url).to eq('http://example.com')
expect(@curl.body_str).to eq('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
expect(@curl.content_type).to eq(content_type)
end
end
context "when response does not include Content-Type header" do
it "returns nil for content_type" do
stub_request(:any, 'example.com').
to_return(body: "abc",
status: 200 )
@curl.http_get
expect(@curl.content_type).to 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
expect(@curl).to 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
expect(@curl).not_to 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
expect(@curl).not_to 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)
expect(c.body_str).to eq("abc")
end
it "should alias body to body_str" do
stub_request(:get, "www.example.com").to_return(body: "abc")
c = Curl::Easy.new
c.url = "http://www.example.com"
c.http(:GET)
expect(c.body).to eq("abc")
end
it "supports array headers passed to Curl::Easy" do
stub_request(:get, "www.example.com").with(headers: {'X-One' => '1'}).to_return(body: "abc")
c = Curl::Easy.new
c.url = "http://www.example.com"
c.headers = ["X-One: 1"]
c.http(:GET)
expect(c.body).to eq("abc")
end
it "supports headers containing the ':' character" do
stub_request(:get, "www.example.com").with(headers: {'Referer'=>'http://www.example.com'}).to_return(body: "abc")
c = Curl::Easy.new
c.url = "http://www.example.com"
c.headers = ["Referer: http://www.example.com"]
c.http(:GET)
expect(c.body).to eq("abc")
expect(c.headers).to eq(["Referer: http://www.example.com"])
end
describe 'match request body' do
it 'for post' do
stub_request(:post, "www.example.com").with(body: 'foo=nhe').to_return(body: "abc")
response = Curl.post("http://www.example.com", {foo: :nhe})
expect(response.body_str).to eq("abc")
end
it 'for patch' do
stub_request(:patch, "www.example.com").with(body: 'foo=nhe').to_return(body: "abc")
response = Curl.patch("http://www.example.com", {foo: :nhe})
expect(response.body_str).to eq("abc")
end
it 'for put' do
stub_request(:put, "www.example.com").with(body: 'foo=nhe').to_return(body: "abc")
response = Curl.put("http://www.example.com", {foo: :nhe})
expect(response.body_str).to eq("abc")
end
end
end
describe "using #http_* methods for requests" do
it_should_behave_like "Curb"
include CurbSpecHelper::NamedHttp
it "should reset @webmock_method after each call" 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
expect {
c.perform
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com))
end
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
expect(c.response_code).to eq(200)
end
it "should work with several body arguments for post using the class method" do
stub_request(:post, "www.example.com").with(body: {user: {first_name: 'Bartosz', last_name: 'Blimke'}})
c = Curl::Easy.http_post "http://www.example.com", 'user[first_name]=Bartosz', 'user[last_name]=Blimke'
expect(c.response_code).to eq(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
expect(c.response_code).to eq(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])
expect(c.response_code).to eq(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
describe "using #reset" do
before do
@curl = Curl::Easy.new
@curl.url = "http://example.com"
stub_request(:any, "example.com").
to_return(body: "abc",
headers: { "Content-Type" => "application/json" })
@curl.http_get
end
it "should clear all memoized response fields" do
@curl.reset
expect(@curl).to have_attributes(
body_str: nil,
content_type: nil,
header_str: nil,
last_effective_url: nil,
response_code: 0,
)
end
end
end
end