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