128
128
('urllib,http', dict(_activity_server=ActivityHTTPServer,
129
129
_transport=_urllib.HttpTransport_urllib,)),
131
if features.HTTPSServerFeature.available():
131
if tests.HTTPSServerFeature.available():
132
132
activity_scenarios.append(
133
133
('urllib,https', dict(_activity_server=ActivityHTTPSServer,
134
134
_transport=_urllib.HttpTransport_urllib,)),)
1049
1048
self.assertEqual('single', t._range_hint)
1052
class TruncatedBeforeBoundaryRequestHandler(
1053
http_server.TestingHTTPRequestHandler):
1054
"""Truncation before a boundary, like in bug 198646"""
1056
_truncated_ranges = 1
1058
def get_multiple_ranges(self, file, file_size, ranges):
1059
self.send_response(206)
1060
self.send_header('Accept-Ranges', 'bytes')
1062
self.send_header('Content-Type',
1063
'multipart/byteranges; boundary=%s' % boundary)
1064
boundary_line = '--%s\r\n' % boundary
1065
# Calculate the Content-Length
1067
for (start, end) in ranges:
1068
content_length += len(boundary_line)
1069
content_length += self._header_line_length(
1070
'Content-type', 'application/octet-stream')
1071
content_length += self._header_line_length(
1072
'Content-Range', 'bytes %d-%d/%d' % (start, end, file_size))
1073
content_length += len('\r\n') # end headers
1074
content_length += end - start # + 1
1075
content_length += len(boundary_line)
1076
self.send_header('Content-length', content_length)
1079
# Send the multipart body
1081
for (start, end) in ranges:
1082
if cur + self._truncated_ranges >= len(ranges):
1083
# Abruptly ends the response and close the connection
1084
self.close_connection = 1
1086
self.wfile.write(boundary_line)
1087
self.send_header('Content-type', 'application/octet-stream')
1088
self.send_header('Content-Range', 'bytes %d-%d/%d'
1089
% (start, end, file_size))
1091
self.send_range_content(file, start, end - start + 1)
1094
self.wfile.write(boundary_line)
1097
class TestTruncatedBeforeBoundary(TestSpecificRequestHandler):
1098
"""Tests the case of bug 198646, disconnecting before a boundary."""
1100
_req_handler_class = TruncatedBeforeBoundaryRequestHandler
1103
super(TestTruncatedBeforeBoundary, self).setUp()
1104
self.build_tree_contents([('a', '0123456789')],)
1106
def test_readv_with_short_reads(self):
1107
server = self.get_readonly_server()
1108
t = self.get_readonly_transport()
1109
# Force separate ranges for each offset
1110
t._bytes_to_read_before_seek = 0
1111
ireadv = iter(t.readv('a', ((0, 1), (2, 1), (4, 2), (9, 1))))
1112
self.assertEqual((0, '0'), ireadv.next())
1113
self.assertEqual((2, '2'), ireadv.next())
1114
self.assertEqual((4, '45'), ireadv.next())
1115
self.assertEqual((9, '9'), ireadv.next())
1118
1051
class LimitedRangeRequestHandler(http_server.TestingHTTPRequestHandler):
1119
1052
"""Errors out when range specifiers exceed the limit"""
1917
1849
# The 'readv' command in the smart protocol both sends and receives
1918
1850
# bulk data, so we use that.
1919
1851
self.build_tree(['data-file'])
1920
http_transport = transport.get_transport_from_url(
1921
self.http_server.get_url())
1852
http_transport = transport.get_transport(self.http_server.get_url())
1922
1853
medium = http_transport.get_smart_medium()
1923
1854
# Since we provide the medium, the url below will be mostly ignored
1924
1855
# during the test, as long as the path is '/'.
2014
1943
r = t._redirected_to('http://www.example.com/foo',
2015
1944
'http://foo.example.com/foo/subdir')
2016
1945
self.assertIsInstance(r, type(t))
2017
self.assertEquals('http://foo.example.com/foo/subdir/',
2020
1947
def test_redirected_to_same_host_sibling_protocol(self):
2021
1948
t = self._transport('http://www.example.com/foo')
2022
1949
r = t._redirected_to('http://www.example.com/foo',
2023
1950
'https://www.example.com/foo')
2024
1951
self.assertIsInstance(r, type(t))
2025
self.assertEquals('https://www.example.com/foo/',
2028
1953
def test_redirected_to_same_host_different_protocol(self):
2029
1954
t = self._transport('http://www.example.com/foo')
2030
1955
r = t._redirected_to('http://www.example.com/foo',
2031
1956
'ftp://www.example.com/foo')
2032
1957
self.assertNotEquals(type(r), type(t))
2033
self.assertEquals('ftp://www.example.com/foo/', r.external_url())
2035
def test_redirected_to_same_host_specific_implementation(self):
2036
t = self._transport('http://www.example.com/foo')
2037
r = t._redirected_to('http://www.example.com/foo',
2038
'https+urllib://www.example.com/foo')
2039
self.assertEquals('https://www.example.com/foo/', r.external_url())
2041
1959
def test_redirected_to_different_host_same_user(self):
2042
1960
t = self._transport('http://joe@www.example.com/foo')
2043
1961
r = t._redirected_to('http://www.example.com/foo',
2044
1962
'https://foo.example.com/foo')
2045
1963
self.assertIsInstance(r, type(t))
2046
self.assertEqual(t._parsed_url.user, r._parsed_url.user)
2047
self.assertEquals('https://joe@foo.example.com/foo/', r.external_url())
1964
self.assertEqual(t._user, r._user)
2050
1967
class PredefinedRequestHandler(http_server.TestingHTTPRequestHandler):