test: add https server test

This commit is contained in:
hengyoush
2024-10-29 00:51:19 +08:00
parent 6b600e393c
commit 9d0ade7048
3 changed files with 57 additions and 7 deletions

View File

@@ -4,21 +4,24 @@ import sys
# 创建一个Session对象
session = requests.Session()
# 可选:禁用 SSL 验证(仅用于测试自签名证书)
session.verify = False
# 设置请求的URL
url = 'https://httpbin.org/headers'
url = sys.argv[2]
count = int(sys.argv[1])
# 设置循环次数例如100次
for i in range(count):
try:
# 发起HTTPS请求
response = session.get(url)
response = session.get(url, verify=False)
# 检查请求是否成功
if response.status_code == 200:
print(f'请求 {i+1}:成功')
# 打印响应内容的前200个字符
print(response.headers)
print(response.text[:200])
else:
print(f'请求 {i+1}:失败,状态码:{response.status_code}')

30
testdata/start_https_server.py vendored Normal file
View File

@@ -0,0 +1,30 @@
import http.server
import ssl
from socketserver import ThreadingMixIn
# 创建自定义的 HTTP 服务器类,支持线程以处理多个连接
class ThreadedHTTPServer(ThreadingMixIn, http.server.HTTPServer):
# 设置 allow_reuse_address 以支持长连接
allow_reuse_address = True
class KeepAliveHandler(http.server.SimpleHTTPRequestHandler):
# 设置响应头以启用长连接
# 重写 `do_GET` 方法处理 GET 请求
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Connection", "keep-alive")
self.send_header("Content-Length", str(len("Hello, this is an HTTPS server with keep-alive support!")))
self.end_headers()
self.wfile.write(b"Hello, this is an HTTPS server with keep-alive support!")
# 服务器地址和端口
server_address = ('localhost', 4443)
httpd = ThreadedHTTPServer(server_address, KeepAliveHandler)
# 加载 SSL 证书
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='server.pem', server_side=True)
print("HTTPS server running on https://localhost:4443 with keep-alive support")
httpd.serve_forever()

View File

@@ -4,27 +4,44 @@ set -ex
CMD="$1"
FILE_PREFIX="/tmp/kyanos"
HTTPS_LNAME="${FILE_PREFIX}_https.log"
HTTPS_CLIENT_LNAME="${FILE_PREFIX}_https_client.log"
HTTPS_SERVER_LNAME="${FILE_PREFIX}_https_server.log"
function test_http_plain_client() {
pip install --break-system-packages requests || true
timeout 30 python3 ./testdata/request_https.py 60 &
timeout 30 python3 ./testdata/request_https.py 60 'https://httpbin.org/headers' &
echo "after python3 exec"
date
sleep 10
echo "after sleep 10s"
date
timeout 30 ${CMD} watch --debug-output http --remote-ports 443 2>&1 | tee "${HTTPS_LNAME}" &
timeout 30 ${CMD} watch --debug-output http --remote-ports 443 2>&1 | tee "${HTTPS_CLIENT_LNAME}" &
wait
cat "${HTTPS_LNAME}"
cat "${HTTPS_CLIENT_LNAME}"
# check_time_detail_completed_with_last_lines "${HTTPS_LNAME}" 2
cat "${HTTPS_LNAME}" | grep "httpbin"
cat "${HTTPS_CLIENT_LNAME}" | grep "httpbin"
}
function test_http_plain_server() {
openssl req -x509 -newkey rsa:2048 -keyout server.pem -out server.pem -days 365 -nodes -subj "/C=US/ST=California/L=San Francisco/O=My Company/CN=localhost"
pip install --break-system-packages ssl || true
timeout 40 python3 ./testdata/start_https_server.py &
timeout 30 python3 ./testdata/request_https.py 60 'https://localhost:4443' &
sleep 10
timeout 30 ${CMD} watch --debug-output http --local-ports 4443 2>&1 | tee "${HTTPS_SERVER_LNAME}" &
wait
cat "${HTTPS_SERVER_LNAME}"
# check_time_detail_completed_with_last_lines "${HTTPS_LNAME}" 2
cat "${HTTPS_SERVER_LNAME}" | grep "python-requests"
}
function main() {
test_http_plain_client
test_http_plain_server
}
main