Skip to content

Commit

Permalink
* moved test_get into unit tests area
Browse files Browse the repository at this point in the history
 * 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
  • Loading branch information
pjkersha committed Jan 9, 2012
1 parent f9c6bde commit ba79812
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
17 changes: 12 additions & 5 deletions ndg/httpsclient/https.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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)

Expand Down
17 changes: 5 additions & 12 deletions ndg/httpsclient/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion ndg/httpsclient/test/test_https.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class TestHTTPSConnection(unittest.TestCase):

'''Test ndg HTTPS client HTTPSConnection class'''

def test01(self):
conn = HTTPSConnection(Constants.HOSTNAME, port=Constants.PORT)
Expand Down

0 comments on commit ba79812

Please sign in to comment.