From 5f0d4698d25221cf55c6e02837fd48cfa9d39247 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Fri, 8 Jun 2012 12:04:52 -0700 Subject: [PATCH] Rename AsyncSSLTestCase to AsyncHTTPTestCase. Move get_ssl_version to the httpserver_test subclasses. Add some quick docs. --- tornado/test/httpserver_test.py | 16 ++++++++++++---- tornado/testing.py | 14 +++++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index f761fd94..de09f234 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -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 diff --git a/tornado/testing.py b/tornado/testing.py index b31ec446..e809123e 100644 --- a/tornado/testing.py +++ b/tornado/testing.py @@ -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'