curb 0.1.4 → 0.7.15

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.
@@ -1,8 +1,11 @@
1
- require File.join(File.dirname(__FILE__), 'helper')
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+ class FooNoToS
3
+ undef to_s
4
+ end
2
5
 
3
6
  class TestCurbCurlEasy < Test::Unit::TestCase
4
7
  def test_class_perform_01
5
- assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL)
8
+ assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL)
6
9
  assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
7
10
  assert_equal "", c.header_str
8
11
  end
@@ -22,6 +25,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
22
25
 
23
26
  def test_new_01
24
27
  c = Curl::Easy.new
28
+ assert_equal Curl::Easy, c.class
25
29
  assert_nil c.url
26
30
  assert_nil c.body_str
27
31
  assert_nil c.header_str
@@ -54,8 +58,26 @@ class TestCurbCurlEasy < Test::Unit::TestCase
54
58
  assert_equal $TEST_URL, c.url
55
59
  assert_equal blk, c.on_body # sets handler nil, returns old handler
56
60
  assert_equal nil, c.on_body
57
- end
58
-
61
+ end
62
+
63
+ class Foo < Curl::Easy ; end
64
+ def test_new_05
65
+ # can use Curl::Easy as a base class
66
+ c = Foo.new
67
+ assert_equal Foo, c.class
68
+ c.url = $TEST_URL
69
+ c.perform
70
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
71
+ end
72
+
73
+ # test invalid use of new
74
+ def test_new_06
75
+ Curl::Easy.new(TestServlet.url) do|curl|
76
+ curl.http_post
77
+ assert_equal "POST\n", curl.body_str
78
+ end
79
+ end
80
+
59
81
  def test_escape
60
82
  c = Curl::Easy.new
61
83
 
@@ -86,7 +108,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
86
108
 
87
109
  def test_get_01
88
110
  c = Curl::Easy.new($TEST_URL)
89
- assert_equal true, c.http_get
111
+ assert_equal true, c.http_get
90
112
  assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
91
113
  assert_equal "", c.header_str
92
114
  end
@@ -110,7 +132,8 @@ class TestCurbCurlEasy < Test::Unit::TestCase
110
132
  assert_equal "", c.body_str
111
133
  assert_equal "", c.header_str
112
134
  end
113
-
135
+
136
+
114
137
  def test_last_effective_url_01
115
138
  c = Curl::Easy.new($TEST_URL)
116
139
 
@@ -124,6 +147,15 @@ class TestCurbCurlEasy < Test::Unit::TestCase
124
147
 
125
148
  assert_not_equal c.last_effective_url, c.url
126
149
  end
150
+
151
+ def test_http_get_block
152
+ curl = Curl::Easy.http_get(TestServlet.url) do|c|
153
+ c.follow_location = true
154
+ c.max_redirects = 3
155
+ end
156
+ assert_equal curl.url, curl.last_effective_url
157
+ assert_equal 'GET', curl.body_str
158
+ end
127
159
 
128
160
  def test_local_port_01
129
161
  c = Curl::Easy.new($TEST_URL)
@@ -192,7 +224,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
192
224
 
193
225
  assert_equal $TEST_URL, c.url
194
226
  assert_equal "http://some.proxy", c.proxy_url
195
-
227
+
196
228
  c.proxy_url = nil
197
229
  assert_equal $TEST_URL, c.url
198
230
  assert_nil c.proxy_url
@@ -322,6 +354,30 @@ class TestCurbCurlEasy < Test::Unit::TestCase
322
354
  assert_equal 30, c.dns_cache_timeout
323
355
  end
324
356
 
357
+ def test_low_speed_limit_01
358
+ c = Curl::Easy.new($TEST_URL)
359
+
360
+ assert_nil c.low_speed_limit
361
+
362
+ c.low_speed_limit = 3
363
+ assert_equal 3, c.low_speed_limit
364
+
365
+ c.low_speed_limit = nil
366
+ assert_nil c.low_speed_limit
367
+ end
368
+
369
+ def test_low_speed_time_01
370
+ c = Curl::Easy.new($TEST_URL)
371
+
372
+ assert_nil c.low_speed_time
373
+
374
+ c.low_speed_time = 3
375
+ assert_equal 3, c.low_speed_time
376
+
377
+ c.low_speed_time = nil
378
+ assert_nil c.low_speed_time
379
+ end
380
+
325
381
  def test_on_body
