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