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 httpplus |
|
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 = httpplus.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 = httpplus.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 ] + list(chunkedblock('there')) + [ |
|
89 chunkedblock(''), |
|
90 ] |
|
91 con.request('GET', '/') |
|
92 self.assertStringEqual('hi there', con.getresponse().read()) |
|
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 |
|
111 def testChunkedDownloadBadEOL(self): |
|
112 con = httpplus.HTTPConnection('1.2.3.4:80') |
|
113 con._connect() |
|
114 sock = con.sock |
|
115 sock.data = ['HTTP/1.1 200 OK\n', |
|
116 'Server: BogusServer 1.0\n', |
|
117 'transfer-encoding: chunked', |
|
118 '\n\n', |
|
119 chunkedblock('hi ', eol='\n'), |
|
120 chunkedblock('there', eol='\n'), |
|
121 chunkedblock('', eol='\n'), |
|
122 ] |
|
123 con.request('GET', '/') |
|
124 self.assertStringEqual('hi there', con.getresponse().read()) |
|
125 |
|
126 def testChunkedDownloadPartialChunkBadEOL(self): |
|
127 con = httpplus.HTTPConnection('1.2.3.4:80') |
|
128 con._connect() |
|
129 sock = con.sock |
|
130 sock.data = ['HTTP/1.1 200 OK\n', |
|
131 'Server: BogusServer 1.0\n', |
|
132 'transfer-encoding: chunked', |
|
133 '\n\n', |
|
134 chunkedblock('hi ', eol='\n'), |
|
135 ] + list(chunkedblock('there\n' * 5, eol='\n')) + [ |
|
136 chunkedblock('', eol='\n')] |
|
137 con.request('GET', '/') |
|
138 self.assertStringEqual('hi there\nthere\nthere\nthere\nthere\n', |
|
139 con.getresponse().read()) |
|
140 |
|
141 def testChunkedDownloadPartialChunk(self): |
|
142 con = httpplus.HTTPConnection('1.2.3.4:80') |
|
143 con._connect() |
|
144 sock = con.sock |
|
145 sock.data = ['HTTP/1.1 200 OK\r\n', |
|
146 'Server: BogusServer 1.0\r\n', |
|
147 'transfer-encoding: chunked', |
|
148 '\r\n\r\n', |
|
149 chunkedblock('hi '), |
|
150 ] + list(chunkedblock('there\n' * 5)) + [chunkedblock('')] |
|
151 con.request('GET', '/') |
|
152 self.assertStringEqual('hi there\nthere\nthere\nthere\nthere\n', |
|
153 con.getresponse().read()) |
|
154 |
|
155 def testChunkedDownloadEarlyHangup(self): |
|
156 con = httpplus.HTTPConnection('1.2.3.4:80') |
|
157 con._connect() |
|
158 sock = con.sock |
|
159 broken = chunkedblock('hi'*20)[:-1] |
|
160 sock.data = ['HTTP/1.1 200 OK\r\n', |
|
161 'Server: BogusServer 1.0\r\n', |
|
162 'transfer-encoding: chunked', |
|
163 '\r\n\r\n', |
|
164 broken, |
|
165 ] |
|
166 sock.close_on_empty = True |
|
167 con.request('GET', '/') |
|
168 resp = con.getresponse() |
|
169 self.assertRaises(httpplus.HTTPRemoteClosedError, resp.read) |
|
170 # no-check-code |
|