Project

General

Profile

Feature #6546 ยป net.http.proxy_from_env.3.patch

Patch using proxy detection from open-uri - drbrain (Eric Hodel), 06/28/2012 09:42 AM

View differences:

lib/open-uri.rb (working copy)
692 692
end
693 693

  
694 694
module URI
695
  class Generic
696
    # returns a proxy URI.
697
    # The proxy URI is obtained from environment variables such as http_proxy,
698
    # ftp_proxy, no_proxy, etc.
699
    # If there is no proper proxy, nil is returned.
700
    #
701
    # Note that capitalized variables (HTTP_PROXY, FTP_PROXY, NO_PROXY, etc.)
702
    # are examined too.
703
    #
704
    # But http_proxy and HTTP_PROXY is treated specially under CGI environment.
705
    # It's because HTTP_PROXY may be set by Proxy: header.
706
    # So HTTP_PROXY is not used.
707
    # http_proxy is not used too if the variable is case insensitive.
708
    # CGI_HTTP_PROXY can be used instead.
709
    def find_proxy
710
      name = self.scheme.downcase + '_proxy'
711
      proxy_uri = nil
712
      if name == 'http_proxy' && ENV.include?('REQUEST_METHOD') # CGI?
713
        # HTTP_PROXY conflicts with *_proxy for proxy settings and
714
        # HTTP_* for header information in CGI.
715
        # So it should be careful to use it.
716
        pairs = ENV.reject {|k, v| /\Ahttp_proxy\z/i !~ k }
717
        case pairs.length
718
        when 0 # no proxy setting anyway.
719
          proxy_uri = nil
720
        when 1
721
          k, _ = pairs.shift
722
          if k == 'http_proxy' && ENV[k.upcase] == nil
723
            # http_proxy is safe to use because ENV is case sensitive.
724
            proxy_uri = ENV[name]
725
          else
726
            proxy_uri = nil
727
          end
728
        else # http_proxy is safe to use because ENV is case sensitive.
729
          proxy_uri = ENV.to_hash[name]
730
        end
731
        if !proxy_uri
732
          # Use CGI_HTTP_PROXY.  cf. libwww-perl.
733
          proxy_uri = ENV["CGI_#{name.upcase}"]
734
        end
735
      elsif name == 'http_proxy'
736
        unless proxy_uri = ENV[name]
737
          if proxy_uri = ENV[name.upcase]
738
            warn 'The environment variable HTTP_PROXY is discouraged.  Use http_proxy.'
739
          end
740
        end
741
      else
742
        proxy_uri = ENV[name] || ENV[name.upcase]
743
      end
744

  
745
      if proxy_uri && self.hostname
746
        require 'socket'
747
        begin
748
          addr = IPSocket.getaddress(self.hostname)
749
          proxy_uri = nil if /\A127\.|\A::1\z/ =~ addr
750
        rescue SocketError
751
        end
752
      end
753

  
754
      if proxy_uri
755
        proxy_uri = URI.parse(proxy_uri)
756
        name = 'no_proxy'
757
        if no_proxy = ENV[name] || ENV[name.upcase]
758
          no_proxy.scan(/([^:,]*)(?::(\d+))?/) {|host, port|
759
            if /(\A|\.)#{Regexp.quote host}\z/i =~ self.host &&
760
               (!port || self.port == port.to_i)
761
              proxy_uri = nil
762
              break
763
            end
764
          }
765
        end
766
        proxy_uri
767
      else
768
        nil
769
      end
770
    end
771
  end
772

  
773 695
  class HTTP
774 696
    def buffer_open(buf, proxy, options) # :nodoc:
775 697
      OpenURI.open_http(buf, self, proxy, options)
lib/uri/generic.rb (working copy)
1596 1596

  
1597 1597
      return oth, self
1598 1598
    end
1599

  
1600
    # returns a proxy URI.
1601
    # The proxy URI is obtained from environment variables such as http_proxy,
