mercurial/httpclient/tests/test_chunked_transfer.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 cStringIO
       
    30 import unittest
       
    31 
       
    32 import http
       
    33 
       
    34 # relative import to ease embedding the library
       
    35 import util
       
    36 
       
    37 
       
    38 def chunkedblock(x, eol='\r\n'):
       
    39     r"""Make a chunked transfer-encoding block.
       
    40 
       
    41     >>> chunkedblock('hi')
       
    42     '2\r\nhi\r\n'
       
    43     >>> chunkedblock('hi' * 10)
       
    44     '14\r\nhihihihihihihihihihi\r\n'
       
    45     >>> chunkedblock('hi', eol='\n')
       
    46     '2\nhi\n'
       
    47     """
       
    48     return ''.join((hex(len(x))[2:], eol, x, eol))
       
    49 
       
    50 
       
    51 class ChunkedTransferTest(util.HttpTestBase, unittest.TestCase):
       
    52     def testChunkedUpload(self):
       
    53         con = http.HTTPConnection('1.2.3.4:80')
       
    54         con._connect()
       
    55         sock = con.sock
       
    56         sock.read_wait_sentinel = '0\r\n\r\n'
       
    57         sock.data = ['HTTP/1.1 200 OK\r\n',
       
    58                      'Server: BogusServer 1.0\r\n',
       
    59                      'Content-Length: 6',
       
    60                      '\r\n\r\n',
       
    61                      "Thanks"]
       
    62 
       
    63         zz = 'zz\n'
       
    64         con.request('POST', '/', body=cStringIO.StringIO(
       
    65             (zz * (0x8010 / 3)) + 'end-of-body'))
       
    66         expected_req = ('POST / HTTP/1.1\r\n'
       
    67                         'transfer-encoding: chunked\r\n'
       
    68                         'Host: 1.2.3.4\r\n'
       
    69                         'accept-encoding: identity\r\n\r\n')
       
    70         expected_req += chunkedblock('zz\n' * (0x8000 / 3) + 'zz')
       
    71         expected_req += chunkedblock(
       
    72             '\n' + 'zz\n' * ((0x1b - len('end-of-body')) / 3) + 'end-of-body')
       
    73         expected_req += '0\r\n\r\n'
       
    74         self.assertEqual(('1.2.3.4', 80), sock.sa)
       
    75         self.assertStringEqual(expected_req, sock.sent)
       
    76         self.assertEqual("Thanks", con.getresponse().read())
       
    77         self.assertEqual(sock.closed, False)
       
    78 
       
    79     def testChunkedDownload(self):
       
    80         con = http.HTTPConnection('1.2.3.4:80')
       
    81         con._connect()
       
    82         sock = con.sock
       
    83         sock.data = ['HTTP/1.1 200 OK\r\n',
       
    84                      'Server: BogusServer 1.0\r\n',
       
    85                      'transfer-encoding: chunked',
       
    86                      '\r\n\r\n',
       
    87                      chunkedblock('hi '),
       
    88                      chunkedblock('there'),
       
    89                      chunkedblock(''),
       
    90                      ]
       
    91         con.request('GET', '/')
       
    92         self.assertStringEqual('hi there', con.getresponse().read())
       
    93 
       
    94     def testChunkedDownloadBadEOL(self):
       
    95         con = http.HTTPConnection('1.2.3.4:80')
       
    96         con._connect()
       
    97         sock = con.sock
       
    98         sock.data = ['HTTP/1.1 200 OK\n',
       
    99                      'Server: BogusServer 1.0\n',
       
   100                      'transfer-encoding: chunked',
       
   101                      '\n\n',
       
   102                      chunkedblock('hi ', eol='\n'),
       
   103                      chunkedblock('there', eol='\n'),
       
   104                      chunkedblock('', eol='\n'),
       
   105                      ]
       
   106         con.request('GET', '/')
       
   107         self.assertStringEqual('hi there', con.getresponse().read())
       
   108 
       
   109     def testChunkedDownloadPartialChunkBadEOL(self):
       
   110         con = http.HTTPConnection('1.2.3.4:80')
       
   111         con._connect()
       
   112         sock = con.sock
       
   113         sock.data = ['HTTP/1.1 200 OK\n',
       
   114                      'Server: BogusServer 1.0\n',
       
   115                      'transfer-encoding: chunked',
       
   116                      '\n\n',
       
   117                      chunkedblock('hi ', eol='\n'),
       
   118                      ] + list(chunkedblock('there\n' * 5, eol='\n')) + [
       
   119                          chunkedblock('', eol='\n')]
       
   120         con.request('GET', '/')
       
   121         self.assertStringEqual('hi there\nthere\nthere\nthere\nthere\n',
       
   122                                con.getresponse().read())
       
   123 
       
   124     def testChunkedDownloadPartialChunk(self):
       
   125         con = http.HTTPConnection('1.2.3.4:80')
       
   126         con._connect()
       
   127         sock = con.sock
       
   128         sock.data = ['HTTP/1.1 200 OK\r\n',
       
   129                      'Server: BogusServer 1.0\r\n',
       
   130                      'transfer-encoding: chunked',
       
   131                      '\r\n\r\n',
       
   132                      chunkedblock('hi '),
       
   133                      ] + list(chunkedblock('there\n' * 5)) + [chunkedblock('')]
       
   134         con.request('GET', '/')
       
   135         self.assertStringEqual('hi there\nthere\nthere\nthere\nthere\n',
       
   136                                con.getresponse().read())
       
   137 
       
   138     def testChunkedDownloadEarlyHangup(self):
       
   139         con = http.HTTPConnection('1.2.3.4:80')
       
   140         con._connect()
       
   141         sock = con.sock
       
   142         broken = chunkedblock('hi'*20)[:-1]
       
   143         sock.data = ['HTTP/1.1 200 OK\r\n',
       
   144                      'Server: BogusServer 1.0\r\n',
       
   145                      'transfer-encoding: chunked',
       
   146                      '\r\n\r\n',
       
   147                      broken,
       
   148                      ]
       
   149         sock.close_on_empty = True
       
   150         con.request('GET', '/')
       
   151         resp = con.getresponse()
       
   152         self.assertRaises(http.HTTPRemoteClosedError, resp.read)
       
   153 # no-check-code