Rename AsyncSSLTestCase to AsyncHTTPTestCase.

Move get_ssl_version to the httpserver_test subclasses.
Add some quick docs.
This commit is contained in:
Ben Darnell 2012-06-08 12:04:52 -07:00
parent 181fdf5ffb
commit 5f0d4698d2
2 changed files with 21 additions and 9 deletions

View File

@ -8,7 +8,7 @@ from tornado.httpserver import HTTPServer
from tornado.httputil import HTTPHeaders
from tornado.iostream import IOStream
from tornado.simple_httpclient import SimpleAsyncHTTPClient
from tornado.testing import AsyncHTTPTestCase, AsyncSSLTestCase, AsyncTestCase, LogTrapTestCase
from tornado.testing import AsyncHTTPTestCase, AsyncHTTPSTestCase, AsyncTestCase, LogTrapTestCase
from tornado.util import b, bytes_type
from tornado.web import Application, RequestHandler
import os
@ -45,13 +45,20 @@ class HelloWorldRequestHandler(RequestHandler):
self.finish("Got %d bytes in POST" % len(self.request.body))
class BaseSSLTest(AsyncSSLTestCase, LogTrapTestCase):
class BaseSSLTest(AsyncHTTPSTestCase, LogTrapTestCase):
def get_app(self):
return Application([('/', HelloWorldRequestHandler,
dict(protocol="https"))])
class SSLTestMixin(object):
def get_ssl_options(self):
return dict(ssl_version = self.get_ssl_version(),
**AsyncHTTPSTestCase.get_ssl_options())
def get_ssl_version(self):
raise NotImplementedError()
def test_ssl(self):
response = self.fetch('/')
self.assertEqual(response.body, b("Hello world"))
@ -94,8 +101,9 @@ class TLSv1Test(BaseSSLTest, SSLTestMixin):
class SSLv2Test(BaseSSLTest):
def get_ssl_version(self):
return ssl.PROTOCOL_SSLv2
def get_ssl_options(self):
return dict(ssl_version=ssl.PROTOCOL_SSLv2,
**AsyncHTTPSTestCase.get_ssl_options(self))
def test_sslv2_fail(self):
# This is really more of a client test, but run it here since

View File

@ -294,10 +294,11 @@ class AsyncHTTPTestCase(AsyncTestCase):
super(AsyncHTTPTestCase, self).tearDown()
class AsyncSSLTestCase(AsyncHTTPTestCase):
def get_ssl_version(self):
raise NotImplementedError()
class AsyncHTTPSTestCase(AsyncHTTPTestCase):
"""A test case that starts an HTTPS server.
Interface is generally the same as `AsyncHTTPTestCase`.
"""
def get_http_client(self):
# Some versions of libcurl have deadlock bugs with ssl,
# so always run these tests with SimpleAsyncHTTPClient.
@ -307,13 +308,16 @@ class AsyncSSLTestCase(AsyncHTTPTestCase):
return dict(ssl_options=self.get_ssl_options())
def get_ssl_options(self):
"""May be overridden by subclasses to select SSL options.
By default includes a self-signed testing certificate.
"""
# Testing keys were generated with:
# openssl req -new -keyout tornado/test/test.key -out tornado/test/test.crt -nodes -days 3650 -x509
module_dir = os.path.dirname(__file__)
return dict(
certfile=os.path.join(module_dir, 'test', 'test.crt'),
keyfile=os.path.join(module_dir, 'test', 'test.key'),
ssl_version=self.get_ssl_version())
keyfile=os.path.join(module_dir, 'test', 'test.key'))
def get_protocol(self):
return 'https'