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
46
from ..sixish import (
49
from ..transport.http import (
53
from .file_utils import (
58
class ReadSocket(object):
59
"""A socket-like object that can be given a predefined content."""
61
def __init__(self, data):
62
self.readfile = BytesIO(data)
64
def makefile(self, mode='r', bufsize=None):
68
class FakeHTTPConnection(_urllib2_wrappers.HTTPConnection):
70
def __init__(self, sock):
71
_urllib2_wrappers.HTTPConnection.__init__(self, 'localhost')
72
# Set the socket to bypass the connection
76
"""Ignores the writes on the socket."""
80
class TestResponseFileIter(tests.TestCase):
82
def test_iter_empty(self):
83
f = response.ResponseFile('empty', BytesIO())
84
self.assertEqual([], list(f))
86
def test_iter_many(self):
87
f = response.ResponseFile('many', BytesIO(b'0\n1\nboo!\n'))
88
self.assertEqual(['0\n', '1\n', 'boo!\n'], list(f))
91
class TestHTTPConnection(tests.TestCase):
93
def test_cleanup_pipe(self):
94
sock = ReadSocket("""HTTP/1.1 200 OK\r
95
Content-Type: text/plain; charset=UTF-8\r
100
conn = FakeHTTPConnection(sock)
101
# Simulate the request sending so that the connection will be able to
103
conn.putrequest('GET', 'http://localhost/fictious')
105
# Now, get the response
106
resp = conn.getresponse()
107
# Read part of the response
108
self.assertEqual('0123456789\n', resp.read(11))
109
# Override the thresold to force the warning emission
110
conn._range_warning_thresold = 6 # There are 7 bytes pending
112
self.assertContainsRe(self.get_log(), 'Got a 200 response when asking')
115
class TestRangeFileMixin(object):
116
"""Tests for accessing the first range in a RangeFile."""
118
# A simple string used to represent a file part (also called a range), in
119
# which offsets are easy to calculate for test writers. It's used as a
120
# building block with slight variations but basically 'a' is the first char
121
# of the range and 'z' is the last.
122
alpha = 'abcdefghijklmnopqrstuvwxyz'
124
def test_can_read_at_first_access(self):
125
"""Test that the just created file can be read."""
126
self.assertEqual(self.alpha, self._file.read())
128
def test_seek_read(self):
129
"""Test seek/read inside the range."""
131
start = self.first_range_start
132
# Before any use, tell() should be at the range start
133
self.assertEqual(start, f.tell())
134
cur = start # For an overall offset assertion
137
self.assertEqual('def', f.read(3))
141
self.assertEqual('klmn', f.read(4))
143
# read(0) in the middle of a range
144
self.assertEqual('', f.read(0))
148
self.assertEqual(here, f.tell())
149
self.assertEqual(cur, f.tell())
151
def test_read_zero(self):
153
self.assertEqual('', f.read(0))
155
self.assertEqual('', f.read(0))
157
def test_seek_at_range_end(self):
161
def test_read_at_range_end(self):
162
"""Test read behaviour at range end."""
164
self.assertEqual(self.alpha, f.read())
165
self.assertEqual('', f.read(0))
166
self.assertRaises(errors.InvalidRange, f.read, 1)
168
def test_unbounded_read_after_seek(self):
171
# Should not cross ranges
172
self.assertEqual('yz', f.read())
174
def test_seek_backwards(self):
176
start = self.first_range_start
179
self.assertRaises(errors.InvalidRange, f.seek, start + 5)
181
def test_seek_outside_single_range(self):
183
if f._size == -1 or f._boundary is not None:
184
raise tests.TestNotApplicable('Needs a fully defined range')
185
# Will seek past the range and then errors out
186
self.assertRaises(errors.InvalidRange,
187
f.seek, self.first_range_start + 27)
189
def test_read_past_end_of_range(self):
192
raise tests.TestNotApplicable("Can't check an unknown size")
193
start = self.first_range_start
195
self.assertRaises(errors.InvalidRange, f.read, 10)
197
def test_seek_from_end(self):
198
"""Test seeking from the end of the file.
200
The semantic is unclear in case of multiple ranges. Seeking from end
201
exists only for the http transports, cannot be used if the file size is
202
unknown and is not used in breezy itself. This test must be (and is)
203
overridden by daughter classes.
205
Reading from end makes sense only when a range has been requested from
206
the end of the file (see HttpTransportBase._get() when using the
207
'tail_amount' parameter). The HTTP response can only be a whole file or
212
self.assertEqual('yz', f.read())
215
class TestRangeFileSizeUnknown(tests.TestCase, TestRangeFileMixin):
216
"""Test a RangeFile for a whole file whose size is not known."""
219
super(TestRangeFileSizeUnknown, self).setUp()
220
self._file = response.RangeFile('Whole_file_size_known',
222
# We define no range, relying on RangeFile to provide default values
223
self.first_range_start = 0 # It's the whole file
225
def test_seek_from_end(self):
226
"""See TestRangeFileMixin.test_seek_from_end.
228
The end of the file can't be determined since the size is unknown.
230
self.assertRaises(errors.InvalidRange, self._file.seek, -1, 2)
232
def test_read_at_range_end(self):
233
"""Test read behaviour at range end."""
235
self.assertEqual(self.alpha, f.read())
236
self.assertEqual('', f.read(0))
237
self.assertEqual('', f.read(1))
240
class TestRangeFileSizeKnown(tests.TestCase, TestRangeFileMixin):
241
"""Test a RangeFile for a whole file whose size is known."""
244
super(TestRangeFileSizeKnown, self).setUp()
245
self._file = response.RangeFile('Whole_file_size_known',
247
self._file.set_range(0, len(self.alpha))
248
self.first_range_start = 0 # It's the whole file
251
class TestRangeFileSingleRange(tests.TestCase, TestRangeFileMixin):
252
"""Test a RangeFile for a single range."""
255
super(TestRangeFileSingleRange, self).setUp()
256
self._file = response.RangeFile('Single_range_file',
258
self.first_range_start = 15
259
self._file.set_range(self.first_range_start, len(self.alpha))
262
def test_read_before_range(self):
263
# This can't occur under normal circumstances, we have to force it
265
f._pos = 0 # Force an invalid pos
266
self.assertRaises(errors.InvalidRange, f.read, 2)
269
class TestRangeFileMultipleRanges(tests.TestCase, TestRangeFileMixin):
270
"""Test a RangeFile for multiple ranges.
272
The RangeFile used for the tests contains three ranges:
274
- at offset 25: alpha
275
- at offset 100: alpha
276
- at offset 126: alpha.upper()
278
The two last ranges are contiguous. This only rarely occurs (should not in
279
fact) in real uses but may lead to hard to track bugs.
282
# The following is used to represent the boundary paramter defined
283
# in HTTP response headers and the boundary lines that separate
286
boundary = "separation"
289
super(TestRangeFileMultipleRanges, self).setUp()
291
boundary = self.boundary
294
self.first_range_start = 25
295
file_size = 200 # big enough to encompass all ranges
296
for (start, part) in [(self.first_range_start, self.alpha),
297
# Two contiguous ranges
299
(126, self.alpha.upper())]:
300
content += self._multipart_byterange(part, start, boundary,
303
content += self._boundary_line()
305
self._file = response.RangeFile('Multiple_ranges_file',
307
self.set_file_boundary()
309
def _boundary_line(self):
310
"""Helper to build the formatted boundary line."""
311
return '--' + self.boundary + '\r\n'
313
def set_file_boundary(self):
314
# Ranges are set by decoding the range headers, the RangeFile user is
315
# supposed to call the following before using seek or read since it
316
# requires knowing the *response* headers (in that case the boundary
317
# which is part of the Content-Type header).
318
self._file.set_boundary(self.boundary)
320
def _multipart_byterange(self, data, offset, boundary, file_size='*'):
321
"""Encode a part of a file as a multipart/byterange MIME type.
323
When a range request is issued, the HTTP response body can be
324
decomposed in parts, each one representing a range (start, size) in a
327
:param data: The payload.
328
:param offset: where data starts in the file
329
:param boundary: used to separate the parts
330
:param file_size: the size of the file containing the range (default to
333
:return: a string containing the data encoded as it will appear in the
336
bline = self._boundary_line()
337
# Each range begins with a boundary line
339
# A range is described by a set of headers, but only 'Content-Range' is
340
# required for our implementation (TestHandleResponse below will
341
# exercise ranges with multiple or missing headers')
342
range += 'Content-Range: bytes %d-%d/%d\r\n' % (offset,
346
# Finally the raw bytes
350
def test_read_all_ranges(self):
352
self.assertEqual(self.alpha, f.read()) # Read first range
353
f.seek(100) # Trigger the second range recognition
354
self.assertEqual(self.alpha, f.read()) # Read second range
355
self.assertEqual(126, f.tell())
356
f.seek(126) # Start of third range which is also the current pos !
357
self.assertEqual('A', f.read(1))
359
self.assertEqual('LMN', f.read(3))
361
def test_seek_from_end(self):
362
"""See TestRangeFileMixin.test_seek_from_end."""
363
# The actual implementation will seek from end for the first range only
364
# and then fail. Since seeking from end is intended to be used for a
365
# single range only anyway, this test just document the actual
369
self.assertEqual('yz', f.read())
370
self.assertRaises(errors.InvalidRange, f.seek, -2, 2)
372
def test_seek_into_void(self):
374
start = self.first_range_start
376
# Seeking to a point between two ranges is possible (only once) but
377
# reading there is forbidden
379
# We crossed a range boundary, so now the file is positioned at the
380
# start of the new range (i.e. trying to seek below 100 will error out)
384
def test_seek_across_ranges(self):
386
f.seek(126) # skip the two first ranges
387
self.assertEqual('AB', f.read(2))
389
def test_checked_read_dont_overflow_buffers(self):
391
# We force a very low value to exercise all code paths in _checked_read
392
f._discarded_buf_size = 8
393
f.seek(126) # skip the two first ranges
394
self.assertEqual('AB', f.read(2))
396
def test_seek_twice_between_ranges(self):
398
start = self.first_range_start
399
f.seek(start + 40) # Past the first range but before the second
400
# Now the file is positioned at the second range start (100)
401
self.assertRaises(errors.InvalidRange, f.seek, start + 41)
403
def test_seek_at_range_end(self):
404
"""Test seek behavior at range end."""
410
def test_read_at_range_end(self):
412
self.assertEqual(self.alpha, f.read())
413
self.assertEqual(self.alpha, f.read())
414
self.assertEqual(self.alpha.upper(), f.read())
415
self.assertRaises(errors.InvalidHttpResponse, f.read, 1)
418
class TestRangeFileMultipleRangesQuotedBoundaries(TestRangeFileMultipleRanges):
419
"""Perform the same tests as TestRangeFileMultipleRanges, but uses
420
an angle-bracket quoted boundary string like IIS 6.0 and 7.0
421
(but not IIS 5, which breaks the RFC in a different way
422
by using square brackets, not angle brackets)
424
This reveals a bug caused by
426
- The bad implementation of RFC 822 unquoting in Python (angles are not
427
quotes), coupled with
429
- The bad implementation of RFC 2046 in IIS (angles are not permitted chars
433
# The boundary as it appears in boundary lines
434
# IIS 6 and 7 use this value
435
_boundary_trimmed = "q1w2e3r4t5y6u7i8o9p0zaxscdvfbgnhmjklkl"
436
boundary = '<' + _boundary_trimmed + '>'
438
def set_file_boundary(self):
439
# Emulate broken rfc822.unquote() here by removing angles
440
self._file.set_boundary(self._boundary_trimmed)
443
class TestRangeFileVarious(tests.TestCase):
444
"""Tests RangeFile aspects not covered elsewhere."""
446
def test_seek_whence(self):
447
"""Test the seek whence parameter values."""
448
f = response.RangeFile('foo', BytesIO(b'abc'))
453
self.assertRaises(ValueError, f.seek, 0, 14)
455
def test_range_syntax(self):
456
"""Test the Content-Range scanning."""
458
f = response.RangeFile('foo', BytesIO())
460
def ok(expected, header_value):
461
f.set_range_from_header(header_value)
462
# Slightly peek under the covers to get the size
463
self.assertEqual(expected, (f.tell(), f._size))
465
ok((1, 10), 'bytes 1-10/11')
466
ok((1, 10), 'bytes 1-10/*')
467
ok((12, 2), '\tbytes 12-13/*')
468
ok((28, 1), ' bytes 28-28/*')
469
ok((2123, 2120), 'bytes 2123-4242/12310')
470
ok((1, 10), 'bytes 1-10/ttt') # We don't check total (ttt)
472
def nok(header_value):
473
self.assertRaises(errors.InvalidHttpRange,
474
f.set_range_from_header, header_value)
478
nok('bytes xx-yyy/zzz')
479
nok('bytes xx-12/zzz')
480
nok('bytes 11-yy/zzz')
484
# Taken from real request responses
485
_full_text_response = (200, """HTTP/1.1 200 OK\r
486
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
487
Server: Apache/2.0.54 (Fedora)\r
488
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
489
ETag: "56691-23-38e9ae00"\r
490
Accept-Ranges: bytes\r
493
Content-Type: text/plain; charset=UTF-8\r
495
""", """Bazaar-NG meta directory, format 1
499
_single_range_response = (206, """HTTP/1.1 206 Partial Content\r
500
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
501
Server: Apache/2.0.54 (Fedora)\r
502
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
503
ETag: "238a3c-16ec2-805c5540"\r
504
Accept-Ranges: bytes\r
505
Content-Length: 100\r
506
Content-Range: bytes 100-199/93890\r
508
Content-Type: text/plain; charset=UTF-8\r
510
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
511
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
514
_single_range_no_content_type = (206, """HTTP/1.1 206 Partial Content\r
515
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
516
Server: Apache/2.0.54 (Fedora)\r
517
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
518
ETag: "238a3c-16ec2-805c5540"\r
519
Accept-Ranges: bytes\r
520
Content-Length: 100\r
521
Content-Range: bytes 100-199/93890\r
524
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
525
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
528
_multipart_range_response = (206, """HTTP/1.1 206 Partial Content\r
529
Date: Tue, 11 Jul 2006 04:49:48 GMT\r
530
Server: Apache/2.0.54 (Fedora)\r
531
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
532
ETag: "238a3c-16ec2-805c5540"\r
533
Accept-Ranges: bytes\r
534
Content-Length: 1534\r
536
Content-Type: multipart/byteranges; boundary=418470f848b63279b\r
538
\r""", """--418470f848b63279b\r
539
Content-type: text/plain; charset=UTF-8\r
540
Content-range: bytes 0-254/93890\r
542
mbp@sourcefrog.net-20050309040815-13242001617e4a06
543
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e7627
544
mbp@sourcefrog.net-20050309040957-6cad07f466bb0bb8
545
mbp@sourcefrog.net-20050309041501-c840e09071de3b67
546
mbp@sourcefrog.net-20050309044615-c24a3250be83220a
548
--418470f848b63279b\r
549
Content-type: text/plain; charset=UTF-8\r
550
Content-range: bytes 1000-2049/93890\r
553
mbp@sourcefrog.net-20050311063625-07858525021f270b
554
mbp@sourcefrog.net-20050311231934-aa3776aff5200bb9
555
mbp@sourcefrog.net-20050311231953-73aeb3a131c3699a
556
mbp@sourcefrog.net-20050311232353-f5e33da490872c6a
557
mbp@sourcefrog.net-20050312071639-0a8f59a34a024ff0
558
mbp@sourcefrog.net-20050312073432-b2c16a55e0d6e9fb
559
mbp@sourcefrog.net-20050312073831-a47c3335ece1920f
560
mbp@sourcefrog.net-20050312085412-13373aa129ccbad3
561
mbp@sourcefrog.net-20050313052251-2bf004cb96b39933
562
mbp@sourcefrog.net-20050313052856-3edd84094687cb11
563
mbp@sourcefrog.net-20050313053233-e30a4f28aef48f9d
564
mbp@sourcefrog.net-20050313053853-7c64085594ff3072
565
mbp@sourcefrog.net-20050313054757-a86c3f5871069e22
566
mbp@sourcefrog.net-20050313061422-418f1f73b94879b9
567
mbp@sourcefrog.net-20050313120651-497bd231b19df600
568
mbp@sourcefrog.net-20050314024931-eae0170ef25a5d1a
569
mbp@sourcefrog.net-20050314025438-d52099f915fe65fc
570
mbp@sourcefrog.net-20050314025539-637a636692c055cf
571
mbp@sourcefrog.net-20050314025737-55eb441f430ab4ba
572
mbp@sourcefrog.net-20050314025901-d74aa93bb7ee8f62
574
--418470f848b63279b--\r
578
_multipart_squid_range_response = (206, """HTTP/1.0 206 Partial Content\r
579
Date: Thu, 31 Aug 2006 21:16:22 GMT\r
580
Server: Apache/2.2.2 (Unix) DAV/2\r
581
Last-Modified: Thu, 31 Aug 2006 17:57:06 GMT\r
582
Accept-Ranges: bytes\r
583
Content-Type: multipart/byteranges; boundary="squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196"\r
584
Content-Length: 598\r
585
X-Cache: MISS from localhost.localdomain\r
586
X-Cache-Lookup: HIT from localhost.localdomain:3128\r
587
Proxy-Connection: keep-alive\r
591
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
592
Content-Type: text/plain\r
593
Content-Range: bytes 0-99/18672\r
597
scott@netsplit.com-20050708230047-47c7868f276b939f fulltext 0 863 :
599
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
600
Content-Type: text/plain\r
601
Content-Range: bytes 300-499/18672\r
603
com-20050708231537-2b124b835395399a :
604
scott@netsplit.com-20050820234126-551311dbb7435b51 line-delta 1803 479 .scott@netsplit.com-20050820232911-dc4322a084eadf7e :
605
scott@netsplit.com-20050821213706-c86\r
606
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196--\r
611
_full_text_response_no_content_type = (200, """HTTP/1.1 200 OK\r
612
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
613
Server: Apache/2.0.54 (Fedora)\r
614
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
615
ETag: "56691-23-38e9ae00"\r
616
Accept-Ranges: bytes\r
620
""", """Bazaar-NG meta directory, format 1
624
_full_text_response_no_content_length = (200, """HTTP/1.1 200 OK\r
625
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
626
Server: Apache/2.0.54 (Fedora)\r
627
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
628
ETag: "56691-23-38e9ae00"\r
629
Accept-Ranges: bytes\r
631
Content-Type: text/plain; charset=UTF-8\r
633
""", """Bazaar-NG meta directory, format 1
637
_single_range_no_content_range = (206, """HTTP/1.1 206 Partial Content\r
638
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
639
Server: Apache/2.0.54 (Fedora)\r
640
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
641
ETag: "238a3c-16ec2-805c5540"\r
642
Accept-Ranges: bytes\r
643
Content-Length: 100\r
646
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
647
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
650
_single_range_response_truncated = (206, """HTTP/1.1 206 Partial Content\r
651
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
652
Server: Apache/2.0.54 (Fedora)\r
653
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
654
ETag: "238a3c-16ec2-805c5540"\r
655
Accept-Ranges: bytes\r
656
Content-Length: 100\r
657
Content-Range: bytes 100-199/93890\r
659
Content-Type: text/plain; charset=UTF-8\r
661
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06""")
664
_invalid_response = (444, """HTTP/1.1 444 Bad Response\r
665
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
667
Content-Type: text/html; charset=iso-8859-1\r
669
""", """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
671
<title>404 Not Found</title>
674
<p>I don't know what I'm doing</p>
680
_multipart_no_content_range = (206, """HTTP/1.0 206 Partial Content\r
681
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
682
Content-Length: 598\r
687
Content-Type: text/plain\r
694
_multipart_no_boundary = (206, """HTTP/1.0 206 Partial Content\r
695
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
696
Content-Length: 598\r
701
Content-Type: text/plain\r
702
Content-Range: bytes 0-18/18672\r
706
The range ended at the line above, this text is garbage instead of a boundary
711
class TestHandleResponse(tests.TestCase):
713
def _build_HTTPMessage(self, raw_headers):
714
status_and_headers = BytesIO(raw_headers)
715
# Get rid of the status line
716
status_and_headers.readline()
717
msg = httplib.HTTPMessage(status_and_headers)
720
def get_response(self, a_response):
721
"""Process a supplied response, and return the result."""
722
code, raw_headers, body = a_response
723
msg = self._build_HTTPMessage(raw_headers)
724
return response.handle_response('http://foo', code, msg,
725
BytesIO(a_response[2]))
727
def test_full_text(self):
728
out = self.get_response(_full_text_response)
729
# It is a BytesIO from the original data
730
self.assertEqual(_full_text_response[2], out.read())
732
def test_single_range(self):
733
out = self.get_response(_single_range_response)
736
self.assertEqual(_single_range_response[2], out.read(100))
738
def test_single_range_no_content(self):
739
out = self.get_response(_single_range_no_content_type)
742
self.assertEqual(_single_range_no_content_type[2], out.read(100))
744
def test_single_range_truncated(self):
745
out = self.get_response(_single_range_response_truncated)
746
# Content-Range declares 100 but only 51 present
747
self.assertRaises(errors.ShortReadvError, out.seek, out.tell() + 51)
749
def test_multi_range(self):
750
out = self.get_response(_multipart_range_response)
752
# Just make sure we can read the right contents
759
def test_multi_squid_range(self):
760
out = self.get_response(_multipart_squid_range_response)
762
# Just make sure we can read the right contents
769
def test_invalid_response(self):
770
self.assertRaises(errors.InvalidHttpResponse,
771
self.get_response, _invalid_response)
773
def test_full_text_no_content_type(self):
774
# We should not require Content-Type for a full response
775
code, raw_headers, body = _full_text_response_no_content_type
776
msg = self._build_HTTPMessage(raw_headers)
777
out = response.handle_response('http://foo', code, msg, BytesIO(body))
778
self.assertEqual(body, out.read())
780
def test_full_text_no_content_length(self):
781
code, raw_headers, body = _full_text_response_no_content_length
782
msg = self._build_HTTPMessage(raw_headers)
783
out = response.handle_response('http://foo', code, msg, BytesIO(body))
784
self.assertEqual(body, out.read())
786
def test_missing_content_range(self):
787
code, raw_headers, body = _single_range_no_content_range
788
msg = self._build_HTTPMessage(raw_headers)
789
self.assertRaises(errors.InvalidHttpResponse,
790
response.handle_response,
791
'http://bogus', code, msg, BytesIO(body))
793
def test_multipart_no_content_range(self):
794
code, raw_headers, body = _multipart_no_content_range
795
msg = self._build_HTTPMessage(raw_headers)
796
self.assertRaises(errors.InvalidHttpResponse,
797
response.handle_response,
798
'http://bogus', code, msg, BytesIO(body))
800
def test_multipart_no_boundary(self):
801
out = self.get_response(_multipart_no_boundary)
802
out.read() # Read the whole range
803
# Fail to find the boundary line
804
self.assertRaises(errors.InvalidHttpResponse, out.seek, 1, 1)
807
class TestRangeFileSizeReadLimited(tests.TestCase):
808
"""Test RangeFile _max_read_size functionality which limits the size of
809
read blocks to prevent MemoryError messages in socket.recv.
813
super(TestRangeFileSizeReadLimited, self).setUp()
814
# create a test datablock larger than _max_read_size.
815
chunk_size = response.RangeFile._max_read_size
816
test_pattern = '0123456789ABCDEF'
817
self.test_data = test_pattern * (3 * chunk_size / len(test_pattern))
818
self.test_data_len = len(self.test_data)
820
def test_max_read_size(self):
821
"""Read data in blocks and verify that the reads are not larger than
822
the maximum read size.
824
# retrieve data in large blocks from response.RangeFile object
825
mock_read_file = FakeReadFile(self.test_data)
826
range_file = response.RangeFile('test_max_read_size', mock_read_file)
827
response_data = range_file.read(self.test_data_len)
829
# verify read size was equal to the maximum read size
830
self.assertTrue(mock_read_file.get_max_read_size() > 0)
831
self.assertEqual(mock_read_file.get_max_read_size(),
832
response.RangeFile._max_read_size)
833
self.assertEqual(mock_read_file.get_read_count(), 3)
835
# report error if the data wasn't equal (we only report the size due
836
# to the length of the data)
837
if response_data != self.test_data:
838
message = "Data not equal. Expected %d bytes, received %d."
839
self.fail(message % (len(response_data), self.test_data_len))