Exemple #1
0
 def test_fails_with_no_cert_returning(self, mock_getpeercert):
     """
     Test get ValueError if pypi returns no cert.
     """
     mock_getpeercert.return_value = None
     o = urlopen.get_opener(scheme='https')
     assert_raises_regexp(ValueError, 'empty or no certificate', o.open, pypi_https)
Exemple #2
0
 def test_http_ok(self):
     """
     Test http pypi access with pip urlopener
     """
     os.environ['PIP_INSECURE'] = ''
     response = urlopen.get_opener().open(pypi_http)
     assert response.code == 200, str(dir(response))
Exemple #3
0
 def test_fails_with_no_cert_returning(self, mock_getpeercert):
     """
     Test get ValueError if pypi returns no cert.
     """
     mock_getpeercert.return_value = None
     o = urlopen.get_opener(scheme='https')
     assert_raises_regexp(ValueError, 'empty or no certificate', o.open, pypi_https)
Exemple #4
0
 def test_https_ok_with_flag(self):
     """
     Test py25 access https url ok with --insecure flag
     This doesn't mean it's doing cert verification, just accessing over https
     """
     os.environ['PIP_INSECURE'] = '1'
     response = urlopen.get_opener().open(pypi_https)
     assert response.code == 200, str(dir(response))
Exemple #5
0
 def test_bad_pem_fails(self):
     """
     Test ssl verification fails with bad pem file.
     Also confirms alternate --cert-path option works
     """
     bad_cert = os.path.join(tests_data, 'packages', 'README.txt')
     os.environ['PIP_CERT'] = bad_cert
     o = urlopen.get_opener(scheme='https')
     assert_raises_regexp(URLError, '[sS][sS][lL]', o.open, pypi_https)
Exemple #6
0
 def test_bad_pem_fails(self):
     """
     Test ssl verification fails with bad pem file.
     Also confirms alternate --cert-path option works
     """
     bad_cert = os.path.join(tests_data, 'packages', 'README.txt')
     os.environ['PIP_CERT'] = bad_cert
     o = urlopen.get_opener(scheme='https')
     assert_raises_regexp(URLError, '[sS][sS][lL]', o.open, pypi_https)
Exemple #7
0
    def test_raises_certificate_error(self, mock_match_hostname):
        """
        Test CertificateError gets raised, which implicity confirms the sock.shutdown/sock.close calls ran
        TODO: mock socket._socket.close (to explicitly confirm the close upon exception)
        """
        def mock_matchhostname(cert, host):
            raise CertificateError()

        mock_match_hostname.side_effect = mock_matchhostname
        opener = urlopen.get_opener(scheme='https')
        assert_raises(CertificateError, opener.open, pypi_https)
Exemple #8
0
    def test_raises_certificate_error(self, mock_match_hostname):
        """
        Test CertificateError gets raised, which implicity confirms the sock.shutdown/sock.close calls ran
        TODO: mock socket._socket.close (to explicitly confirm the close upon exception)
        """
        def mock_matchhostname(cert, host):
            raise CertificateError()

        mock_match_hostname.side_effect = mock_matchhostname
        opener = urlopen.get_opener(scheme='https')
        assert_raises(CertificateError, opener.open, pypi_https)
Exemple #9
0
    def test_https_opener_director_handlers(self):
        """
        Confirm the expected handlers in our https OpenerDirector instance
        We're specifically testing it does *not* contain the default http handler
        """
        o = urlopen.get_opener(scheme='https')
        handler_types = [h.__class__ for h in o.handlers]

        assert handler_types == [
            urllib2.UnknownHandler,
            urllib2.HTTPDefaultErrorHandler,
            urllib2.HTTPRedirectHandler,
            urllib2.FTPHandler,
            urllib2.FileHandler,
            VerifiedHTTPSHandler,  #our cert check handler
            urllib2.HTTPErrorProcessor
        ], str(handler_types)
Exemple #10
0
    def test_https_opener_director_handlers(self):
        """
        Confirm the expected handlers in our https OpenerDirector instance
        We're specifically testing it does *not* contain the default http handler
        """
        o = urlopen.get_opener(scheme='https')
        handler_types = [h.__class__ for h in o.handlers]

        assert handler_types == [
            urllib2.UnknownHandler,
            urllib2.HTTPDefaultErrorHandler,
            urllib2.HTTPRedirectHandler,
            urllib2.FTPHandler,
            urllib2.FileHandler,
            VerifiedHTTPSHandler,  #our cert check handler
            urllib2.HTTPErrorProcessor
            ], str(handler_types)
Exemple #11
0
def test_proxy_handlers_present():
    """
    Confirm the proxy handlers are present
    """

    try:
        urlopen.setup(proxystr='http://proxy')
        o = urlopen.get_opener(scheme='https')
    finally:
        #teardown the proxy_handler for other tests.
        urlopen.proxy_handler = None

    handler_types = [h.__class__ for h in o.handlers]

    assert handler_types == [
        urllib2.ProxyHandler, # this is needed
        urllib2.UnknownHandler,
        urllib2.HTTPDefaultErrorHandler,
        urllib2.HTTPRedirectHandler,
        urllib2.FileHandler,
        VerifiedHTTPSHandler,
        urllib2.CacheFTPHandler,  # and this
        urllib2.HTTPErrorProcessor
        ], str(handler_types)
Exemple #12
0
 def test_http_ok(self):
     """
     Test http pypi access with pip urlopener
     """
     response = urlopen.get_opener().open(pypi_http)
     assert response.getcode() == 200, str(dir(response))
Exemple #13
0
 def test_https_ok(self):
     """
     Test https pypi access with pip urlopener
     """
     response = urlopen.get_opener(scheme='https').open(pypi_https)
     assert response.getcode() == 200, str(dir(response))
Exemple #14
0
 def test_http_ok(self):
     """
     Test http pypi access with pip urlopener
     """
     response = urlopen.get_opener().open(pypi_http)
     assert response.getcode() == 200, str(dir(response))
Exemple #15
0
 def test_https_ok(self):
     """
     Test https pypi access with pip urlopener
     """
     response = urlopen.get_opener(scheme='https').open(pypi_https)
     assert response.getcode() == 200, str(dir(response))