Skip to content

Commit a57b959

Browse files
committed
add cli test
1 parent 4c315b0 commit a57b959

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Lib/test/test_httpservers.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import contextlib
1212
import os
1313
import socket
14+
import subprocess
1415
import sys
1516
import re
1617
import base64
@@ -22,6 +23,7 @@
2223
import html
2324
import http, http.client
2425
import urllib.parse
26+
import urllib.request
2527
import tempfile
2628
import time
2729
import datetime
@@ -34,6 +36,7 @@
3436
from test.support import (
3537
is_apple, import_helper, os_helper, requires_subprocess, threading_helper
3638
)
39+
from test.support.socket_helper import find_unused_port
3740

3841
try:
3942
import ssl
@@ -1555,13 +1558,18 @@ class CommandLineTestCase(unittest.TestCase):
15551558
'tls_key': None,
15561559
'tls_password': None,
15571560
}
1561+
random_data = os.urandom(1024)
1562+
random_file_name = 'random.bin'
15581563

15591564
def setUp(self):
15601565
super().setUp()
15611566
self.tls_password_file = tempfile.mktemp()
15621567
with open(self.tls_password_file, 'wb') as f:
15631568
f.write(self.tls_password.encode())
15641569
self.addCleanup(os_helper.unlink, self.tls_password_file)
1570+
with open(self.random_file_name, 'wb') as f:
1571+
f.write(self.random_data)
1572+
self.addCleanup(os_helper.unlink, self.random_file_name)
15651573

15661574
def invoke_httpd(self, *args):
15671575
output = StringIO()
@@ -1726,6 +1734,44 @@ def test_unknown_flag(self, _):
17261734
output = self.invoke_httpd('--unknown-flag')
17271735
self.assertStartsWith(output, 'usage: ')
17281736

1737+
def fetch_file(self, path, allow_self_signed_cert=True) -> bytes:
1738+
context = ssl.create_default_context()
1739+
if allow_self_signed_cert:
1740+
context.check_hostname = False
1741+
context.verify_mode = ssl.CERT_NONE
1742+
req = urllib.request.Request(path, method='GET')
1743+
res = urllib.request.urlopen(req, context=context)
1744+
return res.read()
1745+
1746+
def test_http_client(self):
1747+
port = find_unused_port()
1748+
bind = '127.0.0.1'
1749+
proc = subprocess.Popen([sys.executable, '-m', 'http.server',
1750+
str(port), '-b', bind],
1751+
stdout=subprocess.DEVNULL,
1752+
stderr=subprocess.DEVNULL)
1753+
time.sleep(0.5) # Wait for the server to start.
1754+
# TODO: Find a better way to wait for the server to start.
1755+
res = self.fetch_file(f'http://{bind}:{port}/{self.random_file_name}')
1756+
self.assertEqual(res, self.random_data)
1757+
proc.kill()
1758+
proc.wait()
1759+
1760+
def test_https_client(self):
1761+
port = find_unused_port()
1762+
bind = '127.0.0.1'
1763+
proc = subprocess.Popen([sys.executable, '-m', 'http.server',
1764+
str(port), '-b', bind,
1765+
'--tls-cert', self.tls_cert,
1766+
'--tls-key', self.tls_key,
1767+
'--tls-password-file', self.tls_password_file],
1768+
stdout=subprocess.DEVNULL,
1769+
stderr=subprocess.DEVNULL)
1770+
time.sleep(0.5)
1771+
res = self.fetch_file(f'https://{bind}:{port}/{self.random_file_name}')
1772+
self.assertEqual(res, self.random_data)
1773+
proc.kill()
1774+
proc.wait()
17291775

17301776
def setUpModule():
17311777
unittest.addModuleCleanup(os.chdir, os.getcwd())

0 commit comments

Comments
 (0)