From 72f3b4a27036180b07083aa97a49828b46090834 Mon Sep 17 00:00:00 2001 From: spencercjh Date: Mon, 30 Dec 2024 08:34:03 +0000 Subject: [PATCH] fix: correctly implement the test Signed-off-by: spencercjh --- testdata/start_http_server.py | 27 +++++++++++++++++++++++++++ testdata/test_filter_by_comm.sh | 21 +++++++++------------ 2 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 testdata/start_http_server.py diff --git a/testdata/start_http_server.py b/testdata/start_http_server.py new file mode 100644 index 00000000..0c186a84 --- /dev/null +++ b/testdata/start_http_server.py @@ -0,0 +1,27 @@ +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 HTTP server with keep-alive support!"))) + self.end_headers() + self.wfile.write(b"Hello, this is an HTTP server with keep-alive support!") + +# 服务器地址和端口 +server_address = ('localhost', 8080) +httpd = ThreadedHTTPServer(server_address, KeepAliveHandler) + +print("HTTP server running on http://localhost:8080 with keep-alive support") +httpd.serve_forever() diff --git a/testdata/test_filter_by_comm.sh b/testdata/test_filter_by_comm.sh index 367375f3..3b3ae45a 100644 --- a/testdata/test_filter_by_comm.sh +++ b/testdata/test_filter_by_comm.sh @@ -8,27 +8,24 @@ BEFORE_LNAME="${FILE_PREFIX}_filter_by_comm_before.log" AFTER_LNAME="${FILE_PREFIX}_filter_by_comm_after.log" function test_filter_by_comm() { - 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 - # server start before kyanos - timeout 40 python3 ./testdata/start_https_server.py & + timeout 40 python3 ./testdata/start_http_server.py & timeout 30 ${CMD} watch --debug-output http --comm python3 2>&1 | tee "${BEFORE_LNAME}" & sleep 10 - curl --insecure https://127.0.0.1:4443 &>/dev/null || true + curl http://127.0.0.1:8080 &>/dev/null || true wait - cat "${BEFORE_LNAME}" | grep "127.0.0.1:4443" + cat "${BEFORE_LNAME}" + cat "${BEFORE_LNAME}" | grep "Host: 127.0.0.1:8080" | grep "\\[side\\]=server" - # server start after kyanos - timeout 40 ${CMD} watch --debug-output http --comm python3 2>&1 | tee "${AFTER_LNAME}" & - timeout 30 python3 ./testdata/start_https_server.py & + # client start after kyanos + timeout 40 ${CMD} watch --debug-output http --comm curl 2>&1 | tee "${AFTER_LNAME}" & sleep 10 - curl --insecure https://127.0.0.1:4443 &>/dev/null || true + curl http://github.com &>/dev/null || true wait - cat "${AFTER_LNAME}" | grep "127.0.0.1:4443" + cat "${AFTER_LNAME}" + cat "${AFTER_LNAME}" | grep "Host: github.com" | grep "\\[side\\]=client" } function main() {