Skip to content

Commit 0c40dd7

Browse files
committed
Add more suggestions
1 parent 1ee542f commit 0c40dd7

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

Doc/library/http.server.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ handler. Code to create and run the server looks like this::
5656
password=None, alpn_protocols=None)
5757
5858
Subclass of :class:`HTTPServer` with a wrapped socket using the :mod:`ssl` module.
59-
If the :mod:`ssl` module is not available, instantiating an :class:`!HTTPSServer`
59+
If the :mod:`ssl` module is not available, instantiating a :class:`!HTTPSServer`
6060
object fails with an :exc:`ImportError`.
6161

6262
The *certfile* argument is the path to the SSL certificate chain file,
@@ -497,15 +497,15 @@ following command runs an HTTP/1.1 conformant server::
497497
Added the ``--protocol`` option.
498498

499499
The server can also support TLS encryption. The options ``--tls-cert`` and
500-
``--tls-key`` allow specifying a TLS certificate chain and private key for
500+
``--tls-key`` allow specifying a TLS certificate chain and a private key for
501501
secure HTTPS connections. For example, the following command runs the server with
502502
TLS enabled:
503503

504504
.. code-block:: bash
505505
506506
python -m http.server --tls-cert fullchain.pem
507507
508-
Use ``--tls-password-file`` option if private keys are password-protected:
508+
Use the ``--tls-password-file`` option if private keys are password-protected:
509509

510510
.. code-block:: bash
511511

Lib/test/test_httpservers.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ def stop(self):
8282

8383

8484
class BaseTestCase(unittest.TestCase):
85+
86+
# Optional tuple (certfile, keyfile, password) to use for HTTPS servers.
8587
tls = None
8688

8789
def setUp(self):
@@ -335,10 +337,26 @@ def test_head_via_send_error(self):
335337
def certdata_file(*path):
336338
return os.path.join(os.path.dirname(__file__), "certdata", *path)
337339

340+
class DummyRequestHandler(NoLogRequestHandler, SimpleHTTPRequestHandler):
341+
pass
342+
343+
def create_https_server(
344+
certfile,
345+
keyfile=None,
346+
password=None,
347+
*,
348+
address=('localhost', 0),
349+
request_handler=DummyRequestHandler,
350+
351+
):
352+
return HTTPSServer(
353+
address, request_handler,
354+
certfile=certfile, keyfile=keyfile, password=password
355+
)
356+
338357

339358
@unittest.skipIf(ssl is None, "requires ssl")
340359
class BaseHTTPSServerTestCase(BaseTestCase):
341-
342360
CERTFILE = certdata_file("keycert.pem")
343361
ONLYCERT = certdata_file("ssl_cert.pem")
344362
ONLYKEY = certdata_file("ssl_key.pem")
@@ -374,15 +392,10 @@ def test_valid_certdata(self):
374392
(self.ONLYCERT, self.ONLYKEY_PROTECTED, self.KEY_PASSWORD),
375393
]
376394
for certfile, keyfile, password in valid_certdata:
377-
server = HTTPSServer(
378-
('localhost', 0),
379-
BaseHTTPRequestHandler,
380-
certfile=certfile,
381-
keyfile=keyfile,
382-
password=password,
383-
)
384-
self.assertIsInstance(server, HTTPSServer)
385-
server.server_close()
395+
with self.subTest(certfile=certfile, keyfile=keyfile):
396+
server = create_https_server(certfile, keyfile, password)
397+
self.assertIsInstance(server, HTTPSServer)
398+
server.server_close()
386399

387400
def test_invalid_certdata(self):
388401
invalid_certdata = [
@@ -393,15 +406,10 @@ def test_invalid_certdata(self):
393406
(self.ONLYKEY, self.ONLYCERT, None),
394407
(self.CERTFILE_PROTECTED, None, self.BADPASSWORD),
395408
]
396-
for cerfile, keyfile, password in invalid_certdata:
397-
with self.assertRaises(ssl.SSLError):
398-
HTTPSServer(
399-
('localhost', 0),
400-
self.request_handler,
401-
certfile=cerfile,
402-
keyfile=keyfile,
403-
password=password,
404-
)
409+
for certfile, keyfile, password in invalid_certdata:
410+
with self.subTest(certfile=certfile, keyfile=keyfile):
411+
with self.assertRaises(ssl.SSLError):
412+
create_https_server(certfile, keyfile, password)
405413

406414

407415
class RequestHandlerLoggingTestCase(BaseTestCase):

0 commit comments

Comments
 (0)