curb 0.9.3 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,11 +5,89 @@ end
5
5
 
6
6
  class TestCurbCurlEasy < Test::Unit::TestCase
7
7
  def test_global_reset
8
- c = Curl.get($TEST_URL)
8
+ Curl.get($TEST_URL)
9
9
  # in a Timeout block you should reset the thread current handle
10
10
  Curl.reset
11
11
  end
12
12
 
13
+ def test_nested_easy_methods
14
+ easy = Curl.get(TestServlet.url) {|http|
15
+ res = Curl.get(TestServlet.url + '/not_here')
16
+ assert_equal 404, res.code
17
+ }
18
+ assert_equal 200, easy.code
19
+ end
20
+
21
+ def test_curlopt_stderr_with_file
22
+ # does not work with Tempfile directly
23
+ path = Tempfile.new('curb_test_curlopt_stderr').path
24
+ File.open(path, 'w') do |file|
25
+ easy = Curl::Easy.new(TestServlet.url)
26
+ easy.verbose = true
27
+ easy.setopt(Curl::CURLOPT_STDERR, file)
28
+ easy.perform
29
+ end
30
+ output = File.read(path)
31
+
32
+ assert_match(/HTTP\/1\.1\ 200\ OK(?:\ )?/, output)
33
+ assert_match('Host: 127.0.0.1:9129', output)
34
+ end
35
+
36
+ def test_curlopt_stderr_with_io
37
+ path = Tempfile.new('curb_test_curlopt_stderr').path
38
+ fd = IO.sysopen(path, 'w')
39
+ io = IO.for_fd(fd)
40
+
41
+ easy = Curl::Easy.new(TestServlet.url)
42
+ easy.verbose = true
43
+ easy.setopt(Curl::CURLOPT_STDERR, io)
44
+ easy.perform
45
+
46
+
47
+ output = File.read(path)
48
+
49
+ assert_match(output, 'HTTP/1.1 200 OK')
50
+ assert_match(output, 'Host: 127.0.0.1:9129')
51
+ end
52
+
53
+ def test_curlopt_stderr_fails_with_tempdir
54
+ Tempfile.open('curb_test_curlopt_stderr') do |tempfile|
55
+ easy = Curl::Easy.new(TestServlet.url)
56
+
57
+ assert_raise(TypeError) do
58
+ easy.setopt(Curl::CURLOPT_STDERR, tempfile)
59
+ end
60
+ end
61
+ end
62
+
63
+ def test_curlopt_stderr_fails_with_stringio
64
+ stringio = StringIO.new
65
+ easy = Curl::Easy.new(TestServlet.url)
66
+
67
+ assert_raise(TypeError) do
68
+ easy.setopt(Curl::CURLOPT_STDERR, stringio)
69
+ end
70
+ end
71
+
72
+ def test_curlopt_stderr_fails_with_string
73
+ string = String.new
74
+ easy = Curl::Easy.new(TestServlet.url)
75
+
76
+ assert_raise(TypeError) do
77
+ easy.setopt(Curl::CURLOPT_STDERR, string)
78
+ end
79
+ end
80
+
81
+ def test_exception
82
+ begin
83
+ Curl.get('NOT_FOUND_URL')
84
+ rescue
85
+ assert true
86
+ rescue Exception
87
+ assert false, "We should raise StandardError"
88
+ end
89
+ end
90
+
13
91
  def test_threads
14
92
  t = []
15
93
  5.times do
@@ -18,20 +96,17 @@ class TestCurbCurlEasy < Test::Unit::TestCase
18
96
  c = Curl.get($TEST_URL)
19
97
  assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
20
98
  assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body)
21
- assert_equal "", c.header_str
22
- assert_equal "", c.head
23
99
  end
24
100
  end
25
101
  end
26
102
 
27
- t.each {|t| t.join }
103
+ t.each {|x| x.join }
28
104
  end
29
105
 
30
106
  def test_class_perform_01
31
107
  assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL)
32
108
  assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
33
109
  assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body)
34
- assert_equal "", c.header_str
35
110
  end
36
111
 
37
112
  def test_class_perform_02
@@ -39,12 +114,11 @@ class TestCurbCurlEasy < Test::Unit::TestCase
39
114
  assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL) { |curl| curl.on_body { |d| data << d; d.length } }
40
115
 
41
116
  assert_nil c.body_str
