/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006-2010, 2012, 2013, 2016 Canonical Ltd
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
2
#
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.
7
#
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.
12
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
16
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
17
"""Tests from HTTP response parsing.
18
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
19
The handle_response method read the response body of a GET request an returns
20
the corresponding RangeFile.
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
21
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
27
  and size.
28
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,
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
33
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
34
- the expected pattern of use is either seek(offset)+read(size) or a single
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
35
  read with no size specified. For multiple range files, multiple read() will
36
  return the corresponding ranges, trying to read further will raise
37
  InvalidHttpResponse.
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
38
"""
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
39
6791.2.3 by Jelmer Vernooij
Fix more imports.
40
try:
41
    import http.client as http_client
42
except ImportError:  # python < 3
43
    import httplib as http_client
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
44
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
45
from .. import (
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
46
    errors,
47
    tests,
48
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
49
from ..sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
50
    BytesIO,
51
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
52
from ..transport.http import (
3104.3.4 by Vincent Ladeuil
Add test.
53
    response,
54
    _urllib2_wrappers,
55
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
56
from .file_utils import (
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
57
    FakeReadFile,
58
    )
3104.3.4 by Vincent Ladeuil
Add test.
59
60
61
class ReadSocket(object):
62
    """A socket-like object that can be given a predefined content."""
63
64
    def __init__(self, data):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
65
        self.readfile = BytesIO(data)
3104.3.4 by Vincent Ladeuil
Add test.
66
67
    def makefile(self, mode='r', bufsize=None):
68
        return self.readfile
69
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
70
3104.3.4 by Vincent Ladeuil
Add test.
71
class FakeHTTPConnection(_urllib2_wrappers.HTTPConnection):
72
73
    def __init__(self, sock):
74
        _urllib2_wrappers.HTTPConnection.__init__(self, 'localhost')
75
        # Set the socket to bypass the connection
76
        self.sock = sock
77
78
    def send(self, str):
79
        """Ignores the writes on the socket."""
80
        pass
81
82
6575.1.2 by Vincent Ladeuil
TDD backwards, works here ;)
83
class TestResponseFileIter(tests.TestCase):
84
85
    def test_iter_empty(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
86
        f = response.ResponseFile('empty', BytesIO())
6575.1.3 by Vincent Ladeuil
Simpler.
87
        self.assertEqual([], list(f))
6575.1.2 by Vincent Ladeuil
TDD backwards, works here ;)
88
89
    def test_iter_many(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
90
        f = response.ResponseFile('many', BytesIO(b'0\n1\nboo!\n'))
6973.11.6 by Jelmer Vernooij
Fix more http tests.
91
        self.assertEqual([b'0\n', b'1\n', b'boo!\n'], list(f))
6575.1.2 by Vincent Ladeuil
TDD backwards, works here ;)
92
93
3104.3.4 by Vincent Ladeuil
Add test.
94
class TestHTTPConnection(tests.TestCase):
95
96
    def test_cleanup_pipe(self):
6973.11.6 by Jelmer Vernooij
Fix more http tests.
97
        sock = ReadSocket(b"""HTTP/1.1 200 OK\r
3104.3.4 by Vincent Ladeuil
Add test.
98
Content-Type: text/plain; charset=UTF-8\r
99
Content-Length: 18
100
\r
101
0123456789
102
garbage""")
103
        conn = FakeHTTPConnection(sock)
104
        # Simulate the request sending so that the connection will be able to
105
        # read the response.
106
        conn.putrequest('GET', 'http://localhost/fictious')
107
        conn.endheaders()
108
        # Now, get the response
109
        resp = conn.getresponse()
110
        # Read part of the response
6973.11.6 by Jelmer Vernooij
Fix more http tests.
111
        self.assertEqual(b'0123456789\n', resp.read(11))
3104.3.4 by Vincent Ladeuil
Add test.
112
        # Override the thresold to force the warning emission
113
        conn._range_warning_thresold = 6 # There are 7 bytes pending
114
        conn.cleanup_pipe()
4794.1.15 by Robert Collins
Review feedback.
115
        self.assertContainsRe(self.get_log(), 'Got a 200 response when asking')
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
116
117
118
class TestRangeFileMixin(object):
119
    """Tests for accessing the first range in a RangeFile."""
120
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
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
123
    # building block with slight variations but basically 'a' is the first char
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
124
    # of the range and 'z' is the last.
6973.11.6 by Jelmer Vernooij
Fix more http tests.
125
    alpha = b'abcdefghijklmnopqrstuvwxyz'
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
126
127
    def test_can_read_at_first_access(self):
128
        """Test that the just created file can be read."""
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
129
        self.assertEqual(self.alpha, self._file.read())
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
130
131
    def test_seek_read(self):
132
        """Test seek/read inside the range."""
133
        f = self._file
134
        start = self.first_range_start
135
        # Before any use, tell() should be at the range start
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
136
        self.assertEqual(start, f.tell())
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
137
        cur = start # For an overall offset assertion
138
        f.seek(start + 3)
139
        cur += 3
6973.11.6 by Jelmer Vernooij
Fix more http tests.
140
        self.assertEqual(b'def', f.read(3))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
141
        cur += len('def')
142
        f.seek(4, 1)
143
        cur += 4
6973.11.6 by Jelmer Vernooij
Fix more http tests.
144
        self.assertEqual(b'klmn', f.read(4))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
145
        cur += len('klmn')
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
146
        # read(0) in the middle of a range
6973.11.6 by Jelmer Vernooij
Fix more http tests.
147
        self.assertEqual(b'', f.read(0))
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
148
        # seek in place
149
        here = f.tell()
150
        f.seek(0, 1)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
151
        self.assertEqual(here, f.tell())
152
        self.assertEqual(cur, f.tell())
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
153
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
154
    def test_read_zero(self):
155
        f = self._file
6973.11.6 by Jelmer Vernooij
Fix more http tests.
156
        self.assertEqual(b'', f.read(0))
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
157
        f.seek(10, 1)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
158
        self.assertEqual(b'', f.read(0))
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
159
160
    def test_seek_at_range_end(self):
161
        f = self._file
162
        f.seek(26, 1)
163
164
    def test_read_at_range_end(self):
165
        """Test read behaviour at range end."""
166
        f = self._file
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
167
        self.assertEqual(self.alpha, f.read())
6973.11.6 by Jelmer Vernooij
Fix more http tests.
168
        self.assertEqual(b'', f.read(0))
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
169
        self.assertRaises(errors.InvalidRange, f.read, 1)
170
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
171
    def test_unbounded_read_after_seek(self):
172
        f = self._file
173
        f.seek(24, 1)
174
        # Should not cross ranges
6973.11.6 by Jelmer Vernooij
Fix more http tests.
175
        self.assertEqual(b'yz', f.read())
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
176
177
    def test_seek_backwards(self):
178
        f = self._file
179
        start = self.first_range_start
180
        f.seek(start)
181
        f.read(12)
182
        self.assertRaises(errors.InvalidRange, f.seek, start + 5)
183
184
    def test_seek_outside_single_range(self):
185
        f = self._file
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)
191
192
    def test_read_past_end_of_range(self):
