feat(api): M-04 添加版本信息端点
- 添加 /version 端点返回版本信息 - 版本变量支持构建时 ldflags 注入 - 返回 version、commit、build_time、go_version
This commit is contained in:
73
internal/app/version_test.go
Normal file
73
internal/app/version_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHandleVersion(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/version", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handleVersion(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("Expected status 200, got %d", rr.Code)
|
||||
}
|
||||
|
||||
contentType := rr.Header().Get("Content-Type")
|
||||
if contentType != "application/json" {
|
||||
t.Errorf("Expected Content-Type application/json, got %s", contentType)
|
||||
}
|
||||
|
||||
var info VersionInfo
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &info); err != nil {
|
||||
t.Fatalf("Failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
// 验证版本字段
|
||||
if info.Version == "" {
|
||||
t.Error("Version should not be empty")
|
||||
}
|
||||
if info.Version != version {
|
||||
t.Errorf("Version = %q, want %q", info.Version, version)
|
||||
}
|
||||
|
||||
// 验证 Go 版本
|
||||
if info.GoVersion != runtime.Version() {
|
||||
t.Errorf("GoVersion = %q, want %q", info.GoVersion, runtime.Version())
|
||||
}
|
||||
|
||||
// Commit 和 BuildTime 可能为默认值
|
||||
if info.Commit == "" {
|
||||
t.Error("Commit should not be empty (may be 'unknown' during development)")
|
||||
}
|
||||
if info.BuildTime == "" {
|
||||
t.Error("BuildTime should not be empty (may be 'unknown' during development)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionEndpoint_Integration(t *testing.T) {
|
||||
handler := NewAPIHandler("", ActionSet{})
|
||||
|
||||
req := httptest.NewRequest("GET", "/version", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("Expected status 200, got %d", rr.Code)
|
||||
}
|
||||
|
||||
var info VersionInfo
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &info); err != nil {
|
||||
t.Fatalf("Failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if info.Version != version {
|
||||
t.Errorf("Version = %q, want %q", info.Version, version)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user