~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-02 04:09:08 UTC
  • mfrom: (6046.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20110802040908-hqz2wr82nyrk14gk
(mbp) merge 2.3 and 2.4 to trunk (Martin Pool)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1048
1048
        self.assertEqual('single', t._range_hint)
1049
1049
 
1050
1050
 
 
1051
class TruncatedBeforeBoundaryRequestHandler(
 
1052
    http_server.TestingHTTPRequestHandler):
 
1053
    """Truncation before a boundary, like in bug 198646"""
 
1054
 
 
1055
    _truncated_ranges = 1
 
1056
 
 
1057
    def get_multiple_ranges(self, file, file_size, ranges):
 
1058
        self.send_response(206)
 
1059
        self.send_header('Accept-Ranges', 'bytes')
 
1060
        boundary = 'tagada'
 
1061
        self.send_header('Content-Type',
 
1062
                         'multipart/byteranges; boundary=%s' % boundary)
 
1063
        boundary_line = '--%s\r\n' % boundary
 
1064
        # Calculate the Content-Length
 
1065
        content_length = 0
 
1066
        for (start, end) in ranges:
 
1067
            content_length += len(boundary_line)
 
1068
            content_length += self._header_line_length(
 
1069
                'Content-type', 'application/octet-stream')
 
1070
            content_length += self._header_line_length(
 
1071
                'Content-Range', 'bytes %d-%d/%d' % (start, end, file_size))
 
1072
            content_length += len('\r\n') # end headers
 
1073
            content_length += end - start # + 1
 
1074
        content_length += len(boundary_line)
 
1075
        self.send_header('Content-length', content_length)
 
1076
        self.end_headers()
 
1077
 
 
1078
        # Send the multipart body
 
1079
        cur = 0
 
1080
        for (start, end) in ranges:
 
1081
            if cur + self._truncated_ranges >= len(ranges):
 
1082
                # Abruptly ends the response and close the connection
 
1083
                self.close_connection = 1
 
1084
                return
 
1085
            self.wfile.write(boundary_line)
 
1086
            self.send_header('Content-type', 'application/octet-stream')
 
1087
            self.send_header('Content-Range', 'bytes %d-%d/%d'
 
1088
                             % (start, end, file_size))
 
1089
            self.end_headers()
 
1090
            self.send_range_content(file, start, end - start + 1)
 
1091
            cur += 1
 
1092
        # Final boundary
 
1093
        self.wfile.write(boundary_line)
 
1094
 
 
1095
 
 
1096
class TestTruncatedBeforeBoundary(TestSpecificRequestHandler):
 
1097
    """Tests the case of bug 198646, disconnecting before a boundary."""
 
1098
 
 
1099
    _req_handler_class = TruncatedBeforeBoundaryRequestHandler
 
1100
 
 
1101
    def setUp(self):
 
1102
        super(TestTruncatedBeforeBoundary, self).setUp()
 
1103
        self.build_tree_contents([('a', '0123456789')],)
 
1104
 
 
1105
    def test_readv_with_short_reads(self):
 
1106
        server = self.get_readonly_server()
 
1107
        t = self.get_readonly_transport()
 
1108
        # Force separate ranges for each offset
 
1109
        t._bytes_to_read_before_seek = 0
 
1110
        ireadv = iter(t.readv('a', ((0, 1), (2, 1), (4, 2), (9, 1))))
 
1111
        self.assertEqual((0, '0'), ireadv.next())
 
1112
        self.assertEqual((2, '2'), ireadv.next())
 
1113
        self.assertEqual((4, '45'), ireadv.next())
 
1114
        self.assertEqual((9, '9'), ireadv.next())
 
1115
 
 
1116
 
1051
1117
class LimitedRangeRequestHandler(http_server.TestingHTTPRequestHandler):
1052
1118
    """Errors out when range specifiers exceed the limit"""
1053
1119