1602
    # ftp_proxy, no_proxy, etc.
1603
    # If there is no proper proxy, nil is returned.
1604
    #
1605
    # Note that capitalized variables (HTTP_PROXY, FTP_PROXY, NO_PROXY, etc.)
1606
    # are examined too.
1607
    #
1608
    # But http_proxy and HTTP_PROXY is treated specially under CGI environment.
1609
    # It's because HTTP_PROXY may be set by Proxy: header.
1610
    # So HTTP_PROXY is not used.
1611
    # http_proxy is not used too if the variable is case insensitive.
1612
    # CGI_HTTP_PROXY can be used instead.
1613
    def find_proxy
1614
      name = self.scheme.downcase + '_proxy'
1615
      proxy_uri = nil
1616
      if name == 'http_proxy' && ENV.include?('REQUEST_METHOD') # CGI?
1617
        # HTTP_PROXY conflicts with *_proxy for proxy settings and
1618
        # HTTP_* for header information in CGI.
1619
        # So it should be careful to use it.
1620
        pairs = ENV.reject {|k, v| /\Ahttp_proxy\z/i !~ k }
1621
        case pairs.length
1622
        when 0 # no proxy setting anyway.
1623
          proxy_uri = nil
1624
        when 1
1625
          k, _ = pairs.shift
1626
          if k == 'http_proxy' && ENV[k.upcase] == nil
1627
            # http_proxy is safe to use because ENV is case sensitive.
1628
            proxy_uri = ENV[name]
1629
          else
1630
            proxy_uri = nil
1631
          end
1632
        else # http_proxy is safe to use because ENV is case sensitive.
1633
          proxy_uri = ENV.to_hash[name]
1634
        end
1635
        if !proxy_uri
1636
          # Use CGI_HTTP_PROXY.  cf. libwww-perl.
1637
          proxy_uri = ENV["CGI_#{name.upcase}"]
1638
        end
1639
      elsif name == 'http_proxy'
1640
        unless proxy_uri = ENV[name]
1641
          if proxy_uri = ENV[name.upcase]
1642
            warn 'The environment variable HTTP_PROXY is discouraged.  Use http_proxy.'
1643
          end
1644
        end
1645
      else
1646
        proxy_uri = ENV[name] || ENV[name.upcase]
1647
      end
1648

  
1649
      if proxy_uri && self.hostname
1650
        require 'socket'
1651
        begin
1652
          addr = IPSocket.getaddress(self.hostname)
1653
          proxy_uri = nil if /\A127\.|\A::1\z/ =~ addr
1654
        rescue SocketError
1655
        end
1656
      end
1657

  
1658
      if proxy_uri
1659
        proxy_uri = URI.parse(proxy_uri)
1660
        name = 'no_proxy'
1661
        if no_proxy = ENV[name] || ENV[name.upcase]
1662
          no_proxy.scan(/([^:,]*)(?::(\d+))?/) {|host, port|
1663
            if /(\A|\.)#{Regexp.quote host}\z/i =~ self.host &&
1664
               (!port || self.port == port.to_i)
1665
              proxy_uri = nil
1666
              break
1667
            end
1668
          }
1669
        end
1670
        proxy_uri
1671
      else
1672
        nil
1673
      end
1674
    end
1599 1675
  end
1600 1676
end
lib/net/http.rb (working copy)
267 267
  #
268 268
  # === Proxies
269 269
  #
270
  # Net::HTTP::Proxy has the same methods as Net::HTTP but its instances always
271
  # connect via the proxy instead of directly to the given host.
270
  # Net::HTTP will automatically create a proxy from the +http_proxy+
271
  # environment variable if it is present.  To disable use of +http_proxy+,
272
  # pass +nil+ for the proxy address.
273
  #
274
  # You may also create a custom proxy:
272 275
  #
273 276
  #   proxy_addr = 'your.proxy.host'
274 277
  #   proxy_port = 8080
