File: tc_curl_easy.rb

package info (click to toggle)
ruby-curb 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 860 kB
  • sloc: ansic: 5,798; ruby: 4,466; makefile: 4
file content (1309 lines) | stat: -rw-r--r-- 34,435 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
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_nested_easy_methods
    easy = Curl.get(TestServlet.url) {|http|
      res = Curl.get(TestServlet.url + '/not_here')
      assert_equal 404, res.code
    }
    assert_equal 200, easy.code
  end

  def test_curlopt_resolve
    require 'resolv'
    uri = URI.parse(TestServlet.url)
    resolved_ip = Resolv.getaddress(uri.host)   # perform DNS lookup once
    mapping = "#{uri.host}:#{uri.port}:#{resolved_ip}"

    http = Curl::Easy.new(TestServlet.url)
    http.setopt(Curl::CURLOPT_RESOLVE, [mapping])

    http.get

    assert_match(/GET/, http.body)
  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
    tmp = Tempfile.new('curb_test_curlopt_stderr')
    path = tmp.path
    fd = IO.sysopen(path, 'w')
    io = IO.new(fd, 'w')
    io.sync = true

    easy = Curl::Easy.new(TestServlet.url)
    easy.verbose = true
    easy.setopt(Curl::CURLOPT_STDERR, io)
    easy.perform

    io.flush
    io.close
    output = File.read(path)

    assert_match(/HTTP\/1\.1\ 200\ OK(?:\ )?/, output)
    assert_match('Host: 127.0.0.1:9129', output)
  ensure
    tmp.close! if defined?(tmp) && tmp
  end

  def test_curlopt_stderr_gc_safe
    tmp = Tempfile.new('curb_test_curlopt_stderr_gc')
    path = tmp.path
    fd = IO.sysopen(path, 'w')
    io = IO.new(fd, 'w')

    easy = Curl::Easy.new(TestServlet.url)
    easy.verbose = true
    easy.setopt(Curl::CURLOPT_STDERR, io)

    # Drop our Ruby reference and force GC; curb should retain it internally
    io = nil
    GC.start

    easy.perform

    output = File.read(path)
    assert_match(/HTTP\/1\.1\ 200\ OK(?:\ )?/, output)
    assert_match('Host: 127.0.0.1:9129', output)
  ensure
    tmp.close! if defined?(tmp) && tmp
  end

  def test_head_request_no_body_and_no_timeout
    # Ensure a HEAD request completes and does not attempt to read a body
    # even when the server advertises a Content-Length.
    easy = nil
    assert_nothing_raised do
      easy = Curl.http(:HEAD, TestServlet.url)
    end
    assert_not_nil easy
    # Header string should contain the HTTP status line.
    assert_match(/HTTP\/1\.1\s\d+\s/, easy.header_str.to_s)
    # Body should be empty for HEAD requests (libcurl won't call the write callback).
    assert_equal "", easy.body_str.to_s
  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 = String.new
    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 = String.new
    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
    omit('Windows file URL semantics differ') if WINDOWS
    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
      assert_match(/Content-Length: /, c.head)
      assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body)
    }
    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_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)

    curl.http_put([Curl::PostField.content('document_id', 5)])
    assert_equal "PUT\ndocument_id=5", curl.unescape(curl.body_str)

    curl.http_patch([Curl::PostField.content('document_id', 5)])
    assert_equal "PATCH\ndocument_id=5", curl.unescape(curl.body_str)
  end

  def test_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, :patch, :put, :get, :head, :delete].each do |method|
      retries = 0
      begin
        count = 0
        m = "http_#{method}".to_sym
        Curl::Easy.send(m, 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
      rescue ArgumentError => e
        assert false, "#{m} - #{e.message}"
      end
    end
  end

  # see: https://github.com/rvanlieshout/curb/commit/8bcdefddc0162484681ebd1a92d52a642666a445
  def test_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.md'))),
      Curl::PostField.file('bar', File.expand_path(File.join(File.dirname(__FILE__),'..','README.md')))
    ]
    curl.http_post(fields)
    assert_match(/HTTP POST file upload/, curl.body_str)
    assert_match(/Content-Disposition: form-data/, curl.body_str)

    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.md'))),
      Curl::PostField.file('bar', File.expand_path(File.join(File.dirname(__FILE__),'..','README.md')))
    ]
    curl.http_put(fields)
    assert_match(/HTTP POST file upload/, curl.body_str)
    assert_match(/Content-Disposition: form-data/, curl.body_str)

    curl.http_patch(fields)
    assert_match(/HTTP POST file upload/, curl.body_str)
    assert_match(/Content-Disposition: form-data/, curl.body_str)
  end
  
  def test_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

    curl = Curl::Easy.new(TestServlet.url)
    curl.put_data = 'foo=bar&encoded%20string=val'
    
    curl.perform
    
    assert_equal "PUT\nfoo=bar&encoded%20string=val", curl.body_str
  end
  
  def test_form_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

    curl = Curl::Easy.new(TestServlet.url)
    curl.http_put('foo=bar', 'encoded%20string=val')
    
    assert_equal "PUT\nfoo=bar&encoded%20string=val", curl.body_str

    curl = Curl::Easy.new(TestServlet.url)
    curl.http_patch('foo=bar', 'encoded%20string=val')
    
    assert_equal "PATCH\nfoo=bar&encoded%20string=val", curl.body_str
  end

  def test_multipart_file_remote
    [:put, :post, :patch].each {|method|
      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.md')))
      curl.send("http_#{method}", 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...
    if WINDOWS
      # On Windows, libcurl often uses SSPI for NTLM which yields a different
      # header value and encoding; skip the NTLM-specific assertion.
      return
    end
    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
    readme = File.expand_path(File.join(File.dirname(__FILE__),'..','README.md'))
    
    pf = Curl::PostField.file("filename", readme)

    easy = Curl::Easy.new

    easy.url = TestServlet.url
    easy.multipart_form_post = true
    easy.http_post(pf)

    assert_not_equal(0,easy.body.size)
    assert_equal(Digest::MD5.hexdigest(easy.body), Digest::MD5.hexdigest(File.binread(readme)))
  end

  def test_easy_close
    easy = Curl::Easy.new
    easy.close
    easy.url = TestServlet.url
    easy.http_get
  end

  def test_easy_reset
    easy = Curl::Easy.new
    easy.url = TestServlet.url + "?query=foo"
    easy.http_get
    settings = easy.reset
    assert settings.key?(:url)
    assert settings.key?(:body_data)
    assert settings.key?(:header_data)
    easy.url = TestServlet.url
    easy.http_get
  end

  def test_last_result_initialization
    # Test for issue #463 - ensure last_result is properly initialized to 0
    easy = Curl::Easy.new
    assert_equal 0, easy.last_result, "last_result should be initialized to 0 for new instance"
    
    # Test that reset also sets last_result to 0
    easy.url = TestServlet.url
    easy.http_get
    easy.reset
    assert_equal 0, easy.last_result, "last_result should be reset to 0 after calling reset"
  end

  def test_easy_use_http_versions
    easy = Curl::Easy.new
    easy.url = TestServlet.url + "?query=foo"
    #puts "http none: #{Curl::HTTP_NONE.inspect}"
    #puts "http1.0: #{Curl::HTTP_1_0.inspect}"
    #puts "http1.1: #{Curl::HTTP_1_1.inspect}"
    easy.version = Curl::HTTP_1_1
    #easy.verbose = true
    easy.http_get
  end

  def test_easy_http_verbs
    curl = Curl::Easy.new(TestServlet.url)
    curl.http_delete
    assert_equal 'DELETE', curl.body_str
    curl.http_get
    assert_equal 'GET', curl.body_str
    curl.http_post
    assert_equal "POST\n", curl.body_str
    curl.http('PURGE')
    assert_equal 'PURGE', curl.body_str
    curl.http_put('hello')
    assert_equal "PUT\nhello", curl.body_str
    curl.http('COPY')
    assert_equal 'COPY', curl.body_str

    curl.http_patch
    assert_equal "PATCH\n", curl.body_str

    curl.http_put
    assert_equal "PUT\n", curl.body_str
  end

  def test_easy_http_verbs_must_respond_to_str
    # issue http://github.com/taf2/curb/issues/45
    assert_nothing_raised do
      c = Curl::Easy.new ; c.url = TestServlet.url ; c.http(:get)
    end

    assert_raise RuntimeError do
      c = Curl::Easy.new ; c.url = TestServlet.url ; c.http(FooNoToS.new)
    end

  end

  # http://github.com/taf2/curb/issues/#issue/33
  def test_easy_http_verbs_with_errors
    curl = Curl::Easy.new("http://127.0.0.1:9012/") # test will fail if http server on port 9012
    assert_raise Curl::Err::ConnectionFailedError do
      curl.http_delete
    end
    curl.url = TestServlet.url
    curl.http_get
    assert_equal 'GET', curl.body_str
  end

  def test_easy_can_put_with_content_length
    curl = Curl::Easy.new(TestServlet.url)
    rd, wr = IO.pipe
    buf = (("hello")* (1000 / 5))

    producer = Thread.new do
      5.times do
        wr << buf
        sleep 0.1 # act as a slow producer
      end
    end

    consumer = Thread.new do

      #curl.verbose = true
      curl.headers['Content-Length'] = buf.size * 5
      curl.headers['User-Agent']     = "Something else"
      curl.headers['Content-Type']   = "text/javascript"
      curl.headers['Date']           = Time.now.httpdate
      curl.headers['Host']           = 's3.amazonaws.com'
      curl.headers['Accept']         = '*/*'
      curl.headers['Authorization']  = 'Foo Bar Biz Baz'
      curl.http_put(rd)
      assert_match(/^PUT/, curl.body_str)
      assert_match(/hello$/, curl.body_str)
      curl.header_str
      curl.body_str
    end

    producer.join
    wr.close
    consumer.join

  end

  def test_get_set_multi_on_easy
    easy = Curl::Easy.new
    assert_nil easy.multi
    multi = Curl::Multi.new
    easy.multi = multi
    assert_not_nil easy.multi
    assert_equal multi, easy.multi
  end

  def test_raise_on_progress
    c = Curl::Easy.new($TEST_URL)
    c.on_progress {|w,x,y,z| raise "error" }
    c.perform
  rescue => e
    assert_equal 'Curl::Err::AbortedByCallbackError', e.class.to_s
    c.close
  end

  def test_raise_on_success
    c = Curl::Easy.new($TEST_URL)
    c.on_success {|x| raise "error" }
    c.perform
  rescue Curl::Err::AbortedByCallbackError => e
    assert_equal 'Curl::Err::AbortedByCallbackError', e.class.to_s
    c.close
  end

  def test_raise_on_debug
    c = Curl::Easy.new($TEST_URL)
    c.on_debug { raise "error" }
    c.perform
    assert true, "raise in on debug has no effect"
  end

  def test_status_codes
    curl = Curl::Easy.new(TestServlet.url)
    curl.perform
    assert_equal '200 OK', curl.status
  end

  def test_close_in_on_callbacks
    curl = Curl::Easy.new(TestServlet.url)
    curl.on_body {|d| curl.close; d.size }
    assert_raises RuntimeError do
      curl.perform
    end
  end

  def test_set_unsupported_options
    curl = Curl::Easy.new
    assert_raises TypeError do
      curl.set(99999, 1)
    end
  end

  include TestServerMethods 

  def setup
    server_setup
  end

end