42
- assert_equal "", c.header_str
43
117
  assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)
44
118
  end
45
119
 
46
120
  def test_class_perform_03
47
- assert_raise(Curl::Err::CouldntReadError) { c = Curl::Easy.perform($TEST_URL + "nonexistent") }
121
+ assert_raise(Curl::Err::CouldntReadError) { Curl::Easy.perform($TEST_URL + "nonexistent") }
48
122
  end
49
123
 
50
124
  def test_new_01
@@ -134,7 +208,6 @@ class TestCurbCurlEasy < Test::Unit::TestCase
134
208
  c = Curl::Easy.new($TEST_URL)
135
209
  assert_equal true, c.http_get
136
210
  assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
137
- assert_equal "", c.header_str
138
211
  end
139
212
 
140
213
  def test_get_02
@@ -146,7 +219,6 @@ class TestCurbCurlEasy < Test::Unit::TestCase
146
219
  assert_equal true, c.http_get
147
220
 
148
221
  assert_nil c.body_str
149
- assert_equal "", c.header_str
150
222
  assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)
151
223
  end
152
224
 
@@ -329,29 +401,69 @@ class TestCurbCurlEasy < Test::Unit::TestCase
329
401
  c.max_redirects = nil
330
402
  assert_nil c.max_redirects
331
403
  end
332
-
404
+
405
+ def test_timeout_with_floats
406
+ c = Curl::Easy.new($TEST_URL)
407
+
408
+ c.timeout = 1.5
409
+ assert_equal 1500, c.timeout_ms
410
+ assert_equal 1.5, c.timeout
411
+ end
412
+
413
+ def test_timeout_with_negative
414
+ c = Curl::Easy.new($TEST_URL)
415
+
416
+ c.timeout = -1.5
417
+ assert_equal 0, c.timeout
418
+ assert_equal 0, c.timeout_ms
419
+
420
+ c.timeout = -4.8
421
+ assert_equal 0, c.timeout
422
+ assert_equal 0, c.timeout_ms
423
+ end
424
+
333
425
  def test_timeout_01
334
426
  c = Curl::Easy.new($TEST_URL)
335
-
336
- assert_nil c.timeout
337
-
427
+
428
+ assert_equal 0, c.timeout
429
+
338
430
  c.timeout = 3
339
431
  assert_equal 3, c.timeout
340
-
341
- c.timeout = nil
342
- assert_nil c.timeout
432
+
433
+ c.timeout = 0
434
+ assert_equal 0, c.timeout
343
435
  end
344
436
 
345
437
  def test_timeout_ms_01
346
438
  c = Curl::Easy.new($TEST_URL)
347
439
 
348
- assert_nil c.timeout_ms
440
+ assert_equal 0, c.timeout_ms
349
441
 
350
442
  c.timeout_ms = 100
351
443
  assert_equal 100, c.timeout_ms
352
444
 
353
445
  c.timeout_ms = nil
354
- assert_nil c.timeout_ms
446
+ assert_equal 0, c.timeout_ms
447
+ end
448
+
449
+ def test_timeout_ms_with_floats
450
+ c = Curl::Easy.new($TEST_URL)
451
+
452
+ c.timeout_ms = 55.5
453
+ assert_equal 55, c.timeout_ms
454
+ assert_equal 0.055, c.timeout
455
+ end
456
+
457
+ def test_timeout_ms_with_negative
458
+ c = Curl::Easy.new($TEST_URL)
459
+
460
+ c.timeout_ms = -1.5
461
+ assert_equal 0, c.timeout
462
+ assert_equal 0, c.timeout_ms
463
+
464
+ c.timeout_ms = -4.8
465
+ assert_equal 0, c.timeout
466
+ assert_equal 0, c.timeout_ms
355
467
  end
356
468
 
357
469
  def test_connect_timeout_01
@@ -595,14 +707,23 @@ class TestCurbCurlEasy < Test::Unit::TestCase
595
707
  assert_equal "some.file", c.cookiejar
596
708
  end
597
709
 
710
+ def test_cookielist
711
+ c = Curl::Easy.new TestServlet.url
712
+ c.enable_cookies = true
713
+ c.post_body = URI.encode_www_form('c' => 'somename=somevalue')
714
+ assert_nil c.cookielist
715
+ c.perform
716
+ assert_match(/somevalue/, c.cookielist.join(''))
717
+ end
718
+
598
719
  def test_on_success
