621
636
"""Tests range requests refusing server for pycurl implementation"""
639
class TestProxyHttpServer(object):
640
"""Tests proxy server.
642
This MUST be used by daughter classes that also inherit from
643
TestCaseWithTwoWebservers.
645
We can't inherit directly from TestCaseWithTwoWebservers or
646
the test framework will try to create an instance which
647
cannot run, its implementation being incomplete.
649
Be aware that we do not setup a real proxy here. Instead, we
650
check that the *connection* goes through the proxy by serving
651
different content (the faked proxy server append '-proxied'
655
# FIXME: We don't have an https server available, so we don't
656
# test https connections.
659
TestCaseWithTwoWebservers.setUp(self)
660
self.build_tree_contents([('foo', 'contents of foo\n'),
661
('foo-proxied', 'proxied contents of foo\n')])
662
# Let's setup some attributes for tests
663
self.server = self.get_readonly_server()
664
self.no_proxy_host = 'localhost:%d' % self.server.port
665
# The secondary server is the proxy
666
self.proxy = self.get_secondary_server()
667
self.proxy_url = self.proxy.get_url()
670
def create_transport_secondary_server(self):
671
"""Creates an http server that will serve files with
672
'-proxied' appended to their names.
674
return HttpServer(FakeProxyRequestHandler)
676
def _set_and_capture_env_var(self, name, new_value):
677
"""Set an environment variable, and reset it when finished."""
678
self._old_env[name] = osutils.set_or_unset_env(name, new_value)
680
def _install_env(self, env):
681
for name, value in env.iteritems():
682
self._set_and_capture_env_var(name, value)
684
def _restore_env(self):
685
for name, value in self._old_env.iteritems():
686
osutils.set_or_unset_env(name, value)
688
def proxied_in_env(self, env):
689
self._install_env(env)
690
url = self.server.get_url()
691
t = self._transport(url)
693
self.assertEqual(t.get('foo').read(), 'proxied contents of foo\n')
697
def not_proxied_in_env(self, env):
698
self._install_env(env)
699
url = self.server.get_url()
700
t = self._transport(url)
702
self.assertEqual(t.get('foo').read(), 'contents of foo\n')
706
def test_http_proxy(self):
707
self.proxied_in_env({'http_proxy': self.proxy_url})
709
def test_HTTP_PROXY(self):
710
self.proxied_in_env({'HTTP_PROXY': self.proxy_url})
712
def test_all_proxy(self):
713
self.proxied_in_env({'all_proxy': self.proxy_url})
715
def test_ALL_PROXY(self):
716
self.proxied_in_env({'ALL_PROXY': self.proxy_url})
718
def test_http_proxy_with_no_proxy(self):
719
self.not_proxied_in_env({'http_proxy': self.proxy_url,
720
'no_proxy': self.no_proxy_host})
722
def test_HTTP_PROXY_with_NO_PROXY(self):
723
self.not_proxied_in_env({'HTTP_PROXY': self.proxy_url,
724
'NO_PROXY': self.no_proxy_host})
726
def test_all_proxy_with_no_proxy(self):
727
self.not_proxied_in_env({'all_proxy': self.proxy_url,
728
'no_proxy': self.no_proxy_host})
730
def test_ALL_PROXY_with_NO_PROXY(self):
731
self.not_proxied_in_env({'ALL_PROXY': self.proxy_url,
732
'NO_PROXY': self.no_proxy_host})
735
class TestProxyHttpServer_urllib(TestProxyHttpServer,
736
TestCaseWithTwoWebservers):
737
"""Tests proxy server for urllib implementation"""
739
_transport = HttpTransport_urllib
742
class TestProxyHttpServer_pycurl(TestWithTransport_pycurl,
744
TestCaseWithTwoWebservers):
745
"""Tests proxy server for pycurl implementation"""
748
TestProxyHttpServer.setUp(self)
749
# Oh my ! pycurl does not check for the port as part of
750
# no_proxy :-( So we just test the host part
751
self.no_proxy_host = 'localhost'
753
def test_HTTP_PROXY(self):
754
# pycurl do not check HTTP_PROXY for security reasons
755
# (for use in a CGI context that we do not care
756
# about. Should we ?)
759
def test_HTTP_PROXY_with_NO_PROXY(self):
763
class TestRanges(object):
764
"""Test the Range header in GET methods..
766
This MUST be used by daughter classes that also inherit from
767
TestCaseWithWebserver.
769
We can't inherit directly from TestCaseWithWebserver or the
770
test framework will try to create an instance which cannot
771
run, its implementation being incomplete.
775
TestCaseWithWebserver.setUp(self)
776
self.build_tree_contents([('a', '0123456789')],)
777
server = self.get_readonly_server()
778
self.transport = self._transport(server.get_url())
780
def _file_contents(self, relpath, ranges, tail_amount=0):
781
code, data = self.transport._get(relpath, ranges)
782
self.assertTrue(code in (200, 206),'_get returns: %d' % code)
783
for start, end in ranges:
785
yield data.read(end - start + 1)
787
def _file_tail(self, relpath, tail_amount):
788
code, data = self.transport._get(relpath, [], tail_amount)
789
self.assertTrue(code in (200, 206),'_get returns: %d' % code)
790
data.seek(-tail_amount + 1, 2)
791
return data.read(tail_amount)
793
def test_range_header(self):
795
map(self.assertEqual,['0', '234'],
796
list(self._file_contents('a', [(0,0), (2,4)])),)
798
self.assertEqual('789', self._file_tail('a', 3))
799
# Syntactically invalid range
800
self.assertRaises(errors.InvalidRange,
801
self.transport._get, 'a', [(4, 3)])
802
# Semantically invalid range
803
self.assertRaises(errors.InvalidRange,
804
self.transport._get, 'a', [(42, 128)])
807
class TestRanges_urllib(TestRanges, TestCaseWithWebserver):
808
"""Test the Range header in GET methods for urllib implementation"""
810
_transport = HttpTransport_urllib
813
class TestRanges_pycurl(TestWithTransport_pycurl,
815
TestCaseWithWebserver):
816
"""Test the Range header in GET methods for pycurl implementation"""