Skip to content
Snippets Groups Projects
Commit f0394e59 authored by ale's avatar ale
Browse files

update tests for python 2.7 ssl

parent 47268a9b
Branches
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ from gevent import subprocess
import httplib
import os
import socket
import ssl
import sys
import time
import urllib
......@@ -22,8 +23,10 @@ def _relpath(x):
class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
def __init__(self, cert, key):
urllib2.HTTPSHandler.__init__(self)
def __init__(self, cert, key, cafile):
urllib2.HTTPSHandler.__init__(self, context=ssl.create_default_context(
purpose=ssl.Purpose.SERVER_AUTH,
cafile=cafile))
self.key = key
self.cert = cert
......@@ -34,7 +37,8 @@ class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
return self.do_open(self.getConnection, req)
def getConnection(self, host, timeout=300):
return httplib.HTTPSConnection(host, key_file=self.key, cert_file=self.cert)
return httplib.HTTPSConnection(host, key_file=self.key, cert_file=self.cert,
context=self._context)
class SSLServerTest(unittest.TestCase):
......@@ -67,14 +71,14 @@ class SSLServerTest(unittest.TestCase):
print >>sys.stderr, 'starting HTTP server on port', cls.port
server.run(
app, '127.0.0.1', cls.port, cls.ssl_ca,
app, 'localhost', cls.port, cls.ssl_ca,
cls.ssl_cert, cls.ssl_key, cls.dhparams)
gevent.spawn(_runserver)
gevent.sleep(1)
cls.opener = urllib2.build_opener(
HTTPSClientAuthHandler(cls.client_cert, cls.client_key))
HTTPSClientAuthHandler(cls.client_cert, cls.client_key, cls.ssl_ca))
@classmethod
def teardown_class(cls):
......@@ -82,7 +86,7 @@ class SSLServerTest(unittest.TestCase):
os.kill(cls.pid, 15)
def test_python_request_failure_without_cert(self):
req = urllib2.Request('https://127.0.0.1:%d%s' % (self.port, URL),
req = urllib2.Request('https://localhost:%d%s' % (self.port, URL),
data=urllib.urlencode(
{'username': 'user2',
'password': 'pass2',
......@@ -91,7 +95,7 @@ class SSLServerTest(unittest.TestCase):
self.assertRaises(urllib2.URLError, urllib2.urlopen, req)
def test_python_auth_simple_ok(self):
req = urllib2.Request('https://127.0.0.1:%d%s' % (self.port, URL),
req = urllib2.Request('https://localhost:%d%s' % (self.port, URL),
data=urllib.urlencode(
{'username': 'user2',
'password': 'pass2',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment