From ba7981226244ee12916234a92f5238ebdb978a4d Mon Sep 17 00:00:00 2001 From: pjkersha Date: Mon, 9 Jan 2012 16:12:52 +0000 Subject: [PATCH] * moved test_get into unit tests area * allow for default ssl method for HTTPSConnection class. git-svn-id: http://proj.badc.rl.ac.uk/svn/ndg-security/trunk/ndg_httpsclient@7974 051b1e3e-aa0c-0410-b6c2-bfbade6052be --- ndg/httpsclient/https.py | 17 ++++++++++++----- ndg/httpsclient/test/test.py | 17 +++++------------ ndg/httpsclient/{ => test}/test_get.py | 0 ndg/httpsclient/test/test_https.py | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) rename ndg/httpsclient/{ => test}/test_get.py (100%) diff --git a/ndg/httpsclient/https.py b/ndg/httpsclient/https.py index f1eaf8d..c5e6eb4 100644 --- a/ndg/httpsclient/https.py +++ b/ndg/httpsclient/https.py @@ -29,11 +29,16 @@ class HTTPSConnection(HTTPConnection): Note: This uses the constructor inherited from HTTPConnection to allow it to be used with httplib and HTTPSContextHandler. To use the class directly with an SSL context set ssl_context after construction. + @cvar default_port: default port for this class (443) @type default_port: int + @cvar default_ssl_method: default SSL method used if no SSL context is + explicitly set - defaults to version 2/3. + @type default_ssl_method: int """ default_port = HTTPS_PORT - + default_ssl_method = SSL.SSLv23_METHOD + def __init__(self, host, port=None, strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): HTTPConnection.__init__(self, host, port, strict, timeout) @@ -50,7 +55,7 @@ def connect(self): self.ssl_context) ssl_context = self.ssl_context else: - ssl_context = SSL.Context(SSL.SSLv23_METHOD) + ssl_context = SSL.Context(self.__class__.default_ssl_method) sock = socket.create_connection((self.host, self.port), self.timeout) if getattr(self, '_tunnel_host', None): @@ -66,15 +71,17 @@ def close(self): class HTTPSContextHandler(AbstractHTTPHandler): - '''HTTPS handler that provides allows a SSL context to be set for the SSL + '''HTTPS handler that allows a SSL context to be set for the SSL connections. ''' https_request = AbstractHTTPHandler.do_request_ def __init__(self, ssl_context, debuglevel=0): """ - @param ssl_context - SSL context - @param debuglevel - debug level for HTTPSHandler + @param ssl_context:SSL context + @type ssl_context: OpenSSL.SSL.Context + @param debuglevel: debug level for HTTPSHandler + @type debuglevel: int """ AbstractHTTPHandler.__init__(self, debuglevel) diff --git a/ndg/httpsclient/test/test.py b/ndg/httpsclient/test/test.py index 28c6dd2..893d791 100644 --- a/ndg/httpsclient/test/test.py +++ b/ndg/httpsclient/test/test.py @@ -4,26 +4,19 @@ @author: philipkershaw ''' import unittest -from ndg.httpsclient.urllib2_build_opener import urllib2_build_opener -from ndg.httpsclient.https import HTTPSConnection +from ndg.httpsclient.urllib2_build_opener import build_opener class Urllib2PyOpenSslTestCase(unittest.TestCase): """Unit tests for PyOpenSSL HTTPS interface for urllib2""" TEST_URI = 'https://localhost:4443' - def test01_httpsconnection(self): - conn = HTTPSConnection('localhost', port=4443) - conn.connect() - - conn.close() - - def test02_urllib2_build_opener(self): - opener = urllib2_build_opener() + def test01_urllib2_build_opener(self): + opener = build_opener() self.assert_(opener) - def test03_open(self): - opener = urllib2_build_opener() + def test02_open(self): + opener = build_opener() res = opener.open(self.__class__.TEST_URI) self.assert_(res) print("res = %s" % res.read()) diff --git a/ndg/httpsclient/test_get.py b/ndg/httpsclient/test/test_get.py similarity index 100% rename from ndg/httpsclient/test_get.py rename to ndg/httpsclient/test/test_get.py diff --git a/ndg/httpsclient/test/test_https.py b/ndg/httpsclient/test/test_https.py index aa63fa9..c9091e6 100644 --- a/ndg/httpsclient/test/test_https.py +++ b/ndg/httpsclient/test/test_https.py @@ -12,7 +12,7 @@ class TestHTTPSConnection(unittest.TestCase): - + '''Test ndg HTTPS client HTTPSConnection class''' def test01(self): conn = HTTPSConnection(Constants.HOSTNAME, port=Constants.PORT)