~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http_response.py

  • Committer: John Arbash Meinel
  • Date: 2008-07-11 21:41:24 UTC
  • mto: This revision was merged to the branch mainline in revision 3543.
  • Revision ID: john@arbash-meinel.com-20080711214124-qi09irlj7pd5cuzg
Shortcut the case when one revision is in the ancestry of the other.

At the cost of a heads() check, when one parent supersedes, we don't have to extract
the text for the other. Changes merge time from 3m37s => 3m21s. Using a
CachingParentsProvider would drop the time down to 3m11s.

Show diffs side-by-side

added added

removed removed

Lines of Context:
225
225
        self.assertEquals('', f.read(0))
226
226
        self.assertEquals('', f.read(1))
227
227
 
228
 
 
229
228
class TestRangeFileSizeKnown(tests.TestCase, TestRangeFileMixin):
230
229
    """Test a RangeFile for a whole file whose size is known."""
231
230
 
254
253
        f._pos = 0 # Force an invalid pos
255
254
        self.assertRaises(errors.InvalidRange, f.read, 2)
256
255
 
257
 
 
258
256
class TestRangeFileMultipleRanges(tests.TestCase, TestRangeFileMixin):
259
257
    """Test a RangeFile for multiple ranges.
260
258
 
268
266
    fact) in real uses but may lead to hard to track bugs.
269
267
    """
270
268
 
271
 
    # The following is used to represent the boundary paramter defined
272
 
    # in HTTP response headers and the boundary lines that separate
273
 
    # multipart content.
274
 
 
275
 
    boundary = "separation"
276
 
 
277
269
    def setUp(self):
278
270
        super(TestRangeFileMultipleRanges, self).setUp()
279
271
 
280
 
        boundary = self.boundary
 
272
        boundary = 'separation'
281
273
 
282
274
        content = ''
283
275
        self.first_range_start = 25
289
281
            content += self._multipart_byterange(part, start, boundary,
290
282
                                                 file_size)
291
283
        # Final boundary
292
 
        content += self._boundary_line()
 
284
        content += self._boundary_line(boundary)
293
285
 
294
286
        self._file = response.RangeFile('Multiple_ranges_file',
295
287
                                        StringIO(content))
296
 
        self.set_file_boundary()
297
 
 
298
 
    def _boundary_line(self):
299
 
        """Helper to build the formatted boundary line."""
300
 
        return '--' + self.boundary + '\r\n'
301
 
 
302
 
    def set_file_boundary(self):
303
288
        # Ranges are set by decoding the range headers, the RangeFile user is
304
289
        # supposed to call the following before using seek or read since it
305
290
        # requires knowing the *response* headers (in that case the boundary
306
291
        # which is part of the Content-Type header).
307
 
        self._file.set_boundary(self.boundary)
 
292
        self._file.set_boundary(boundary)
 
293
 
 
294
    def _boundary_line(self, boundary):
 
295
        """Helper to build the formatted boundary line."""
 
296
        return '--' + boundary + '\r\n'
308
297
 
309
298
    def _multipart_byterange(self, data, offset, boundary, file_size='*'):
310
299
        """Encode a part of a file as a multipart/byterange MIME type.
322
311
        :return: a string containing the data encoded as it will appear in the
323
312
            HTTP response body.
324
313
        """
325
 
        bline = self._boundary_line()
 
314
        bline = self._boundary_line(boundary)
326
315
        # Each range begins with a boundary line
327
316
        range = bline
328
317
        # A range is described by a set of headers, but only 'Content-Range' is
406
395
        self.assertRaises(errors.InvalidHttpResponse, f.read, 1)
407
396
 
408
397
 
409
 
class TestRangeFileMultipleRangesQuotedBoundaries(TestRangeFileMultipleRanges):
410
 
    """Perform the same tests as TestRangeFileMultipleRanges, but uses 
411
 
    an angle-bracket quoted boundary string like IIS 6.0 and 7.0
412
 
    (but not IIS 5, which breaks the RFC in a different way
413
 
    by using square brackets, not angle brackets)
414
 
    
415
 
    This reveals a bug caused by 
416
 
    
417
 
    - The bad implementation of RFC 822 unquoting in Python (angles are not 
418
 
      quotes), coupled with 
419
 
 
420
 
    - The bad implementation of RFC 2046 in IIS (angles are not permitted chars
421
 
      in boundary lines).
422
 
 
423
 
    """
424
 
    # The boundary as it appears in boundary lines
425
 
    # IIS 6 and 7 use this value
426
 
    _boundary_trimmed = "q1w2e3r4t5y6u7i8o9p0zaxscdvfbgnhmjklkl"
427
 
    boundary = '<' + _boundary_trimmed + '>'
428
 
 
429
 
    def set_file_boundary(self):
430
 
        # Emulate broken rfc822.unquote() here by removing angles
431
 
        self._file.set_boundary(self._boundary_trimmed)
432
 
 
433
 
 
434
398
class TestRangeFileVarious(tests.TestCase):
435
399
    """Tests RangeFile aspects not covered elsewhere."""
436
400