ghazel-curb 0.5.9.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.
@@ -0,0 +1,664 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestCurbCurlEasy < Test::Unit::TestCase
4
+ def test_class_perform_01
5
+ assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL)
6
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
7
+ assert_equal "", c.header_str
8
+ end
9
+
10
+ def test_class_perform_02
11
+ data = ""
12
+ assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL) { |curl| curl.on_body { |d| data << d; d.length } }
13
+
14
+ assert_nil c.body_str
15
+ assert_equal "", c.header_str
16
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)
17
+ end
18
+
19
+ def test_class_perform_03
20
+ assert_raise(Curl::Err::CouldntReadError) { c = Curl::Easy.perform($TEST_URL + "nonexistent") }
21
+ end
22
+
23
+ def test_new_01
24
+ c = Curl::Easy.new
25
+ assert_equal Curl::Easy, c.class
26
+ assert_nil c.url
27
+ assert_nil c.body_str
28
+ assert_nil c.header_str
29
+ end
30
+
31
+ def test_new_02
32
+ c = Curl::Easy.new($TEST_URL)
33
+ assert_equal $TEST_URL, c.url
34
+ end
35
+
36
+ def test_new_03
37
+ blk = lambda { |i| i.length }
38
+
39
+ c = Curl::Easy.new do |curl|
40
+ curl.on_body(&blk)
41
+ end
42
+
43
+ assert_nil c.url
44
+ assert_equal blk, c.on_body # sets handler nil, returns old handler
45
+ assert_equal nil, c.on_body
46
+ end
47
+
48
+ def test_new_04
49
+ blk = lambda { |i| i.length }
50
+
51
+ c = Curl::Easy.new($TEST_URL) do |curl|
52
+ curl.on_body(&blk)
53
+ end
54
+
55
+ assert_equal $TEST_URL, c.url
56
+ assert_equal blk, c.on_body # sets handler nil, returns old handler
57
+ assert_equal nil, c.on_body
58
+ end
59
+
60
+ class Foo < Curl::Easy
61
+ end
62
+ def test_new_05
63
+ # can use Curl::Easy as a base class
64
+ c = Foo.new
65
+ assert_equal Foo, c.class
66
+ c.url = $TEST_URL
67
+ c.perform
68
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
69
+ end
70
+
71
+ # test invalid use of new
72
+ def test_new_06
73
+ Curl::Easy.new(TestServlet.url) do|curl|
74
+ curl.http_post
75
+ assert_equal "POST\n", curl.body_str
76
+ end
77
+ end
78
+
79
+ def test_escape
80
+ c = Curl::Easy.new
81
+
82
+ assert_equal "one%20two", c.escape('one two')
83
+ assert_equal "one%00two%20three", c.escape("one\000two three")
84
+ end
85
+
86
+ def test_unescape
87
+ c = Curl::Easy.new
88
+
89
+ assert_equal "one two", c.unescape('one%20two')
90
+
91
+ # prior to 7.15.4 embedded nulls cannot be unescaped
92
+ if Curl::VERNUM >= 0x070f04
93
+ assert_equal "one\000two three", c.unescape("one%00two%20three")
94
+ end
95
+ end
96
+
97
+ def test_headers
98
+ c = Curl::Easy.new($TEST_URL)
99
+
100
+ assert_equal({}, c.headers)
101
+ c.headers = "Expect:"
102
+ assert_equal "Expect:", c.headers
103
+ c.headers = ["Expect:", "User-Agent: myapp-0.0.0"]
104
+ assert_equal ["Expect:", "User-Agent: myapp-0.0.0"], c.headers
105
+ end
106
+
107
+ def test_get_01
108
+ c = Curl::Easy.new($TEST_URL)
109
+ assert_equal true, c.http_get
110
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
111
+ assert_equal "", c.header_str
112
+ end
113
+
114
+ def test_get_02
115
+ data = ""
116
+ c = Curl::Easy.new($TEST_URL) do |curl|
117
+ curl.on_body { |d| data << d; d.length }
118
+ end
119
+
120
+ assert_equal true, c.http_get
121
+
122
+ assert_nil c.body_str
123
+ assert_equal "", c.header_str
124
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)
125
+ end
126
+
127
+ def test_get_03
128
+ c = Curl::Easy.new($TEST_URL + "nonexistent")
129
+ assert_raise(Curl::Err::CouldntReadError) { c.http_get }
130
+ assert_equal "", c.body_str
131
+ assert_equal "", c.header_str
132
+ end
133
+
134
+
135
+ def test_last_effective_url_01
136
+ c = Curl::Easy.new($TEST_URL)
137
+
138
+ assert_equal $TEST_URL, c.url
139
+ assert_nil c.last_effective_url
140
+
141
+ assert c.http_get
142
+
143
+ assert_equal c.url, c.last_effective_url
144
+ c.url = "file://some/new.url"
145
+
146
+ assert_not_equal c.last_effective_url, c.url
147
+ end
148
+
149
+ def test_http_get_block
150
+ curl = Curl::Easy.http_get(TestServlet.url) do|c|
151
+ c.follow_location = true
152
+ c.max_redirects = 3
153
+ end
154
+ assert_equal curl.url, curl.last_effective_url
155
+ assert_equal 'GET', curl.body_str
156
+ end
157
+
158
+ def test_local_port_01
159
+ c = Curl::Easy.new($TEST_URL)
160
+
161
+ assert_nil c.local_port
162
+ assert_nil c.local_port_range
163
+ assert_nil c.proxy_port
164
+
165
+ c.local_port = 88
166
+
167
+ assert_equal 88, c.local_port
168
+ assert_nil c.local_port_range
169
+ assert_nil c.proxy_port
170
+
171
+ c.local_port = nil
172
+
173
+ assert_nil c.local_port
174
+ assert_nil c.local_port_range
175
+ assert_nil c.proxy_port
176
+ end
177
+
178
+ def test_local_port_02
179
+ c = Curl::Easy.new($TEST_URL)
180
+
181
+ assert_nil c.local_port
182
+ assert_raise(ArgumentError) { c.local_port = 0 }
183
+ assert_raise(ArgumentError) { c.local_port = 65536 }
184
+ assert_raise(ArgumentError) { c.local_port = -1 }
185
+ end
186
+
187
+ def test_local_port_range_01
188
+ c = Curl::Easy.new($TEST_URL)
189
+
190
+ assert_nil c.local_port_range
191
+ assert_nil c.local_port
192
+ assert_nil c.proxy_port
193
+
194
+ c.local_port_range = 88
195
+ assert_equal 88, c.local_port_range
196
+ assert_nil c.local_port
197
+ assert_nil c.proxy_port
198
+
199
+ c.local_port_range = nil
200
+
201
+ assert_nil c.local_port_range
202
+ assert_nil c.local_port
203
+ assert_nil c.proxy_port
204
+ end
205
+
206
+ def test_local_port_range_02
207
+ c = Curl::Easy.new($TEST_URL)
208
+
209
+ assert_nil c.local_port_range
210
+ assert_raise(ArgumentError) { c.local_port_range = 0 }
211
+ assert_raise(ArgumentError) { c.local_port_range = 65536 }
212
+ assert_raise(ArgumentError) { c.local_port_range = -1 }
213
+ end
214
+
215
+ def test_proxy_url_01
216
+ c = Curl::Easy.new($TEST_URL)
217
+
218
+ assert_equal $TEST_URL, c.url
219
+ assert_nil c.proxy_url
220
+
221
+ c.proxy_url = "http://some.proxy"
222
+
223
+ assert_equal $TEST_URL, c.url
224
+ assert_equal "http://some.proxy", c.proxy_url
225
+
226
+ c.proxy_url = nil
227
+ assert_equal $TEST_URL, c.url
228
+ assert_nil c.proxy_url
229
+ end
230
+
231
+ def test_proxy_port_01
232
+ c = Curl::Easy.new($TEST_URL)
233
+
234
+ assert_nil c.local_port
235
+ assert_nil c.local_port_range
236
+ assert_nil c.proxy_port
237
+
238
+ c.proxy_port = 88
239
+
240
+ assert_equal 88, c.proxy_port
241
+ assert_nil c.local_port
242
+ assert_nil c.local_port_range
243
+
244
+ c.proxy_port = nil
245
+ assert_nil c.proxy_port
246
+ assert_nil c.local_port
247
+ assert_nil c.local_port_range
248
+ end
249
+
250
+ def test_proxy_port_02
251
+ c = Curl::Easy.new($TEST_URL)
252
+
253
+ assert_nil c.proxy_port
254
+ assert_raise(ArgumentError) { c.proxy_port = 0 }
255
+ assert_raise(ArgumentError) { c.proxy_port = 65536 }
256
+ assert_raise(ArgumentError) { c.proxy_port = -1 }
257
+ end
258
+
259
+ def test_proxy_type_01
260
+ c = Curl::Easy.new($TEST_URL)
261
+
262
+ assert_nil c.proxy_type
263
+
264
+ c.proxy_type = 3
265
+ assert_equal 3, c.proxy_type
266
+
267
+ c.proxy_type = nil
268
+ assert_nil c.proxy_type
269
+ end
270
+
271
+ def test_http_auth_types_01
272
+ c = Curl::Easy.new($TEST_URL)
273
+
274
+ assert_nil c.http_auth_types
275
+
276
+ c.http_auth_types = 3
277
+ assert_equal 3, c.http_auth_types
278
+
279
+ c.http_auth_types = nil
280
+ assert_nil c.http_auth_types
281
+ end
282
+
283
+ def test_proxy_auth_types_01
284
+ c = Curl::Easy.new($TEST_URL)
285
+
286
+ assert_nil c.proxy_auth_types
287
+
288
+ c.proxy_auth_types = 3
289
+ assert_equal 3, c.proxy_auth_types
290
+
291
+ c.proxy_auth_types = nil
292
+ assert_nil c.proxy_auth_types
293
+ end
294
+
295
+ def test_max_redirects_01
296
+ c = Curl::Easy.new($TEST_URL)
297
+
298
+ assert_nil c.max_redirects
299
+
300
+ c.max_redirects = 3
301
+ assert_equal 3, c.max_redirects
302
+
303
+ c.max_redirects = nil
304
+ assert_nil c.max_redirects
305
+ end
306
+
307
+ def test_timeout_01
308
+ c = Curl::Easy.new($TEST_URL)
309
+
310
+ assert_nil c.timeout
311
+
312
+ c.timeout = 3
313
+ assert_equal 3, c.timeout
314
+
315
+ c.timeout = nil
316
+ assert_nil c.timeout
317
+ end
318
+
319
+ def test_connect_timeout_01
320
+ c = Curl::Easy.new($TEST_URL)
321
+
322
+ assert_nil c.connect_timeout
323
+
324
+ c.connect_timeout = 3
325
+ assert_equal 3, c.connect_timeout
326
+
327
+ c.connect_timeout = nil
328
+ assert_nil c.connect_timeout
329
+ end
330
+
331
+ def test_ftp_response_timeout_01
332
+ c = Curl::Easy.new($TEST_URL)
333
+
334
+ assert_nil c.ftp_response_timeout
335
+
336
+ c.ftp_response_timeout = 3
337
+ assert_equal 3, c.ftp_response_timeout
338
+
339
+ c.ftp_response_timeout = nil
340
+ assert_nil c.ftp_response_timeout
341
+ end
342
+
343
+ def test_dns_cache_timeout_01
344
+ c = Curl::Easy.new($TEST_URL)
345
+
346
+ assert_equal 60, c.dns_cache_timeout
347
+
348
+ c.dns_cache_timeout = nil
349
+ assert_nil c.dns_cache_timeout
350
+
351
+ c.dns_cache_timeout = 30
352
+ assert_equal 30, c.dns_cache_timeout
353
+ end
354
+
355
+ def test_on_body
356
+ blk = lambda { |i| i.length }
357
+
358
+ c = Curl::Easy.new
359
+ c.on_body(&blk)
360
+
361
+ assert_equal blk, c.on_body # sets handler nil, returns old handler
362
+ assert_equal nil, c.on_body
363
+ end
364
+
365
+ def test_on_header
366
+ blk = lambda { |i| i.length }
367
+
368
+ c = Curl::Easy.new
369
+ c.on_header(&blk)
370
+
371
+ assert_equal blk, c.on_header # sets handler nil, returns old handler
372
+ assert_equal nil, c.on_header
373
+ end
374
+
375
+ def test_on_progress
376
+ blk = lambda { |*args| true }
377
+
378
+ c = Curl::Easy.new
379
+ c.on_progress(&blk)
380
+
381
+ assert_equal blk, c.on_progress # sets handler nil, returns old handler
382
+ assert_equal nil, c.on_progress
383
+ end
384
+
385
+ def test_on_debug
386
+ blk = lambda { |*args| true }
387
+
388
+ c = Curl::Easy.new
389
+ c.on_debug(&blk)
390
+
391
+ assert_equal blk, c.on_debug # sets handler nil, returns old handler
392
+ assert_equal nil, c.on_debug
393
+ end
394
+
395
+ def test_proxy_tunnel
396
+ c = Curl::Easy.new
397
+ assert !c.proxy_tunnel?
398
+ assert c.proxy_tunnel = true
399
+ assert c.proxy_tunnel?
400
+ end
401
+
402
+ def test_fetch_file_time
403
+ c = Curl::Easy.new
404
+ assert !c.fetch_file_time?
405
+ assert c.fetch_file_time = true
406
+ assert c.fetch_file_time?
407
+ end
408
+
409
+ def test_ssl_verify_peer
410
+ c = Curl::Easy.new
411
+ assert c.ssl_verify_peer?
412
+ assert !c.ssl_verify_peer = false
413
+ assert !c.ssl_verify_peer?
414
+ end
415
+
416
+ def test_ssl_verify_host
417
+ c = Curl::Easy.new
418
+ assert c.ssl_verify_host?
419
+ assert !c.ssl_verify_host = false
420
+ assert !c.ssl_verify_host?
421
+ end
422
+
423
+ def test_header_in_body
424
+ c = Curl::Easy.new
425
+ assert !c.header_in_body?
426
+ assert c.header_in_body = true
427
+ assert c.header_in_body?
428
+ end
429
+
430
+ def test_use_netrc
431
+ c = Curl::Easy.new
432
+ assert !c.use_netrc?
433
+ assert c.use_netrc = true
434
+ assert c.use_netrc?
435
+ end
436
+
437
+ def test_follow_location
438
+ c = Curl::Easy.new
439
+ assert !c.follow_location?
440
+ assert c.follow_location = true
441
+ assert c.follow_location?
442
+ end
443
+
444
+ def test_unrestricted_auth
445
+ c = Curl::Easy.new
446
+ assert !c.unrestricted_auth?
447
+ assert c.unrestricted_auth = true
448
+ assert c.unrestricted_auth?
449
+ end
450
+
451
+ def test_multipart_form_post
452
+ c = Curl::Easy.new
453
+ assert !c.multipart_form_post?
454
+ assert c.multipart_form_post = true
455
+ assert c.multipart_form_post?
456
+ end
457
+
458
+ def test_enable_cookies
459
+ c = Curl::Easy.new
460
+ assert !c.enable_cookies?
461
+ assert c.enable_cookies = true
462
+ assert c.enable_cookies?
463
+ end
464
+
465
+ def test_cookies_option
466
+ c = Curl::Easy.new
467
+ assert_nil c.cookies
468
+ assert_equal "name1=content1; name2=content2;", c.cookies = "name1=content1; name2=content2;"
469
+ assert_equal "name1=content1; name2=content2;", c.cookies
470
+ end
471
+
472
+ def test_cookiefile
473
+ c = Curl::Easy.new
474
+ assert_nil c.cookiefile
475
+ assert_equal "some.file", c.cookiefile = "some.file"
476
+ assert_equal "some.file", c.cookiefile
477
+ end
478
+
479
+ def test_cookiejar
480
+ c = Curl::Easy.new
481
+ assert_nil c.cookiejar
482
+ assert_equal "some.file", c.cookiejar = "some.file"
483
+ assert_equal "some.file", c.cookiejar
484
+ end
485
+
486
+ def test_on_success
487
+ curl = Curl::Easy.new($TEST_URL)
488
+ on_success_called = false
489
+ curl.on_success {|c|
490
+ on_success_called = true
491
+ assert_not_nil c.body_str
492
+ assert_equal "", c.header_str
493
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
494
+ }
495
+ curl.perform
496
+ assert on_success_called, "Success handler not called"
497
+ end
498
+
499
+ def test_on_success_with_on_failure
500
+ curl = Curl::Easy.new("#{$TEST_URL.gsub(/file:\/\//,'')}/not_here")
501
+ on_failure_called = false
502
+ curl.on_success {|c| } # make sure we get the failure call even though this handler is defined
503
+ curl.on_failure {|c,code| on_failure_called = true }
504
+ curl.perform
505
+ assert on_failure_called, "Failure handler not called"
506
+ end
507
+
508
+ def test_get_remote
509
+ curl = Curl::Easy.new(TestServlet.url)
510
+ curl.http_get
511
+ assert_equal 'GET', curl.body_str
512
+ end
513
+
514
+ def test_post_remote
515
+ curl = Curl::Easy.new(TestServlet.url)
516
+ curl.http_post
517
+ assert_equal "POST\n", curl.body_str
518
+ end
519
+
520
+ def test_post_remote_is_easy_handle
521
+ # see: http://pastie.org/560852 and
522
+ # http://groups.google.com/group/curb---ruby-libcurl-bindings/browse_thread/thread/216bb2d9b037f347?hl=en
523
+ [:post, :get,:head,:delete].each do |method|
524
+ count = 0
525
+ curl = Curl::Easy.send("http_#{method}", TestServlet.url) do|c|
526
+ count += 1
527
+ assert_equal Curl::Easy, c.class
528
+ end
529
+ assert_equal 1, count, "For request method: #{method.to_s.upcase}"
530
+ end
531
+ end
532
+
533
+ def test_post_with_body_remote
534
+ curl = Curl::Easy.new(TestServlet.url)
535
+ curl.post_body = 'foo=bar&encoded%20string=val'
536
+
537
+ curl.perform
538
+
539
+ assert_equal "POST\nfoo=bar&encoded%20string=val", curl.body_str
540
+ assert_equal 'foo=bar&encoded%20string=val', curl.post_body
541
+ end
542
+
543
+ def test_form_post_body_remote
544
+ curl = Curl::Easy.new(TestServlet.url)
545
+ curl.http_post('foo=bar', 'encoded%20string=val')
546
+
547
+ assert_equal "POST\nfoo=bar&encoded%20string=val", curl.body_str
548
+ assert_equal 'foo=bar&encoded%20string=val', curl.post_body
549
+ end
550
+
551
+ def test_delete_remote
552
+ curl = Curl::Easy.new(TestServlet.url)
553
+ curl.http_delete
554
+ assert_equal 'DELETE', curl.body_str
555
+ end
556
+
557
+ def test_head_remote
558
+ curl = Curl::Easy.new(TestServlet.url)
559
+ curl.http_head
560
+
561
+ redirect = curl.header_str.match(/Location: (.*)/)
562
+
563
+ assert_equal '', curl.body_str
564
+ assert_match '/nonexistent', redirect[1]
565
+ end
566
+
567
+ def test_head_accessor
568
+ curl = Curl::Easy.new(TestServlet.url)
569
+ curl.head = true
570
+ curl.perform
571
+
572
+ redirect = curl.header_str.match(/Location: (.*)/)
573
+
574
+ assert_equal '', curl.body_str
575
+ assert_match '/nonexistent', redirect[1]
576
+ curl.head = false
577
+ curl.perform
578
+ assert_equal 'GET', curl.body_str
579
+ end
580
+
581
+ def test_put_remote
582
+ curl = Curl::Easy.new(TestServlet.url)
583
+ curl.headers['Content-Type'] = 'application/json'
584
+ assert curl.http_put("message")
585
+ assert_match /^PUT/, curl.body_str
586
+ assert_match /message$/, curl.body_str
587
+ assert_match /application\/json/, curl.header_str
588
+ end
589
+
590
+ def test_put_data
591
+ curl = Curl::Easy.new(TestServlet.url)
592
+ curl.put_data = 'message'
593
+
594
+ curl.perform
595
+
596
+ assert_match /^PUT/, curl.body_str
597
+ assert_match /message$/, curl.body_str
598
+ end
599
+
600
+ def test_put_remote_file
601
+ curl = Curl::Easy.new(TestServlet.url)
602
+ File.open(__FILE__,'r') do|f|
603
+ assert curl.http_put(f)
604
+ end
605
+ assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str
606
+ end
607
+
608
+ def test_put_class_method
609
+ count = 0
610
+ curl = Curl::Easy.http_put(TestServlet.url,File.open(__FILE__,'rb')) do|c|
611
+ count += 1
612
+ assert_equal Curl::Easy, c.class
613
+ end
614
+ assert_equal 1, count
615
+ assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str
616
+ end
617
+
618
+ # Generate a self-signed cert with
619
+ # openssl req -new -newkey rsa:1024 -days 365 -nodes -x509 \
620
+ # -keyout tests/cert.pem -out tests/cert.pem
621
+ def test_cert
622
+ curl = Curl::Easy.new(TestServlet.url)
623
+ curl.cert= File.join(File.dirname(__FILE__),"cert.pem")
624
+ assert /cert.pem$/,curl.cert
625
+ end
626
+
627
+ def test_cert_with_password
628
+ curl = Curl::Easy.new(TestServlet.url)
629
+ curl.cert= File.join(File.dirname(__FILE__),"cert.pem:password")
630
+ assert /cert.pem$/,curl.cert
631
+ end
632
+
633
+ def test_cert_type
634
+ curl = Curl::Easy.new(TestServlet.url)
635
+ curl.certtype= "DER"
636
+ assert "DER", curl.certtype
637
+ end
638
+
639
+ def test_default_certtype
640
+ curl = Curl::Easy.new(TestServlet.url)
641
+ assert "PEM", curl.certtype
642
+ end
643
+
644
+ # Generate a CA cert with instructions at
645
+ # http://technocage.com/~caskey/openssl/
646
+ def test_ca_cert
647
+ curl = Curl::Easy.new(TestServlet.url)
648
+ curl.cacert= File.join(File.dirname(__FILE__),"cacert.pem")
649
+ assert /cacert.pem$/, curl.cacert
650
+ end
651
+
652
+ def test_user_agent
653
+ curl = Curl::Easy.new(TestServlet.url)
654
+ curl.useragent= "Curb-Easy/Ruby"
655
+ assert /ScrubDog$/,curl.useragent
656
+ end
657
+
658
+ include TestServerMethods
659
+
660
+ def setup
661
+ server_setup
662
+ end
663
+
664
+ end