From 763e46d493c4ea890928655a773dd3715e8e46a5 Mon Sep 17 00:00:00 2001 From: xfy911 Date: Thu, 4 Jun 2026 00:18:31 +0800 Subject: [PATCH] =?UTF-8?q?test(integration):=20=E6=96=B0=E5=A2=9E=20POST/?= =?UTF-8?q?multipart=20=E7=AB=AF=E5=88=B0=E7=AB=AF=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 curl 测试:multipart 文件上传、JSON 回显、表单回显 - 验证 uploads/ 目录创建和文件保存 - 补充现有测试的覆盖范围 --- tests/integration_test.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/integration_test.sh b/tests/integration_test.sh index 38e1b1c..684a83b 100755 --- a/tests/integration_test.sh +++ b/tests/integration_test.sh @@ -213,6 +213,38 @@ assert_304_modified_since() { fi } +assert_post_multipart() { + local url="$1" + local desc="${2:-$url}" + local resp + local boundary="----CocoonTestBoundary" + local body + body="--${boundary}\r\n" + body+="Content-Disposition: form-data; name=\"file\"; filename=\"upload_test.txt\"\r\n" + body+="Content-Type: text/plain\r\n" + body+="\r\n" + body+="Cocoon multipart upload test content\r\n" + body+="--${boundary}--\r\n" + + resp=$(curl -s -X POST -H "Content-Type: multipart/form-data; boundary=${boundary}" -d "$body" "$url") + if echo "$resp" | grep -q '"uploaded"'; then + echo " ✓ $desc — 响应包含上传结果" + pass + else + echo " ✗ $desc — 响应期望包含上传结果, 实际: $resp" + fail + fi + + # 验证文件是否被保存 + if [ -f "$ROOT/uploads/upload_test.txt" ]; then + echo " ✓ $desc — 文件已保存到 uploads/" + pass + else + echo " ✗ $desc — 文件未保存到 uploads/" + fail + fi +} + assert_post_json() { local url="$1" local body="$2" @@ -329,6 +361,10 @@ assert_post_json "$BASE/api/echo" '{"test": "hello"}' '"test"' "POST JSON 回显 assert_post_form "$BASE/api/echo" 'name=cocoon&version=1.0' 'cocoon' "POST 表单回显" assert_status_405 "$BASE/index.html" "PUT 405" +echo "" +echo "=== 文件上传测试 ===" +assert_post_multipart "$BASE/upload" "multipart 文件上传" + echo "" echo "=== 结果汇总 ===" echo "通过: $PASS"