/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_http_response.py

  • Committer: Jelmer Vernooij
  • Date: 2017-08-07 11:49:46 UTC
  • mto: (6747.3.4 avoid-set-revid-3)
  • mto: This revision was merged to the branch mainline in revision 6750.
  • Revision ID: jelmer@jelmer.uk-20170807114946-luclmxuawyzhpiot
Avoid setting revision_ids.

Show diffs side-by-side

added added

removed removed

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