mercurial/httpclient/tests/test_proxy_support.py
branchstable
changeset 17225 a06e2681dd17
parent 17222 98823bd0d697
parent 17224 23b247234454
child 17226 436cc9d017c6
equal deleted inserted replaced
17222:98823bd0d697 17225:a06e2681dd17
     1 # Copyright 2010, Google Inc.
       
     2 # All rights reserved.
       
     3 #
       
     4 # Redistribution and use in source and binary forms, with or without
       
     5 # modification, are permitted provided that the following conditions are
       
     6 # met:
       
     7 #
       
     8 #     * Redistributions of source code must retain the above copyright
       
     9 # notice, this list of conditions and the following disclaimer.
       
    10 #     * Redistributions in binary form must reproduce the above
       
    11 # copyright notice, this list of conditions and the following disclaimer
       
    12 # in the documentation and/or other materials provided with the
       
    13 # distribution.
       
    14 #     * Neither the name of Google Inc. nor the names of its
       
    15 # contributors may be used to endorse or promote products derived from
       
    16 # this software without specific prior written permission.
       
    17 
       
    18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29 import unittest
       
    30 import socket
       
    31 
       
    32 import http
       
    33 
       
    34 # relative import to ease embedding the library
       
    35 import util
       
    36 
       
    37 
       
    38 def make_preloaded_socket(data):
       
    39     """Make a socket pre-loaded with data so it can be read during connect.
       
    40 
       
    41     Useful for https proxy tests because we have to read from the
       
    42     socket during _connect rather than later on.
       
    43     """
       
    44     def s(*args, **kwargs):
       
    45         sock = util.MockSocket(*args, **kwargs)
       
    46         sock.early_data = data[:]
       
    47         return sock
       
    48     return s
       
    49 
       
    50 
       
    51 class ProxyHttpTest(util.HttpTestBase, unittest.TestCase):
       
    52 
       
    53     def _run_simple_test(self, host, server_data, expected_req, expected_data):
       
    54         con = http.HTTPConnection(host)
       
    55         con._connect()
       
    56         con.sock.data = server_data
       
    57         con.request('GET', '/')
       
    58 
       
    59         self.assertEqual(expected_req, con.sock.sent)
       
    60         self.assertEqual(expected_data, con.getresponse().read())
       
    61 
       
    62     def testSimpleRequest(self):
       
    63         con = http.HTTPConnection('1.2.3.4:80',
       
    64                                   proxy_hostport=('magicproxy', 4242))
       
    65         con._connect()
       
    66         con.sock.data = ['HTTP/1.1 200 OK\r\n',
       
    67                          'Server: BogusServer 1.0\r\n',
       
    68                          'MultiHeader: Value\r\n'
       
    69                          'MultiHeader: Other Value\r\n'
       
    70                          'MultiHeader: One More!\r\n'
       
    71                          'Content-Length: 10\r\n',
       
    72                          '\r\n'
       
    73                          '1234567890'
       
    74                          ]
       
    75         con.request('GET', '/')
       
    76 
       
    77         expected_req = ('GET http://1.2.3.4/ HTTP/1.1\r\n'
       
    78                         'Host: 1.2.3.4\r\n'
       
    79                         'accept-encoding: identity\r\n\r\n')
       
    80 
       
    81         self.assertEqual(('127.0.0.42', 4242), con.sock.sa)
       
    82         self.assertStringEqual(expected_req, con.sock.sent)
       
    83         resp = con.getresponse()
       
    84         self.assertEqual('1234567890', resp.read())
       
    85         self.assertEqual(['Value', 'Other Value', 'One More!'],
       
    86                          resp.headers.getheaders('multiheader'))
       
    87         self.assertEqual(['BogusServer 1.0'],
       
    88                          resp.headers.getheaders('server'))
       
    89 
       
    90     def testSSLRequest(self):
       
    91         con = http.HTTPConnection('1.2.3.4:443',
       
    92                                   proxy_hostport=('magicproxy', 4242))
       
    93         socket.socket = make_preloaded_socket(
       
    94             ['HTTP/1.1 200 OK\r\n',
       
    95              'Server: BogusServer 1.0\r\n',
       
    96              'Content-Length: 10\r\n',
       
    97              '\r\n'
       
    98              '1234567890'])
       
    99         con._connect()
       
   100         con.sock.data = ['HTTP/1.1 200 OK\r\n',
       
   101                          'Server: BogusServer 1.0\r\n',
       
   102                          'Content-Length: 10\r\n',
       
   103                          '\r\n'
       
   104                          '1234567890'
       
   105                          ]
       
   106         connect_sent = con.sock.sent
       
   107         con.sock.sent = ''
       
   108         con.request('GET', '/')
       
   109 
       
   110         expected_connect = ('CONNECT 1.2.3.4:443 HTTP/1.0\r\n'
       
   111                             'Host: 1.2.3.4\r\n'
       
   112                             'accept-encoding: identity\r\n'
       
   113                             '\r\n')
       
   114         expected_request = ('GET / HTTP/1.1\r\n'
       
   115                             'Host: 1.2.3.4\r\n'
       
   116                             'accept-encoding: identity\r\n\r\n')
       
   117 
       
   118         self.assertEqual(('127.0.0.42', 4242), con.sock.sa)
       
   119         self.assertStringEqual(expected_connect, connect_sent)
       
   120         self.assertStringEqual(expected_request, con.sock.sent)
       
   121         resp = con.getresponse()
       
   122         self.assertEqual(resp.status, 200)
       
   123         self.assertEqual('1234567890', resp.read())
       
   124         self.assertEqual(['BogusServer 1.0'],
       
   125                          resp.headers.getheaders('server'))
       
   126 
       
   127     def testSSLProxyFailure(self):
       
   128         con = http.HTTPConnection('1.2.3.4:443',
       
   129                                   proxy_hostport=('magicproxy', 4242))
       
   130         socket.socket = make_preloaded_socket(
       
   131             ['HTTP/1.1 407 Proxy Authentication Required\r\n\r\n'])
       
   132         self.assertRaises(http.HTTPProxyConnectFailedException, con._connect)
       
   133         self.assertRaises(http.HTTPProxyConnectFailedException,
       
   134                           con.request, 'GET', '/')
       
   135 # no-check-code