统一证书导入路径请求解析

This commit is contained in:
xuans committed 2026-08-30 20:38:53 +08:00
1 parent f74c8f1f24
commit 9ac6f47b5e
2 files changed
+26 -2

No files matched your search

+1 -2
View File
@@ -2,7 +2,6 @@ package httpapi
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@@ -173,7 +172,7 @@ func parseCertificateImportRequest(c *gin.Context) (certificate.ImportRequest, f
}, cleanup, nil }, cleanup, nil
} }
var body certificatePathRequest var body certificatePathRequest
if err := json.NewDecoder(c.Request.Body).Decode(&body); err != nil { if err := bindJSON(c, &body, certificateUploadLimit); err != nil {
return certificate.ImportRequest{}, cleanup, fmt.Errorf("读取证书路径失败:%w", err) return certificate.ImportRequest{}, cleanup, fmt.Errorf("读取证书路径失败:%w", err)
} }
if strings.TrimSpace(body.CertificatePath) == "" { if strings.TrimSpace(body.CertificatePath) == "" {
@@ -135,3 +135,28 @@ func TestCertificateImportAPIRejectsMissingFiles(t *testing.T) {
t.Fatalf("缺少证书路径应返回参数错误:%d %s", response.Code, response.Body.String()) t.Fatalf("缺少证书路径应返回参数错误:%d %s", response.Code, response.Body.String())
} }
} }
func TestCertificateImportAPIRejectsInvalidPathJSON(t *testing.T) {
server := New(Dependencies{
Config: readyTestConfig(t.TempDir()),
Repository: &stubDomainRepository{},
ImportCertificate: func(context.Context, certificate.ImportRequest) (certificate.IssueResult, deployment.Report, error) {
t.Fatal("无效证书路径请求不应调用导入服务")
return certificate.IssueResult{}, deployment.Report{}, nil
},
})
tests := []string{
`{"certificate_path":"/ssl/cert.pem","private_key_path":"/ssl/key.pem","unexpected":true}`,
`{"certificate_path":"/ssl/cert.pem","private_key_path":"/ssl/key.pem"}{"certificate_path":"again"}`,
}
for _, body := range tests {
request := httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/certificate/import", strings.NewReader(body))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
serveAdminHTTP(server, response, request)
if response.Code != http.StatusBadRequest {
t.Fatalf("无效证书路径请求应返回 400,实际为 %d:%s", response.Code, response.Body.String())
}
}
}