275 278
  #
276
  #   Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') {|http|
277
  #     # always connect to your.proxy.addr:8080
279
  #   Net::HTTP.new('example.com', nil, proxy_addr, proxy_port).start { |http|
280
  #     # always proxy via your.proxy.addr:8080
278 281
  #   }
279 282
  #
280
  # Net::HTTP::Proxy returns a Net::HTTP instance when proxy_addr is nil so
281
  # there is no need for conditional code.
282
  #
283
  # See Net::HTTP::Proxy for further details and examples such as proxies that
283
  # See Net::HTTP.new for further details and examples such as proxies that
284 284
  # require a username and password.
285 285
  #
286 286
  # == HTTP Request Classes
......
569 569
      http.start(&block)
570 570
    end
571 571

  
572
    class << HTTP
573
      alias newobj new
574
    end
575

  
576 572
    # Creates a new Net::HTTP object without opening a TCP connection or
577 573
    # HTTP session.
578
    # The +address+ should be a DNS hostname or IP address.
579
    # If +p_addr+ is given, creates a Net::HTTP object with proxy support.
580
    def HTTP.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil)
581
      Proxy(p_addr, p_port, p_user, p_pass).newobj(address, port)
574
    #
575
    # The +address+ should be a DNS hostname or IP address, the +port+ is the
576
    # port the server operates on.  If no +port+ is given the default port for
577
    # HTTP or HTTPS is used.
578
    #
579
    # If none of the +p_+ arguments are given, the proxy host and port are
580
    # taken from the +http_proxy+ environment variable (or its uppercase
581
    # equivalent) if present.  If the proxy requires authentication you must
582
    # supply it by hand.  See URI::Generic#find_proxy for details of proxy
583
    # detection from the environment.  To disable proxy detection set +p_addr+
584
    # to nil.
585
    #
586
    # If you are connecting to a custom proxy, +p_addr+ the DNS name or IP
587
    # address of the proxy host, +p_port+ the port to use to access the proxy,
588
    # and +p_user+ and +p_pass+ the username and password if authorization is
589
    # required to use the proxy.
590
    #
591
    def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil)
592
      http = super address, port
593

  
594
      if proxy_class? then # from Net::HTTP::Proxy()
595
        http.proxy_from_env = @proxy_from_env
596
        http.proxy_address  = @proxy_address
597
        http.proxy_port     = @proxy_port
598
        http.proxy_user     = @proxy_user
599
        http.proxy_pass     = @proxy_pass
600
      elsif p_addr == :ENV then
601
        http.proxy_from_env = true
602
      else
603
        http.proxy_address = p_addr
604
        http.proxy_port    = p_port
605
        http.proxy_user    = p_user
606
        http.proxy_pass    = p_pass
607
      end
608

  
609
      http
582 610
    end
583 611

  
584 612
    # Creates a new Net::HTTP object for the specified server address,
......
597 625
      @read_timeout = 60
598 626
      @continue_timeout = nil
599 627
      @debug_output = nil
628

  
629
      @proxy_from_env = false
630
      @proxy_uri      = nil
631
      @proxy_address  = nil
632
      @proxy_port     = nil
633
      @proxy_user     = nil
634
      @proxy_pass     = nil
635

  
600 636
      @use_ssl = false
601 637
      @ssl_context = nil
602 638
      @enable_post_connection_check = true
......
631 667
    # The port number to connect to.
632 668
    attr_reader :port
633 669

  
670
    attr_writer :proxy_from_env
671
    attr_writer :proxy_address
672
    attr_writer :proxy_port
673
    attr_writer :proxy_user
674
    attr_writer :proxy_pass
675

  
634 676
    # Number of seconds to wait for the connection to open. Any number
635 677
    # may be used, including Floats for fractional seconds. If the HTTP
636 678
    # object cannot open a connection in this many seconds, it raises a
......
797 839
    private :do_start
798 840

  
799 841
    def connect
