mercurial/httpclient/tests/test_chunked_transfer.py
changeset 16643 24dbef11f477
parent 15218 c81dce8a7bb6
equal deleted inserted replaced
16642:5cf18921bb7b 16643:24dbef11f477
    27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    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.
    28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    29 import cStringIO
    29 import cStringIO
    30 import unittest
    30 import unittest
    31 
    31 
    32 import http
    32 import httpplus
    33 
    33 
    34 # relative import to ease embedding the library
    34 # relative import to ease embedding the library
    35 import util
    35 import util
    36 
    36 
    37 
    37 
    48     return ''.join((hex(len(x))[2:], eol, x, eol))
    48     return ''.join((hex(len(x))[2:], eol, x, eol))
    49 
    49 
    50 
    50 
    51 class ChunkedTransferTest(util.HttpTestBase, unittest.TestCase):
    51 class ChunkedTransferTest(util.HttpTestBase, unittest.TestCase):
    52     def testChunkedUpload(self):
    52     def testChunkedUpload(self):
    53         con = http.HTTPConnection('1.2.3.4:80')
    53         con = httpplus.HTTPConnection('1.2.3.4:80')
    54         con._connect()
    54         con._connect()
    55         sock = con.sock
    55         sock = con.sock
    56         sock.read_wait_sentinel = '0\r\n\r\n'
    56         sock.read_wait_sentinel = '0\r\n\r\n'
    57         sock.data = ['HTTP/1.1 200 OK\r\n',
    57         sock.data = ['HTTP/1.1 200 OK\r\n',
    58                      'Server: BogusServer 1.0\r\n',
    58                      'Server: BogusServer 1.0\r\n',
    75         self.assertStringEqual(expected_req, sock.sent)
    75         self.assertStringEqual(expected_req, sock.sent)
    76         self.assertEqual("Thanks", con.getresponse().read())
    76         self.assertEqual("Thanks", con.getresponse().read())
    77         self.assertEqual(sock.closed, False)
    77         self.assertEqual(sock.closed, False)
    78 
    78 
    79     def testChunkedDownload(self):
    79     def testChunkedDownload(self):
    80         con = http.HTTPConnection('1.2.3.4:80')
    80         con = httpplus.HTTPConnection('1.2.3.4:80')
    81         con._connect()
    81         con._connect()
    82         sock = con.sock
    82         sock = con.sock
    83         sock.data = ['HTTP/1.1 200 OK\r\n',
    83         sock.data = ['HTTP/1.1 200 OK\r\n',
    84                      'Server: BogusServer 1.0\r\n',
    84                      'Server: BogusServer 1.0\r\n',
    85                      'transfer-encoding: chunked',
    85                      'transfer-encoding: chunked',
    86                      '\r\n\r\n',
    86                      '\r\n\r\n',
    87                      chunkedblock('hi '),
    87                      chunkedblock('hi '),
    88                      chunkedblock('there'),
    88                      ] + list(chunkedblock('there')) + [
    89                      chunkedblock(''),
    89                      chunkedblock(''),
    90                      ]
    90                      ]
    91         con.request('GET', '/')
    91         con.request('GET', '/')
    92         self.assertStringEqual('hi there', con.getresponse().read())
    92         self.assertStringEqual('hi there', con.getresponse().read())
    93 
    93 
       
    94     def testChunkedDownloadOddReadBoundaries(self):
       
    95         con = httpplus.HTTPConnection('1.2.3.4:80')
       
    96         con._connect()
       
    97         sock = con.sock
       
    98         sock.data = ['HTTP/1.1 200 OK\r\n',
       
    99                      'Server: BogusServer 1.0\r\n',
       
   100                      'transfer-encoding: chunked',
       
   101                      '\r\n\r\n',
       
   102                      chunkedblock('hi '),
       
   103                      ] + list(chunkedblock('there')) + [
       
   104                      chunkedblock(''),
       
   105                      ]
       
   106         con.request('GET', '/')
       
   107         resp = con.getresponse()
       
   108         for amt, expect in [(1, 'h'), (5, 'i the'), (100, 're')]:
       
   109             self.assertEqual(expect, resp.read(amt))
       
   110 
    94     def testChunkedDownloadBadEOL(self):
   111     def testChunkedDownloadBadEOL(self):
    95         con = http.HTTPConnection('1.2.3.4:80')
   112         con = httpplus.HTTPConnection('1.2.3.4:80')
    96         con._connect()
   113         con._connect()
    97         sock = con.sock
   114         sock = con.sock
    98         sock.data = ['HTTP/1.1 200 OK\n',
   115         sock.data = ['HTTP/1.1 200 OK\n',
    99                      'Server: BogusServer 1.0\n',
   116                      'Server: BogusServer 1.0\n',
   100                      'transfer-encoding: chunked',
   117                      'transfer-encoding: chunked',
   105                      ]
   122                      ]
   106         con.request('GET', '/')
   123         con.request('GET', '/')
   107         self.assertStringEqual('hi there', con.getresponse().read())
   124         self.assertStringEqual('hi there', con.getresponse().read())
   108 
   125 
   109     def testChunkedDownloadPartialChunkBadEOL(self):
   126     def testChunkedDownloadPartialChunkBadEOL(self):
   110         con = http.HTTPConnection('1.2.3.4:80')
   127         con = httpplus.HTTPConnection('1.2.3.4:80')
   111         con._connect()
   128         con._connect()
   112         sock = con.sock
   129         sock = con.sock
   113         sock.data = ['HTTP/1.1 200 OK\n',
   130         sock.data = ['HTTP/1.1 200 OK\n',
   114                      'Server: BogusServer 1.0\n',
   131                      'Server: BogusServer 1.0\n',
   115                      'transfer-encoding: chunked',
   132                      'transfer-encoding: chunked',
   120         con.request('GET', '/')
   137         con.request('GET', '/')
   121         self.assertStringEqual('hi there\nthere\nthere\nthere\nthere\n',
   138         self.assertStringEqual('hi there\nthere\nthere\nthere\nthere\n',
   122                                con.getresponse().read())
   139                                con.getresponse().read())
   123 
   140 
   124     def testChunkedDownloadPartialChunk(self):
   141     def testChunkedDownloadPartialChunk(self):
   125         con = http.HTTPConnection('1.2.3.4:80')
   142         con = httpplus.HTTPConnection('1.2.3.4:80')
   126         con._connect()
   143         con._connect()
   127         sock = con.sock
   144         sock = con.sock
   128         sock.data = ['HTTP/1.1 200 OK\r\n',
   145         sock.data = ['HTTP/1.1 200 OK\r\n',
   129                      'Server: BogusServer 1.0\r\n',
   146                      'Server: BogusServer 1.0\r\n',
   130                      'transfer-encoding: chunked',
   147                      'transfer-encoding: chunked',
   134         con.request('GET', '/')
   151         con.request('GET', '/')
   135         self.assertStringEqual('hi there\nthere\nthere\nthere\nthere\n',
   152         self.assertStringEqual('hi there\nthere\nthere\nthere\nthere\n',
   136                                con.getresponse().read())
   153                                con.getresponse().read())
   137 
   154 
   138     def testChunkedDownloadEarlyHangup(self):
   155     def testChunkedDownloadEarlyHangup(self):
   139         con = http.HTTPConnection('1.2.3.4:80')
   156         con = httpplus.HTTPConnection('1.2.3.4:80')
   140         con._connect()
   157         con._connect()
   141         sock = con.sock
   158         sock = con.sock
   142         broken = chunkedblock('hi'*20)[:-1]
   159         broken = chunkedblock('hi'*20)[:-1]
   143         sock.data = ['HTTP/1.1 200 OK\r\n',
   160         sock.data = ['HTTP/1.1 200 OK\r\n',
   144                      'Server: BogusServer 1.0\r\n',
   161                      'Server: BogusServer 1.0\r\n',
   147                      broken,
   164                      broken,
   148                      ]
   165                      ]
   149         sock.close_on_empty = True
   166         sock.close_on_empty = True
   150         con.request('GET', '/')
   167         con.request('GET', '/')
   151         resp = con.getresponse()
   168         resp = con.getresponse()
   152         self.assertRaises(http.HTTPRemoteClosedError, resp.read)
   169         self.assertRaises(httpplus.HTTPRemoteClosedError, resp.read)
   153 # no-check-code
   170 # no-check-code