599
720
  curl = Curl::Easy.new($TEST_URL)
600
721
  on_success_called = false
601
722
  curl.on_success {|c|
602
723
  on_success_called = true
603
- assert_not_nil c.body_str
604
- assert_equal "", c.header_str
605
- assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
724
+ assert_not_nil c.body
725
+ assert_match(/Content-Length: /, c.head)
726
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body)
606
727
  }
607
728
  curl.perform
608
729
  assert on_success_called, "Success handler not called"
@@ -657,7 +778,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
657
778
  retries = 0
658
779
  begin
659
780
  count = 0
660
- curl = Curl::Easy.send("http_#{method}", TestServlet.url) do|c|
781
+ Curl::Easy.send("http_#{method}", TestServlet.url) do|c|
661
782
  count += 1
662
783
  assert_equal Curl::Easy, c.class
663
784
  end
@@ -679,8 +800,8 @@ class TestCurbCurlEasy < Test::Unit::TestCase
679
800
  Curl::PostField.file('bar', File.expand_path(File.join(File.dirname(__FILE__),'..','README.markdown')))
680
801
  ]
681
802
  curl.http_post(fields)
682
- assert_match /HTTP POST file upload/, curl.body_str
683
- assert_match /Content-Disposition: form-data/, curl.body_str
803
+ assert_match(/HTTP POST file upload/, curl.body_str)
804
+ assert_match(/Content-Disposition: form-data/, curl.body_str)
684
805
  end
685
806
 
686
807
  def test_post_with_body_remote
@@ -706,8 +827,8 @@ class TestCurbCurlEasy < Test::Unit::TestCase
706
827
  curl.multipart_form_post = true
707
828
  pf = Curl::PostField.file('readme', File.expand_path(File.join(File.dirname(__FILE__),'..','README.markdown')))
708
829
  curl.http_post(pf)
709
- assert_match /HTTP POST file upload/, curl.body_str
710
- assert_match /Content-Disposition: form-data/, curl.body_str
830
+ assert_match(/HTTP POST file upload/, curl.body_str)
831
+ assert_match(/Content-Disposition: form-data/, curl.body_str)
711
832
  end
712
833
 
713
834
  def test_delete_remote
@@ -729,7 +850,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
729
850
  redirect = curl.header_str.match(/Location: (.*)/)
730
851
 
731
852
  assert_equal '', curl.body_str
732
- assert_match '/nonexistent', redirect[1]
853
+ assert_match('/nonexistent', redirect[1])
733
854
  end
734
855
 
735
856
  def test_head_accessor
@@ -740,7 +861,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
740
861
  redirect = curl.header_str.match(/Location: (.*)/)
741
862
 
742
863
  assert_equal '', curl.body_str
743
- assert_match '/nonexistent', redirect[1]
864
+ assert_match('/nonexistent', redirect[1])
744
865
  curl.head = false
745
866
  curl.perform
746
867
  assert_equal 'GET', curl.body_str
@@ -750,11 +871,11 @@ class TestCurbCurlEasy < Test::Unit::TestCase
750
871
  curl = Curl::Easy.new(TestServlet.url)
751
872
  curl.headers['Content-Type'] = 'application/json'
752
873
  assert curl.http_put("message")
753
- assert_match /^PUT/, curl.body_str
754
- assert_match /message$/, curl.body_str
755
- assert_match /message$/, curl.body
756
- assert_match /application\/json/, curl.header_str
757
- assert_match /application\/json/, curl.head
874
+ assert_match(/^PUT/, curl.body_str)
875
+ assert_match(/message$/, curl.body_str)
876
+ assert_match(/message$/, curl.body)
877
+ assert_match(/application\/json/, curl.header_str)
878
+ assert_match(/application\/json/, curl.head)
758
879
  end
759
880
 
760
881
  def test_put_data
@@ -763,8 +884,8 @@ class TestCurbCurlEasy < Test::Unit::TestCase
763
884
 
764
885
  curl.perform
765
886
 
766
- assert_match /^PUT/, curl.body_str
767
- assert_match /message$/, curl.body_str
887
+ assert_match(/^PUT/, curl.body_str)
888
+ assert_match(/message$/, curl.body_str)
768
889
  end
769
890
 
770
891
  # https://github.com/taf2/curb/issues/101
@@ -774,8 +895,8 @@ class TestCurbCurlEasy < Test::Unit::TestCase
774
895
 
