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 without future
43
import httplib as http_client
46
parse_headers = http_client.parse_headers
47
except AttributeError: # python 2
48
parse_headers = http_client.HTTPMessage
54
from ..sixish import (
58
from ..transport.http import (
62
from .file_utils import (
67
class ReadSocket(object):
68
"""A socket-like object that can be given a predefined content."""
70
def __init__(self, data):
71
self.readfile = BytesIO(data)
73
def makefile(self, mode='r', bufsize=None):
77
class FakeHTTPConnection(HTTPConnection):
79
def __init__(self, sock):
80
HTTPConnection.__init__(self, 'localhost')
81
# Set the socket to bypass the connection
85
"""Ignores the writes on the socket."""
89
class TestResponseFileIter(tests.TestCase):
91
def test_iter_empty(self):
92
f = response.ResponseFile('empty', BytesIO())
93
self.assertEqual([], list(f))
95
def test_iter_many(self):
96
f = response.ResponseFile('many', BytesIO(b'0\n1\nboo!\n'))
97
self.assertEqual([b'0\n', b'1\n', b'boo!\n'], list(f))
100
class TestHTTPConnection(tests.TestCase):
102
def test_cleanup_pipe(self):
103
sock = ReadSocket(b"""HTTP/1.1 200 OK\r
104
Content-Type: text/plain; charset=UTF-8\r
109
conn = FakeHTTPConnection(sock)
110
# Simulate the request sending so that the connection will be able to
112
conn.putrequest('GET', 'http://localhost/fictious')
114
# Now, get the response
115
resp = conn.getresponse()
116
# Read part of the response
117
self.assertEqual(b'0123456789\n', resp.read(11))
118
# Override the thresold to force the warning emission
119
conn._range_warning_thresold = 6 # There are 7 bytes pending
121
self.assertContainsRe(self.get_log(), 'Got a 200 response when asking')
124
class TestRangeFileMixin(object):
125
"""Tests for accessing the first range in a RangeFile."""
127
# A simple string used to represent a file part (also called a range), in
128
# which offsets are easy to calculate for test writers. It's used as a
129
# building block with slight variations but basically 'a' is the first char
130
# of the range and 'z' is the last.
131
alpha = b'abcdefghijklmnopqrstuvwxyz'
133
def test_can_read_at_first_access(self):
134
"""Test that the just created file can be read."""
135
self.assertEqual(self.alpha, self._file.read())
137
def test_seek_read(self):
138
"""Test seek/read inside the range."""
140
start = self.first_range_start
141
# Before any use, tell() should be at the range start
142
self.assertEqual(start, f.tell())
143
cur = start # For an overall offset assertion
146
self.assertEqual(b'def', f.read(3))
150
self.assertEqual(b'klmn', f.read(4))
152
# read(0) in the middle of a range
153
self.assertEqual(b'', f.read(0))
157
self.assertEqual(here, f.tell())
158
self.assertEqual(cur, f.tell())
160
def test_read_zero(self):
162
self.assertEqual(b'', f.read(0))
164
self.assertEqual(b'', f.read(0))
166
def test_seek_at_range_end(self):
170
def test_read_at_range_end(self):
171
"""Test read behaviour at range end."""
173
self.assertEqual(self.alpha, f.read())
174
self.assertEqual(b'', f.read(0))
175
self.assertRaises(errors.InvalidRange, f.read, 1)
177
def test_unbounded_read_after_seek(self):
180
# Should not cross ranges
181
self.assertEqual(b'yz', f.read())
183
def test_seek_backwards(self):
185
start = self.first_range_start
188
self.assertRaises(errors.InvalidRange, f.seek, start + 5)
190
def test_seek_outside_single_range(self):
192
if f._size == -1 or f._boundary is not None:
193
raise tests.TestNotApplicable('Needs a fully defined range')
194
# Will seek past the range and then errors out
195
self.assertRaises(errors.InvalidRange,
196
f.seek, self.first_range_start + 27)
198
def test_read_past_end_of_range(self):
201
raise tests.TestNotApplicable("Can't check an unknown size")
202
start = self.first_range_start
204
self.assertRaises(errors.InvalidRange, f.read, 10)
206
def test_seek_from_end(self):
207
"""Test seeking from the end of the file.
209
The semantic is unclear in case of multiple ranges. Seeking from end
210
exists only for the http transports, cannot be used if the file size is
211
unknown and is not used in breezy itself. This test must be (and is)
212
overridden by daughter classes.
214
Reading from end makes sense only when a range has been requested from
215
the end of the file (see HttpTransportBase._get() when using the
216
'tail_amount' parameter). The HTTP response can only be a whole file or
221
self.assertEqual(b'yz', f.read())
224
class TestRangeFileSizeUnknown(tests.TestCase, TestRangeFileMixin):
225
"""Test a RangeFile for a whole file whose size is not known."""
228
super(TestRangeFileSizeUnknown, self).setUp()
229
self._file = response.RangeFile('Whole_file_size_known',
231
# We define no range, relying on RangeFile to provide default values
232
self.first_range_start = 0 # It's the whole file
234
def test_seek_from_end(self):
235
"""See TestRangeFileMixin.test_seek_from_end.
237
The end of the file can't be determined since the size is unknown.
239
self.assertRaises(errors.InvalidRange, self._file.seek, -1, 2)
241
def test_read_at_range_end(self):
242
"""Test read behaviour at range end."""
244
self.assertEqual(self.alpha, f.read())
245
self.assertEqual(b'', f.read(0))
246
self.assertEqual(b'', f.read(1))
249
class TestRangeFileSizeKnown(tests.TestCase, TestRangeFileMixin):
250
"""Test a RangeFile for a whole file whose size is known."""
253
super(TestRangeFileSizeKnown, self).setUp()
254
self._file = response.RangeFile('Whole_file_size_known',
256
self._file.set_range(0, len(self.alpha))
257
self.first_range_start = 0 # It's the whole file
260
class TestRangeFileSingleRange(tests.TestCase, TestRangeFileMixin):
261
"""Test a RangeFile for a single range."""
264
super(TestRangeFileSingleRange, self).setUp()
265
self._file = response.RangeFile('Single_range_file',
267
self.first_range_start = 15
268
self._file.set_range(self.first_range_start, len(self.alpha))
270
def test_read_before_range(self):
271
# This can't occur under normal circumstances, we have to force it
273
f._pos = 0 # Force an invalid pos
274
self.assertRaises(errors.InvalidRange, f.read, 2)
277
class TestRangeFileMultipleRanges(tests.TestCase, TestRangeFileMixin):
278
"""Test a RangeFile for multiple ranges.
280
The RangeFile used for the tests contains three ranges:
282
- at offset 25: alpha
283
- at offset 100: alpha
284
- at offset 126: alpha.upper()
286
The two last ranges are contiguous. This only rarely occurs (should not in
287
fact) in real uses but may lead to hard to track bugs.
290
# The following is used to represent the boundary paramter defined
291
# in HTTP response headers and the boundary lines that separate
294
boundary = b"separation"
297
super(TestRangeFileMultipleRanges, self).setUp()
299
boundary = self.boundary
302
self.first_range_start = 25
303
file_size = 200 # big enough to encompass all ranges
304
for (start, part) in [(self.first_range_start, self.alpha),
305
# Two contiguous ranges
307
(126, self.alpha.upper())]:
308
content += self._multipart_byterange(part, start, boundary,
311
content += self._boundary_line()
313
self._file = response.RangeFile('Multiple_ranges_file',
315
self.set_file_boundary()
317
def _boundary_line(self):
318
"""Helper to build the formatted boundary line."""
319
return b'--' + self.boundary + b'\r\n'
321
def set_file_boundary(self):
322
# Ranges are set by decoding the range headers, the RangeFile user is
323
# supposed to call the following before using seek or read since it
324
# requires knowing the *response* headers (in that case the boundary
325
# which is part of the Content-Type header).
326
self._file.set_boundary(self.boundary)
328
def _multipart_byterange(self, data, offset, boundary, file_size=b'*'):
329
"""Encode a part of a file as a multipart/byterange MIME type.
331
When a range request is issued, the HTTP response body can be
332
decomposed in parts, each one representing a range (start, size) in a
335
:param data: The payload.
336
:param offset: where data starts in the file
337
:param boundary: used to separate the parts
338
:param file_size: the size of the file containing the range (default to
341
:return: a string containing the data encoded as it will appear in the
344
bline = self._boundary_line()
345
# Each range begins with a boundary line
347
# A range is described by a set of headers, but only 'Content-Range' is
348
# required for our implementation (TestHandleResponse below will
349
# exercise ranges with multiple or missing headers')
350
if isinstance(file_size, int):
351
file_size = b'%d' % file_size
352
range += b'Content-Range: bytes %d-%d/%s\r\n' % (offset,
357
# Finally the raw bytes
361
def test_read_all_ranges(self):
363
self.assertEqual(self.alpha, f.read()) # Read first range
364
f.seek(100) # Trigger the second range recognition
365
self.assertEqual(self.alpha, f.read()) # Read second range
366
self.assertEqual(126, f.tell())
367
f.seek(126) # Start of third range which is also the current pos !
368
self.assertEqual(b'A', f.read(1))
370
self.assertEqual(b'LMN', f.read(3))
372
def test_seek_from_end(self):
373
"""See TestRangeFileMixin.test_seek_from_end."""
374
# The actual implementation will seek from end for the first range only
375
# and then fail. Since seeking from end is intended to be used for a
376
# single range only anyway, this test just document the actual
380
self.assertEqual(b'yz', f.read())
381
self.assertRaises(errors.InvalidRange, f.seek, -2, 2)
383
def test_seek_into_void(self):
385
start = self.first_range_start
387
# Seeking to a point between two ranges is possible (only once) but
388
# reading there is forbidden
390
# We crossed a range boundary, so now the file is positioned at the
391
# start of the new range (i.e. trying to seek below 100 will error out)
395
def test_seek_across_ranges(self):
397
f.seek(126) # skip the two first ranges
398
self.assertEqual(b'AB', f.read(2))
400
def test_checked_read_dont_overflow_buffers(self):
402
# We force a very low value to exercise all code paths in _checked_read
403
f._discarded_buf_size = 8
404
f.seek(126) # skip the two first ranges
405
self.assertEqual(b'AB', f.read(2))
407
def test_seek_twice_between_ranges(self):
409
start = self.first_range_start
410
f.seek(start + 40) # Past the first range but before the second
411
# Now the file is positioned at the second range start (100)
412
self.assertRaises(errors.InvalidRange, f.seek, start + 41)
414
def test_seek_at_range_end(self):
415
"""Test seek behavior at range end."""
421
def test_read_at_range_end(self):
423
self.assertEqual(self.alpha, f.read())
424
self.assertEqual(self.alpha, f.read())
425
self.assertEqual(self.alpha.upper(), f.read())
426
self.assertRaises(errors.InvalidHttpResponse, f.read, 1)
429
class TestRangeFileMultipleRangesQuotedBoundaries(TestRangeFileMultipleRanges):
430
"""Perform the same tests as TestRangeFileMultipleRanges, but uses
431
an angle-bracket quoted boundary string like IIS 6.0 and 7.0
432
(but not IIS 5, which breaks the RFC in a different way
433
by using square brackets, not angle brackets)
435
This reveals a bug caused by
437
- The bad implementation of RFC 822 unquoting in Python (angles are not
438
quotes), coupled with
440
- The bad implementation of RFC 2046 in IIS (angles are not permitted chars
444
# The boundary as it appears in boundary lines
445
# IIS 6 and 7 use this value
446
_boundary_trimmed = b"q1w2e3r4t5y6u7i8o9p0zaxscdvfbgnhmjklkl"
447
boundary = b'<' + _boundary_trimmed + b'>'
449
def set_file_boundary(self):
450
# Emulate broken rfc822.unquote() here by removing angles
451
self._file.set_boundary(self._boundary_trimmed)
454
class TestRangeFileVarious(tests.TestCase):
455
"""Tests RangeFile aspects not covered elsewhere."""
457
def test_seek_whence(self):
458
"""Test the seek whence parameter values."""
459
f = response.RangeFile('foo', BytesIO(b'abc'))
464
self.assertRaises(ValueError, f.seek, 0, 14)
466
def test_range_syntax(self):
467
"""Test the Content-Range scanning."""
469
f = response.RangeFile('foo', BytesIO())
471
def ok(expected, header_value):
472
f.set_range_from_header(header_value)
473
# Slightly peek under the covers to get the size
474
self.assertEqual(expected, (f.tell(), f._size))
476
ok((1, 10), 'bytes 1-10/11')
477
ok((1, 10), 'bytes 1-10/*')
478
ok((12, 2), '\tbytes 12-13/*')
479
ok((28, 1), ' bytes 28-28/*')
480
ok((2123, 2120), 'bytes 2123-4242/12310')
481
ok((1, 10), 'bytes 1-10/ttt') # We don't check total (ttt)
483
def nok(header_value):
484
self.assertRaises(errors.InvalidHttpRange,
485
f.set_range_from_header, header_value)
489
nok('bytes xx-yyy/zzz')
490
nok('bytes xx-12/zzz')
491
nok('bytes 11-yy/zzz')
495
# Taken from real request responses
496
_full_text_response = (200, b"""HTTP/1.1 200 OK\r
497
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
498
Server: Apache/2.0.54 (Fedora)\r
499
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
500
ETag: "56691-23-38e9ae00"\r
501
Accept-Ranges: bytes\r
504
Content-Type: text/plain; charset=UTF-8\r
506
""", b"""Bazaar-NG meta directory, format 1
510
_single_range_response = (206, b"""HTTP/1.1 206 Partial Content\r
511
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
512
Server: Apache/2.0.54 (Fedora)\r
513
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
514
ETag: "238a3c-16ec2-805c5540"\r
515
Accept-Ranges: bytes\r
516
Content-Length: 100\r
517
Content-Range: bytes 100-199/93890\r
519
Content-Type: text/plain; charset=UTF-8\r
521
""", b"""mbp@sourcefrog.net-20050309040815-13242001617e4a06
522
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
525
_single_range_no_content_type = (206, b"""HTTP/1.1 206 Partial Content\r
526
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
527
Server: Apache/2.0.54 (Fedora)\r
528
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
529
ETag: "238a3c-16ec2-805c5540"\r
530
Accept-Ranges: bytes\r
531
Content-Length: 100\r
532
Content-Range: bytes 100-199/93890\r
535
""", b"""mbp@sourcefrog.net-20050309040815-13242001617e4a06
536
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
539
_multipart_range_response = (206, b"""HTTP/1.1 206 Partial Content\r
540
Date: Tue, 11 Jul 2006 04:49:48 GMT\r
541
Server: Apache/2.0.54 (Fedora)\r
542
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
543
ETag: "238a3c-16ec2-805c5540"\r
544
Accept-Ranges: bytes\r
545
Content-Length: 1534\r
547
Content-Type: multipart/byteranges; boundary=418470f848b63279b\r
549
\r""", b"""--418470f848b63279b\r
550
Content-type: text/plain; charset=UTF-8\r
551
Content-range: bytes 0-254/93890\r
553
mbp@sourcefrog.net-20050309040815-13242001617e4a06
554
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e7627
555
mbp@sourcefrog.net-20050309040957-6cad07f466bb0bb8
556
mbp@sourcefrog.net-20050309041501-c840e09071de3b67
557
mbp@sourcefrog.net-20050309044615-c24a3250be83220a
559
--418470f848b63279b\r
560
Content-type: text/plain; charset=UTF-8\r
561
Content-range: bytes 1000-2049/93890\r
564
mbp@sourcefrog.net-20050311063625-07858525021f270b
565
mbp@sourcefrog.net-20050311231934-aa3776aff5200bb9
566
mbp@sourcefrog.net-20050311231953-73aeb3a131c3699a
567
mbp@sourcefrog.net-20050311232353-f5e33da490872c6a
568
mbp@sourcefrog.net-20050312071639-0a8f59a34a024ff0
569
mbp@sourcefrog.net-20050312073432-b2c16a55e0d6e9fb
570
mbp@sourcefrog.net-20050312073831-a47c3335ece1920f
571
mbp@sourcefrog.net-20050312085412-13373aa129ccbad3
572
mbp@sourcefrog.net-20050313052251-2bf004cb96b39933
573
mbp@sourcefrog.net-20050313052856-3edd84094687cb11
574
mbp@sourcefrog.net-20050313053233-e30a4f28aef48f9d
575
mbp@sourcefrog.net-20050313053853-7c64085594ff3072
576
mbp@sourcefrog.net-20050313054757-a86c3f5871069e22
577
mbp@sourcefrog.net-20050313061422-418f1f73b94879b9
578
mbp@sourcefrog.net-20050313120651-497bd231b19df600
579
mbp@sourcefrog.net-20050314024931-eae0170ef25a5d1a
580
mbp@sourcefrog.net-20050314025438-d52099f915fe65fc
581
mbp@sourcefrog.net-20050314025539-637a636692c055cf
582
mbp@sourcefrog.net-20050314025737-55eb441f430ab4ba
583
mbp@sourcefrog.net-20050314025901-d74aa93bb7ee8f62
585
--418470f848b63279b--\r
589
_multipart_squid_range_response = (206, b"""HTTP/1.0 206 Partial Content\r
590
Date: Thu, 31 Aug 2006 21:16:22 GMT\r
591
Server: Apache/2.2.2 (Unix) DAV/2\r
592
Last-Modified: Thu, 31 Aug 2006 17:57:06 GMT\r
593
Accept-Ranges: bytes\r
594
Content-Type: multipart/byteranges; boundary="squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196"\r
595
Content-Length: 598\r
596
X-Cache: MISS from localhost.localdomain\r
597
X-Cache-Lookup: HIT from localhost.localdomain:3128\r
598
Proxy-Connection: keep-alive\r
602
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
603
Content-Type: text/plain\r
604
Content-Range: bytes 0-99/18672\r
608
scott@netsplit.com-20050708230047-47c7868f276b939f fulltext 0 863 :
610
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
611
Content-Type: text/plain\r
612
Content-Range: bytes 300-499/18672\r
614
com-20050708231537-2b124b835395399a :
615
scott@netsplit.com-20050820234126-551311dbb7435b51 line-delta 1803 479 .scott@netsplit.com-20050820232911-dc4322a084eadf7e :
616
scott@netsplit.com-20050821213706-c86\r
617
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196--\r
622
_full_text_response_no_content_type = (200, b"""HTTP/1.1 200 OK\r
623
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
624
Server: Apache/2.0.54 (Fedora)\r
625
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
626
ETag: "56691-23-38e9ae00"\r
627
Accept-Ranges: bytes\r
631
""", b"""Bazaar-NG meta directory, format 1
635
_full_text_response_no_content_length = (200, b"""HTTP/1.1 200 OK\r
636
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
637
Server: Apache/2.0.54 (Fedora)\r
638
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
639
ETag: "56691-23-38e9ae00"\r
640
Accept-Ranges: bytes\r
642
Content-Type: text/plain; charset=UTF-8\r
644
""", b"""Bazaar-NG meta directory, format 1
648
_single_range_no_content_range = (206, b"""HTTP/1.1 206 Partial Content\r
649
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
650
Server: Apache/2.0.54 (Fedora)\r
651
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
652
ETag: "238a3c-16ec2-805c5540"\r
653
Accept-Ranges: bytes\r
654
Content-Length: 100\r
657
""", b"""mbp@sourcefrog.net-20050309040815-13242001617e4a06
658
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
661
_single_range_response_truncated = (206, b"""HTTP/1.1 206 Partial Content\r
662
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
663
Server: Apache/2.0.54 (Fedora)\r
664
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
665
ETag: "238a3c-16ec2-805c5540"\r
666
Accept-Ranges: bytes\r
667
Content-Length: 100\r
668
Content-Range: bytes 100-199/93890\r
670
Content-Type: text/plain; charset=UTF-8\r
672
""", b"""mbp@sourcefrog.net-20050309040815-13242001617e4a06""")
675
_invalid_response = (444, b"""HTTP/1.1 444 Bad Response\r
676
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
678
Content-Type: text/html; charset=iso-8859-1\r
680
""", b"""<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
682
<title>404 Not Found</title>
685
<p>I don't know what I'm doing</p>
691
_multipart_no_content_range = (206, b"""HTTP/1.0 206 Partial Content\r
692
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
693
Content-Length: 598\r
698
Content-Type: text/plain\r
705
_multipart_no_boundary = (206, b"""HTTP/1.0 206 Partial Content\r
706
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
707
Content-Length: 598\r
712
Content-Type: text/plain\r
713
Content-Range: bytes 0-18/18672\r
717
The range ended at the line above, this text is garbage instead of a boundary
722
class TestHandleResponse(tests.TestCase):
724
def _build_HTTPMessage(self, raw_headers):
725
status_and_headers = BytesIO(raw_headers)
726
# Get rid of the status line
727
status_and_headers.readline()
728
msg = parse_headers(status_and_headers)
734
def get_response(self, a_response):
735
"""Process a supplied response, and return the result."""
736
code, raw_headers, body = a_response
737
getheader = self._build_HTTPMessage(raw_headers)
738
return response.handle_response(
739
'http://foo', code, getheader, BytesIO(a_response[2]))
741
def test_full_text(self):
742
out = self.get_response(_full_text_response)
743
# It is a BytesIO from the original data
744
self.assertEqual(_full_text_response[2], out.read())
746
def test_single_range(self):
747
out = self.get_response(_single_range_response)
750
self.assertEqual(_single_range_response[2], out.read(100))
752
def test_single_range_no_content(self):
753
out = self.get_response(_single_range_no_content_type)
756
self.assertEqual(_single_range_no_content_type[2], out.read(100))
758
def test_single_range_truncated(self):
759
out = self.get_response(_single_range_response_truncated)
760
# Content-Range declares 100 but only 51 present
761
self.assertRaises(errors.ShortReadvError, out.seek, out.tell() + 51)
763
def test_multi_range(self):
764
out = self.get_response(_multipart_range_response)
766
# Just make sure we can read the right contents
773
def test_multi_squid_range(self):
774
out = self.get_response(_multipart_squid_range_response)
776
# Just make sure we can read the right contents
783
def test_invalid_response(self):
784
self.assertRaises(errors.InvalidHttpResponse,
785
self.get_response, _invalid_response)
787
def test_full_text_no_content_type(self):
788
# We should not require Content-Type for a full response
789
code, raw_headers, body = _full_text_response_no_content_type
790
getheader = self._build_HTTPMessage(raw_headers)
791
out = response.handle_response(
792
'http://foo', code, getheader, BytesIO(body))
793
self.assertEqual(body, out.read())
795
def test_full_text_no_content_length(self):
796
code, raw_headers, body = _full_text_response_no_content_length
797
getheader = self._build_HTTPMessage(raw_headers)
798
out = response.handle_response(
799
'http://foo', code, getheader, BytesIO(body))
800
self.assertEqual(body, out.read())
802
def test_missing_content_range(self):
803
code, raw_headers, body = _single_range_no_content_range
804
getheader = self._build_HTTPMessage(raw_headers)
805
self.assertRaises(errors.InvalidHttpResponse,
806
response.handle_response,
807
'http://bogus', code, getheader, BytesIO(body))
809
def test_multipart_no_content_range(self):
810
code, raw_headers, body = _multipart_no_content_range
811
getheader = self._build_HTTPMessage(raw_headers)
812
self.assertRaises(errors.InvalidHttpResponse,
813
response.handle_response,
814
'http://bogus', code, getheader, BytesIO(body))
816
def test_multipart_no_boundary(self):
817
out = self.get_response(_multipart_no_boundary)
818
out.read() # Read the whole range
819
# Fail to find the boundary line
820
self.assertRaises(errors.InvalidHttpResponse, out.seek, 1, 1)
823
class TestRangeFileSizeReadLimited(tests.TestCase):
824
"""Test RangeFile _max_read_size functionality which limits the size of
825
read blocks to prevent MemoryError messages in socket.recv.
829
super(TestRangeFileSizeReadLimited, self).setUp()
830
# create a test datablock larger than _max_read_size.
831
chunk_size = response.RangeFile._max_read_size
832
test_pattern = b'0123456789ABCDEF'
833
self.test_data = test_pattern * (3 * chunk_size // len(test_pattern))
834
self.test_data_len = len(self.test_data)
836
def test_max_read_size(self):
837
"""Read data in blocks and verify that the reads are not larger than
838
the maximum read size.
840
# retrieve data in large blocks from response.RangeFile object
841
mock_read_file = FakeReadFile(self.test_data)
842
range_file = response.RangeFile('test_max_read_size', mock_read_file)
843
response_data = range_file.read(self.test_data_len)
845
# verify read size was equal to the maximum read size
846
self.assertTrue(mock_read_file.get_max_read_size() > 0)
847
self.assertEqual(mock_read_file.get_max_read_size(),
848
response.RangeFile._max_read_size)
849
self.assertEqual(mock_read_file.get_read_count(), 3)
851
# report error if the data wasn't equal (we only report the size due
852
# to the length of the data)
853
if response_data != self.test_data:
854
message = "Data not equal. Expected %d bytes, received %d."
855
self.fail(message % (len(response_data), self.test_data_len))