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)
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))
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))
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)
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)
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)
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)
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))
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))