1
# Copyright (C) 2006-2010, 2012, 2013, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
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
41
import http.client as http_client
42
except ImportError: # python < 3
43
import httplib as http_client
49
from ..sixish import (
52
from ..transport.http import (
56
from .file_utils import (
61
class ReadSocket(object):
62
"""A socket-like object that can be given a predefined content."""
64
def __init__(self, data):
65
self.readfile = BytesIO(data)
67
def makefile(self, mode='r', bufsize=None):
71
class FakeHTTPConnection(_urllib2_wrappers.HTTPConnection):
73
def __init__(self, sock):
74
_urllib2_wrappers.HTTPConnection.__init__(self, 'localhost')
75
# Set the socket to bypass the connection
79
"""Ignores the writes on the socket."""
83
class TestResponseFileIter(tests.TestCase):
85
def test_iter_empty(self):
86
f = response.ResponseFile('empty', BytesIO())
87
self.assertEqual([], list(f))
89
def test_iter_many(self):
90
f = response.ResponseFile('many', BytesIO(b'0\n1\nboo!\n'))
91
self.assertEqual(['0\n', '1\n', 'boo!\n'], list(f))
94
class TestHTTPConnection(tests.TestCase):
96
def test_cleanup_pipe(self):
97
sock = ReadSocket("""HTTP/1.1 200 OK\r
98
Content-Type: text/plain; charset=UTF-8\r
103
conn = FakeHTTPConnection(sock)
104
# Simulate the request sending so that the connection will be able to
106
conn.putrequest('GET', 'http://localhost/fictious')
108
# Now, get the response
109
resp = conn.getresponse()
110
# Read part of the response
111
self.assertEqual('0123456789\n', resp.read(11))
112
# Override the thresold to force the warning emission
113
conn._range_warning_thresold = 6 # There are 7 bytes pending
115
self.assertContainsRe(self.get_log(), 'Got a 200 response when asking')
118
class TestRangeFileMixin(object):
119
"""Tests for accessing the first range in a RangeFile."""
121
# A simple string used to represent a file part (also called a range), in
122
# which offsets are easy to calculate for test writers. It's used as a
123
# building block with slight variations but basically 'a' is the first char
124
# of the range and 'z' is the last.
125
alpha = 'abcdefghijklmnopqrstuvwxyz'
127
def test_can_read_at_first_access(self):
128
"""Test that the just created file can be read."""
129
self.assertEqual(self.alpha, self._file.read())
131
def test_seek_read(self):
132
"""Test seek/read inside the range."""
134
start = self.first_range_start
135
# Before any use, tell() should be at the range start
136
self.assertEqual(start, f.tell())
137
cur = start # For an overall offset assertion
140
self.assertEqual('def', f.read(3))
144
self.assertEqual('klmn', f.read(4))
146
# read(0) in the middle of a range
147
self.assertEqual('', f.read(0))
151
self.assertEqual(here, f.tell())
152
self.assertEqual(cur, f.tell())
154
def test_read_zero(self):
156
self.assertEqual('', f.read(0))
158
self.assertEqual('', f.read(0))
160
def test_seek_at_range_end(self):
164
def test_read_at_range_end(self):
165
"""Test read behaviour at range end."""
167
self.assertEqual(self.alpha, f.read())
168
self.assertEqual('', f.read(0))
169
self.assertRaises(errors.InvalidRange, f.read, 1)
171
def test_unbounded_read_after_seek(self):
174
# Should not cross ranges
175
self.assertEqual('yz', f.read())
177
def test_seek_backwards(self):
179
start = self.first_range_start
182
self.assertRaises(errors.InvalidRange, f.seek, start + 5)
184
def test_seek_outside_single_range(self):
186
if f._size == -1 or f._boundary is not None:
187
raise tests.TestNotApplicable('Needs a fully defined range')
188
# Will seek past the range and then errors out
189
self.assertRaises(errors.InvalidRange,
190
f.seek, self.first_range_start + 27)
192
def test_read_past_end_of_range(self):
195
raise tests.TestNotApplicable("Can't check an unknown size")
196
start = self.first_range_start
198
self.assertRaises(errors.InvalidRange, f.read, 10)
200
def test_seek_from_end(self):
201
"""Test seeking from the end of the file.
203
The semantic is unclear in case of multiple ranges. Seeking from end
204
exists only for the http transports, cannot be used if the file size is
205
unknown and is not used in breezy itself. This test must be (and is)
206
overridden by daughter classes.
208
Reading from end makes sense only when a range has been requested from
209
the end of the file (see HttpTransportBase._get() when using the
210
'tail_amount' parameter). The HTTP response can only be a whole file or
215
self.assertEqual('yz', f.read())
218
class TestRangeFileSizeUnknown(tests.TestCase, TestRangeFileMixin):
219
"""Test a RangeFile for a whole file whose size is not known."""
222
super(TestRangeFileSizeUnknown, self).setUp()
223
self._file = response.RangeFile('Whole_file_size_known',
225
# We define no range, relying on RangeFile to provide default values
226
self.first_range_start = 0 # It's the whole file
228
def test_seek_from_end(self):
229
"""See TestRangeFileMixin.test_seek_from_end.
231
The end of the file can't be determined since the size is unknown.
233
self.assertRaises(errors.InvalidRange, self._file.seek, -1, 2)
235
def test_read_at_range_end(self):
236
"""Test read behaviour at range end."""
238
self.assertEqual(self.alpha, f.read())
239
self.assertEqual('', f.read(0))
240
self.assertEqual('', f.read(1))
243
class TestRangeFileSizeKnown(tests.TestCase, TestRangeFileMixin):
244
"""Test a RangeFile for a whole file whose size is known."""
247
super(TestRangeFileSizeKnown, self).setUp()
248
self._file = response.RangeFile('Whole_file_size_known',
250
self._file.set_range(0, len(self.alpha))
251
self.first_range_start = 0 # It's the whole file
254
class TestRangeFileSingleRange(tests.TestCase, TestRangeFileMixin):
255
"""Test a RangeFile for a single range."""
258
super(TestRangeFileSingleRange, self).setUp()
259
self._file = response.RangeFile('Single_range_file',
261
self.first_range_start = 15
262
self._file.set_range(self.first_range_start, len(self.alpha))
265
def test_read_before_range(self):
266
# This can't occur under normal circumstances, we have to force it
268
f._pos = 0 # Force an invalid pos
269
self.assertRaises(errors.InvalidRange, f.read, 2)
272
class TestRangeFileMultipleRanges(tests.TestCase, TestRangeFileMixin):
273
"""Test a RangeFile for multiple ranges.
275
The RangeFile used for the tests contains three ranges:
277
- at offset 25: alpha
278
- at offset 100: alpha
279
- at offset 126: alpha.upper()
281
The two last ranges are contiguous. This only rarely occurs (should not in
282
fact) in real uses but may lead to hard to track bugs.
285
# The following is used to represent the boundary paramter defined
286
# in HTTP response headers and the boundary lines that separate
289
boundary = "separation"
292
super(TestRangeFileMultipleRanges, self).setUp()
294
boundary = self.boundary
297
self.first_range_start = 25
298
file_size = 200 # big enough to encompass all ranges
299
for (start, part) in [(self.first_range_start, self.alpha),
300
# Two contiguous ranges
302
(126, self.alpha.upper())]:
303
content += self._multipart_byterange(part, start, boundary,
306
content += self._boundary_line()
308
self._file = response.RangeFile('Multiple_ranges_file',
310
self.set_file_boundary()
312
def _boundary_line(self):
313
"""Helper to build the formatted boundary line."""
314
return '--' + self.boundary + '\r\n'
316
def set_file_boundary(self):
317
# Ranges are set by decoding the range headers, the RangeFile user is
318
# supposed to call the following before using seek or read since it
319
# requires knowing the *response* headers (in that case the boundary
320
# which is part of the Content-Type header).
321
self._file.set_boundary(self.boundary)
323
def _multipart_byterange(self, data, offset, boundary, file_size='*'):
324
"""Encode a part of a file as a multipart/byterange MIME type.
326
When a range request is issued, the HTTP response body can be
327
decomposed in parts, each one representing a range (start, size) in a
330
:param data: The payload.
331
:param offset: where data starts in the file
332
:param boundary: used to separate the parts
333
:param file_size: the size of the file containing the range (default to
336
:return: a string containing the data encoded as it will appear in the
339
bline = self._boundary_line()
340
# Each range begins with a boundary line
342
# A range is described by a set of headers, but only 'Content-Range' is
343
# required for our implementation (TestHandleResponse below will
344
# exercise ranges with multiple or missing headers')
345
range += 'Content-Range: bytes %d-%d/%d\r\n' % (offset,
349
# Finally the raw bytes
353
def test_read_all_ranges(self):
355
self.assertEqual(self.alpha, f.read()) # Read first range
356
f.seek(100) # Trigger the second range recognition
357
self.assertEqual(self.alpha, f.read()) # Read second range
358
self.assertEqual(126, f.tell())
359
f.seek(126) # Start of third range which is also the current pos !
360
self.assertEqual('A', f.read(1))
362
self.assertEqual('LMN', f.read(3))
364
def test_seek_from_end(self):
365
"""See TestRangeFileMixin.test_seek_from_end."""
366
# The actual implementation will seek from end for the first range only
367
# and then fail. Since seeking from end is intended to be used for a
368
# single range only anyway, this test just document the actual
372
self.assertEqual('yz', f.read())
373
self.assertRaises(errors.InvalidRange, f.seek, -2, 2)
375
def test_seek_into_void(self):
377
start = self.first_range_start
379
# Seeking to a point between two ranges is possible (only once) but
380
# reading there is forbidden
382
# We crossed a range boundary, so now the file is positioned at the
383
# start of the new range (i.e. trying to seek below 100 will error out)
387
def test_seek_across_ranges(self):
389
f.seek(126) # skip the two first ranges
390
self.assertEqual('AB', f.read(2))
392
def test_checked_read_dont_overflow_buffers(self):
394
# We force a very low value to exercise all code paths in _checked_read
395
f._discarded_buf_size = 8
396
f.seek(126) # skip the two first ranges
397
self.assertEqual('AB', f.read(2))
399
def test_seek_twice_between_ranges(self):
401
start = self.first_range_start
402
f.seek(start + 40) # Past the first range but before the second
403
# Now the file is positioned at the second range start (100)
404
self.assertRaises(errors.InvalidRange, f.seek, start + 41)
406
def test_seek_at_range_end(self):
407
"""Test seek behavior at range end."""
413
def test_read_at_range_end(self):
415
self.assertEqual(self.alpha, f.read())
416
self.assertEqual(self.alpha, f.read())
417
self.assertEqual(self.alpha.upper(), f.read())
418
self.assertRaises(errors.InvalidHttpResponse, f.read, 1)
421
class TestRangeFileMultipleRangesQuotedBoundaries(TestRangeFileMultipleRanges):
422
"""Perform the same tests as TestRangeFileMultipleRanges, but uses
423
an angle-bracket quoted boundary string like IIS 6.0 and 7.0
424
(but not IIS 5, which breaks the RFC in a different way
425
by using square brackets, not angle brackets)
427
This reveals a bug caused by
429
- The bad implementation of RFC 822 unquoting in Python (angles are not
430
quotes), coupled with
432
- The bad implementation of RFC 2046 in IIS (angles are not permitted chars
436
# The boundary as it appears in boundary lines
437
# IIS 6 and 7 use this value
438
_boundary_trimmed = "q1w2e3r4t5y6u7i8o9p0zaxscdvfbgnhmjklkl"
439
boundary = '<' + _boundary_trimmed + '>'
441
def set_file_boundary(self):
442
# Emulate broken rfc822.unquote() here by removing angles
443
self._file.set_boundary(self._boundary_trimmed)
446
class TestRangeFileVarious(tests.TestCase):
447
"""Tests RangeFile aspects not covered elsewhere."""
449
def test_seek_whence(self):
450
"""Test the seek whence parameter values."""
451
f = response.RangeFile('foo', BytesIO(b'abc'))
456
self.assertRaises(ValueError, f.seek, 0, 14)
458
def test_range_syntax(self):
459
"""Test the Content-Range scanning."""
461
f = response.RangeFile('foo', BytesIO())
463
def ok(expected, header_value):
464
f.set_range_from_header(header_value)
465
# Slightly peek under the covers to get the size
466
self.assertEqual(expected, (f.tell(), f._size))
468
ok((1, 10), 'bytes 1-10/11')
469
ok((1, 10), 'bytes 1-10/*')
470
ok((12, 2), '\tbytes 12-13/*')
471
ok((28, 1), ' bytes 28-28/*')
472
ok((2123, 2120), 'bytes 2123-4242/12310')
473
ok((1, 10), 'bytes 1-10/ttt') # We don't check total (ttt)
475
def nok(header_value):
476
self.assertRaises(errors.InvalidHttpRange,
477
f.set_range_from_header, header_value)
481
nok('bytes xx-yyy/zzz')
482
nok('bytes xx-12/zzz')
483
nok('bytes 11-yy/zzz')
487
# Taken from real request responses
488
_full_text_response = (200, """HTTP/1.1 200 OK\r
489
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
490
Server: Apache/2.0.54 (Fedora)\r
491
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
492
ETag: "56691-23-38e9ae00"\r
493
Accept-Ranges: bytes\r
496
Content-Type: text/plain; charset=UTF-8\r
498
""", """Bazaar-NG meta directory, format 1
502
_single_range_response = (206, """HTTP/1.1 206 Partial Content\r
503
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
504
Server: Apache/2.0.54 (Fedora)\r
505
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
506
ETag: "238a3c-16ec2-805c5540"\r
507
Accept-Ranges: bytes\r
508
Content-Length: 100\r
509
Content-Range: bytes 100-199/93890\r
511
Content-Type: text/plain; charset=UTF-8\r
513
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
514
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
517
_single_range_no_content_type = (206, """HTTP/1.1 206 Partial Content\r
518
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
519
Server: Apache/2.0.54 (Fedora)\r
520
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
521
ETag: "238a3c-16ec2-805c5540"\r
522
Accept-Ranges: bytes\r
523
Content-Length: 100\r
524
Content-Range: bytes 100-199/93890\r
527
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
528
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
531
_multipart_range_response = (206, """HTTP/1.1 206 Partial Content\r
532
Date: Tue, 11 Jul 2006 04:49:48 GMT\r
533
Server: Apache/2.0.54 (Fedora)\r
534
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
535
ETag: "238a3c-16ec2-805c5540"\r
536
Accept-Ranges: bytes\r
537
Content-Length: 1534\r
539
Content-Type: multipart/byteranges; boundary=418470f848b63279b\r
541
\r""", """--418470f848b63279b\r
542
Content-type: text/plain; charset=UTF-8\r
543
Content-range: bytes 0-254/93890\r
545
mbp@sourcefrog.net-20050309040815-13242001617e4a06
546
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e7627
547
mbp@sourcefrog.net-20050309040957-6cad07f466bb0bb8
548
mbp@sourcefrog.net-20050309041501-c840e09071de3b67
549
mbp@sourcefrog.net-20050309044615-c24a3250be83220a
551
--418470f848b63279b\r
552
Content-type: text/plain; charset=UTF-8\r
553
Content-range: bytes 1000-2049/93890\r
556
mbp@sourcefrog.net-20050311063625-07858525021f270b
557
mbp@sourcefrog.net-20050311231934-aa3776aff5200bb9
558
mbp@sourcefrog.net-20050311231953-73aeb3a131c3699a
559
mbp@sourcefrog.net-20050311232353-f5e33da490872c6a
560
mbp@sourcefrog.net-20050312071639-0a8f59a34a024ff0
561
mbp@sourcefrog.net-20050312073432-b2c16a55e0d6e9fb
562
mbp@sourcefrog.net-20050312073831-a47c3335ece1920f
563
mbp@sourcefrog.net-20050312085412-13373aa129ccbad3
564
mbp@sourcefrog.net-20050313052251-2bf004cb96b39933
565
mbp@sourcefrog.net-20050313052856-3edd84094687cb11
566
mbp@sourcefrog.net-20050313053233-e30a4f28aef48f9d
567
mbp@sourcefrog.net-20050313053853-7c64085594ff3072
568
mbp@sourcefrog.net-20050313054757-a86c3f5871069e22
569
mbp@sourcefrog.net-20050313061422-418f1f73b94879b9
570
mbp@sourcefrog.net-20050313120651-497bd231b19df600
571
mbp@sourcefrog.net-20050314024931-eae0170ef25a5d1a
572
mbp@sourcefrog.net-20050314025438-d52099f915fe65fc
573
mbp@sourcefrog.net-20050314025539-637a636692c055cf
574
mbp@sourcefrog.net-20050314025737-55eb441f430ab4ba
575
mbp@sourcefrog.net-20050314025901-d74aa93bb7ee8f62
577
--418470f848b63279b--\r
581
_multipart_squid_range_response = (206, """HTTP/1.0 206 Partial Content\r
582
Date: Thu, 31 Aug 2006 21:16:22 GMT\r
583
Server: Apache/2.2.2 (Unix) DAV/2\r
584
Last-Modified: Thu, 31 Aug 2006 17:57:06 GMT\r
585
Accept-Ranges: bytes\r
586
Content-Type: multipart/byteranges; boundary="squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196"\r
587
Content-Length: 598\r
588
X-Cache: MISS from localhost.localdomain\r
589
X-Cache-Lookup: HIT from localhost.localdomain:3128\r
590
Proxy-Connection: keep-alive\r
594
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
595
Content-Type: text/plain\r
596
Content-Range: bytes 0-99/18672\r
600
scott@netsplit.com-20050708230047-47c7868f276b939f fulltext 0 863 :
602
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
603
Content-Type: text/plain\r
604
Content-Range: bytes 300-499/18672\r
606
com-20050708231537-2b124b835395399a :
607
scott@netsplit.com-20050820234126-551311dbb7435b51 line-delta 1803 479 .scott@netsplit.com-20050820232911-dc4322a084eadf7e :
608
scott@netsplit.com-20050821213706-c86\r
609
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196--\r
614
_full_text_response_no_content_type = (200, """HTTP/1.1 200 OK\r
615
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
616
Server: Apache/2.0.54 (Fedora)\r
617
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
618
ETag: "56691-23-38e9ae00"\r
619
Accept-Ranges: bytes\r
623
""", """Bazaar-NG meta directory, format 1
627
_full_text_response_no_content_length = (200, """HTTP/1.1 200 OK\r
628
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
629
Server: Apache/2.0.54 (Fedora)\r
630
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
631
ETag: "56691-23-38e9ae00"\r
632
Accept-Ranges: bytes\r
634
Content-Type: text/plain; charset=UTF-8\r
636
""", """Bazaar-NG meta directory, format 1
640
_single_range_no_content_range = (206, """HTTP/1.1 206 Partial Content\r
641
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
642
Server: Apache/2.0.54 (Fedora)\r
643
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
644
ETag: "238a3c-16ec2-805c5540"\r
645
Accept-Ranges: bytes\r
646
Content-Length: 100\r
649
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
650
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
653
_single_range_response_truncated = (206, """HTTP/1.1 206 Partial Content\r
654
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
655
Server: Apache/2.0.54 (Fedora)\r
656
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
657
ETag: "238a3c-16ec2-805c5540"\r
658
Accept-Ranges: bytes\r
659
Content-Length: 100\r
660
Content-Range: bytes 100-199/93890\r
662
Content-Type: text/plain; charset=UTF-8\r
664
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06""")
667
_invalid_response = (444, """HTTP/1.1 444 Bad Response\r
668
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
670
Content-Type: text/html; charset=iso-8859-1\r
672
""", """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
674
<title>404 Not Found</title>
677
<p>I don't know what I'm doing</p>
683
_multipart_no_content_range = (206, """HTTP/1.0 206 Partial Content\r
684
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
685
Content-Length: 598\r
690
Content-Type: text/plain\r
697
_multipart_no_boundary = (206, """HTTP/1.0 206 Partial Content\r
698
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
699
Content-Length: 598\r
704
Content-Type: text/plain\r
705
Content-Range: bytes 0-18/18672\r
709
The range ended at the line above, this text is garbage instead of a boundary
714
class TestHandleResponse(tests.TestCase):
716
def _build_HTTPMessage(self, raw_headers):
717
status_and_headers = BytesIO(raw_headers)
718
# Get rid of the status line
719
status_and_headers.readline()
720
msg = http_client.HTTPMessage(status_and_headers)
723
def get_response(self, a_response):
724
"""Process a supplied response, and return the result."""
725
code, raw_headers, body = a_response
726
msg = self._build_HTTPMessage(raw_headers)
727
return response.handle_response('http://foo', code, msg,
728
BytesIO(a_response[2]))
730
def test_full_text(self):
731
out = self.get_response(_full_text_response)
732
# It is a BytesIO from the original data
733
self.assertEqual(_full_text_response[2], out.read())
735
def test_single_range(self):
736
out = self.get_response(_single_range_response)
739
self.assertEqual(_single_range_response[2], out.read(100))
741
def test_single_range_no_content(self):
742
out = self.get_response(_single_range_no_content_type)
745
self.assertEqual(_single_range_no_content_type[2], out.read(100))
747
def test_single_range_truncated(self):
748
out = self.get_response(_single_range_response_truncated)
749
# Content-Range declares 100 but only 51 present
750
self.assertRaises(errors.ShortReadvError, out.seek, out.tell() + 51)
752
def test_multi_range(self):
753
out = self.get_response(_multipart_range_response)
755
# Just make sure we can read the right contents
762
def test_multi_squid_range(self):
763
out = self.get_response(_multipart_squid_range_response)
765
# Just make sure we can read the right contents
772
def test_invalid_response(self):
773
self.assertRaises(errors.InvalidHttpResponse,
774
self.get_response, _invalid_response)
776
def test_full_text_no_content_type(self):
777
# We should not require Content-Type for a full response
778
code, raw_headers, body = _full_text_response_no_content_type
779
msg = self._build_HTTPMessage(raw_headers)
780
out = response.handle_response('http://foo', code, msg, BytesIO(body))
781
self.assertEqual(body, out.read())
783
def test_full_text_no_content_length(self):
784
code, raw_headers, body = _full_text_response_no_content_length
785
msg = self._build_HTTPMessage(raw_headers)
786
out = response.handle_response('http://foo', code, msg, BytesIO(body))
787
self.assertEqual(body, out.read())
789
def test_missing_content_range(self):
790
code, raw_headers, body = _single_range_no_content_range
791
msg = self._build_HTTPMessage(raw_headers)
792
self.assertRaises(errors.InvalidHttpResponse,
793
response.handle_response,
794
'http://bogus', code, msg, BytesIO(body))
796
def test_multipart_no_content_range(self):
797
code, raw_headers, body = _multipart_no_content_range
798
msg = self._build_HTTPMessage(raw_headers)
799
self.assertRaises(errors.InvalidHttpResponse,
800
response.handle_response,
801
'http://bogus', code, msg, BytesIO(body))
803
def test_multipart_no_boundary(self):
804
out = self.get_response(_multipart_no_boundary)
805
out.read() # Read the whole range
806
# Fail to find the boundary line
807
self.assertRaises(errors.InvalidHttpResponse, out.seek, 1, 1)
810
class TestRangeFileSizeReadLimited(tests.TestCase):
811
"""Test RangeFile _max_read_size functionality which limits the size of
812
read blocks to prevent MemoryError messages in socket.recv.
816
super(TestRangeFileSizeReadLimited, self).setUp()
817
# create a test datablock larger than _max_read_size.
818
chunk_size = response.RangeFile._max_read_size
819
test_pattern = '0123456789ABCDEF'
820
self.test_data = test_pattern * (3 * chunk_size / len(test_pattern))
821
self.test_data_len = len(self.test_data)
823
def test_max_read_size(self):
824
"""Read data in blocks and verify that the reads are not larger than
825
the maximum read size.
827
# retrieve data in large blocks from response.RangeFile object
828
mock_read_file = FakeReadFile(self.test_data)
829
range_file = response.RangeFile('test_max_read_size', mock_read_file)
830
response_data = range_file.read(self.test_data_len)
832
# verify read size was equal to the maximum read size
833
self.assertTrue(mock_read_file.get_max_read_size() > 0)
834
self.assertEqual(mock_read_file.get_max_read_size(),
835
response.RangeFile._max_read_size)
836
self.assertEqual(mock_read_file.get_read_count(), 3)
838
# report error if the data wasn't equal (we only report the size due
839
# to the length of the data)
840
if response_data != self.test_data:
841
message = "Data not equal. Expected %d bytes, received %d."
842
self.fail(message % (len(response_data), self.test_data_len))