800
      D "opening connection to #{conn_address()}..."
842
      if proxy? then
843
        conn_address = proxy_address
844
        conn_port    = proxy_port
845
      else
846
        conn_address = address
847
        conn_port    = port
848
      end
849

  
850
      D "opening connection to #{conn_address}..."
801 851
      s = Timeout.timeout(@open_timeout, Net::OpenTimeout) {
802
        TCPSocket.open(conn_address(), conn_port())
852
        TCPSocket.open(conn_address, conn_port)
803 853
      }
804 854
      D "opened"
805 855
      if use_ssl?
......
876 926

  
877 927
    # no proxy
878 928
    @is_proxy_class = false
929
    @proxy_from_env = false
879 930
    @proxy_addr = nil
880 931
    @proxy_port = nil
881 932
    @proxy_user = nil
......
884 935
    # Creates an HTTP proxy class which behaves like Net::HTTP, but
885 936
    # performs all access via the specified proxy.
886 937
    #
887
    # The arguments are the DNS name or IP address of the proxy host,
888
    # the port to use to access the proxy, and a username and password
889
    # if authorization is required to use the proxy.
890
    #
891
    # You can replace any use of the Net::HTTP class with use of the
892
    # proxy class created.
893
    #
894
    # If +p_addr+ is nil, this method returns self (a Net::HTTP object).
895
    #
896
    #   # Example
897
    #   proxy_class = Net::HTTP::Proxy('proxy.example.com', 8080)
898
    #
899
    #   proxy_class.start('www.ruby-lang.org') {|http|
900
    #     # connecting proxy.foo.org:8080
901
    #   }
902
    #
903
    # You may use them to work with authorization-enabled proxies:
904
    #
905
    #   proxy_host = 'your.proxy.example'
906
    #   proxy_port = 8080
907
    #   proxy_user = 'user'
908
    #   proxy_pass = 'pass'
909
    #
910
    #   proxy = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass)
911
    #   proxy.start('www.example.com') { |http|
912
    #     # always connect to your.proxy.example:8080 using specified username
913
    #     # and password
914
    #   }
915
    #
916
    # Note that net/http does not use the HTTP_PROXY environment variable.
917
    # If you want to use a proxy, you must set it explicitly.
918
    #
919
    def HTTP.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil)
938
    # This class is obsolete.  You may pass these same parameters directly to
939
    # Net::HTTP.new.  See Net::HTTP.new for details of the arguments.
940
    def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil)
920 941
      return self unless p_addr
921
      delta = ProxyDelta
922
      proxyclass = Class.new(self)
