Browse Source

* 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

pjkersha 13 years ago
parent
commit
ba79812262

+ 12 - 5
ndg/httpsclient/https.py

@@ -29,11 +29,16 @@ class HTTPSConnection(HTTPConnection):
     Note: This uses the constructor inherited from HTTPConnection to allow it to
     Note: This uses the constructor inherited from HTTPConnection to allow it to
     be used with httplib and HTTPSContextHandler. To use the class directly with
     be used with httplib and HTTPSContextHandler. To use the class directly with
     an SSL context set ssl_context after construction.
     an SSL context set ssl_context after construction.
+    
     @cvar default_port: default port for this class (443)
     @cvar default_port: default port for this class (443)
     @type default_port: int
     @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_port = HTTPS_PORT
-
+    default_ssl_method = SSL.SSLv23_METHOD
+    
     def __init__(self, host, port=None, strict=None,
     def __init__(self, host, port=None, strict=None,
                  timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
                  timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
         HTTPConnection.__init__(self, host, port, strict, timeout)
         HTTPConnection.__init__(self, host, port, strict, timeout)
@@ -50,7 +55,7 @@ class HTTPSConnection(HTTPConnection):
                                 self.ssl_context)
                                 self.ssl_context)
             ssl_context = self.ssl_context
             ssl_context = self.ssl_context
         else:
         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)
         sock = socket.create_connection((self.host, self.port), self.timeout)
         if getattr(self, '_tunnel_host', None):
         if getattr(self, '_tunnel_host', None):
@@ -66,15 +71,17 @@ class HTTPSConnection(HTTPConnection):
         
         
 
 
 class HTTPSContextHandler(AbstractHTTPHandler):
 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.
     connections.
     '''
     '''
     https_request = AbstractHTTPHandler.do_request_
     https_request = AbstractHTTPHandler.do_request_
 
 
     def __init__(self, ssl_context, debuglevel=0):
     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)
         AbstractHTTPHandler.__init__(self, debuglevel)
 
 

+ 5 - 12
ndg/httpsclient/test/test.py

@@ -4,26 +4,19 @@ Created on Jan 5, 2012
 @author: philipkershaw
 @author: philipkershaw
 '''
 '''
 import unittest
 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):
 class Urllib2PyOpenSslTestCase(unittest.TestCase):
     """Unit tests for PyOpenSSL HTTPS interface for urllib2"""
     """Unit tests for PyOpenSSL HTTPS interface for urllib2"""
     TEST_URI = 'https://localhost:4443'
     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)
         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)
         res = opener.open(self.__class__.TEST_URI)
         self.assert_(res)
         self.assert_(res)
         print("res = %s" % res.read())
         print("res = %s" % res.read())

ndg/httpsclient/test_get.py → ndg/httpsclient/test/test_get.py


+ 1 - 1
ndg/httpsclient/test/test_https.py

@@ -12,7 +12,7 @@ from ndg.httpsclient.https import HTTPSConnection
 
 
 
 
 class TestHTTPSConnection(unittest.TestCase):
 class TestHTTPSConnection(unittest.TestCase):
-
+    '''Test ndg HTTPS client HTTPSConnection class'''
 
 
     def test01(self):
     def test01(self):
         conn = HTTPSConnection(Constants.HOSTNAME, port=Constants.PORT)
         conn = HTTPSConnection(Constants.HOSTNAME, port=Constants.PORT)