193
        f = self._file
194
        if f._size == -1:
195
            raise tests.TestNotApplicable("Can't check an unknown size")
196
        start = self.first_range_start
197
        f.seek(start + 20)
198
        self.assertRaises(errors.InvalidRange, f.read, 10)
199
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
200
    def test_seek_from_end(self):
201
       """Test seeking from the end of the file.
202
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
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
205
       unknown and is not used in breezy itself. This test must be (and is)
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
206
       overridden by daughter classes.
207
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
211
       a single range.
212
       """
213
       f = self._file
214
       f.seek(-2, 2)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
215
       self.assertEqual(b'yz', f.read())
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
216
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
217
218
class TestRangeFileSizeUnknown(tests.TestCase, TestRangeFileMixin):
219
    """Test a RangeFile for a whole file whose size is not known."""
220
221
    def setUp(self):
222
        super(TestRangeFileSizeUnknown, self).setUp()
223
        self._file = response.RangeFile('Whole_file_size_known',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
224
                                        BytesIO(self.alpha))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
225
        # We define no range, relying on RangeFile to provide default values
226
        self.first_range_start = 0 # It's the whole file
227
228
    def test_seek_from_end(self):
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
229
        """See TestRangeFileMixin.test_seek_from_end.
230
231
        The end of the file can't be determined since the size is unknown.
232
        """
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
233
        self.assertRaises(errors.InvalidRange, self._file.seek, -1, 2)
234
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
235
    def test_read_at_range_end(self):
236
        """Test read behaviour at range end."""
237
        f = self._file
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
238
        self.assertEqual(self.alpha, f.read())
6973.11.6 by Jelmer Vernooij
Fix more http tests.
239
        self.assertEqual(b'', f.read(0))
240
        self.assertEqual(b'', f.read(1))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
241
3537.1.1 by Vincent Ladeuil
Fix some more PEP8isms and delete useless import
242
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
243
class TestRangeFileSizeKnown(tests.TestCase, TestRangeFileMixin):
244
    """Test a RangeFile for a whole file whose size is known."""
245
246
    def setUp(self):
247
        super(TestRangeFileSizeKnown, self).setUp()
248
        self._file = response.RangeFile('Whole_file_size_known',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
249
                                        BytesIO(self.alpha))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
250
        self._file.set_range(0, len(self.alpha))
251
        self.first_range_start = 0 # It's the whole file
252
253
254
class TestRangeFileSingleRange(tests.TestCase, TestRangeFileMixin):
255
    """Test a RangeFile for a single range."""
256
257
    def setUp(self):
