-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtc_curl_easy.rb
1181 lines (942 loc) · 29.5 KB
/
tc_curl_easy.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class FooNoToS
undef to_s
end
class TestCurbCurlEasy < Test::Unit::TestCase
def test_global_reset
Curl.get($TEST_URL)
# in a Timeout block you should reset the thread current handle
Curl.reset
end
def test_curlopt_stderr_with_file
# does not work with Tempfile directly
path = Tempfile.new('curb_test_curlopt_stderr').path
File.open(path, 'w') do |file|
easy = Curl::Easy.new(TestServlet.url)
easy.verbose = true
easy.setopt(Curl::CURLOPT_STDERR, file)
easy.perform
end
output = File.read(path)
assert_match('HTTP/1.1 200 OK ', output)
assert_match('Host: 127.0.0.1:9129', output)
end
def test_curlopt_stderr_with_io
path = Tempfile.new('curb_test_curlopt_stderr').path
fd = IO.sysopen(path, 'w')
io = IO.for_fd(fd)
easy = Curl::Easy.new(TestServlet.url)
easy.verbose = true
easy.setopt(Curl::CURLOPT_STDERR, io)
easy.perform
output = File.read(path)
assert_match(output, 'HTTP/1.1 200 OK')
assert_match(output, 'Host: 127.0.0.1:9129')
end
def test_curlopt_stderr_fails_with_tempdir
Tempfile.open('curb_test_curlopt_stderr') do |tempfile|
easy = Curl::Easy.new(TestServlet.url)
assert_raise(TypeError) do
easy.setopt(Curl::CURLOPT_STDERR, tempfile)
end
end
end
def test_curlopt_stderr_fails_with_stringio
stringio = StringIO.new
easy = Curl::Easy.new(TestServlet.url)
assert_raise(TypeError) do
easy.setopt(Curl::CURLOPT_STDERR, stringio)
end
end
def test_curlopt_stderr_fails_with_string
string = String.new
easy = Curl::Easy.new(TestServlet.url)
assert_raise(TypeError) do
easy.setopt(Curl::CURLOPT_STDERR, string)
end
end
def test_exception
begin
Curl.get('NOT_FOUND_URL')
rescue
assert true
rescue Exception
assert false, "We should raise StandardError"
end
end
def test_threads
t = []
5.times do
t << Thread.new do
5.times do
c = Curl.get($TEST_URL)
assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body)
end
end
end
t.each {|x| x.join }
end
def test_class_perform_01
assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL)
assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body)
end
def test_class_perform_02
data = ""
assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL) { |curl| curl.on_body { |d| data << d; d.length } }
assert_nil c.body_str
assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)
end
def test_class_perform_03
assert_raise(Curl::Err::CouldntReadError) { Curl::Easy.perform($TEST_URL + "nonexistent") }
end
def test_new_01
c = Curl::Easy.new
assert_equal Curl::Easy, c.class
assert_nil c.url
assert_nil c.body_str
assert_nil c.header_str
end
def test_new_02
c = Curl::Easy.new($TEST_URL)
assert_equal $TEST_URL, c.url
end
def test_new_03
blk = lambda { |i| i.length }
c = Curl::Easy.new do |curl|
curl.on_body(&blk)
end
assert_nil c.url
assert_equal blk, c.on_body # sets handler nil, returns old handler
assert_equal nil, c.on_body
end
def test_new_04
blk = lambda { |i| i.length }
c = Curl::Easy.new($TEST_URL) do |curl|
curl.on_body(&blk)
end
assert_equal $TEST_URL, c.url
assert_equal blk, c.on_body # sets handler nil, returns old handler
assert_equal nil, c.on_body
end
class Foo < Curl::Easy ; end
def test_new_05
# can use Curl::Easy as a base class
c = Foo.new
assert_equal Foo, c.class
c.url = $TEST_URL
c.perform
assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
end
# test invalid use of new
def test_new_06
Curl::Easy.new(TestServlet.url) do|curl|
curl.http_post
assert_equal "POST\n", curl.body_str
end
end
def test_escape
c = Curl::Easy.new
assert_equal "one%20two", c.escape('one two')
assert_equal "one%00two%20three", c.escape("one\000two three")
end
def test_unescape
c = Curl::Easy.new
assert_equal "one two", c.unescape('one%20two')
# prior to 7.15.4 embedded nulls cannot be unescaped
if Curl::VERNUM >= 0x070f04
assert_equal "one\000two three", c.unescape("one%00two%20three")
end
end
def test_headers
c = Curl::Easy.new($TEST_URL)
assert_equal({}, c.headers)
c.headers = "Expect:"
assert_equal "Expect:", c.headers
c.headers = ["Expect:", "User-Agent: myapp-0.0.0"]
assert_equal ["Expect:", "User-Agent: myapp-0.0.0"], c.headers
end
def test_get_01
c = Curl::Easy.new($TEST_URL)
assert_equal true, c.http_get
assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
end
def test_get_02
data = ""
c = Curl::Easy.new($TEST_URL) do |curl|
curl.on_body { |d| data << d; d.length }
end
assert_equal true, c.http_get
assert_nil c.body_str
assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)
end
def test_get_03
c = Curl::Easy.new($TEST_URL + "nonexistent")
assert_raise(Curl::Err::CouldntReadError) { c.http_get }
assert_equal "", c.body_str
assert_equal "", c.header_str
end
def test_last_effective_url_01
c = Curl::Easy.new($TEST_URL)
assert_equal $TEST_URL, c.url
assert_nil c.last_effective_url
assert c.http_get
assert_equal c.url, c.last_effective_url
c.url = "file://some/new.url"
assert_not_equal c.last_effective_url, c.url
end
def test_http_get_block
curl = Curl::Easy.http_get(TestServlet.url) do|c|
c.follow_location = true
c.max_redirects = 3
end
assert_equal curl.url, curl.last_effective_url
assert_equal 'GET', curl.body_str
end
def test_local_port_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.local_port
assert_nil c.local_port_range
assert_nil c.proxy_port
c.local_port = 88
assert_equal 88, c.local_port
assert_nil c.local_port_range
assert_nil c.proxy_port
c.local_port = nil
assert_nil c.local_port
assert_nil c.local_port_range
assert_nil c.proxy_port
end
def test_local_port_02
c = Curl::Easy.new($TEST_URL)
assert_nil c.local_port
assert_raise(ArgumentError) { c.local_port = 0 }
assert_raise(ArgumentError) { c.local_port = 65536 }
assert_raise(ArgumentError) { c.local_port = -1 }
end
def test_local_port_range_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.local_port_range
assert_nil c.local_port
assert_nil c.proxy_port
c.local_port_range = 88
assert_equal 88, c.local_port_range
assert_nil c.local_port
assert_nil c.proxy_port
c.local_port_range = nil
assert_nil c.local_port_range
assert_nil c.local_port
assert_nil c.proxy_port
end
def test_local_port_range_02
c = Curl::Easy.new($TEST_URL)
assert_nil c.local_port_range
assert_raise(ArgumentError) { c.local_port_range = 0 }
assert_raise(ArgumentError) { c.local_port_range = 65536 }
assert_raise(ArgumentError) { c.local_port_range = -1 }
end
def test_proxy_url_01
c = Curl::Easy.new($TEST_URL)
assert_equal $TEST_URL, c.url
assert_nil c.proxy_url
c.proxy_url = "http://some.proxy"
assert_equal $TEST_URL, c.url
assert_equal "http://some.proxy", c.proxy_url
c.proxy_url = nil
assert_equal $TEST_URL, c.url
assert_nil c.proxy_url
end
def test_proxy_port_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.local_port
assert_nil c.local_port_range
assert_nil c.proxy_port
c.proxy_port = 88
assert_equal 88, c.proxy_port
assert_nil c.local_port
assert_nil c.local_port_range
c.proxy_port = nil
assert_nil c.proxy_port
assert_nil c.local_port
assert_nil c.local_port_range
end
def test_proxy_port_02
c = Curl::Easy.new($TEST_URL)
assert_nil c.proxy_port
assert_raise(ArgumentError) { c.proxy_port = 0 }
assert_raise(ArgumentError) { c.proxy_port = 65536 }
assert_raise(ArgumentError) { c.proxy_port = -1 }
end
def test_proxy_type_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.proxy_type
c.proxy_type = 3
assert_equal 3, c.proxy_type
c.proxy_type = nil
assert_nil c.proxy_type
end
def test_http_auth_types_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.http_auth_types
c.http_auth_types = 3
assert_equal 3, c.http_auth_types
c.http_auth_types = nil
assert_nil c.http_auth_types
end
def test_proxy_auth_types_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.proxy_auth_types
c.proxy_auth_types = 3
assert_equal 3, c.proxy_auth_types
c.proxy_auth_types = nil
assert_nil c.proxy_auth_types
end
def test_max_redirects_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.max_redirects
c.max_redirects = 3
assert_equal 3, c.max_redirects
c.max_redirects = nil
assert_nil c.max_redirects
end
def test_timeout_with_floats
c = Curl::Easy.new($TEST_URL)
c.timeout = 1.5
assert_equal 1500, c.timeout_ms
assert_equal 1.5, c.timeout
end
def test_timeout_with_negative
c = Curl::Easy.new($TEST_URL)
c.timeout = -1.5
assert_equal 0, c.timeout
assert_equal 0, c.timeout_ms
c.timeout = -4.8
assert_equal 0, c.timeout
assert_equal 0, c.timeout_ms
end
def test_timeout_01
c = Curl::Easy.new($TEST_URL)
assert_equal 0, c.timeout
c.timeout = 3
assert_equal 3, c.timeout
c.timeout = 0
assert_equal 0, c.timeout
end
def test_timeout_ms_01
c = Curl::Easy.new($TEST_URL)
assert_equal 0, c.timeout_ms
c.timeout_ms = 100
assert_equal 100, c.timeout_ms
c.timeout_ms = nil
assert_equal 0, c.timeout_ms
end
def test_timeout_ms_with_floats
c = Curl::Easy.new($TEST_URL)
c.timeout_ms = 55.5
assert_equal 55, c.timeout_ms
assert_equal 0.055, c.timeout
end
def test_timeout_ms_with_negative
c = Curl::Easy.new($TEST_URL)
c.timeout_ms = -1.5
assert_equal 0, c.timeout
assert_equal 0, c.timeout_ms
c.timeout_ms = -4.8
assert_equal 0, c.timeout
assert_equal 0, c.timeout_ms
end
def test_connect_timeout_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.connect_timeout
c.connect_timeout = 3
assert_equal 3, c.connect_timeout
c.connect_timeout = nil
assert_nil c.connect_timeout
end
def test_connect_timeout_ms_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.connect_timeout_ms
c.connect_timeout_ms = 100
assert_equal 100, c.connect_timeout_ms
c.connect_timeout_ms = nil
assert_nil c.connect_timeout_ms
end
def test_ftp_response_timeout_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.ftp_response_timeout
c.ftp_response_timeout = 3
assert_equal 3, c.ftp_response_timeout
c.ftp_response_timeout = nil
assert_nil c.ftp_response_timeout
end
def test_dns_cache_timeout_01
c = Curl::Easy.new($TEST_URL)
assert_equal 60, c.dns_cache_timeout
c.dns_cache_timeout = nil
assert_nil c.dns_cache_timeout
c.dns_cache_timeout = 30
assert_equal 30, c.dns_cache_timeout
end
def test_low_speed_limit_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.low_speed_limit
c.low_speed_limit = 3
assert_equal 3, c.low_speed_limit
c.low_speed_limit = nil
assert_nil c.low_speed_limit
end
def test_low_speed_time_01
c = Curl::Easy.new($TEST_URL)
assert_nil c.low_speed_time
c.low_speed_time = 3
assert_equal 3, c.low_speed_time
c.low_speed_time = nil
assert_nil c.low_speed_time
end
def test_on_body
blk = lambda { |i| i.length }
c = Curl::Easy.new
c.on_body(&blk)
assert_equal blk, c.on_body # sets handler nil, returns old handler
assert_equal nil, c.on_body
end
def test_inspect_with_no_url
c = Curl::Easy.new
assert_equal '#<Curl::Easy>', c.inspect
end
def test_inspect_with_short_url
c = Curl::Easy.new('http://www.google.com/')
assert_equal "#<Curl::Easy http://www.google.com/>", c.inspect
end
def test_inspect_truncates_to_64_chars
base_url = 'http://www.google.com/'
truncated_url = base_url + 'x' * (64 - '#<Curl::Easy >'.size - base_url.size)
long_url = truncated_url + 'yyyy'
c = Curl::Easy.new(long_url)
assert_equal 64, c.inspect.size
assert_equal "#<Curl::Easy #{truncated_url}>", c.inspect
end
def test_on_header
blk = lambda { |i| i.length }
c = Curl::Easy.new
c.on_header(&blk)
assert_equal blk, c.on_header # sets handler nil, returns old handler
assert_equal nil, c.on_header
end
def test_on_progress
blk = lambda { |*args| true }
c = Curl::Easy.new
c.on_progress(&blk)
assert_equal blk, c.on_progress # sets handler nil, returns old handler
assert_equal nil, c.on_progress
end
def test_on_debug
blk = lambda { |*args| true }
c = Curl::Easy.new
c.on_debug(&blk)
assert_equal blk, c.on_debug # sets handler nil, returns old handler
assert_equal nil, c.on_debug
end
def test_proxy_tunnel
c = Curl::Easy.new
assert !c.proxy_tunnel?
assert c.proxy_tunnel = true
assert c.proxy_tunnel?
end
def test_fetch_file_time
c = Curl::Easy.new
assert !c.fetch_file_time?
assert c.fetch_file_time = true
assert c.fetch_file_time?
end
def test_ssl_verify_peer
c = Curl::Easy.new
assert c.ssl_verify_peer?
assert !c.ssl_verify_peer = false
assert !c.ssl_verify_peer?
end
def test_ssl_verify_host
c = Curl::Easy.new
assert c.ssl_verify_host?
c.ssl_verify_host = 0
c.ssl_verify_host = false
assert !c.ssl_verify_host?
end
def test_header_in_body
c = Curl::Easy.new
assert !c.header_in_body?
assert c.header_in_body = true
assert c.header_in_body?
end
def test_use_netrc
c = Curl::Easy.new
assert !c.use_netrc?
assert c.use_netrc = true
assert c.use_netrc?
end
def test_follow_location
c = Curl::Easy.new
assert !c.follow_location?
assert c.follow_location = true
assert c.follow_location?
end
def test_unrestricted_auth
c = Curl::Easy.new
assert !c.unrestricted_auth?
assert c.unrestricted_auth = true
assert c.unrestricted_auth?
end
def test_multipart_form_post
c = Curl::Easy.new
assert !c.multipart_form_post?
assert c.multipart_form_post = true
assert c.multipart_form_post?
end
def test_ignore_content_length
c = Curl::Easy.new
assert !c.ignore_content_length?
assert c.ignore_content_length = true
assert c.ignore_content_length?
end
def test_resolve_mode
c = Curl::Easy.new
assert_equal :auto, c.resolve_mode
c.resolve_mode = :ipv4
assert_equal :ipv4, c.resolve_mode
c.resolve_mode = :ipv6
assert_equal :ipv6, c.resolve_mode
assert_raises(ArgumentError) { c.resolve_mode = :bad }
end
def test_enable_cookies
c = Curl::Easy.new
assert !c.enable_cookies?
assert c.enable_cookies = true
assert c.enable_cookies?
end
def test_cookies_option
c = Curl::Easy.new
assert_nil c.cookies
assert_equal "name1=content1; name2=content2;", c.cookies = "name1=content1; name2=content2;"
assert_equal "name1=content1; name2=content2;", c.cookies
end
def test_cookiefile
c = Curl::Easy.new
assert_nil c.cookiefile
assert_equal "some.file", c.cookiefile = "some.file"
assert_equal "some.file", c.cookiefile
end
def test_cookiejar
c = Curl::Easy.new
assert_nil c.cookiejar
assert_equal "some.file", c.cookiejar = "some.file"
assert_equal "some.file", c.cookiejar
end
def test_cookielist
c = Curl::Easy.new TestServlet.url
c.enable_cookies = true
c.post_body = URI.encode_www_form('c' => 'somename=somevalue')
assert_nil c.cookielist
c.perform
assert_match(/somevalue/, c.cookielist.join(''))
end
def test_on_success
curl = Curl::Easy.new($TEST_URL)
on_success_called = false
curl.on_success {|c|
on_success_called = true
assert_not_nil c.body_str
assert_equal "", c.header_str
assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
}
curl.perform
assert on_success_called, "Success handler not called"
end
def test_on_success_with_on_failure
curl = Curl::Easy.new(TestServlet.url + '/error')
on_failure_called = false
curl.on_success {|c| } # make sure we get the failure call even though this handler is defined
curl.on_failure {|c,code| on_failure_called = true }
curl.perform
assert_equal 500, curl.response_code
assert on_failure_called, "Failure handler not called"
end
def test_on_success_with_on_missing
curl = Curl::Easy.new(TestServlet.url + '/not_here')
on_missing_called = false
curl.on_success {|c| } # make sure we get the missing call even though this handler is defined
curl.on_missing {|c,code| on_missing_called = true }
curl.perform
assert_equal 404, curl.response_code
assert on_missing_called, "Missing handler not called"
end
def test_on_success_with_on_redirect
curl = Curl::Easy.new(TestServlet.url + '/redirect')
on_redirect_called = false
curl.on_success {|c| } # make sure we get the redirect call even though this handler is defined
curl.on_redirect {|c,code| on_redirect_called = true }
curl.perform
assert_equal 302, curl.response_code
assert on_redirect_called, "Redirect handler not called"
end
def test_get_remote
curl = Curl::Easy.new(TestServlet.url)
curl.http_get
assert_equal 'GET', curl.body_str
end
def test_post_remote
curl = Curl::Easy.new(TestServlet.url)
curl.http_post([Curl::PostField.content('document_id', 5)])
assert_equal "POST\ndocument_id=5", curl.unescape(curl.body_str)
end
def test_post_remote_is_easy_handle
# see: http://pastie.org/560852 and
# http://groups.google.com/group/curb---ruby-libcurl-bindings/browse_thread/thread/216bb2d9b037f347?hl=en
[:post, :get, :head, :delete].each do |method|
retries = 0
begin
count = 0
Curl::Easy.send("http_#{method}", TestServlet.url) do|c|
count += 1
assert_equal Curl::Easy, c.class
end
assert_equal 1, count, "For request method: #{method.to_s.upcase}"
rescue Curl::Err::HostResolutionError => e # travis-ci.org fails to resolve... try again?
retries+=1
retry if retries < 3
raise e
end
end
end
# see: https://github.com/rvanlieshout/curb/commit/8bcdefddc0162484681ebd1a92d52a642666a445
def test_post_multipart_array_remote
curl = Curl::Easy.new(TestServlet.url)
curl.multipart_form_post = true
fields = [
Curl::PostField.file('foo', File.expand_path(File.join(File.dirname(__FILE__),'..','README.markdown'))),
Curl::PostField.file('bar', File.expand_path(File.join(File.dirname(__FILE__),'..','README.markdown')))
]
curl.http_post(fields)
assert_match(/HTTP POST file upload/, curl.body_str)
assert_match(/Content-Disposition: form-data/, curl.body_str)
end
def test_post_with_body_remote
curl = Curl::Easy.new(TestServlet.url)
curl.post_body = 'foo=bar&encoded%20string=val'
curl.perform
assert_equal "POST\nfoo=bar&encoded%20string=val", curl.body_str
assert_equal 'foo=bar&encoded%20string=val', curl.post_body
end
def test_form_post_body_remote
curl = Curl::Easy.new(TestServlet.url)
curl.http_post('foo=bar', 'encoded%20string=val')
assert_equal "POST\nfoo=bar&encoded%20string=val", curl.body_str
assert_equal 'foo=bar&encoded%20string=val', curl.post_body
end
def test_post_multipart_file_remote
curl = Curl::Easy.new(TestServlet.url)
curl.multipart_form_post = true
pf = Curl::PostField.file('readme', File.expand_path(File.join(File.dirname(__FILE__),'..','README.markdown')))
curl.http_post(pf)
assert_match(/HTTP POST file upload/, curl.body_str)
assert_match(/Content-Disposition: form-data/, curl.body_str)
end
def test_delete_remote
curl = Curl::Easy.new(TestServlet.url)
curl.http_delete
assert_equal 'DELETE', curl.body_str
end
def test_arbitrary_http_verb
curl = Curl::Easy.new(TestServlet.url)
curl.http('PURGE')
assert_equal 'PURGE', curl.body_str
end
def test_head_remote
curl = Curl::Easy.new(TestServlet.url)
curl.http_head
redirect = curl.header_str.match(/Location: (.*)/)
assert_equal '', curl.body_str
assert_match('/nonexistent', redirect[1])
end
def test_head_accessor
curl = Curl::Easy.new(TestServlet.url)
curl.head = true
curl.perform
redirect = curl.header_str.match(/Location: (.*)/)
assert_equal '', curl.body_str
assert_match('/nonexistent', redirect[1])
curl.head = false
curl.perform
assert_equal 'GET', curl.body_str
end
def test_put_remote
curl = Curl::Easy.new(TestServlet.url)
curl.headers['Content-Type'] = 'application/json'
assert curl.http_put("message")
assert_match(/^PUT/, curl.body_str)
assert_match(/message$/, curl.body_str)
assert_match(/message$/, curl.body)
assert_match(/application\/json/, curl.header_str)
assert_match(/application\/json/, curl.head)
end
def test_put_data
curl = Curl::Easy.new(TestServlet.url)
curl.put_data = 'message'
curl.perform
assert_match(/^PUT/, curl.body_str)
assert_match(/message$/, curl.body_str)
end
# https://github.com/taf2/curb/issues/101
def test_put_data_null_bytes
curl = Curl::Easy.new(TestServlet.url)
curl.put_data = "a\0b"
curl.perform
assert_match(/^PUT/, curl.body_str)
assert_match("a\0b", curl.body_str)
end
def test_put_nil_data_no_crash
curl = Curl::Easy.new(TestServlet.url)
curl.put_data = nil
curl.perform
end
def test_put_remote_file
curl = Curl::Easy.new(TestServlet.url)
File.open(__FILE__,'rb') do|f|
assert curl.http_put(f)
end
assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str.tr("\r", '')
end
def test_put_class_method
count = 0
curl = Curl::Easy.http_put(TestServlet.url,File.open(__FILE__,'rb')) do|c|
count += 1
assert_equal Curl::Easy, c.class
end
assert_equal 1, count
assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str.tr("\r", '')
end
# Generate a self-signed cert with
# openssl req -new -newkey rsa:1024 -days 365 -nodes -x509 \
# -keyout tests/cert.pem -out tests/cert.pem
def test_cert
curl = Curl::Easy.new(TestServlet.url)
curl.cert= File.join(File.dirname(__FILE__),"cert.pem")
assert_match(/cert.pem$/,curl.cert)
end
def test_cert_with_password
curl = Curl::Easy.new(TestServlet.url)
path = File.join(File.dirname(__FILE__),"cert.pem")
curl.certpassword = 'password'
curl.cert = path
assert_match(/cert.pem$/,curl.cert)
end
def test_cert_type
curl = Curl::Easy.new(TestServlet.url)
curl.certtype= "DER"
assert_equal "DER", curl.certtype
end
def test_default_certtype
curl = Curl::Easy.new(TestServlet.url)
assert_nil curl.certtype
curl.certtype = "PEM"
assert_equal "PEM", curl.certtype
end
# Generate a CA cert with instructions at
# http://technocage.com/~caskey/openssl/
def test_ca_cert
curl = Curl::Easy.new(TestServlet.url)
curl.cacert= File.join(File.dirname(__FILE__),"cacert.pem")
assert_match(/cacert.pem$/, curl.cacert)
end
def test_user_agent
curl = Curl::Easy.new(TestServlet.url)
curl.useragent= "Curb-Easy/Ruby"
assert_equal "Curb-Easy/Ruby",curl.useragent
end
def test_username_password
curl = Curl::Easy.new(TestServlet.url)
curl.username = "foo"
curl.password = "bar"
if !curl.username.nil?
assert_equal "foo", curl.username
assert_equal "bar", curl.password
else
curl.userpwd = "foo:bar"
end
curl.http_auth_types = :basic
#curl.verbose = true
curl.perform
assert_equal 'Basic Zm9vOmJhcg==', $auth_header
$auth_header = nil
# curl checks the auth type supported by the server, so we have to create a
# new easy handle if we're going to change the auth type...
curl = Curl::Easy.new(TestServlet.url)
curl.username = "foo"
curl.password = "bar"
if curl.username.nil?
curl.userpwd = "foo:bar"
end
curl.http_auth_types = :ntlm
curl.perform
assert_equal 'NTLM TlRMTVNTUAABAAAABoIIAAAAAAAAAAAAAAAAAAAAAAA=', $auth_header
end
def test_primary_ip
curl = Curl::Easy.new(TestServlet.url)
if curl.respond_to?(:primary_ip)
curl.perform
assert_equal '127.0.0.1', curl.primary_ip
end
end
def test_post_streaming