-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain_test.go
More file actions
86 lines (75 loc) · 1.85 KB
/
main_test.go
File metadata and controls
86 lines (75 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//go:build !openai
// +build !openai
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestGetEnv(t *testing.T) {
tests := []struct {
name string
key string
defaultValue string
envValue string
want string
}{
{
name: "环境变量存在",
key: "TEST_KEY",
defaultValue: "default",
envValue: "custom",
want: "custom",
},
{
name: "环境变量不存在",
key: "NON_EXISTENT_KEY",
defaultValue: "default",
envValue: "",
want: "default",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue != "" {
t.Setenv(tt.key, tt.envValue)
}
if got := getEnv(tt.key, tt.defaultValue); got != tt.want {
t.Errorf("getEnv() = %v, want %v", got, tt.want)
}
})
}
}
func TestHealthHandler(t *testing.T) {
// 重置 startTime 为当前时间以便测试
startTime = time.Now()
req, err := http.NewRequest("GET", "/health", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(healthHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
contentType := rr.Header().Get("Content-Type")
if contentType != "application/json" {
t.Errorf("handler returned wrong content type: got %v want %v",
contentType, "application/json")
}
}
func TestResponseRecorder(t *testing.T) {
rec := httptest.NewRecorder()
recorder := &responseRecorder{
ResponseWriter: rec,
statusCode: http.StatusOK,
}
// 测试 WriteHeader
recorder.WriteHeader(http.StatusNotFound)
if recorder.statusCode != http.StatusNotFound {
t.Errorf("WriteHeader() statusCode = %v, want %v", recorder.statusCode, http.StatusNotFound)
}
}