258
        super(TestRangeFileSingleRange, self).setUp()
259
        self._file = response.RangeFile('Single_range_file',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
260
                                        BytesIO(self.alpha))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
261
        self.first_range_start = 15
262
        self._file.set_range(self.first_range_start, len(self.alpha))
263
264
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
265
    def test_read_before_range(self):
266
        # This can't occur under normal circumstances, we have to force it
267
        f = self._file
268
        f._pos = 0 # Force an invalid pos
269
        self.assertRaises(errors.InvalidRange, f.read, 2)
270
3537.1.1 by Vincent Ladeuil
Fix some more PEP8isms and delete useless import
271
3146.3.2 by Vincent Ladeuil
Fix #179368 by keeping the current range hint on ShortReadvErrors.
272
class TestRangeFileMultipleRanges(tests.TestCase, TestRangeFileMixin):
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
273
    """Test a RangeFile for multiple ranges.
274
275
    The RangeFile used for the tests contains three ranges:
276
277
    - at offset 25: alpha
278
    - at offset 100: alpha
279
    - at offset 126: alpha.upper()
280
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.
283
    """
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
284
285
    # The following is used to represent the boundary paramter defined
286
    # in HTTP response headers and the boundary lines that separate
287
    # multipart content.
288
6973.11.6 by Jelmer Vernooij
Fix more http tests.
289
    boundary = b"separation"
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
290
291
    def setUp(self):
3146.3.2 by Vincent Ladeuil
Fix #179368 by keeping the current range hint on ShortReadvErrors.
292
        super(TestRangeFileMultipleRanges, self).setUp()
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
293
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
294
        boundary = self.boundary
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
295
6973.11.6 by Jelmer Vernooij
Fix more http tests.
296
        content = b''
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
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
301
                              (100, self.alpha),
302
                              (126, self.alpha.upper())]:
303
            content += self._multipart_byterange(part, start, boundary,
304
                                                 file_size)
305
        # Final boundary
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
306
        content += self._boundary_line()
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
307
308
        self._file = response.RangeFile('Multiple_ranges_file',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
309
                                        BytesIO(content))
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
310
        self.set_file_boundary()
311
312
    def _boundary_line(self):
313
        """Helper to build the formatted boundary line."""
6973.11.6 by Jelmer Vernooij
Fix more http tests.
314
        return b'--' + self.boundary + b'\r\n'
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
315
316
    def set_file_boundary(self):
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
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).
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
321
        self._file.set_boundary(self.boundary)
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
322
6973.11.6 by Jelmer Vernooij
Fix more http tests.
323
    def _multipart_byterange(self, data, offset, boundary, file_size=b'*'):
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
324
        """Encode a part of a file as a multipart/byterange MIME type.
325
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
328
        file.
329
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
334
            '*' meaning unknown)
335
336
        :return: a string containing the data encoded as it will appear in the
337
            HTTP response body.
338
        """
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
339
        bline = self._boundary_line()
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
340
        # Each range begins with a boundary line
341
        range = bline
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')
6973.11.6 by Jelmer Vernooij
Fix more http tests.
345
        if isinstance(file_size, int):
346
            file_size = b'%d' % file_size
347
        range += b'Content-Range: bytes %d-%d/%s\r\n' % (offset,
348
                                                         offset+len(data)-1,
349
                                                         file_size)
350
        range += b'\r\n'
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
351
        # Finally the raw bytes
352
        range += data
353
        return range
354
355
    def test_read_all_ranges(self):
356
        f = self._file
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
357
        self.assertEqual(self.alpha, f.read()) # Read first range
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
358
        f.seek(100) # Trigger the second range recognition
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
359
        self.assertEqual(self.alpha, f.read()) # Read second range
360
        self.assertEqual(126, f.tell())
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
361
        f.seek(126) # Start of third range which is also the current pos !
6973.11.6 by Jelmer Vernooij
Fix more http tests.
362
        self.assertEqual(b'A', f.read(1))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
363
        f.seek(10, 1)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
364
        self.assertEqual(b'LMN', f.read(3))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
365
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
366
    def test_seek_from_end(self):
367
        """See TestRangeFileMixin.test_seek_from_end."""
368
        # The actual implementation will seek from end for the first range only
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
369
        # and then fail. Since seeking from end is intended to be used for a
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
370
        # single range only anyway, this test just document the actual
371
        # behaviour.
372
        f = self._file
373
        f.seek(-2, 2)
6973.11.6 by Jelmer Vernooij
Fix more http tests.
374
        self.assertEqual(b'yz', f.read())
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
375
        self.assertRaises(errors.InvalidRange, f.seek, -2, 2)
376
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
377
    def test_seek_into_void(self):
378
        f = self._file
379
        start = self.first_range_start
380
        f.seek(start)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
381
        # Seeking to a point between two ranges is possible (only once) but
