1073
1073
Only the urllib implementation is tested here.
1077
tests.TestCase.setUp(self)
1079
self.addCleanup(self._restore_env)
1081
def _install_env(self, env):
1082
for name, value in env.iteritems():
1083
self._old_env[name] = osutils.set_or_unset_env(name, value)
1085
def _restore_env(self):
1086
for name, value in self._old_env.iteritems():
1087
osutils.set_or_unset_env(name, value)
1089
1076
def _proxied_request(self):
1090
1077
handler = _urllib2_wrappers.ProxyHandler()
1091
1078
request = _urllib2_wrappers.Request('GET','http://baz/buzzle')
1092
1079
handler.set_proxy(request, 'http')
1082
def assertEvaluateProxyBypass(self, expected, host, no_proxy):
1083
handler = _urllib2_wrappers.ProxyHandler()
1084
self.assertEquals(expected,
1085
handler.evaluate_proxy_bypass(host, no_proxy))
1095
1087
def test_empty_user(self):
1096
self._install_env({'http_proxy': 'http://bar.com'})
1088
self.overrideEnv('http_proxy', 'http://bar.com')
1097
1089
request = self._proxied_request()
1098
1090
self.assertFalse(request.headers.has_key('Proxy-authorization'))
1100
1092
def test_invalid_proxy(self):
1101
1093
"""A proxy env variable without scheme"""
1102
self._install_env({'http_proxy': 'host:1234'})
1094
self.overrideEnv('http_proxy', 'host:1234')
1103
1095
self.assertRaises(errors.InvalidURL, self._proxied_request)
1097
def test_evaluate_proxy_bypass_true(self):
1098
"""The host is not proxied"""
1099
self.assertEvaluateProxyBypass(True, 'example.com', 'example.com')
1100
self.assertEvaluateProxyBypass(True, 'bzr.example.com', '*example.com')
1102
def test_evaluate_proxy_bypass_false(self):
1103
"""The host is proxied"""
1104
self.assertEvaluateProxyBypass(False, 'bzr.example.com', None)
1106
def test_evaluate_proxy_bypass_unknown(self):
1107
"""The host is not explicitly proxied"""
1108
self.assertEvaluateProxyBypass(None, 'example.com', 'not.example.com')
1109
self.assertEvaluateProxyBypass(None, 'bzr.example.com', 'example.com')
1111
def test_evaluate_proxy_bypass_empty_entries(self):
1112
"""Ignore empty entries"""
1113
self.assertEvaluateProxyBypass(None, 'example.com', '')
1114
self.assertEvaluateProxyBypass(None, 'example.com', ',')
1115
self.assertEvaluateProxyBypass(None, 'example.com', 'foo,,bar')
1106
1118
class TestProxyHttpServer(http_utils.TestCaseWithTwoWebservers):
1107
1119
"""Tests proxy server.
1136
1148
self.no_proxy_host = self.server_host_port
1137
1149
# The secondary server is the proxy
1138
1150
self.proxy_url = self.get_secondary_url()
1141
1152
def _testing_pycurl(self):
1142
1153
# TODO: This is duplicated for lots of the classes in this file
1143
1154
return (features.pycurl.available()
1144
1155
and self._transport == PyCurlTransport)
1146
def _install_env(self, env):
1147
for name, value in env.iteritems():
1148
self._old_env[name] = osutils.set_or_unset_env(name, value)
1150
def _restore_env(self):
1151
for name, value in self._old_env.iteritems():
1152
osutils.set_or_unset_env(name, value)
1154
def proxied_in_env(self, env):
1155
self._install_env(env)
1156
t = self.get_readonly_transport()
1158
self.assertEqual('proxied contents of foo\n', t.get('foo').read())
1162
def not_proxied_in_env(self, env):
1163
self._install_env(env)
1164
t = self.get_readonly_transport()
1166
self.assertEqual('contents of foo\n', t.get('foo').read())
1157
def assertProxied(self):
1158
t = self.get_readonly_transport()
1159
self.assertEqual('proxied contents of foo\n', t.get('foo').read())
1161
def assertNotProxied(self):
1162
t = self.get_readonly_transport()
1163
self.assertEqual('contents of foo\n', t.get('foo').read())
1170
1165
def test_http_proxy(self):
1171
self.proxied_in_env({'http_proxy': self.proxy_url})
1166
self.overrideEnv('http_proxy', self.proxy_url)
1167
self.assertProxied()
1173
1169
def test_HTTP_PROXY(self):
1174
1170
if self._testing_pycurl():
1177
1173
# about. Should we ?)
1178
1174
raise tests.TestNotApplicable(
1179
1175
'pycurl does not check HTTP_PROXY for security reasons')
1180
self.proxied_in_env({'HTTP_PROXY': self.proxy_url})
1176
self.overrideEnv('HTTP_PROXY', self.proxy_url)
1177
self.assertProxied()
1182
1179
def test_all_proxy(self):
1183
self.proxied_in_env({'all_proxy': self.proxy_url})
1180
self.overrideEnv('all_proxy', self.proxy_url)
1181
self.assertProxied()
1185
1183
def test_ALL_PROXY(self):
1186
self.proxied_in_env({'ALL_PROXY': self.proxy_url})
1184
self.overrideEnv('ALL_PROXY', self.proxy_url)
1185
self.assertProxied()
1188
1187
def test_http_proxy_with_no_proxy(self):
1189
self.not_proxied_in_env({'http_proxy': self.proxy_url,
1190
'no_proxy': self.no_proxy_host})
1188
self.overrideEnv('no_proxy', self.no_proxy_host)
1189
self.overrideEnv('http_proxy', self.proxy_url)
1190
self.assertNotProxied()
1192
1192
def test_HTTP_PROXY_with_NO_PROXY(self):
1193
1193
if self._testing_pycurl():
1194
1194
raise tests.TestNotApplicable(
1195
1195
'pycurl does not check HTTP_PROXY for security reasons')
1196
self.not_proxied_in_env({'HTTP_PROXY': self.proxy_url,
1197
'NO_PROXY': self.no_proxy_host})
1196
self.overrideEnv('NO_PROXY', self.no_proxy_host)
1197
self.overrideEnv('HTTP_PROXY', self.proxy_url)
1198
self.assertNotProxied()
1199
1200
def test_all_proxy_with_no_proxy(self):
1200
self.not_proxied_in_env({'all_proxy': self.proxy_url,
1201
'no_proxy': self.no_proxy_host})
1201
self.overrideEnv('no_proxy', self.no_proxy_host)
1202
self.overrideEnv('all_proxy', self.proxy_url)
1203
self.assertNotProxied()
1203
1205
def test_ALL_PROXY_with_NO_PROXY(self):
1204
self.not_proxied_in_env({'ALL_PROXY': self.proxy_url,
1205
'NO_PROXY': self.no_proxy_host})
1206
self.overrideEnv('NO_PROXY', self.no_proxy_host)
1207
self.overrideEnv('ALL_PROXY', self.proxy_url)
1208
self.assertNotProxied()
1207
1210
def test_http_proxy_without_scheme(self):
1211
self.overrideEnv('http_proxy', self.server_host_port)
1208
1212
if self._testing_pycurl():
1209
1213
# pycurl *ignores* invalid proxy env variables. If that ever change
1210
1214
# in the future, this test will fail indicating that pycurl do not
1211
1215
# ignore anymore such variables.
1212
self.not_proxied_in_env({'http_proxy': self.server_host_port})
1216
self.assertNotProxied()
1214
self.assertRaises(errors.InvalidURL,
1215
self.proxied_in_env,
1216
{'http_proxy': self.server_host_port})
1218
self.assertRaises(errors.InvalidURL, self.assertProxied)
1219
1221
class TestRanges(http_utils.TestCaseWithWebserver):
1719
1719
def get_user_transport(self, user, password):
1720
self._install_env({'all_proxy': self.get_user_url(user, password)})
1720
self.overrideEnv('all_proxy', self.get_user_url(user, password))
1721
1721
return TestAuth.get_user_transport(self, user, password)
1723
def _install_env(self, env):
1724
for name, value in env.iteritems():
1725
self._old_env[name] = osutils.set_or_unset_env(name, value)
1727
def _restore_env(self):
1728
for name, value in self._old_env.iteritems():
1729
osutils.set_or_unset_env(name, value)
1731
1723
def test_empty_pass(self):
1732
1724
if self._testing_pycurl():