775
896
  curl.perform
776
897
 
777
- assert_match /^PUT/, curl.body_str
778
- assert_match "a\0b", curl.body_str
898
+ assert_match(/^PUT/, curl.body_str)
899
+ assert_match("a\0b", curl.body_str)
779
900
  end
780
901
 
781
902
  def test_put_nil_data_no_crash
@@ -790,7 +911,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
790
911
  File.open(__FILE__,'rb') do|f|
791
912
  assert curl.http_put(f)
792
913
  end
793
- assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str
914
+ assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str.tr("\r", '')
794
915
  end
795
916
 
796
917
  def test_put_class_method
@@ -800,7 +921,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
800
921
  assert_equal Curl::Easy, c.class
801
922
  end
802
923
  assert_equal 1, count
803
- assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str
924
+ assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str.tr("\r", '')
804
925
  end
805
926
 
806
927
  # Generate a self-signed cert with
@@ -809,7 +930,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
809
930
  def test_cert
810
931
  curl = Curl::Easy.new(TestServlet.url)
811
932
  curl.cert= File.join(File.dirname(__FILE__),"cert.pem")
812
- assert_match /cert.pem$/,curl.cert
933
+ assert_match(/cert.pem$/,curl.cert)
813
934
  end
814
935
 
815
936
  def test_cert_with_password
@@ -817,7 +938,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
817
938
  path = File.join(File.dirname(__FILE__),"cert.pem")
818
939
  curl.certpassword = 'password'
819
940
  curl.cert = path
820
- assert_match /cert.pem$/,curl.cert
941
+ assert_match(/cert.pem$/,curl.cert)
821
942
  end
822
943
 
823
944
  def test_cert_type
@@ -838,7 +959,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
838
959
  def test_ca_cert
839
960
  curl = Curl::Easy.new(TestServlet.url)
840
961
  curl.cacert= File.join(File.dirname(__FILE__),"cacert.pem")
841
- assert_match /cacert.pem$/, curl.cacert
962
+ assert_match(/cacert.pem$/, curl.cacert)
842
963
  end
843
964
 
844
965
  def test_user_agent
@@ -896,7 +1017,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
896
1017
  easy.http_post(pf)
897
1018
 
898
1019
  assert_not_equal(0,easy.body_str.size)
899
- assert_equal(easy.body_str,File.read(readme))
1020
+ assert_equal(easy.body_str.tr("\r", ''), File.read(readme))
900
1021
  end
901
1022
 
902
1023
 
@@ -992,8 +1113,8 @@ class TestCurbCurlEasy < Test::Unit::TestCase
992
1113
  curl.headers['Accept'] = '*/*'
993
1114
  curl.headers['Authorization'] = 'Foo Bar Biz Baz'
994
1115
  curl.http_put(rd)
995
- assert_match /^PUT/, curl.body_str
996
- assert_match /hello$/, curl.body_str
1116
+ assert_match(/^PUT/, curl.body_str)
1117
+ assert_match(/hello$/, curl.body_str)
997
1118
  curl.header_str
998
1119
  curl.body_str
999
1120
  end
@@ -1026,7 +1147,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
1026
1147
  c = Curl::Easy.new($TEST_URL)
1027
1148
  c.on_success {|x| raise "error" }
1028
1149
  c.perform
1029
- rescue => e
1150
+ rescue Curl::Err::AbortedByCallbackError => e
1030
1151
  assert_equal 'Curl::Err::AbortedByCallbackError', e.class.to_s
1031
1152
  c.close
1032
1153
  end
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestCurbCurlEasyResolve < Test::Unit::TestCase
4
+ def setup
5
+ @easy = Curl::Easy.new
6
+ end
7
+
8
+ def test_resolve
9
+ @easy.resolve = [ "example.com:80:127.0.0.1" ]
10
+ assert_equal @easy.resolve, [ "example.com:80:127.0.0.1" ]
11
+ end
12
+
13
+ def test_empty_resolve
14
+ assert_equal @easy.resolve, nil
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestCurbCurlMaxFileSize < Test::Unit::TestCase
4
+ def setup
5
+ @easy = Curl::Easy.new
6
+ end
7
+
8
+ def test_maxfilesize
9
+ @easy.set(Curl::CURLOPT_MAXFILESIZE, 5000000)
10
+ end
11
+
12
+ end