382
        # reading there is forbidden
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
383
        f.seek(start + 40)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
384
        # We crossed a range boundary, so now the file is positioned at the
385
        # start of the new range (i.e. trying to seek below 100 will error out)
386
        f.seek(100)
3059.2.7 by Vincent Ladeuil
Allow pycurl users to watch the blinkenlights and fix a bug when ranges are contiguous.
387
        f.seek(125)
388
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
389
    def test_seek_across_ranges(self):
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
390
        f = self._file
391
        f.seek(126) # skip the two first ranges
6973.11.6 by Jelmer Vernooij
Fix more http tests.
392
        self.assertEqual(b'AB', f.read(2))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
393
3146.3.2 by Vincent Ladeuil
Fix #179368 by keeping the current range hint on ShortReadvErrors.
394
    def test_checked_read_dont_overflow_buffers(self):
395
        f = self._file
396
        # We force a very low value to exercise all code paths in _checked_read
397
        f._discarded_buf_size = 8
398
        f.seek(126) # skip the two first ranges
6973.11.6 by Jelmer Vernooij
Fix more http tests.
399
        self.assertEqual(b'AB', f.read(2))
3146.3.2 by Vincent Ladeuil
Fix #179368 by keeping the current range hint on ShortReadvErrors.
400
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
401
    def test_seek_twice_between_ranges(self):
402
        f = self._file
403
        start = self.first_range_start
404
        f.seek(start + 40) # Past the first range but before the second
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
405
        # Now the file is positioned at the second range start (100)
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
406
        self.assertRaises(errors.InvalidRange, f.seek, start + 41)
407
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
408
    def test_seek_at_range_end(self):
409
        """Test seek behavior at range end."""
410
        f = self._file
411
        f.seek(25 + 25)
412
        f.seek(100 + 25)
413
        f.seek(126 + 25)
414
415
    def test_read_at_range_end(self):
416
        f = self._file
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
417
        self.assertEqual(self.alpha, f.read())
418
        self.assertEqual(self.alpha, f.read())
419
        self.assertEqual(self.alpha.upper(), f.read())
3059.2.18 by Vincent Ladeuil
Take spiv review comments into account.
420
        self.assertRaises(errors.InvalidHttpResponse, f.read, 1)
421
3537.1.1 by Vincent Ladeuil
Fix some more PEP8isms and delete useless import
422
3535.1.1 by Adrian Wilkins
Made the behaviour of the existing multi-range test more like the real thing by
423
class TestRangeFileMultipleRangesQuotedBoundaries(TestRangeFileMultipleRanges):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
424
    """Perform the same tests as TestRangeFileMultipleRanges, but uses
3535.1.1 by Adrian Wilkins
Made the behaviour of the existing multi-range test more like the real thing by
425
    an angle-bracket quoted boundary string like IIS 6.0 and 7.0
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
426
    (but not IIS 5, which breaks the RFC in a different way
427
    by using square brackets, not angle brackets)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
428
429
    This reveals a bug caused by
430
431
    - The bad implementation of RFC 822 unquoting in Python (angles are not
432
      quotes), coupled with
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
433
434
    - The bad implementation of RFC 2046 in IIS (angles are not permitted chars
435
      in boundary lines).
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
436
3535.1.1 by Adrian Wilkins
Made the behaviour of the existing multi-range test more like the real thing by
437
    """
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
438
    # The boundary as it appears in boundary lines
439
    # IIS 6 and 7 use this value
6973.11.6 by Jelmer Vernooij
Fix more http tests.
440
    _boundary_trimmed = b"q1w2e3r4t5y6u7i8o9p0zaxscdvfbgnhmjklkl"
441
    boundary = b'<' + _boundary_trimmed + b'>'
3535.1.4 by adwi2
Changes as suggested by Mr Ladeuil.
442
443
    def set_file_boundary(self):
444
        # Emulate broken rfc822.unquote() here by removing angles
445
        self._file.set_boundary(self._boundary_trimmed)
3537.1.1 by Vincent Ladeuil
Fix some more PEP8isms and delete useless import
446
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
447
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
448
class TestRangeFileVarious(tests.TestCase):
449
    """Tests RangeFile aspects not covered elsewhere."""
450
451
    def test_seek_whence(self):
452
        """Test the seek whence parameter values."""
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
453
        f = response.RangeFile('foo', BytesIO(b'abc'))
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
454
        f.set_range(0, 3)
455
        f.seek(0)
456
        f.seek(1, 1)
457
        f.seek(-1, 2)
458
        self.assertRaises(ValueError, f.seek, 0, 14)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
459
460
    def test_range_syntax(self):
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
461
        """Test the Content-Range scanning."""
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
462
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
463
        f = response.RangeFile('foo', BytesIO())
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
464
465
        def ok(expected, header_value):
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
466
            f.set_range_from_header(header_value)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
467
            # Slightly peek under the covers to get the size
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
468
            self.assertEqual(expected, (f.tell(), f._size))
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
469
470
        ok((1, 10), 'bytes 1-10/11')
471
        ok((1, 10), 'bytes 1-10/*')
472
        ok((12, 2), '\tbytes 12-13/*')
473
        ok((28, 1), '  bytes 28-28/*')
474
        ok((2123, 2120), 'bytes  2123-4242/12310')
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
475
        ok((1, 10), 'bytes 1-10/ttt') # We don't check total (ttt)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
476
477
        def nok(header_value):
478
            self.assertRaises(errors.InvalidHttpRange,
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
479
                              f.set_range_from_header, header_value)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
480
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
481
        nok('bytes 10-2/3')
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
482
        nok('chars 1-2/3')
483
        nok('bytes xx-yyy/zzz')
484
        nok('bytes xx-12/zzz')
485
        nok('bytes 11-yy/zzz')
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
486
        nok('bytes10-2/3')
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
487
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
488
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
489
# Taken from real request responses
6973.11.6 by Jelmer Vernooij
Fix more http tests.
490
_full_text_response = (200, b"""HTTP/1.1 200 OK\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
491
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
492
Server: Apache/2.0.54 (Fedora)\r
493
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
494
ETag: "56691-23-38e9ae00"\r
495
Accept-Ranges: bytes\r
496
Content-Length: 35\r
497
Connection: close\r
498
Content-Type: text/plain; charset=UTF-8\r
499
\r
6973.11.6 by Jelmer Vernooij
Fix more http tests.
500
""", b"""Bazaar-NG meta directory, format 1
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
501
""")
502
503
6973.11.6 by Jelmer Vernooij
Fix more http tests.
504
_single_range_response = (206, b"""HTTP/1.1 206 Partial Content\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
505
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
506
Server: Apache/2.0.54 (Fedora)\r
507
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
508
ETag: "238a3c-16ec2-805c5540"\r
509
Accept-Ranges: bytes\r
510
Content-Length: 100\r
1786.1.26 by John Arbash Meinel
Update and test handle_response.
511
Content-Range: bytes 100-199/93890\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
512
Connection: close\r
513
Content-Type: text/plain; charset=UTF-8\r
514
\r
6973.11.6 by Jelmer Vernooij
Fix more http tests.
515
""", b"""mbp@sourcefrog.net-20050309040815-13242001617e4a06
1786.1.26 by John Arbash Meinel
Update and test handle_response.
516
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
517
518
6973.11.6 by Jelmer Vernooij
Fix more http tests.
519
_single_range_no_content_type = (206, b"""HTTP/1.1 206 Partial Content\r
2070.1.1 by John Arbash Meinel
Fix bug #62473 by not requiring content-type in range responses
520
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
521
Server: Apache/2.0.54 (Fedora)\r
522
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
523
ETag: "238a3c-16ec2-805c5540"\r
524
Accept-Ranges: bytes\r
525
Content-Length: 100\r
526
Content-Range: bytes 100-199/93890\r
527
Connection: close\r
528
\r
6973.11.6 by Jelmer Vernooij
Fix more http tests.
529
""", b"""mbp@sourcefrog.net-20050309040815-13242001617e4a06
2070.1.1 by John Arbash Meinel
Fix bug #62473 by not requiring content-type in range responses
530
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
531
532
6973.11.6 by Jelmer Vernooij
Fix more http tests.
533
_multipart_range_response = (206, b"""HTTP/1.1 206 Partial Content\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
534
Date: Tue, 11 Jul 2006 04:49:48 GMT\r
535
Server: Apache/2.0.54 (Fedora)\r
536
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
537
ETag: "238a3c-16ec2-805c5540"\r
538
Accept-Ranges: bytes\r
539
Content-Length: 1534\r
540
Connection: close\r
541
Content-Type: multipart/byteranges; boundary=418470f848b63279b\r
542
\r
6973.11.6 by Jelmer Vernooij
Fix more http tests.
543
\r""", b"""--418470f848b63279b\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
544
Content-type: text/plain; charset=UTF-8\r
545
Content-range: bytes 0-254/93890\r
546
\r
547
mbp@sourcefrog.net-20050309040815-13242001617e4a06
548
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e7627
549
mbp@sourcefrog.net-20050309040957-6cad07f466bb0bb8
550
mbp@sourcefrog.net-20050309041501-c840e09071de3b67
551
mbp@sourcefrog.net-20050309044615-c24a3250be83220a
552
\r
553
--418470f848b63279b\r
554
Content-type: text/plain; charset=UTF-8\r
555
Content-range: bytes 1000-2049/93890\r
556
\r
557
40-fd4ec249b6b139ab
558
mbp@sourcefrog.net-20050311063625-07858525021f270b
559
mbp@sourcefrog.net-20050311231934-aa3776aff5200bb9
560
mbp@sourcefrog.net-20050311231953-73aeb3a131c3699a
561
mbp@sourcefrog.net-20050311232353-f5e33da490872c6a
562
mbp@sourcefrog.net-20050312071639-0a8f59a34a024ff0
563
mbp@sourcefrog.net-20050312073432-b2c16a55e0d6e9fb
564
mbp@sourcefrog.net-20050312073831-a47c3335ece1920f
565
mbp@sourcefrog.net-20050312085412-13373aa129ccbad3
566
mbp@sourcefrog.net-20050313052251-2bf004cb96b39933
567
mbp@sourcefrog.net-20050313052856-3edd84094687cb11
568
mbp@sourcefrog.net-20050313053233-e30a4f28aef48f9d
569
mbp@sourcefrog.net-20050313053853-7c64085594ff3072
570
mbp@sourcefrog.net-20050313054757-a86c3f5871069e22
571
mbp@sourcefrog.net-20050313061422-418f1f73b94879b9
572
mbp@sourcefrog.net-20050313120651-497bd231b19df600
573
mbp@sourcefrog.net-20050314024931-eae0170ef25a5d1a
574
mbp@sourcefrog.net-20050314025438-d52099f915fe65fc
575
mbp@sourcefrog.net-20050314025539-637a636692c055cf
576
mbp@sourcefrog.net-20050314025737-55eb441f430ab4ba
577
mbp@sourcefrog.net-20050314025901-d74aa93bb7ee8f62
578
mbp@source\r
1979.1.1 by John Arbash Meinel
Fix bug #57723, parse boundary="" correctly, since Squid uses it
579
--418470f848b63279b--\r
580
""")
581
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
582
6973.11.6 by Jelmer Vernooij
Fix more http tests.
583
_multipart_squid_range_response = (206, b"""HTTP/1.0 206 Partial Content\r
1979.1.1 by John Arbash Meinel
Fix bug #57723, parse boundary="" correctly, since Squid uses it
584
Date: Thu, 31 Aug 2006 21:16:22 GMT\r
585
Server: Apache/2.2.2 (Unix) DAV/2\r
586
Last-Modified: Thu, 31 Aug 2006 17:57:06 GMT\r
587
Accept-Ranges: bytes\r
588
Content-Type: multipart/byteranges; boundary="squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196"\r
589
Content-Length: 598\r
590
X-Cache: MISS from localhost.localdomain\r
591
X-Cache-Lookup: HIT from localhost.localdomain:3128\r
592
Proxy-Connection: keep-alive\r
593
\r
594
""",
6973.11.6 by Jelmer Vernooij
Fix more http tests.
595
b"""\r
1979.1.1 by John Arbash Meinel
Fix bug #57723, parse boundary="" correctly, since Squid uses it
596
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
597
Content-Type: text/plain\r
598
Content-Range: bytes 0-99/18672\r
599
\r
600
# bzr knit index 8
601
602
scott@netsplit.com-20050708230047-47c7868f276b939f fulltext 0 863  :
603
scott@netsp\r
604
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
605
Content-Type: text/plain\r
606
Content-Range: bytes 300-499/18672\r
607
\r
608
com-20050708231537-2b124b835395399a :
609
scott@netsplit.com-20050820234126-551311dbb7435b51 line-delta 1803 479 .scott@netsplit.com-20050820232911-dc4322a084eadf7e :
610
scott@netsplit.com-20050821213706-c86\r
611
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196--\r
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
612
""")
613
614
1786.1.26 by John Arbash Meinel
Update and test handle_response.
615
# This is made up
6973.11.6 by Jelmer Vernooij
Fix more http tests.
616
_full_text_response_no_content_type = (200, b"""HTTP/1.1 200 OK\r
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
617
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
618
Server: Apache/2.0.54 (Fedora)\r
619
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
620
ETag: "56691-23-38e9ae00"\r
621
Accept-Ranges: bytes\r
622
Content-Length: 35\r
623
Connection: close\r
624
\r
6973.11.6 by Jelmer Vernooij
Fix more http tests.
625
""", b"""Bazaar-NG meta directory, format 1
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
626
""")
627
628
6973.11.6 by Jelmer Vernooij
Fix more http tests.
629
_full_text_response_no_content_length = (200, b"""HTTP/1.1 200 OK\r
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
630
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
631
Server: Apache/2.0.54 (Fedora)\r
632
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
633
ETag: "56691-23-38e9ae00"\r
634
Accept-Ranges: bytes\r
635
Connection: close\r
636
Content-Type: text/plain; charset=UTF-8\r
637
\r
6973.11.6 by Jelmer Vernooij
Fix more http tests.
638
""", b"""Bazaar-NG meta directory, format 1
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
639
""")
640
641
6973.11.6 by Jelmer Vernooij
Fix more http tests.
642
_single_range_no_content_range = (206, b"""HTTP/1.1 206 Partial Content\r
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
643
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
644
Server: Apache/2.0.54 (Fedora)\r
645
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
646
ETag: "238a3c-16ec2-805c5540"\r
647
Accept-Ranges: bytes\r
648
Content-Length: 100\r
649
Connection: close\r
650
\r
6973.11.6 by Jelmer Vernooij
Fix more http tests.
651
""", b"""mbp@sourcefrog.net-20050309040815-13242001617e4a06
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
652
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
653
654
6973.11.6 by Jelmer Vernooij
Fix more http tests.
655
_single_range_response_truncated = (206, b"""HTTP/1.1 206 Partial Content\r
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
656
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
657
Server: Apache/2.0.54 (Fedora)\r
658
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
659
ETag: "238a3c-16ec2-805c5540"\r
660
Accept-Ranges: bytes\r
661
Content-Length: 100\r
662
Content-Range: bytes 100-199/93890\r
663
Connection: close\r
664
Content-Type: text/plain; charset=UTF-8\r
665
\r
6973.11.6 by Jelmer Vernooij
Fix more http tests.
666
""", b"""mbp@sourcefrog.net-20050309040815-13242001617e4a06""")
667
668
669
_invalid_response = (444, b"""HTTP/1.1 444 Bad Response\r
1786.1.26 by John Arbash Meinel
Update and test handle_response.
670
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
671
Connection: close\r
672
Content-Type: text/html; charset=iso-8859-1\r
673
\r
6973.11.6 by Jelmer Vernooij
Fix more http tests.
674
""", b"""<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
1786.1.26 by John Arbash Meinel
Update and test handle_response.
675
<html><head>
676
<title>404 Not Found</title>
677
</head><body>
678
<h1>Not Found</h1>
679
<p>I don't know what I'm doing</p>
680
<hr>
681
</body></html>
682
""")
683
684
6973.11.6 by Jelmer Vernooij
Fix more http tests.
685
_multipart_no_content_range = (206, b"""HTTP/1.0 206 Partial Content\r
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
686
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
687
Content-Length: 598\r
688
\r
689
""",
6973.11.6 by Jelmer Vernooij
Fix more http tests.
690
b"""\r
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
691
--THIS_SEPARATES\r
692
Content-Type: text/plain\r
693
\r
694
# bzr knit index 8
695
--THIS_SEPARATES\r
696
""")
697
698
6973.11.6 by Jelmer Vernooij
Fix more http tests.
699
_multipart_no_boundary = (206, b"""HTTP/1.0 206 Partial Content\r
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
700
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
701
Content-Length: 598\r
702
\r
703
""",
6973.11.6 by Jelmer Vernooij
Fix more http tests.
704
b"""\r
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
705
--THIS_SEPARATES\r
706
Content-Type: text/plain\r
707
Content-Range: bytes 0-18/18672\r
708
\r
709
# bzr knit index 8
710
711
The range ended at the line above, this text is garbage instead of a boundary
712
line
713
""")
714
715
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
716
class TestHandleResponse(tests.TestCase):
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
717
718
    def _build_HTTPMessage(self, raw_headers):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
719
        status_and_headers = BytesIO(raw_headers)
3059.2.11 by Vincent Ladeuil
Fix typos mentioned by spiv.
720
        # Get rid of the status line
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
721
        status_and_headers.readline()
6791.2.3 by Jelmer Vernooij
Fix more imports.
722
        msg = http_client.HTTPMessage(status_and_headers)
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
723
        return msg
724
1786.1.26 by John Arbash Meinel
Update and test handle_response.
725
    def get_response(self, a_response):
726
        """Process a supplied response, and return the result."""
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
727
        code, raw_headers, body = a_response
728
        msg = self._build_HTTPMessage(raw_headers)
729
        return response.handle_response('http://foo', code, msg,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
730
                                        BytesIO(a_response[2]))
1786.1.26 by John Arbash Meinel
Update and test handle_response.
731
732
    def test_full_text(self):
733
        out = self.get_response(_full_text_response)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
734
        # It is a BytesIO from the original data
1786.1.26 by John Arbash Meinel
Update and test handle_response.
735
        self.assertEqual(_full_text_response[2], out.read())
736
737
    def test_single_range(self):
738
        out = self.get_response(_single_range_response)
739
740
        out.seek(100)
741
        self.assertEqual(_single_range_response[2], out.read(100))
742
2070.1.1 by John Arbash Meinel
Fix bug #62473 by not requiring content-type in range responses
743
    def test_single_range_no_content(self):
744
        out = self.get_response(_single_range_no_content_type)
745
746
        out.seek(100)
747
        self.assertEqual(_single_range_no_content_type[2], out.read(100))
748
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
749
    def test_single_range_truncated(self):
750
        out = self.get_response(_single_range_response_truncated)
751
        # Content-Range declares 100 but only 51 present
752
        self.assertRaises(errors.ShortReadvError, out.seek, out.tell() + 51)
753
1786.1.26 by John Arbash Meinel
Update and test handle_response.
754
    def test_multi_range(self):
755
        out = self.get_response(_multipart_range_response)
756
757
        # Just make sure we can read the right contents
758
        out.seek(0)
759
        out.read(255)
760
761
        out.seek(1000)
762
        out.read(1050)
763
1979.1.1 by John Arbash Meinel
Fix bug #57723, parse boundary="" correctly, since Squid uses it
764
    def test_multi_squid_range(self):
765
        out = self.get_response(_multipart_squid_range_response)
766
767
        # Just make sure we can read the right contents
768
        out.seek(0)
769
        out.read(100)
770
771
        out.seek(300)
772
        out.read(200)
773
1786.1.26 by John Arbash Meinel
Update and test handle_response.
774
    def test_invalid_response(self):
775
        self.assertRaises(errors.InvalidHttpResponse,
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
776
                          self.get_response, _invalid_response)
1786.1.26 by John Arbash Meinel
Update and test handle_response.
777
778
    def test_full_text_no_content_type(self):
779
        # We should not require Content-Type for a full response
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
780
        code, raw_headers, body = _full_text_response_no_content_type
781
        msg = self._build_HTTPMessage(raw_headers)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
782
        out = response.handle_response('http://foo', code, msg, BytesIO(body))
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
783
        self.assertEqual(body, out.read())
1786.1.26 by John Arbash Meinel
Update and test handle_response.
784
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
785
    def test_full_text_no_content_length(self):
786
        code, raw_headers, body = _full_text_response_no_content_length
787
        msg = self._build_HTTPMessage(raw_headers)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
788
        out = response.handle_response('http://foo', code, msg, BytesIO(body))
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
789
        self.assertEqual(body, out.read())
790
1786.1.26 by John Arbash Meinel
Update and test handle_response.
791
    def test_missing_content_range(self):
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
792
        code, raw_headers, body = _single_range_no_content_range
793
        msg = self._build_HTTPMessage(raw_headers)
1786.1.26 by John Arbash Meinel
Update and test handle_response.
794
        self.assertRaises(errors.InvalidHttpResponse,
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
795
                          response.handle_response,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
796
                          'http://bogus', code, msg, BytesIO(body))
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
797
798
    def test_multipart_no_content_range(self):
799
        code, raw_headers, body = _multipart_no_content_range
800
        msg = self._build_HTTPMessage(raw_headers)
801
        self.assertRaises(errors.InvalidHttpResponse,
802
                          response.handle_response,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
803
                          'http://bogus', code, msg, BytesIO(body))
3059.2.14 by Vincent Ladeuil
Complete coverage by adding tests for more invalid inputs. Fix a
804
805
    def test_multipart_no_boundary(self):
806
        out = self.get_response(_multipart_no_boundary)
807
        out.read()  # Read the whole range
808
        # Fail to find the boundary line
809
        self.assertRaises(errors.InvalidHttpResponse, out.seek, 1, 1)
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
810
811
812
class TestRangeFileSizeReadLimited(tests.TestCase):
813
    """Test RangeFile _max_read_size functionality which limits the size of
814
    read blocks to prevent MemoryError messages in socket.recv.
815
    """
816
817
    def setUp(self):
6552.1.3 by Vincent Ladeuil
Use super() instead of calling <base>.setup(self), as the original fix illustrated a too-easy-to-fall-into trap.
818
        super(TestRangeFileSizeReadLimited, self).setUp()
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
819
        # create a test datablock larger than _max_read_size.
820
        chunk_size = response.RangeFile._max_read_size
6973.11.6 by Jelmer Vernooij
Fix more http tests.
821
        test_pattern = b'0123456789ABCDEF'
822
        self.test_data =  test_pattern * (3 * chunk_size // len(test_pattern))
3408.6.1 by Eric Holmberg
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
823
        self.test_data_len = len(self.test_data)
824
825
    def test_max_read_size(self):
826
        """Read data in blocks and verify that the reads are not larger than
827
           the maximum read size.
828
        """
829
        # retrieve data in large blocks from response.RangeFile object
830
        mock_read_file = FakeReadFile(self.test_data)
831
        range_file = response.RangeFile('test_max_read_size', mock_read_file)
832
        response_data = range_file.read(self.test_data_len)
833
834
        # verify read size was equal to the maximum read size
835
        self.assertTrue(mock_read_file.get_max_read_size() > 0)
836
        self.assertEqual(mock_read_file.get_max_read_size(),
837
                         response.RangeFile._max_read_size)
838
        self.assertEqual(mock_read_file.get_read_count(), 3)
839
840
        # report error if the data wasn't equal (we only report the size due
841
        # to the length of the data)
842
        if response_data != self.test_data:
843
            message = "Data not equal.  Expected %d bytes, received %d."
844
            self.fail(message % (len(response_data), self.test_data_len))
845