mercurial/httpclient/tests/test_ssl.py
branchstable
changeset 17225 a06e2681dd17
parent 17222 98823bd0d697
parent 17224 23b247234454
child 17226 436cc9d017c6
equal deleted inserted replaced
17222:98823bd0d697 17225:a06e2681dd17
     1 # Copyright 2011, 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 
       
    31 import http
       
    32 
       
    33 # relative import to ease embedding the library
       
    34 import util
       
    35 
       
    36 
       
    37 
       
    38 class HttpSslTest(util.HttpTestBase, unittest.TestCase):
       
    39     def testSslRereadRequired(self):
       
    40         con = http.HTTPConnection('1.2.3.4:443')
       
    41         con._connect()
       
    42         # extend the list instead of assign because of how
       
    43         # MockSSLSocket works.
       
    44         con.sock.data = ['HTTP/1.1 200 OK\r\n',
       
    45                          'Server: BogusServer 1.0\r\n',
       
    46                          'MultiHeader: Value\r\n'
       
    47                          'MultiHeader: Other Value\r\n'
       
    48                          'MultiHeader: One More!\r\n'
       
    49                          'Content-Length: 10\r\n',
       
    50                          '\r\n'
       
    51                          '1234567890'
       
    52                          ]
       
    53         con.request('GET', '/')
       
    54 
       
    55         expected_req = ('GET / HTTP/1.1\r\n'
       
    56                         'Host: 1.2.3.4\r\n'
       
    57                         'accept-encoding: identity\r\n\r\n')
       
    58 
       
    59         self.assertEqual(('1.2.3.4', 443), con.sock.sa)
       
    60         self.assertEqual(expected_req, con.sock.sent)
       
    61         resp = con.getresponse()
       
    62         self.assertEqual('1234567890', resp.read())
       
    63         self.assertEqual(['Value', 'Other Value', 'One More!'],
       
    64                          resp.headers.getheaders('multiheader'))
       
    65         self.assertEqual(['BogusServer 1.0'],
       
    66                          resp.headers.getheaders('server'))
       
    67 
       
    68     def testSslRereadInEarlyResponse(self):
       
    69         con = http.HTTPConnection('1.2.3.4:443')
       
    70         con._connect()
       
    71         con.sock.early_data = ['HTTP/1.1 200 OK\r\n',
       
    72                                'Server: BogusServer 1.0\r\n',
       
    73                                'MultiHeader: Value\r\n'
       
    74                                'MultiHeader: Other Value\r\n'
       
    75                                'MultiHeader: One More!\r\n'
       
    76                                'Content-Length: 10\r\n',
       
    77                                '\r\n'
       
    78                                '1234567890'
       
    79                                ]
       
    80 
       
    81         expected_req = self.doPost(con, False)
       
    82         self.assertEqual(None, con.sock,
       
    83                          'Connection should have disowned socket')
       
    84 
       
    85         resp = con.getresponse()
       
    86         self.assertEqual(('1.2.3.4', 443), resp.sock.sa)
       
    87         self.assertEqual(expected_req, resp.sock.sent)
       
    88         self.assertEqual('1234567890', resp.read())
       
    89         self.assertEqual(['Value', 'Other Value', 'One More!'],
       
    90                          resp.headers.getheaders('multiheader'))
       
    91         self.assertEqual(['BogusServer 1.0'],
       
    92                          resp.headers.getheaders('server'))
       
    93 # no-check-code