13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests from HTTP response parsing."""
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests from HTTP response parsing.
19
The handle_response method read the response body of a GET request an returns
20
the corresponding RangeFile.
22
There are four different kinds of RangeFile:
23
- a whole file whose size is unknown, seen as a simple byte stream,
24
- a whole file whose size is known, we can't read past its end,
25
- a single range file, a part of a file with a start and a size,
26
- a multiple range file, several consecutive parts with known start offset
29
Some properties are common to all kinds:
30
- seek can only be forward (its really a socket underneath),
31
- read can't cross ranges,
32
- successive ranges are taken into account transparently,
34
- the expected pattern of use is either seek(offset)+read(size) or a single
35
read with no size specified. For multiple range files, multiple read() will
36
return the corresponding ranges, trying to read further will raise
19
40
from cStringIO import StringIO
22
from bzrlib import errors
23
from bzrlib.transport import http
24
from bzrlib.transport.http import response
25
from bzrlib.tests import TestCase
28
class TestResponseRange(TestCase):
29
"""Test the ResponseRange class."""
32
RR = response.ResponseRange
35
self.assertTrue(r1 < r2)
36
self.assertFalse(r1 > r2)
37
self.assertTrue(r1 < 5)
38
self.assertFalse(r2 < 5)
40
self.assertEqual(RR(0, 10, 5), RR(0, 10, 5))
41
self.assertNotEqual(RR(0, 10, 5), RR(0, 8, 5))
42
self.assertNotEqual(RR(0, 10, 5), RR(0, 10, 6))
44
def test_sort_list(self):
45
"""Ensure longer ranges are sorted after shorter ones"""
46
RR = response.ResponseRange
47
lst = [RR(3, 8, 0), 5, RR(3, 7, 0), 6]
49
self.assertEqual([RR(3,7,0), RR(3,8,0), 5, 6], lst)
52
class TestRangeFile(TestCase):
56
content = "abcdefghijklmnopqrstuvwxyz"
57
self.fp = response.RangeFile('foo', StringIO(content))
58
self.fp._add_range(0, 9, 0)
59
self.fp._add_range(20, 29, 10)
60
self.fp._add_range(30, 39, 15)
62
def test_valid_accesses(self):
63
"""Test so that valid accesses work to the file."""
65
self.assertEquals(self.fp.read(3), 'abc')
66
self.assertEquals(self.fp.read(3), 'def')
67
self.assertEquals(self.fp.tell(), 6)
69
self.assertEquals(self.fp.read(3), 'klm')
70
self.assertEquals(self.fp.read(2), 'no')
71
self.assertEquals(self.fp.tell(), 25)
72
# should wrap over to 30-39 entity
73
self.assertEquals(self.fp.read(3), 'pqr')
75
self.assertEquals(self.fp.read(3), 'def')
76
self.assertEquals(self.fp.tell(), 6)
78
def test_invalid_accesses(self):
79
"""Test so that invalid accesses trigger errors."""
81
self.assertRaises(errors.InvalidRange, self.fp.read, 2)
83
self.assertRaises(errors.InvalidRange, self.fp.read, 2)
85
self.assertRaises(errors.InvalidRange, self.fp.read, 2)
87
def test__finish_ranges(self):
88
"""Test that after RangeFile._finish_ranges the list is sorted."""
89
self.fp._add_range(1, 2, 3)
90
self.fp._add_range(8, 9, 10)
91
self.fp._add_range(3, 4, 5)
93
# TODO: jam 20060706 If we switch to inserting
94
# in sorted order, remove this test
95
self.assertNotEqual(self.fp._ranges, sorted(self.fp._ranges))
97
self.fp._finish_ranges()
98
self.assertEqual(self.fp._ranges, sorted(self.fp._ranges))
100
def test_seek_and_tell(self):
101
# Check for seeking before start
103
self.assertEqual(0, self.fp.tell())
106
self.assertEqual(5, self.fp.tell())
109
self.assertEqual(3, self.fp.tell())
111
# TODO: jam 20060706 following tests will fail if this
112
# is not true, and would be difficult to debug
113
# but it is a layering violation
114
self.assertEqual(39, self.fp._len)
117
self.assertEqual(39, self.fp.tell())
120
self.assertEqual(29, self.fp.tell())
122
self.assertRaises(ValueError, self.fp.seek, 0, 4)
123
self.assertRaises(ValueError, self.fp.seek, 0, -1)
126
class TestRegexes(TestCase):
128
def assertRegexMatches(self, groups, text):
129
"""Check that the regex matches and returns the right values"""
130
m = self.regex.match(text)
131
self.assertNotEqual(None, m, "text %s did not match regex" % (text,))
133
self.assertEqual(groups, m.groups())
135
def test_range_re(self):
136
"""Test that we match valid ranges."""
137
self.regex = response.HttpRangeResponse._CONTENT_RANGE_RE
138
self.assertRegexMatches(('bytes', '1', '10', '11'),
140
self.assertRegexMatches(('bytes', '1', '10', '11'),
142
self.assertRegexMatches(('bytes', '2123', '4242', '1231'),
143
'\tbytes 2123-4242/1231 ')
144
self.assertRegexMatches(('chars', '1', '2', '3'),
147
def test_content_type_re(self):
148
self.regex = response.HttpMultipartRangeResponse._CONTENT_TYPE_RE
149
self.assertRegexMatches(('xxyyzz',),
150
'multipart/byteranges; boundary = xxyyzz')
151
self.assertRegexMatches(('xxyyzz',),
152
'multipart/byteranges;boundary=xxyyzz')
153
self.assertRegexMatches(('xx yy zz',),
154
' multipart/byteranges ; boundary= xx yy zz ')
155
self.assertEqual(None,
156
self.regex.match('multipart byteranges;boundary=xx'))
162
Content-range: bytes 1-10/20\r
166
Content-Range: bytes 21-30/20\r
171
content-range: bytes 41-50/20\r
175
content-range: bytes 51-60/20\r
181
class TestHelpers(TestCase):
182
"""Test the helper functions"""
184
def test__parse_range(self):
185
"""Test that _parse_range acts reasonably."""
186
content = StringIO('')
187
parse_range = response.HttpRangeResponse._parse_range
188
self.assertEqual((1,2), parse_range('bytes 1-2/3'))
189
self.assertEqual((10,20), parse_range('bytes 10-20/2'))
191
self.assertRaises(errors.InvalidHttpRange, parse_range, 'char 1-3/2')
192
self.assertRaises(errors.InvalidHttpRange, parse_range, 'bytes a-3/2')
195
parse_range('bytes x-10/3', path='http://foo/bar')
196
except errors.InvalidHttpRange, e:
197
self.assertContainsRe(str(e), 'http://foo/bar')
198
self.assertContainsRe(str(e), 'bytes x-10/3')
200
self.fail('Did not raise InvalidHttpRange')
202
def test__parse_boundary_simple(self):
203
"""Test that _parse_boundary handles Content-type properly"""
204
parse_boundary = response.HttpMultipartRangeResponse._parse_boundary
205
m = parse_boundary(' multipart/byteranges; boundary=xxyyzz')
206
self.assertNotEqual(None, m)
207
# Check that the returned regex is capable of splitting simple_data
208
matches = list(m.finditer(simple_data))
209
self.assertEqual(4, len(matches))
211
# match.group() should be the content-range entry
212
# and match.end() should be the start of the content
213
self.assertEqual(' bytes 1-10/20', matches[0].group(1))
214
self.assertEqual(simple_data.find('1234567890'), matches[0].end())
215
self.assertEqual(' bytes 21-30/20', matches[1].group(1))
216
self.assertEqual(simple_data.find('abcdefghij'), matches[1].end())
217
self.assertEqual(' bytes 41-50/20', matches[2].group(1))
218
self.assertEqual(simple_data.find('zyxwvutsrq'), matches[2].end())
219
self.assertEqual(' bytes 51-60/20', matches[3].group(1))
220
self.assertEqual(simple_data.find('xxyyzz fbd'), matches[3].end())
222
def test__parse_boundary_invalid(self):
223
parse_boundary = response.HttpMultipartRangeResponse._parse_boundary
225
parse_boundary(' multipart/bytes;boundary=xxyyzz',
226
path='http://foo/bar')
227
except errors.InvalidHttpContentType, e:
228
self.assertContainsRe(str(e), 'http://foo/bar')
229
self.assertContainsRe(str(e), 'multipart/bytes;boundary=xxyyzz')
231
self.fail('Did not raise InvalidHttpContentType')
234
class TestHttpRangeResponse(TestCase):
236
def test_smoketest(self):
237
"""A basic test that HttpRangeResponse is reasonable."""
238
content = StringIO('0123456789')
239
f = response.HttpRangeResponse('http://foo', 'bytes 1-10/9', content)
240
self.assertEqual([response.ResponseRange(1,10,0)], f._ranges)
47
from bzrlib.transport.http import (
51
from bzrlib.tests.file_utils import (
56
class ReadSocket(object):
57
"""A socket-like object that can be given a predefined content."""
59
def __init__(self, data):
60
self.readfile = StringIO(data)
62
def makefile(self, mode='r', bufsize=None):
66
class FakeHTTPConnection(_urllib2_wrappers.HTTPConnection):
68
def __init__(self, sock):
69
_urllib2_wrappers.HTTPConnection.__init__(self, 'localhost')
70
# Set the socket to bypass the connection
74
"""Ignores the writes on the socket."""
78
class TestHTTPConnection(tests.TestCase):
80
def test_cleanup_pipe(self):
81
sock = ReadSocket("""HTTP/1.1 200 OK\r
82
Content-Type: text/plain; charset=UTF-8\r
87
conn = FakeHTTPConnection(sock)
88
# Simulate the request sending so that the connection will be able to
90
conn.putrequest('GET', 'http://localhost/fictious')
92
# Now, get the response
93
resp = conn.getresponse()
94
# Read part of the response
95
self.assertEquals('0123456789\n', resp.read(11))
96
# Override the thresold to force the warning emission
97
conn._range_warning_thresold = 6 # There are 7 bytes pending
99
self.assertContainsRe(self.get_log(), 'Got a 200 response when asking')
102
class TestRangeFileMixin(object):
103
"""Tests for accessing the first range in a RangeFile."""
105
# A simple string used to represent a file part (also called a range), in
106
# which offsets are easy to calculate for test writers. It's used as a
107
# building block with slight variations but basically 'a' is the first char
108
# of the range and 'z' is the last.
109
alpha = 'abcdefghijklmnopqrstuvwxyz'
111
def test_can_read_at_first_access(self):
112
"""Test that the just created file can be read."""
113
self.assertEquals(self.alpha, self._file.read())
115
def test_seek_read(self):
116
"""Test seek/read inside the range."""
118
start = self.first_range_start
119
# Before any use, tell() should be at the range start
120
self.assertEquals(start, f.tell())
121
cur = start # For an overall offset assertion
124
self.assertEquals('def', f.read(3))
128
self.assertEquals('klmn', f.read(4))
130
# read(0) in the middle of a range
131
self.assertEquals('', f.read(0))
135
self.assertEquals(here, f.tell())
136
self.assertEquals(cur, f.tell())
138
def test_read_zero(self):
140
start = self.first_range_start
141
self.assertEquals('', f.read(0))
143
self.assertEquals('', f.read(0))
145
def test_seek_at_range_end(self):
149
def test_read_at_range_end(self):
150
"""Test read behaviour at range end."""
152
self.assertEquals(self.alpha, f.read())
153
self.assertEquals('', f.read(0))
154
self.assertRaises(errors.InvalidRange, f.read, 1)
156
def test_unbounded_read_after_seek(self):
159
# Should not cross ranges
160
self.assertEquals('yz', f.read())
162
def test_seek_backwards(self):
164
start = self.first_range_start
167
self.assertRaises(errors.InvalidRange, f.seek, start + 5)
169
def test_seek_outside_single_range(self):
171
if f._size == -1 or f._boundary is not None:
172
raise tests.TestNotApplicable('Needs a fully defined range')
173
# Will seek past the range and then errors out
174
self.assertRaises(errors.InvalidRange,
175
f.seek, self.first_range_start + 27)
177
def test_read_past_end_of_range(self):
180
raise tests.TestNotApplicable("Can't check an unknown size")
181
start = self.first_range_start
183
self.assertRaises(errors.InvalidRange, f.read, 10)
185
def test_seek_from_end(self):
186
"""Test seeking from the end of the file.
188
The semantic is unclear in case of multiple ranges. Seeking from end
189
exists only for the http transports, cannot be used if the file size is
190
unknown and is not used in bzrlib itself. This test must be (and is)
191
overridden by daughter classes.
193
Reading from end makes sense only when a range has been requested from
194
the end of the file (see HttpTransportBase._get() when using the
195
'tail_amount' parameter). The HTTP response can only be a whole file or
200
self.assertEquals('yz', f.read())
203
class TestRangeFileSizeUnknown(tests.TestCase, TestRangeFileMixin):
204
"""Test a RangeFile for a whole file whose size is not known."""
207
super(TestRangeFileSizeUnknown, self).setUp()
208
self._file = response.RangeFile('Whole_file_size_known',
209
StringIO(self.alpha))
210
# We define no range, relying on RangeFile to provide default values
211
self.first_range_start = 0 # It's the whole file
213
def test_seek_from_end(self):
214
"""See TestRangeFileMixin.test_seek_from_end.
216
The end of the file can't be determined since the size is unknown.
218
self.assertRaises(errors.InvalidRange, self._file.seek, -1, 2)
220
def test_read_at_range_end(self):
221
"""Test read behaviour at range end."""
223
self.assertEquals(self.alpha, f.read())
224
self.assertEquals('', f.read(0))
225
self.assertEquals('', f.read(1))
228
class TestRangeFileSizeKnown(tests.TestCase, TestRangeFileMixin):
229
"""Test a RangeFile for a whole file whose size is known."""
232
super(TestRangeFileSizeKnown, self).setUp()
233
self._file = response.RangeFile('Whole_file_size_known',
234
StringIO(self.alpha))
235
self._file.set_range(0, len(self.alpha))
236
self.first_range_start = 0 # It's the whole file
239
class TestRangeFileSingleRange(tests.TestCase, TestRangeFileMixin):
240
"""Test a RangeFile for a single range."""
243
super(TestRangeFileSingleRange, self).setUp()
244
self._file = response.RangeFile('Single_range_file',
245
StringIO(self.alpha))
246
self.first_range_start = 15
247
self._file.set_range(self.first_range_start, len(self.alpha))
250
def test_read_before_range(self):
251
# This can't occur under normal circumstances, we have to force it
253
f._pos = 0 # Force an invalid pos
254
self.assertRaises(errors.InvalidRange, f.read, 2)
257
class TestRangeFileMultipleRanges(tests.TestCase, TestRangeFileMixin):
258
"""Test a RangeFile for multiple ranges.
260
The RangeFile used for the tests contains three ranges:
262
- at offset 25: alpha
263
- at offset 100: alpha
264
- at offset 126: alpha.upper()
266
The two last ranges are contiguous. This only rarely occurs (should not in
267
fact) in real uses but may lead to hard to track bugs.
270
# The following is used to represent the boundary paramter defined
271
# in HTTP response headers and the boundary lines that separate
274
boundary = "separation"
277
super(TestRangeFileMultipleRanges, self).setUp()
279
boundary = self.boundary
282
self.first_range_start = 25
283
file_size = 200 # big enough to encompass all ranges
284
for (start, part) in [(self.first_range_start, self.alpha),
285
# Two contiguous ranges
287
(126, self.alpha.upper())]:
288
content += self._multipart_byterange(part, start, boundary,
291
content += self._boundary_line()
293
self._file = response.RangeFile('Multiple_ranges_file',
295
self.set_file_boundary()
297
def _boundary_line(self):
298
"""Helper to build the formatted boundary line."""
299
return '--' + self.boundary + '\r\n'
301
def set_file_boundary(self):
302
# Ranges are set by decoding the range headers, the RangeFile user is
303
# supposed to call the following before using seek or read since it
304
# requires knowing the *response* headers (in that case the boundary
305
# which is part of the Content-Type header).
306
self._file.set_boundary(self.boundary)
308
def _multipart_byterange(self, data, offset, boundary, file_size='*'):
309
"""Encode a part of a file as a multipart/byterange MIME type.
311
When a range request is issued, the HTTP response body can be
312
decomposed in parts, each one representing a range (start, size) in a
315
:param data: The payload.
316
:param offset: where data starts in the file
317
:param boundary: used to separate the parts
318
:param file_size: the size of the file containing the range (default to
321
:return: a string containing the data encoded as it will appear in the
324
bline = self._boundary_line()
325
# Each range begins with a boundary line
327
# A range is described by a set of headers, but only 'Content-Range' is
328
# required for our implementation (TestHandleResponse below will
329
# exercise ranges with multiple or missing headers')
330
range += 'Content-Range: bytes %d-%d/%d\r\n' % (offset,
334
# Finally the raw bytes
338
def test_read_all_ranges(self):
340
self.assertEquals(self.alpha, f.read()) # Read first range
341
f.seek(100) # Trigger the second range recognition
342
self.assertEquals(self.alpha, f.read()) # Read second range
343
self.assertEquals(126, f.tell())
344
f.seek(126) # Start of third range which is also the current pos !
345
self.assertEquals('A', f.read(1))
347
self.assertEquals('LMN', f.read(3))
349
def test_seek_from_end(self):
350
"""See TestRangeFileMixin.test_seek_from_end."""
351
# The actual implementation will seek from end for the first range only
352
# and then fail. Since seeking from end is intended to be used for a
353
# single range only anyway, this test just document the actual
357
self.assertEquals('yz', f.read())
358
self.assertRaises(errors.InvalidRange, f.seek, -2, 2)
360
def test_seek_into_void(self):
362
start = self.first_range_start
364
# Seeking to a point between two ranges is possible (only once) but
365
# reading there is forbidden
367
# We crossed a range boundary, so now the file is positioned at the
368
# start of the new range (i.e. trying to seek below 100 will error out)
372
def test_seek_across_ranges(self):
374
start = self.first_range_start
375
f.seek(126) # skip the two first ranges
376
self.assertEquals('AB', f.read(2))
378
def test_checked_read_dont_overflow_buffers(self):
380
start = self.first_range_start
381
# We force a very low value to exercise all code paths in _checked_read
382
f._discarded_buf_size = 8
383
f.seek(126) # skip the two first ranges
384
self.assertEquals('AB', f.read(2))
386
def test_seek_twice_between_ranges(self):
388
start = self.first_range_start
389
f.seek(start + 40) # Past the first range but before the second
390
# Now the file is positioned at the second range start (100)
391
self.assertRaises(errors.InvalidRange, f.seek, start + 41)
393
def test_seek_at_range_end(self):
394
"""Test seek behavior at range end."""
400
def test_read_at_range_end(self):
402
self.assertEquals(self.alpha, f.read())
403
self.assertEquals(self.alpha, f.read())
404
self.assertEquals(self.alpha.upper(), f.read())
405
self.assertRaises(errors.InvalidHttpResponse, f.read, 1)
408
class TestRangeFileMultipleRangesQuotedBoundaries(TestRangeFileMultipleRanges):
409
"""Perform the same tests as TestRangeFileMultipleRanges, but uses
410
an angle-bracket quoted boundary string like IIS 6.0 and 7.0
411
(but not IIS 5, which breaks the RFC in a different way
412
by using square brackets, not angle brackets)
414
This reveals a bug caused by
416
- The bad implementation of RFC 822 unquoting in Python (angles are not
417
quotes), coupled with
419
- The bad implementation of RFC 2046 in IIS (angles are not permitted chars
423
# The boundary as it appears in boundary lines
424
# IIS 6 and 7 use this value
425
_boundary_trimmed = "q1w2e3r4t5y6u7i8o9p0zaxscdvfbgnhmjklkl"
426
boundary = '<' + _boundary_trimmed + '>'
428
def set_file_boundary(self):
429
# Emulate broken rfc822.unquote() here by removing angles
430
self._file.set_boundary(self._boundary_trimmed)
433
class TestRangeFileVarious(tests.TestCase):
434
"""Tests RangeFile aspects not covered elsewhere."""
436
def test_seek_whence(self):
437
"""Test the seek whence parameter values."""
438
f = response.RangeFile('foo', StringIO('abc'))
243
self.assertRaises(errors.InvalidRange, f.read, 2)
245
self.assertEqual('012345', f.read(6))
247
def test_invalid(self):
249
f = response.HttpRangeResponse('http://foo', 'bytes x-10/9',
250
StringIO('0123456789'))
251
except errors.InvalidHttpRange, e:
252
self.assertContainsRe(str(e), 'http://foo')
253
self.assertContainsRe(str(e), 'bytes x-10/9')
255
self.fail('Failed to raise InvalidHttpRange')
258
class TestHttpMultipartRangeResponse(TestCase):
259
"""Test the handling of multipart range responses"""
261
def test_simple(self):
262
content = StringIO(simple_data)
263
multi = response.HttpMultipartRangeResponse('http://foo',
264
'multipart/byteranges; boundary = xxyyzz', content)
266
self.assertEqual(4, len(multi._ranges))
269
self.assertEqual('1234567890', multi.read(10))
271
self.assertEqual('abcdefghij', multi.read(10))
273
self.assertEqual('zyxwvutsrq', multi.read(10))
275
self.assertEqual('xxyyzz fbd', multi.read(10))
276
# TODO: jam 20060706 Currently RangeFile does not support
277
# reading across ranges. Consider adding it.
279
# self.assertEqual('zyxwvutsrqxxyyzz fbd', multi.read(20))
280
self.assertRaises(errors.InvalidRange, multi.read, 20)
283
self.assertRaises(errors.InvalidRange, multi.read, 11)
285
self.assertRaises(errors.InvalidRange, multi.read, 10)
287
def test_invalid(self):
288
content = StringIO('')
290
response.HttpMultipartRangeResponse('http://foo',
291
'multipart/byte;boundary=invalid', content)
292
except errors.InvalidHttpContentType, e:
293
self.assertContainsRe(str(e), 'http://foo')
294
self.assertContainsRe(str(e), 'multipart/byte;')
443
self.assertRaises(ValueError, f.seek, 0, 14)
445
def test_range_syntax(self):
446
"""Test the Content-Range scanning."""
448
f = response.RangeFile('foo', StringIO())
450
def ok(expected, header_value):
451
f.set_range_from_header(header_value)
452
# Slightly peek under the covers to get the size
453
self.assertEquals(expected, (f.tell(), f._size))
455
ok((1, 10), 'bytes 1-10/11')
456
ok((1, 10), 'bytes 1-10/*')
457
ok((12, 2), '\tbytes 12-13/*')
458
ok((28, 1), ' bytes 28-28/*')
459
ok((2123, 2120), 'bytes 2123-4242/12310')
460
ok((1, 10), 'bytes 1-10/ttt') # We don't check total (ttt)
462
def nok(header_value):
463
self.assertRaises(errors.InvalidHttpRange,
464
f.set_range_from_header, header_value)
468
nok('bytes xx-yyy/zzz')
469
nok('bytes xx-12/zzz')
470
nok('bytes 11-yy/zzz')
297
474
# Taken from real request responses
749
def test_multi_squid_range(self):
750
out = self.get_response(_multipart_squid_range_response)
752
# Just make sure we can read the right contents
497
759
def test_invalid_response(self):
498
760
self.assertRaises(errors.InvalidHttpResponse,
499
self.get_response, _invalid_response)
761
self.get_response, _invalid_response)
501
763
def test_full_text_no_content_type(self):
502
764
# We should not require Content-Type for a full response
503
a_response = _full_text_response
504
headers = http._extract_headers(StringIO(a_response[1]))
505
del headers['Content-Type']
506
out = response.handle_response('http://foo', a_response[0], headers,
507
StringIO(a_response[2]))
508
self.assertEqual(_full_text_response[2], out.read())
510
def test_missing_no_content_type(self):
511
# Without Content-Type we should still raise NoSuchFile on a 404
512
a_response = _missing_response
513
headers = http._extract_headers(StringIO(a_response[1]))
514
del headers['Content-Type']
515
self.assertRaises(errors.NoSuchFile,
516
response.handle_response, 'http://missing', a_response[0], headers,
517
StringIO(a_response[2]))
519
def test_missing_content_type(self):
520
a_response = _single_range_response
521
headers = http._extract_headers(StringIO(a_response[1]))
522
del headers['Content-Type']
523
self.assertRaises(errors.InvalidHttpContentType,
524
response.handle_response, 'http://nocontent', a_response[0],
525
headers, StringIO(a_response[2]))
765
code, raw_headers, body = _full_text_response_no_content_type
766
msg = self._build_HTTPMessage(raw_headers)
767
out = response.handle_response('http://foo', code, msg, StringIO(body))
768
self.assertEqual(body, out.read())
770
def test_full_text_no_content_length(self):
771
code, raw_headers, body = _full_text_response_no_content_length
772
msg = self._build_HTTPMessage(raw_headers)
773
out = response.handle_response('http://foo', code, msg, StringIO(body))
774
self.assertEqual(body, out.read())
527
776
def test_missing_content_range(self):
528
a_response = _single_range_response
529
headers = http._extract_headers(StringIO(a_response[1]))
530
del headers['Content-Range']
531
self.assertRaises(errors.InvalidHttpResponse,
532
response.handle_response, 'http://nocontent', a_response[0],
533
headers, StringIO(a_response[2]))
777
code, raw_headers, body = _single_range_no_content_range
778
msg = self._build_HTTPMessage(raw_headers)
779
self.assertRaises(errors.InvalidHttpResponse,
780
response.handle_response,
781
'http://bogus', code, msg, StringIO(body))
783
def test_multipart_no_content_range(self):
784
code, raw_headers, body = _multipart_no_content_range
785
msg = self._build_HTTPMessage(raw_headers)
786
self.assertRaises(errors.InvalidHttpResponse,
787
response.handle_response,
788
'http://bogus', code, msg, StringIO(body))
790
def test_multipart_no_boundary(self):
791
out = self.get_response(_multipart_no_boundary)
792
out.read() # Read the whole range
793
# Fail to find the boundary line
794
self.assertRaises(errors.InvalidHttpResponse, out.seek, 1, 1)
797
class TestRangeFileSizeReadLimited(tests.TestCase):
798
"""Test RangeFile _max_read_size functionality which limits the size of
799
read blocks to prevent MemoryError messages in socket.recv.
803
super(TestRangeFileSizeReadLimited, self).setUp()
804
# create a test datablock larger than _max_read_size.
805
chunk_size = response.RangeFile._max_read_size
806
test_pattern = '0123456789ABCDEF'
807
self.test_data = test_pattern * (3 * chunk_size / len(test_pattern))
808
self.test_data_len = len(self.test_data)
810
def test_max_read_size(self):
811
"""Read data in blocks and verify that the reads are not larger than
812
the maximum read size.
814
# retrieve data in large blocks from response.RangeFile object
815
mock_read_file = FakeReadFile(self.test_data)
816
range_file = response.RangeFile('test_max_read_size', mock_read_file)
817
response_data = range_file.read(self.test_data_len)
819
# verify read size was equal to the maximum read size
820
self.assertTrue(mock_read_file.get_max_read_size() > 0)
821
self.assertEqual(mock_read_file.get_max_read_size(),
822
response.RangeFile._max_read_size)
823
self.assertEqual(mock_read_file.get_read_count(), 3)
825
# report error if the data wasn't equal (we only report the size due
826
# to the length of the data)
827
if response_data != self.test_data:
828
message = "Data not equal. Expected %d bytes, received %d."
829
self.fail(message % (len(response_data), self.test_data_len))