128
127
('urllib,http', dict(_activity_server=ActivityHTTPServer,
129
128
_transport=_urllib.HttpTransport_urllib,)),
130
if features.pycurl.available():
131
activity_scenarios.append(
132
('pycurl,http', dict(_activity_server=ActivityHTTPServer,
133
_transport=PyCurlTransport,)),)
131
134
if features.HTTPSServerFeature.available():
135
# FIXME: Until we have a better way to handle self-signed certificates
136
# (like allowing them in a test specific authentication.conf for
137
# example), we need some specialized pycurl/urllib transport for tests.
139
from bzrlib.tests import (
142
class HTTPS_urllib_transport(_urllib.HttpTransport_urllib):
144
def __init__(self, base, _from_transport=None):
145
super(HTTPS_urllib_transport, self).__init__(
146
base, _from_transport=_from_transport,
147
ca_certs=ssl_certs.build_path('ca.crt'))
132
149
activity_scenarios.append(
133
150
('urllib,https', dict(_activity_server=ActivityHTTPSServer,
134
_transport=_urllib.HttpTransport_urllib,)),)
135
if features.pycurl.available():
136
activity_scenarios.append(
137
('pycurl,http', dict(_activity_server=ActivityHTTPServer,
138
_transport=PyCurlTransport,)),)
139
if features.HTTPSServerFeature.available():
140
from bzrlib.tests import (
143
# FIXME: Until we have a better way to handle self-signed
144
# certificates (like allowing them in a test specific
145
# authentication.conf for example), we need some specialized pycurl
146
# transport for tests.
151
_transport=HTTPS_urllib_transport,)),)
152
if features.pycurl.available():
147
153
class HTTPS_pycurl_transport(PyCurlTransport):
149
155
def __init__(self, base, _from_transport=None):
282
288
self.req_handler = RequestHandler(None, None, None)
284
290
def assertRanges(self, ranges, header, file_size):
285
self.assertEquals(ranges,
291
self.assertEqual(ranges,
286
292
self.req_handler._parse_ranges(header, file_size))
288
294
def test_simple_range(self):
378
384
_transport = property(_get_pycurl_maybe)
381
class TestHttpUrls(tests.TestCase):
383
# TODO: This should be moved to authorization tests once they
386
def test_url_parsing(self):
388
url = http.extract_auth('http://example.com', f)
389
self.assertEqual('http://example.com', url)
390
self.assertEqual(0, len(f.credentials))
391
url = http.extract_auth(
392
'http://user:pass@example.com/bzr/bzr.dev', f)
393
self.assertEqual('http://example.com/bzr/bzr.dev', url)
394
self.assertEqual(1, len(f.credentials))
395
self.assertEqual([None, 'example.com', 'user', 'pass'],
399
387
class TestHttpTransportUrls(tests.TestCase):
400
388
"""Test the http urls."""
669
657
_req_handler_class = BadStatusRequestHandler
660
super(TestBadStatusServer, self).setUp()
661
# See https://bugs.launchpad.net/bzr/+bug/1451448 for details.
662
# TD;LR: Running both a TCP client and server in the same process and
663
# thread uncovers a race in python. The fix is to run the server in a
664
# different process. Trying to fix yet another race here is not worth
665
# the effort. -- vila 2015-09-06
666
if 'HTTP/1.0' in self.id():
667
raise tests.TestSkipped(
668
'Client/Server in the same process and thread can hang')
671
670
def test_http_has(self):
672
671
t = self.get_readonly_transport()
673
self.assertRaises(errors.InvalidHttpResponse, t.has, 'foo/bar')
672
self.assertRaises((errors.ConnectionError, errors.ConnectionReset,
673
errors.InvalidHttpResponse),
675
676
def test_http_get(self):
676
677
t = self.get_readonly_transport()
677
self.assertRaises(errors.InvalidHttpResponse, t.get, 'foo/bar')
678
self.assertRaises((errors.ConnectionError, errors.ConnectionReset,
679
errors.InvalidHttpResponse),
680
683
class InvalidStatusRequestHandler(http_server.TestingHTTPRequestHandler):
1157
1160
protocol_version=self._protocol_version)
1159
1162
def setUp(self):
1160
http_utils.TestCaseWithWebserver.setUp(self)
1163
super(TestLimitedRangeRequestServer, self).setUp()
1161
1164
# We need to manipulate ranges that correspond to real chunks in the
1162
1165
# response, so we build a content appropriately.
1163
1166
filler = ''.join(['abcdefghij' for x in range(102)])
1198
1201
def assertEvaluateProxyBypass(self, expected, host, no_proxy):
1199
1202
handler = _urllib2_wrappers.ProxyHandler()
1200
self.assertEquals(expected,
1203
self.assertEqual(expected,
1201
1204
handler.evaluate_proxy_bypass(host, no_proxy))
1203
1206
def test_empty_user(self):
1905
1908
server._url_protocol = self._url_protocol
1908
def test_open_bzrdir(self):
1911
def test_open_controldir(self):
1909
1912
branch = self.make_branch('relpath')
1910
1913
url = self.http_server.get_url() + 'relpath'
1911
bd = bzrdir.BzrDir.open(url)
1914
bd = controldir.ControlDir.open(url)
1912
1915
self.addCleanup(bd.transport.disconnect)
1913
1916
self.assertIsInstance(bd, _mod_remote.RemoteBzrDir)
1997
2000
self.assertIsInstance(r, type(t))
1998
2001
# Both transports share the some connection
1999
2002
self.assertEqual(t._get_connection(), r._get_connection())
2000
self.assertEquals('http://www.example.com/foo/subdir/', r.base)
2003
self.assertEqual('http://www.example.com/foo/subdir/', r.base)
2002
2005
def test_redirected_to_self_with_slash(self):
2003
2006
t = self._transport('http://www.example.com/foo')
2014
2017
r = t._redirected_to('http://www.example.com/foo',
2015
2018
'http://foo.example.com/foo/subdir')
2016
2019
self.assertIsInstance(r, type(t))
2017
self.assertEquals('http://foo.example.com/foo/subdir/',
2020
self.assertEqual('http://foo.example.com/foo/subdir/',
2018
2021
r.external_url())
2020
2023
def test_redirected_to_same_host_sibling_protocol(self):
2022
2025
r = t._redirected_to('http://www.example.com/foo',
2023
2026
'https://www.example.com/foo')
2024
2027
self.assertIsInstance(r, type(t))
2025
self.assertEquals('https://www.example.com/foo/',
2028
self.assertEqual('https://www.example.com/foo/',
2026
2029
r.external_url())
2028
2031
def test_redirected_to_same_host_different_protocol(self):
2029
2032
t = self._transport('http://www.example.com/foo')
2030
2033
r = t._redirected_to('http://www.example.com/foo',
2031
2034
'ftp://www.example.com/foo')
2032
self.assertNotEquals(type(r), type(t))
2033
self.assertEquals('ftp://www.example.com/foo/', r.external_url())
2035
self.assertNotEqual(type(r), type(t))
2036
self.assertEqual('ftp://www.example.com/foo/', r.external_url())
2035
2038
def test_redirected_to_same_host_specific_implementation(self):
2036
2039
t = self._transport('http://www.example.com/foo')
2037
2040
r = t._redirected_to('http://www.example.com/foo',
2038
2041
'https+urllib://www.example.com/foo')
2039
self.assertEquals('https://www.example.com/foo/', r.external_url())
2042
self.assertEqual('https://www.example.com/foo/', r.external_url())
2041
2044
def test_redirected_to_different_host_same_user(self):
2042
2045
t = self._transport('http://joe@www.example.com/foo')
2044
2047
'https://foo.example.com/foo')
2045
2048
self.assertIsInstance(r, type(t))
2046
2049
self.assertEqual(t._parsed_url.user, r._parsed_url.user)
2047
self.assertEquals('https://joe@foo.example.com/foo/', r.external_url())
2050
self.assertEqual('https://joe@foo.example.com/foo/', r.external_url())
2050
2053
class PredefinedRequestHandler(http_server.TestingHTTPRequestHandler):
2119
2122
def setUp(self):
2120
tests.TestCase.setUp(self)
2121
2123
self.server = self._activity_server(self._protocol_version)
2122
2124
self.server.start_server()
2125
self.addCleanup(self.server.stop_server)
2123
2126
_activities = {} # Don't close over self and create a cycle
2124
2127
def report_activity(t, bytes, direction):
2125
2128
count = _activities.get(direction, 0)
2127
2130
_activities[direction] = count
2128
2131
self.activities = _activities
2130
2132
# We override at class level because constructors may propagate the
2131
2133
# bound method and render instance overriding ineffective (an
2132
2134
# alternative would be to define a specific ui factory instead...)
2133
2135
self.overrideAttr(self._transport, '_report_activity', report_activity)
2134
self.addCleanup(self.server.stop_server)
2136
2137
def get_transport(self):
2137
2138
t = self._transport(self.server.get_url())