326
382
  blk = lambda { |i| i.length }
327
383
 
@@ -331,6 +387,25 @@ class TestCurbCurlEasy < Test::Unit::TestCase
331
387
  assert_equal blk, c.on_body # sets handler nil, returns old handler
332
388
  assert_equal nil, c.on_body
333
389
  end
390
+
391
+ def test_inspect_with_no_url
392
+ c = Curl::Easy.new
393
+ assert_equal '#<Curl::Easy>', c.inspect
394
+ end
395
+
396
+ def test_inspect_with_short_url
397
+ c = Curl::Easy.new('http://www.google.com/')
398
+ assert_equal "#<Curl::Easy http://www.google.com/>", c.inspect
399
+ end
400
+
401
+ def test_inspect_truncates_to_64_chars
402
+ base_url = 'http://www.google.com/'
403
+ truncated_url = base_url + 'x' * (64 - '#<Curl::Easy >'.size - base_url.size)
404
+ long_url = truncated_url + 'yyyy'
405
+ c = Curl::Easy.new(long_url)
406
+ assert_equal 64, c.inspect.size
407
+ assert_equal "#<Curl::Easy #{truncated_url}>", c.inspect
408
+ end
334
409
 
335
410
  def test_on_header
336
411
  blk = lambda { |i| i.length }
@@ -425,18 +500,398 @@ class TestCurbCurlEasy < Test::Unit::TestCase
425
500
  assert c.multipart_form_post?
426
501
  end
427
502
 
503
+ def test_ignore_content_length
504
+ c = Curl::Easy.new
505
+ assert !c.ignore_content_length?
506
+ assert c.ignore_content_length = true
507
+ assert c.ignore_content_length?
508
+ end
509
+
428
510
  def test_enable_cookies
429
511
  c = Curl::Easy.new
430
512
  assert !c.enable_cookies?
431
513
  assert c.enable_cookies = true
432
514
  assert c.enable_cookies?
433
515
  end
434
-
516
+
517
+ def test_cookies_option
518
+ c = Curl::Easy.new
519
+ assert_nil c.cookies
520
+ assert_equal "name1=content1; name2=content2;", c.cookies = "name1=content1; name2=content2;"
521
+ assert_equal "name1=content1; name2=content2;", c.cookies
522
+ end
523
+
524
+ def test_cookiefile
525
+ c = Curl::Easy.new
526
+ assert_nil c.cookiefile
527
+ assert_equal "some.file", c.cookiefile = "some.file"
528
+ assert_equal "some.file", c.cookiefile
529
+ end
530
+
435
531
  def test_cookiejar
436
532
  c = Curl::Easy.new
437
533
  assert_nil c.cookiejar
438
534
  assert_equal "some.file", c.cookiejar = "some.file"
439
535
  assert_equal "some.file", c.cookiejar
440
536
  end
