From e73aa4a21e682715c404fce3b3aae6faeb7b0968 Mon Sep 17 00:00:00 2001 From: wonfit <16996025+wonfit@user.noreply.gitee.com> Date: Sat, 25 Apr 2026 18:24:21 +0800 Subject: [PATCH 1/2] curl testcase --- curl/testcase/curl-command-execution.sh | 103 ++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100755 curl/testcase/curl-command-execution.sh diff --git a/curl/testcase/curl-command-execution.sh b/curl/testcase/curl-command-execution.sh new file mode 100755 index 0000000..7c58699 --- /dev/null +++ b/curl/testcase/curl-command-execution.sh @@ -0,0 +1,103 @@ +#!/usr/bin/bash +# @用例描述: 验证 curl 在不同场景下的基本功能,包括 GET 请求、POST 表单、HTTPS 证书验证、 +# 超时设置、重定向以及代理支持。确保 curl 能正确返回状态码、响应内容, +# 并在错误情况下返回非零退出码。 + +# @测试步骤 +# 1. 检查 curl 是否已安装。 +# 2. 执行 HTTP GET 请求到 httpbin.org/get,验证返回 200 状态码且包含请求的 URL。 +# 3. 执行 HTTP POST 表单请求到 httpbin.org/post,提交字段并验证返回的 JSON 中包含提交的字段。 +# 4. 执行 HTTPS GET 请求到 https://httpbin.org/status/200,验证 SSL 验证通过并返回 200。 +# 5. 使用 --max-time 设置超时,访问一个会延迟响应的接口,验证 curl 在超时后返回非零退出码。 +# 6. 测试重定向处理:访问 http://httpbin.org/redirect/1,使用 -L 选项后应得到最终 200 状态码。 +# 7. (可选)若系统已配置 HTTP 代理,使用 -x 选项通过代理请求,并验证成功。 + +# @预期结果 +# 1. curl 命令可执行,版本信息可打印。 +# 2. GET 请求返回 0 退出码,状态码为 200,响应中包含 "url": "https://httpbin.org/get"。 +# 3. POST 请求返回 0 退出码,响应中 JSON 字段 "form": {"key":"value"} 存在。 +# 4. HTTPS 请求返回 0 退出码,状态码为 200,且没有证书错误。 +# 5. 超时请求返回非零退出码(退出码为 28 表示操作超时)。 +# 6. 使用 -L 后最终返回 0 退出码,状态码为 200。 +# 7. 代理请求返回 0 退出码,状态码为 200(仅在代理可用时验证)。 + +# tc_setup: 准备测试环境,确保 curl 已安装。 +function tc_setup() { + # 确认 curl 可用 + command -v curl >/dev/null 2>&1 || { + echo "curl not found, installing..." + if command -v apt-get >/dev/null 2>&1; then + sudo apt-get update && sudo apt-get install -y curl + elif command -v yum >/dev/null 2>&1; then + sudo yum install -y curl + else + echo "Unsupported package manager, please install curl manually." + exit 1 + fi + } + # 创建临时目录用于保存响应 + TMP_DIR=$(mktemp -d) + export TMP_DIR +} + +# tc_teardown: 清理临时文件。 +function tc_teardown() { + if [[ -n "$TMP_DIR" && -d "$TMP_DIR" ]]; then + rm -rf "$TMP_DIR" + fi +} + +# do_test: 具体测试实现。 +function do_test() { + # 1. 检查 curl 版本 + curl --version >/dev/null 2>&1 + assert_true $? "curl should be installed" + + # 2. HTTP GET 请求 + local get_resp="$TMP_DIR/get.json" + curl -s -o "$get_resp" -w "%{http_code}" https://httpbin.org/get > "$TMP_DIR/get_status" + local get_status=$(cat "$TMP_DIR/get_status") + assert_true [ "$get_status" -eq 200 ] "GET request should return 200" + grep -q "\"url\": \"https://httpbin.org/get\"" "$get_resp" + assert_true [ $? -eq 0 ] "GET response should contain correct URL" + + # 3. HTTP POST 表单 + local post_resp="$TMP_DIR/post.json" + curl -s -o "$post_resp" -w "%{http_code}" -X POST -d "key=value" https://httpbin.org/post > "$TMP_DIR/post_status" + local post_status=$(cat "$TMP_DIR/post_status") + assert_true [ "$post_status" -eq 200 ] "POST request should return 200" + grep -q "\"form\": {\"key\": \"value\"}" "$post_resp" + assert_true [ $? -eq 0 ] "POST response should contain posted form data" + + # 4. HTTPS 请求验证证书 + local https_status=$(curl -s -o /dev/null -w "%{http_code}" https://httpbin.org/status/200) + assert_true [ "$https_status" -eq 200 ] "HTTPS request should return 200" + + # 5. 超时测试(httpbin 提供 /delay/10 会延迟 10 秒) + curl -s -o /dev/null --max-time 3 https://httpbin.org/delay/10 + local timeout_exit=$? + # 28 是 curl 超时错误码 + assert_true [ $timeout_exit -eq 28 ] "Curl should exit with 28 on timeout" + + # 6. 重定向处理 + local redirect_status=$(curl -s -L -o /dev/null -w "%{http_code}" http://httpbin.org/redirect/1) + assert_true [ "$redirect_status" -eq 200 ] "Redirected request should finally return 200" + + # 7. 代理测试(如果环境变量 HTTP_PROXY 已设置) + if [[ -n "$HTTP_PROXY" ]]; then + curl -s -x "$HTTP_PROXY" -o /dev/null -w "%{http_code}" https://httpbin.org/get >/dev/null + assert_true [ $? -eq 0 ] "Curl via proxy should succeed" + fi +} + +# 运行顺序:setup -> test -> teardown +tc_setup +if [[ $? -eq 0 ]]; then + do_test + test_result=$? + tc_teardown + exit $test_result +else + echo "Setup failed" + exit 1 +fi -- Gitee From 5a2ad4ce54e341d7c5c6480b2dcd9907873d98f1 Mon Sep 17 00:00:00 2001 From: wonfit <16996025+wonfit@user.noreply.gitee.com> Date: Sat, 25 Apr 2026 18:28:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E4=B8=AA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- curl/testcase/curl-command-execution.sh | 66 +++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/curl/testcase/curl-command-execution.sh b/curl/testcase/curl-command-execution.sh index 7c58699..e03de65 100755 --- a/curl/testcase/curl-command-execution.sh +++ b/curl/testcase/curl-command-execution.sh @@ -84,6 +84,72 @@ function do_test() { assert_true [ "$redirect_status" -eq 200 ] "Redirected request should finally return 200" # 7. 代理测试(如果环境变量 HTTP_PROXY 已设置) + # 8. HEAD 请求验证返回 200 状态码 + curl -s -I -o /dev/null -w "%{http_code}" https://httpbin.org/get > "$TMP_DIR/head_status" + local head_status=$(cat "$TMP_DIR/head_status") + assert_true [ "$head_status" -eq 200 ] "HEAD request should return 200" + + # 9. 下载文件并校验大小(下载 1KB 的随机数据) + curl -s -o "$TMP_DIR/bytes.bin" https://httpbin.org/bytes/1024 + local size=$(stat -c%s "$TMP_DIR/bytes.bin") + assert_true [ $size -eq 1024 ] "Downloaded file should be 1024 bytes" + + # 10. 使用 --max-redirs 限制重定向次数,确保超出限制时返回 47 错误码 + curl -s -L --max-redirs 0 https://httpbin.org/redirect/1 >/dev/null 2>&1 + local redir_exit=$? + assert_true [ $redir_exit -eq 47 ] "Curl should fail with 47 when max-redirs exceeded" + + # 11. 使用 --cookie 发送并验证返回的 cookie + curl -s -c "$TMP_DIR/cookie.txt" -b "session=abc123" https://httpbin.org/cookies > "$TMP_DIR/cookies.json" + grep -q "session": "abc123" "$TMP_DIR/cookies.json" + assert_true [ $? -eq 0 ] "Cookie should be sent and reflected" + + # 12. 检查 --range 下载部分内容(下载前 100 字节) + curl -s -r 0-99 -o "$TMP_DIR/range.bin" https://httpbin.org/bytes/200 + local range_size=$(stat -c%s "$TMP_DIR/range.bin") + assert_true [ $range_size -eq 100 ] "Range request should return 100 bytes" + + # 13. 使用 --user-agent 自定义 UA 并验证服务器回显 + curl -s -A "OpenClawTest/1.0" https://httpbin.org/user-agent > "$TMP_DIR/ua.json" + grep -q "OpenClawTest/1.0" "$TMP_DIR/ua.json" + assert_true [ $? -eq 0 ] "Custom User-Agent should be echoed back" + + # 14. 重试机制:对一个返回 500 的端点使用 --retry 2 + curl -s -f --retry 2 https://httpbin.org/status/500 >/dev/null 2>&1 + local retry_exit=$? + # 当所有尝试都失败时,curl 返回 22 + assert_true [ $retry_exit -eq 22 ] "Curl should exit with 22 after retries fail" + + # 15. 并发下载:使用 --parallel 多线程下载多个文件 + curl -s --parallel -O https://httpbin.org/bytes/256 -O https://httpbin.org/bytes/256 -o "$TMP_DIR/parallel1.bin" -o "$TMP_DIR/parallel2.bin" + [ -f "$TMP_DIR/parallel1.bin" ] && [ -f "$TMP_DIR/parallel2.bin" ] + assert_true [ $? -eq 0 ] "Parallel download should produce both files" + + # 16. 检查 --compressed 能正确解压 gzip 响应 + curl -s --compressed -o "$TMP_DIR/compressed.json" https://httpbin.org/gzip + grep -q "gzipped": true "$TMP_DIR/compressed.json" + assert_true [ $? -eq 0 ] "Compressed response should be decompressed automatically" + + # 17. 使用 --sftp 下载 SFTP(需要公开测试 SFTP 服务器,这里使用 test.rebex.net) + curl -s -u demo:password sftp://test.rebex.net/pub/example/readme.txt -o "$TMP_DIR/readme.txt" + assert_true [ -s "$TMP_DIR/readme.txt" ] "SFTP download should retrieve non‑empty file" + + # 18. 使用 --limit-rate 限制下载速率(1K/s),确保命令成功执行 + curl -s --limit-rate 1K -o "$TMP_DIR/slow.bin" https://httpbin.org/bytes/512 + assert_true [ -s "$TMP_DIR/slow.bin" ] "Rate‑limited download should succeed" + + # 19. 验证 --dns-servers 指定 DNS 解析(使用 1.1.1.1) + curl -s --dns-servers 1.1.1.1 -o /dev/null https://httpbin.org/ip + assert_true [ $? -eq 0 ] "Curl should succeed with custom DNS server" + + # 20. 使用 --tls-max 限制最高 TLS 版本为 TLS1.2,确保连接成功 + curl -s --tls-max TLSv1.2 -o /dev/null https://httpbin.org/get + assert_true [ $? -eq 0 ] "Connection should succeed with TLS 1.2 max version" + + curl -s -L --max-redirs 0 https://httpbin.org/redirect/1 >/dev/null 2>&1 + local redir_exit=$? + assert_true [ $redir_exit -eq 47 ] "Curl should fail with 47 when max-redirs exceeded" + if [[ -n "$HTTP_PROXY" ]]; then curl -s -x "$HTTP_PROXY" -o /dev/null -w "%{http_code}" https://httpbin.org/get >/dev/null assert_true [ $? -eq 0 ] "Curl via proxy should succeed" -- Gitee