923
      proxyclass.module_eval {
924
        include delta
925
        # with proxy
942

  
943
      Class.new(self) {
926 944
        @is_proxy_class = true
927
        @proxy_address = p_addr
928
        @proxy_port    = p_port || default_port()
929
        @proxy_user    = p_user
930
        @proxy_pass    = p_pass
945

  
946
        if p_addr == :ENV then
947
          @proxy_from_env = true
948
          @proxy_address = nil
949
          @proxy_port    = nil
950
        else
951
          @proxy_address = p_addr
952
          @proxy_port    = p_port
953
        end
954

  
955
        @proxy_user = p_user
956
        @proxy_pass = p_pass
931 957
      }
932
      proxyclass
933 958
    end
934 959

  
935 960
    class << HTTP
......
952 977
      attr_reader :proxy_pass
953 978
    end
954 979

  
955
    # True if self is a HTTP proxy class.
980
    # True if requests for this connection will be proxied
956 981
    def proxy?
957
      self.class.proxy_class?
982
      if @proxy_from_env then
983
        proxy_uri
984
      else
985
        @proxy_address
986
      end
958 987
    end
959 988

  
960
    # A convenience method for accessing value of proxy_address from Net::HTTP.
989
    # True if the proxy for this connection is determined from the environment
990
    def proxy_from_env?
991
      @proxy_from_env
992
    end
993

  
994
    # The proxy URI determined from the environment for this connection.
995
    def proxy_uri # :nodoc:
996
      @proxy_uri ||= URI("http://#{address}:#{port}").find_proxy
997
    end
998

  
999
    # The address of the proxy server, if one is configured.
961 1000
    def proxy_address
962
      self.class.proxy_address
1001
      if @proxy_from_env then
1002
        proxy_uri.hostname
1003
      else
1004
        @proxy_address
1005
      end
963 1006
    end
964 1007

  
965
    # A convenience method for accessing value of proxy_port from Net::HTTP.
1008
    # The port of the proxy server, if one is configured.
966 1009
    def proxy_port
967
      self.class.proxy_port
1010
      if @proxy_from_env then
1011
        proxy_uri.port
1012
      else
1013
        @proxy_port
1014
      end
968 1015
    end
969 1016

  
970
    # A convenience method for accessing value of proxy_user from Net::HTTP.
1017
    # The proxy username, if one is configured
971 1018
    def proxy_user
972
      self.class.proxy_user
1019
      @proxy_user
973 1020
    end
974 1021

  
975
    # A convenience method for accessing value of proxy_pass from Net::HTTP.
1022
    # The proxy password, if one is configured
976 1023
    def proxy_pass
977
      self.class.proxy_pass
1024
      @proxy_pass
978 1025
    end
979 1026

  
980 1027
    alias proxyaddr proxy_address   #:nodoc: obsolete
......
982 1029

  
983 1030
    private
984 1031

  
985
    # without proxy
1032
    # without proxy, obsolete
986 1033

  
987
    def conn_address
1034
    def conn_address # :nodoc:
988 1035
      address()
989 1036
    end
990 1037

  
991
    def conn_port
1038
    def conn_port # :nodoc:
992 1039
      port()
993 1040
    end
994 1041

  
995 1042
    def edit_path(path)
996
      path
1043
      if proxy? and not use_ssl? then
1044
        "http://#{addr_port}#{path}"
1045
      else
1046
        path
1047
      end
997 1048
    end
998 1049

  
999 1050
    #
test/open-uri/test_open-uri.rb (working copy)
504 504

  
505 505
  # 192.0.2.0/24 is TEST-NET.  [RFC3330]
506 506

  
507
  def test_find_proxy
508
    assert_nil(URI("http://192.0.2.1/").find_proxy)
509
    assert_nil(URI("ftp://192.0.2.1/").find_proxy)
510
    with_env('http_proxy'=>'http://127.0.0.1:8080') {
511
      assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)
512
      assert_nil(URI("ftp://192.0.2.1/").find_proxy)
513
    }
514
    with_env('ftp_proxy'=>'http://127.0.0.1:8080') {
515
      assert_nil(URI("http://192.0.2.1/").find_proxy)
516
      assert_equal(URI('http://127.0.0.1:8080'), URI("ftp://192.0.2.1/").find_proxy)
517
    }
518
    with_env('REQUEST_METHOD'=>'GET') {
519
      assert_nil(URI("http://192.0.2.1/").find_proxy)
520
    }
521
    with_env('CGI_HTTP_PROXY'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') {
522
      assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)
523
    }
524
    with_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'192.0.2.2') {
525
      assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)
526
      assert_nil(URI("http://192.0.2.2/").find_proxy)
527
    }
528
  end
529

  
530
  def test_find_proxy_case_sensitive_env
531
    with_env('http_proxy'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') {
532
      assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)
533
    }
534
    with_env('HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') {
535
      assert_nil(nil, URI("http://192.0.2.1/").find_proxy)
536
    }
537
    with_env('http_proxy'=>'http://127.0.0.1:8080', 'HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') {
538
      assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)
539
    }
540
  end unless RUBY_PLATFORM =~ /mswin|mingw/
541

  
542 507
  def test_ftp_invalid_request