537
+
538
+ def test_on_success
539
+ curl = Curl::Easy.new($TEST_URL)
540
+ on_success_called = false
541
+ curl.on_success {|c|
542
+ on_success_called = true
543
+ assert_not_nil c.body_str
544
+ assert_equal "", c.header_str
545
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
546
+ }
547
+ curl.perform
548
+ assert on_success_called, "Success handler not called"
549
+ end
550
+
551
+ def test_on_success_with_on_failure
552
+ curl = Curl::Easy.new("#{$TEST_URL.gsub(/file:\/\//,'')}/not_here")
553
+ on_failure_called = false
554
+ curl.on_success {|c| } # make sure we get the failure call even though this handler is defined
555
+ curl.on_failure {|c,code| on_failure_called = true }
556
+ curl.perform
557
+ assert on_failure_called, "Failure handler not called"
558
+ end
559
+
560
+ def test_get_remote
561
+ curl = Curl::Easy.new(TestServlet.url)
562
+ curl.http_get
563
+ assert_equal 'GET', curl.body_str
564
+ end
565
+
566
+ def test_post_remote
567
+ curl = Curl::Easy.new(TestServlet.url)
568
+ curl.http_post([Curl::PostField.content('document_id', 5)])
569
+ assert_equal "POST\ndocument_id=5", curl.unescape(curl.body_str)
570
+ end
571
+
572
+ def test_post_remote_is_easy_handle
573
+ # see: http://pastie.org/560852 and
574
+ # http://groups.google.com/group/curb---ruby-libcurl-bindings/browse_thread/thread/216bb2d9b037f347?hl=en
575
+ [:post, :get,:head,:delete].each do |method|
576
+ count = 0
577
+ curl = Curl::Easy.send("http_#{method}", TestServlet.url) do|c|
578
+ count += 1
579
+ assert_equal Curl::Easy, c.class
580
+ end
581
+ assert_equal 1, count, "For request method: #{method.to_s.upcase}"
582
+ end
583
+ end
584
+
585
+ def test_post_with_body_remote
586
+ curl = Curl::Easy.new(TestServlet.url)
587
+ curl.post_body = 'foo=bar&encoded%20string=val'
588
+
589
+ curl.perform
590
+
591
+ assert_equal "POST\nfoo=bar&encoded%20string=val", curl.body_str
592
+ assert_equal 'foo=bar&encoded%20string=val', curl.post_body
593
+ end
594
+
595
+ def test_form_post_body_remote
596
+ curl = Curl::Easy.new(TestServlet.url)
597
+ curl.http_post('foo=bar', 'encoded%20string=val')
598
+
599
+ assert_equal "POST\nfoo=bar&encoded%20string=val", curl.body_str
600
+ assert_equal 'foo=bar&encoded%20string=val', curl.post_body
601
+ end
602
+
603
+ def test_post_multipart_file_remote
604
+ curl = Curl::Easy.new(TestServlet.url)
605
+ curl.multipart_form_post = true
606
+ pf = Curl::PostField.file('readme', File.expand_path(File.join(File.dirname(__FILE__),'..','README')))
607
+ curl.http_post(pf)
608
+ assert_match /HTTP POST file upload/, curl.body_str
609
+ assert_match /Content-Disposition: form-data/, curl.body_str
610
+ end
611
+
612
+ def test_delete_remote
613
+ curl = Curl::Easy.new(TestServlet.url)
614
+ curl.http_delete
615
+ assert_equal 'DELETE', curl.body_str
616
+ end
617
+
618
+ def test_arbitrary_http_verb
619
+ curl = Curl::Easy.new(TestServlet.url)
620
+ curl.http('PURGE')
621
+ assert_equal 'PURGE', curl.body_str
622
+ end
623
+
624
+ def test_head_remote
625
+ curl = Curl::Easy.new(TestServlet.url)
626
+ curl.http_head
627
+
628
+ redirect = curl.header_str.match(/Location: (.*)/)
629
+
630
+ assert_equal '', curl.body_str
631
+ assert_match '/nonexistent', redirect[1]
632
+ end
633
+
634
+ def test_head_accessor
635
+ curl = Curl::Easy.new(TestServlet.url)
636
+ curl.head = true
637
+ curl.perform
638
+
639
+ redirect = curl.header_str.match(/Location: (.*)/)
640
+
641
+ assert_equal '', curl.body_str
642
+ assert_match '/nonexistent', redirect[1]
643
+ curl.head = false
644
+ curl.perform
645
+ assert_equal 'GET', curl.body_str
646
+ end
647
+
648
+ def test_put_remote
649
+ curl = Curl::Easy.new(TestServlet.url)
650
+ curl.headers['Content-Type'] = 'application/json'
651
+ assert curl.http_put("message")
652
+ assert_match /^PUT/, curl.body_str
653
+ assert_match /message$/, curl.body_str
654
+ assert_match /application\/json/, curl.header_str
655
+ end
441
656
 
