-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuse_test.go
338 lines (298 loc) · 8.48 KB
/
use_test.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package requests
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
// TestServerSentEvents_Basic 测试 ServerSentEvents 的基本功能
func TestServerSentEvents_Basic(t *testing.T) {
w := httptest.NewRecorder()
sse := &ServerSentEvents{w: w}
// 测试 WriteHeader
sse.WriteHeader(http.StatusOK)
if w.Code != http.StatusOK {
t.Errorf("期望状态码 %d, 得到 %d", http.StatusOK, w.Code)
}
// 测试 Header
sse.Header().Set("Test", "value")
if w.Header().Get("Test") != "value" {
t.Error("Header 设置失败")
}
// 测试 Write
data := []byte("test data")
n, err := sse.Write(data)
if err != nil {
t.Errorf("Write 失败: %v", err)
}
if !strings.Contains(w.Body.String(), "data:test data\n") {
t.Error("Write 输出格式错误")
}
if n != len("data:test data\n") {
t.Error("Write 返回长度错误")
}
// 测试 Send
_, err = sse.Send("event", []byte("test event"))
if err != nil {
t.Errorf("Send 失败: %v", err)
}
if !strings.Contains(w.Body.String(), "event:test event\n") {
t.Error("Send 输出格式错误")
}
// 测试 End
sse.End()
if !strings.HasSuffix(w.Body.String(), "\n\n") {
t.Error("End 没有正确添加结束标记")
}
}
// TestServerSentEvents_Read 测试 Read 方法的所有分支
func TestServerSentEvents_Read(t *testing.T) {
sse := &ServerSentEvents{}
tests := []struct {
name string
input []byte
wantData []byte
wantErr bool
}{
{
name: "空行",
input: []byte("\n"),
wantData: nil,
wantErr: false,
},
{
name: "注释行",
input: []byte(": comment\n"),
wantData: nil,
wantErr: false,
},
{
name: "事件声明",
input: []byte("event:message\n"),
wantData: nil,
wantErr: false,
},
{
name: "数据行",
input: []byte("data:test data\n"),
wantData: []byte("test data"),
wantErr: false,
},
{
name: "未知事件",
input: []byte("unknown:data\n"),
wantData: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := sse.Read(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("Read() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !bytes.Equal(data, tt.wantData) {
t.Errorf("Read() = %v, want %v", data, tt.wantData)
}
})
}
}
// TestSSEMiddleware 测试 SSE 中间件
func TestSSEMiddleware(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("test message"))
})
server := httptest.NewServer(SSE()(handler))
defer server.Close()
resp, err := http.Get(server.URL)
if err != nil {
t.Fatalf("请求失败: %v", err)
}
defer resp.Body.Close()
// 验证响应头
expectedHeaders := map[string]string{
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
}
for k, v := range expectedHeaders {
if resp.Header.Get(k) != v {
t.Errorf("期望 header %s=%s, 得到 %s", k, v, resp.Header.Get(k))
}
}
// 验证响应内容
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("读取响应失败: %v", err)
}
if !strings.Contains(string(body), "data:test message") {
t.Error("响应内容格式错误")
}
}
// TestCORSMiddleware 测试 CORS 中间件
func TestCORSMiddleware(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
})
server := httptest.NewServer(CORS()(handler))
defer server.Close()
// 测试 OPTIONS 请求
req, _ := http.NewRequest(http.MethodOptions, server.URL, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("OPTIONS 请求失败: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
t.Errorf("OPTIONS 请求期望状态码 %d, 得到 %d", http.StatusNoContent, resp.StatusCode)
}
// 测试正常请求
resp, err = http.Get(server.URL)
if err != nil {
t.Fatalf("GET 请求失败: %v", err)
}
defer resp.Body.Close()
// 验证 CORS 头
expectedHeaders := map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
}
for k, v := range expectedHeaders {
if resp.Header.Get(k) != v {
t.Errorf("期望 header %s=%s, 得到 %s", k, v, resp.Header.Get(k))
}
}
}
// TestPrintHandler 测试打印处理器
func TestPrintHandler(t *testing.T) {
var statReceived *Stat
printFunc := func(ctx context.Context, stat *Stat) {
statReceived = stat
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("test response"))
})
server := httptest.NewServer(printHandler(printFunc)(handler))
defer server.Close()
// 发送带 body 的 POST 请求
resp, err := http.Post(server.URL, "application/json", strings.NewReader(`{"test":"data"}`))
if err != nil {
t.Fatalf("请求失败: %v", err)
}
defer resp.Body.Close()
// 验证统计信息
if statReceived == nil {
t.Fatal("未收到统计信息")
}
if statReceived.Response.StatusCode != http.StatusOK {
t.Errorf("统计信息状态码错误: 期望 %d, 得到 %d", http.StatusOK, statReceived.Response.StatusCode)
}
if statReceived.Cost < 0 {
t.Errorf("统计信息处理时间异常: cost=%d", statReceived.Cost)
}
}
func requestIdMiddle() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 从请求头中获取 request-id
requestId := GenId(r.Header.Get("request-id"))
// 将 request-id 添加到响应头
r.Header.Set("request-id", requestId)
w.Header().Set("request-id", requestId)
// 调用下一个处理器
// 定义请求ID的上下文键类型
type requestIDKey struct{}
// 使用自定义类型作为上下文键
r2 := r.WithContext(context.WithValue(r.Context(), requestIDKey{}, requestId))
next.ServeHTTP(w, r2)
})
}
}
func Test_SSE(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
r := NewServeMux(Logf(LogS), Use(requestIdMiddle()))
r.Route("/123", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("hello world"))
}, Method("PUT"))
r.Route("/sse", func(w http.ResponseWriter, r *http.Request) {
for i := 0; i < 3; i++ {
select {
case <-r.Context().Done():
return
case <-time.After(1 * time.Second):
w.Write([]byte(fmt.Sprintf(`{"a":"12345\n", "b": %d}`, i)))
}
}
}, Use(SSE()), Method("DELETE"))
s := NewServer(ctx, r, URL("http://0.0.0.0:1234"))
go s.ListenAndServe()
time.Sleep(1 * time.Second)
c := New(Logf(LogS))
_, err := c.DoRequest(ctx, URL("http://0.0.0.0:1234/sse"),
Stream(func(i int64, b []byte) error {
t.Logf("i=%d, b=%s", i, b)
return nil
}),
)
if err != nil {
t.Fatal(err)
}
_, err = c.DoRequest(ctx, URL("http://0.0.0.0:1234/123"), Body(`{"a":"b"}`))
if err != nil {
t.Fatal(err)
}
cancel()
time.Sleep(1 * time.Second)
}
func SSERound(i int64, b []byte, f func([]byte) error) error {
name, value, _ := bytes.Cut(bytes.TrimRight(b, "\n"), []byte(":"))
switch string(name) {
case "data":
return f(value)
default:
return nil
}
}
func Test_UseStep(t *testing.T) {
var ss []string
var use = func(stage, step string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ss = append(ss, fmt.Sprintf("%s-%s-start", stage, step))
t.Logf("use: %s-%s-start", stage, step)
next.ServeHTTP(w, r)
ss = append(ss, fmt.Sprintf("%s-%s-end", stage, step))
t.Logf("use: %s-%s-end", stage, step)
})
}
}
mux := NewServeMux(
Use(requestIdMiddle()),
Logf(func(ctx context.Context, stat *Stat) {
t.Logf("mux: Logf: %v", ctx.Value("request-id"))
}),
Use(use("mux", "1")),
Use(use("mux", "2")),
Use(use("mux", "3")),
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mux.Route("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
}, Use(use("route", "1"), use("route", "2"), use("route", "3")))
server := NewServer(ctx, mux, URL("http://0.0.0.0:9090"), Use(use("server", "1"), use("server", "2"), use("server", "3")))
go server.ListenAndServe()
Get("http://127.0.0.1:9090/")
time.Sleep(1 * time.Second)
}