543 508
    assert_raise(ArgumentError) { URI("ftp://127.0.0.1/").read }
544 509
    assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Db").read }
test/uri/test_generic.rb (working copy)
732 732
    URI::Generic.build2(path: "/foo bar/baz")
733 733
    URI::Generic.build2(['http', nil, 'example.com', 80, nil, '/foo bar' , nil, nil, nil])
734 734
  end
735

  
736
  # 192.0.2.0/24 is TEST-NET.  [RFC3330]
737

  
738
  def test_find_proxy
739
    assert_nil(URI("http://192.0.2.1/").find_proxy)
740
    assert_nil(URI("ftp://192.0.2.1/").find_proxy)
741
    with_env('http_proxy'=>'http://127.0.0.1:8080') {
742
      assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)
743
      assert_nil(URI("ftp://192.0.2.1/").find_proxy)
744
    }
745
    with_env('ftp_proxy'=>'http://127.0.0.1:8080') {
746
      assert_nil(URI("http://192.0.2.1/").find_proxy)
747
      assert_equal(URI('http://127.0.0.1:8080'), URI("ftp://192.0.2.1/").find_proxy)
748
    }
749
    with_env('REQUEST_METHOD'=>'GET') {
750
      assert_nil(URI("http://192.0.2.1/").find_proxy)
751
    }
752
    with_env('CGI_HTTP_PROXY'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') {
753
      assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)
754
    }
755
    with_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'192.0.2.2') {
756
      assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)
757
      assert_nil(URI("http://192.0.2.2/").find_proxy)
758
    }
759
  end
760

  
761
  def test_find_proxy_case_sensitive_env
762
    with_env('http_proxy'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') {
763
      assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)
764
    }
765
    with_env('HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') {
766
      assert_nil(nil, URI("http://192.0.2.1/").find_proxy)
767
    }
768
    with_env('http_proxy'=>'http://127.0.0.1:8080', 'HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') {
769
      assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)
770
    }
771
  end unless RUBY_PLATFORM =~ /mswin|mingw/
772

  
773
  def with_env(h)
774
    begin
775
      old = {}
776
      h.each_key {|k| old[k] = ENV[k] }
777
      h.each {|k, v| ENV[k] = v }
778
      yield
779
    ensure
780
      h.each_key {|k| ENV[k] = old[k] }
781
    end
782
  end
783

  
735 784
end
test/net/http/test_http.rb (working copy)
5 5
require 'stringio'
6 6
require_relative 'utils'
7 7

  
8
class TestNetHTTP < Test::Unit::TestCase
9

  
10
  def test_class_Proxy
11
    no_proxy_class = Net::HTTP.Proxy nil
12

  
13
    assert_equal Net::HTTP, no_proxy_class
14

  
15
    proxy_class = Net::HTTP.Proxy 'proxy.example', 8000, 'user', 'pass'
16

  
17
    refute_equal Net::HTTP, proxy_class
18

  
19
    assert_operator proxy_class, :<, Net::HTTP
20

  
21
    assert_equal 'proxy.example', proxy_class.proxy_address
22
    assert_equal 8000,            proxy_class.proxy_port
23
    assert_equal 'user',          proxy_class.proxy_user
24
    assert_equal 'pass',          proxy_class.proxy_pass
25

  
26
    http = proxy_class.new 'example'
27

  
28
    refute http.proxy_from_env?
29
  end
30

  
31
  def test_class_Proxy_from_ENV
32
    clean_http_proxy_env do
33
      ENV['http_proxy']      = 'http://proxy.example:8000'
34

  
35
      # These are ignored on purpose.  See Bug 4388 and Feature 6546
36
      ENV['http_proxy_user'] = 'user'
37
      ENV['http_proxy_pass'] = 'pass'
38

  
39
      proxy_class = Net::HTTP.Proxy :ENV
40

  
41
      refute_equal Net::HTTP, proxy_class