442
- end
657
+ def test_put_data
658
+ curl = Curl::Easy.new(TestServlet.url)
659
+ curl.put_data = 'message'
660
+
661
+ curl.perform
662
+
663
+ assert_match /^PUT/, curl.body_str
664
+ assert_match /message$/, curl.body_str
665
+ end
666
+
667
+ def test_put_nil_data_no_crash
668
+ curl = Curl::Easy.new(TestServlet.url)
669
+ curl.put_data = nil
670
+
671
+ curl.perform
672
+ end
673
+
674
+ def test_put_remote_file
675
+ curl = Curl::Easy.new(TestServlet.url)
676
+ File.open(__FILE__,'r') do|f|
677
+ assert curl.http_put(f)
678
+ end
679
+ assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str
680
+ end
681
+
682
+ def test_put_class_method
683
+ count = 0
684
+ curl = Curl::Easy.http_put(TestServlet.url,File.open(__FILE__,'rb')) do|c|
685
+ count += 1
686
+ assert_equal Curl::Easy, c.class
687
+ end
688
+ assert_equal 1, count
689
+ assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str
690
+ end
691
+
692
+ # Generate a self-signed cert with
693
+ # openssl req -new -newkey rsa:1024 -days 365 -nodes -x509 \
694
+ # -keyout tests/cert.pem -out tests/cert.pem
695
+ def test_cert
696
+ curl = Curl::Easy.new(TestServlet.url)
697
+ curl.cert= File.join(File.dirname(__FILE__),"cert.pem")
698
+ assert_match /cert.pem$/,curl.cert
699
+ end
700
+
701
+ def test_cert_with_password
702
+ curl = Curl::Easy.new(TestServlet.url)
703
+ curl.cert= File.join(File.dirname(__FILE__),"cert.pem:password")
704
+ assert_match /cert.pem$/,curl.cert
705
+ end
706
+
707
+ def test_cert_type
708
+ curl = Curl::Easy.new(TestServlet.url)
709
+ curl.certtype= "DER"
710
+ assert_equal "DER", curl.certtype
711
+ end
712
+
713
+ def test_default_certtype
714
+ curl = Curl::Easy.new(TestServlet.url)
715
+ assert_nil curl.certtype
716
+ curl.certtype = "PEM"
717
+ assert_equal "PEM", curl.certtype
718
+ end
719
+
720
+ # Generate a CA cert with instructions at
721
+ # http://technocage.com/~caskey/openssl/
722
+ def test_ca_cert
723
+ curl = Curl::Easy.new(TestServlet.url)
724
+ curl.cacert= File.join(File.dirname(__FILE__),"cacert.pem")
725
+ assert_match /cacert.pem$/, curl.cacert
726
+ end
727
+
728
+ def test_user_agent
729
+ curl = Curl::Easy.new(TestServlet.url)
730
+ curl.useragent= "Curb-Easy/Ruby"
731
+ assert_equal "Curb-Easy/Ruby",curl.useragent
732
+ end
733
+
734
+ def test_username_password
735
+ curl = Curl::Easy.new(TestServlet.url)
736
+ curl.username = "foo"
737
+ curl.password = "bar"
738
+ if !curl.username.nil?
739
+ assert_equal "foo", curl.username
740
+ assert_equal "bar", curl.password
741
+ else
742
+ curl.userpwd = "foo:bar"
743
+ end
744
+ curl.http_auth_types = :basic
745
+ #curl.verbose = true
746
+ curl.perform
747
+ assert_equal 'Basic Zm9vOmJhcg==', $auth_header
748
+ $auth_header = nil
749
+ # curl checks the auth type supported by the server, so we have to create a
750
+ # new easy handle if we're going to change the auth type...
751
+
752
+ curl = Curl::Easy.new(TestServlet.url)
753
+ curl.username = "foo"
754
+ curl.password = "bar"
755
+ if curl.username.nil?
756
+ curl.userpwd = "foo:bar"
757
+ end
758
+ curl.http_auth_types = :ntlm
759
+ curl.perform
760
+ assert_equal 'NTLM TlRMTVNTUAABAAAABoIIAAAAAAAAAAAAAAAAAAAAAAA=', $auth_header
761
+ end
762
+
763
+ def test_primary_ip
764
+ curl = Curl::Easy.new(TestServlet.url)
765
+ if curl.respond_to?(:primary_ip)
766
+ curl.perform
767
+ assert_equal '127.0.0.1', curl.primary_ip
768
+ end
769
+ end
770
+
771
+ def test_post_streaming
772
+ readme = File.expand_path(File.join(File.dirname(__FILE__),'..','README'))
773
+
774
+ pf = Curl::PostField.file("filename", readme)
775
+
776
+ easy = Curl::Easy.new
777
+
778
+ easy.url = TestServlet.url
779
+ easy.multipart_form_post = true
780
+ easy.http_post(pf)
781
+
782
+ assert_not_equal(0,easy.body_str.size)
783
+ assert_equal(easy.body_str,File.read(readme))
784
+ end
785
+
786
+
787
+ def test_easy_close
788
+ easy = Curl::Easy.new
789
+ easy.close
790
+ easy.url = TestServlet.url
791
+ easy.http_get
792
+ end
793
+
794
+ def test_easy_reset
795
+ easy = Curl::Easy.new
796
+ easy.url = TestServlet.url + "?query=foo"
797
+ easy.http_get
798
+ settings = easy.reset
799
+ assert settings.key?(:url)
800
+ assert settings.key?(:body_data)
801
+ assert settings.key?(:header_data)
802
+ easy.url = TestServlet.url
803
+ easy.http_get
804
+ end
805
+
806
+ def test_easy_use_http_versions
807
+ easy = Curl::Easy.new
808
+ easy.url = TestServlet.url + "?query=foo"
809
+ #puts "http none: #{Curl::HTTP_NONE.inspect}"
810
+ #puts "http1.0: #{Curl::HTTP_1_0.inspect}"
811
+ #puts "http1.1: #{Curl::HTTP_1_1.inspect}"
812
+ easy.version = Curl::HTTP_1_1
813
+ #easy.verbose = true
814
+ easy.http_get
815
+ end
816
+
817
+ def test_easy_http_verbs
818
+ curl = Curl::Easy.new(TestServlet.url)
819
+ curl.http_delete
820
+ assert_equal 'DELETE', curl.body_str
821
+ curl.http_get
822
+ assert_equal 'GET', curl.body_str
823
+ curl.http_post
824
+ assert_equal "POST\n", curl.body_str
825
+ curl.http('PURGE')
826
+ assert_equal 'PURGE', curl.body_str
827
+ curl.http_put('hello')
828
+ assert_equal "PUT\nhello", curl.body_str
829
+ curl.http('COPY')
830
+ assert_equal 'COPY', curl.body_str
831
+ end
832
+
833
+ def test_easy_http_verbs_must_respond_to_str
834
+ # issue http://github.com/taf2/curb/issues#issue/45
835
+ assert_nothing_raised do
836
+ c = Curl::Easy.new ; c.url = 'http://example.com' ; c.http(:get)
837
+ end
838
+
839
+ assert_raise RuntimeError do
840
+ c = Curl::Easy.new ; c.url = 'http://example.com' ; c.http(FooNoToS.new)
841
+ end
842
+
843
+ end
844
+
845
+ # http://github.com/taf2/curb/issues/#issue/33
846
+ def test_easy_http_verbs_with_errors
847
+ curl = Curl::Easy.new("http://127.0.0.1:9012/") # test will fail if http server on port 9012
848
+ assert_raise Curl::Err::ConnectionFailedError do
849
+ curl.http_delete
850
+ end
851
+ curl.url = TestServlet.url
852
+ curl.http_get
853
+ assert_equal 'GET', curl.body_str
854
+ end
855
+
856
+ def test_easy_can_put_with_content_length
857
+ curl = Curl::Easy.new(TestServlet.url)
858
+ rd, wr = IO.pipe
859
+ buf = (("hello")* (1000 / 5))
860
+
861
+ producer = Thread.new do
862
+ 5.times do
863
+ wr << buf
864
+ sleep 0.1 # act as a slow producer
865
+ end
866
+ end
867
+
868
+ consumer = Thread.new do
869
+
870
+ #curl.verbose = true
871
+ curl.headers['Content-Length'] = buf.size * 5
872
+ curl.headers['User-Agent'] = "Something else"
873
+ curl.headers['Content-Type'] = "text/javascript"
874
+ curl.headers['Date'] = Time.now.httpdate
875
+ curl.headers['Host'] = 's3.amazonaws.com'
876
+ curl.headers['Accept'] = '*/*'
877
+ curl.headers['Authorization'] = 'Foo Bar Biz Baz'
878
+ curl.http_put(rd)
879
+ assert_match /^PUT/, curl.body_str
880
+ assert_match /hello$/, curl.body_str
881
+ curl.header_str
882
+ curl.body_str
883
+ end
884
+
885
+ producer.join
886
+ wr.close
887
+ consumer.join
888
+
889
+ end
890
+
891
+ include TestServerMethods
892
+
893
+ def setup
894
+ server_setup
895
+ end
896
+
897
+ end