42

  
43
      assert_operator proxy_class, :<, Net::HTTP
44

  
45
      assert_nil proxy_class.proxy_address
46
      assert_nil proxy_class.proxy_user
47
      assert_nil proxy_class.proxy_pass
48

  
49
      refute_equal 8000, proxy_class.proxy_port
50

  
51
      http = proxy_class.new 'example'
52

  
53
      assert http.proxy_from_env?
54
    end
55
  end
56

  
57
  def test_edit_path
58
    http = Net::HTTP.new 'example', nil, nil
59

  
60
    edited = http.send :edit_path, '/path'
61

  
62
    assert_equal '/path', edited
63

  
64
    http.use_ssl = true
65

  
66
    edited = http.send :edit_path, '/path'
67

  
68
    assert_equal '/path', edited
69
  end
70

  
71
  def test_edit_path_proxy
72
    http = Net::HTTP.new 'example', nil, 'proxy.example'
73

  
74
    edited = http.send :edit_path, '/path'
75

  
76
    assert_equal 'http://example/path', edited
77

  
78
    http.use_ssl = true
79

  
80
    edited = http.send :edit_path, '/path'
81

  
82
    assert_equal '/path', edited
83
  end
84

  
85
  def test_proxy_address
86
    http = Net::HTTP.new 'example', nil, 'proxy.example'
87

  
88
    assert_equal 'proxy.example', http.proxy_address
89
  end
90

  
91
  def test_proxy_address_ENV
92
    clean_http_proxy_env do
93
      ENV['http_proxy'] = 'http://proxy.example:8000'
94

  
95
      http = Net::HTTP.new 'example'
96

  
97
      assert_equal 'proxy.example', http.proxy_address
98
    end
99
  end
100

  
101
  def test_proxy_eh_no_proxy
102
    clean_http_proxy_env do
103
      refute Net::HTTP.new('example', nil, nil).proxy?
104
    end
105
  end
106

  
107
  def test_proxy_eh_ENV
108
    clean_http_proxy_env do
109
      ENV['http_proxy'] = 'http://proxy.example:8000'
110

  
111
      http = Net::HTTP.new 'example'
112

  
113
      assert http.proxy?
114
    end
115
  end
116

  
117
  def test_proxy_eh_ENV_none_set
118
    clean_http_proxy_env do
119
      refute Net::HTTP.new('example').proxy?
120
    end
121
  end
122

  
123
  def test_proxy_eh_ENV_no_proxy
124
    clean_http_proxy_env do
125
      ENV['http_proxy'] = 'http://proxy.example:8000'
126
      ENV['no_proxy']   = 'example'
127

  
128
      refute Net::HTTP.new('example').proxy?
129
    end
130
  end
131

  
132
  def test_proxy_port
133
    http = Net::HTTP.new 'exmaple', nil, 'proxy.example', 8000
134

  
135
    assert_equal 8000, http.proxy_port
136
  end
137

  
138
  def test_proxy_port_ENV
139
    clean_http_proxy_env do
140
      ENV['http_proxy'] = 'http://proxy.example:8000'
141

  
142
      http = Net::HTTP.new 'example'
143

  
144
      assert_equal 8000, http.proxy_port
145
    end
146
  end
147

  
148
  def clean_http_proxy_env
149
    orig = {
150
      'http_proxy'      => ENV['http_proxy'],
151
      'http_proxy_user' => ENV['http_proxy_user'],
152
      'http_proxy_pass' => ENV['http_proxy_pass'],
153
      'no_proxy'        => ENV['no_proxy'],
154
    }
155

  
156
    orig.each_key do |key|
157
      ENV.delete key
158
    end
159

  
160
    yield
161
  ensure
162
    orig.each do |key, value|
163
      ENV[key] = value
164
    end
165
  end
166

  
167
end
168

  
8 169
module TestNetHTTP_version_1_1_methods
9 170

  
10 171
  def test_s_get