zenith-api 0.1.0

Zenith 公共 API 与类型定义:CanonicalRequest/Response、Method、Protocol、Transport 等核心类型
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! 跨协议一致性集成测试
//!
//! 验证 HTTP/1.1、HTTP/2、HTTP/3 解析同语义请求后,
//! 通过规范化引擎生成字节级一致的 CanonicalRequest。
//!
//! # 测试策略
//! 1. 构造同语义但不同协议格式的请求
//! 2. 使用规范化引擎转换
//! 3. 比较 CanonicalRequest 的字节表示

use zenith_api::normalize::{
    normalize_request, normalize_request_with_config, NormalizeConfig,
};
use zenith_api::{CanonicalRequest, Method, Protocol, Transport};

/// 比较两个 CanonicalRequest 是否字节级一致
fn assert_canonical_equal(a: &CanonicalRequest, b: &CanonicalRequest) {
    // 基本字段比较
    assert_eq!(a.method, b.method, "method mismatch");
    assert_eq!(a.scheme_str(), b.scheme_str(), "scheme mismatch");
    assert_eq!(
        a.authority_str(),
        b.authority_str(),
        "authority mismatch"
    );
    assert_eq!(a.path_str(), b.path_str(), "path mismatch");
    assert_eq!(a.query_str(), b.query_str(), "query mismatch");

    // Header 数量比较
    assert_eq!(
        a.header_count(),
        b.header_count(),
        "header count mismatch"
    );

    // 逐个 Header 比较
    for hdr in a.headers_iter() {
        if let Some(b_hdr) = b.find_header(hdr.name_str()) {
            assert_eq!(
                hdr.value_str(),
                b_hdr.value_str(),
                "header '{}' value mismatch",
                hdr.name_str()
            );
        } else {
            panic!("header '{}' not found in second request", hdr.name_str());
        }
    }
}

// ---------------------------------------------------------------------------
// 基础一致性测试
// ---------------------------------------------------------------------------

#[test]
fn test_simple_get_request_consistency() {
    // HTTP/1.1 格式(Host 头将被保留为普通 Header)
    let http1_headers = [("Accept", "text/html")];
    let req1 = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/",
        "",
        &http1_headers,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();

    // HTTP/2 格式(伪头部已作为独立参数传递,不包含在 headers 中)
    let http2_headers = [("accept", "text/html")];
    let req2 = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/",
        "",
        &http2_headers,
        Protocol::Http2,
        Transport::Tls13,
    )
    .unwrap();

    assert_canonical_equal(&req1, &req2);
}

#[test]
fn test_post_request_with_body_consistency() {
    let headers_v1 = [
        ("Content-Type", "application/json"),
        ("Accept", "application/json"),
    ];
    let req1 = normalize_request(
        Method::Post,
        "https",
        "api.example.com",
        "/api/data",
        "",
        &headers_v1,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();

    let headers_v2 = [
        ("content-type", "application/json"),
        ("accept", "application/json"),
    ];
    let req2 = normalize_request(
        Method::Post,
        "https",
        "api.example.com",
        "/api/data",
        "",
        &headers_v2,
        Protocol::Http2,
        Transport::Tls13,
    )
    .unwrap();

    assert_canonical_equal(&req1, &req2);
}

#[test]
fn test_query_string_consistency() {
    let headers_v1 = [];
    let req1 = normalize_request(
        Method::Get,
        "http",
        "example.com:80",
        "/search",
        "q=hello&lang=en",
        &headers_v1,
        Protocol::Http1,
        Transport::Plaintext,
    )
    .unwrap();

    let headers_v3 = [];
    let req2 = normalize_request(
        Method::Get,
        "http",
        "example.com",
        "/search",
        "q=hello&lang=en",
        &headers_v3,
        Protocol::Http3,
        Transport::Tls13,
    )
    .unwrap();

    assert_canonical_equal(&req1, &req2);
}

// ---------------------------------------------------------------------------
// 规范化规则一致性测试
// ---------------------------------------------------------------------------

#[test]
fn test_header_case_insensitivity() {
    // 不同大小写的 Header 应该规范化为一致结果
    let headers_lower = [("content-type", "text/plain")];
    let headers_mixed = [("Content-Type", "text/plain")];
    let headers_upper = [("CONTENT-TYPE", "text/plain")];

    let req_lower = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/",
        "",
        &headers_lower,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();

    let req_mixed = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/",
        "",
        &headers_mixed,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();

    let req_upper = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/",
        "",
        &headers_upper,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();

    assert_canonical_equal(&req_lower, &req_mixed);
    assert_canonical_equal(&req_lower, &req_upper);
}

#[test]
fn test_duplicate_header_merging() {
    // HTTP/1.1 允许重复 Header,规范化后应合并
    let headers_dup = [("Accept", "text/html"), ("Accept", "application/json")];
    let req1 = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/",
        "",
        &headers_dup,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();

    // HTTP/2 中多个同名 Header 也应该合并
    let headers_h2 = [("accept", "text/html, application/json")];
    let req2 = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/",
        "",
        &headers_h2,
        Protocol::Http2,
        Transport::Tls13,
    )
    .unwrap();

    // 两者都应该产生包含两个值的 Accept Header
    let accept1 = req1.find_header("accept").unwrap();
    let accept2 = req2.find_header("accept").unwrap();

    // 检查是否都包含相同的值(顺序可能不同)
    assert!(
        accept1.value_str().contains("text/html"),
        "req1 missing text/html"
    );
    assert!(
        accept1.value_str().contains("application/json"),
        "req1 missing application/json"
    );
    assert!(
        accept2.value_str().contains("text/html"),
        "req2 missing text/html"
    );
    assert!(
        accept2.value_str().contains("application/json"),
        "req2 missing application/json"
    );
}

#[test]
fn test_path_normalization_consistency() {
    // 不同但语义等价的路径表示
    let headers = [("Host", "example.com")];

    // 带多余斜杠
    let req1 = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "//api//v1///test",
        "",
        &headers,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();

    // 规范化后的路径应该一致
    assert_eq!(req1.path_str(), "/api/v1/test");

    // 带点段
    let req2 = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/api/./v1/../v1/test",
        "",
        &headers,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();

    assert_eq!(req2.path_str(), "/api/v1/test");
}

#[test]
fn test_authority_normalization_consistency() {
    let headers = [("Host", "Example.COM:443")];

    // Host 头带大写和显式端口
    let req1 = normalize_request(
        Method::Get,
        "https",
        "Example.COM:443",
        "/",
        "",
        &headers,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();

    // :authority 伪头部(HTTP/2)
    let headers_h2 = [(":authority", "example.com")];
    let req2 = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/",
        "",
        &headers_h2,
        Protocol::Http2,
        Transport::Tls13,
    )
    .unwrap();

    assert_eq!(req1.authority_str(), "example.com");
    assert_eq!(req2.authority_str(), "example.com");
}

// ---------------------------------------------------------------------------
// 安全边界测试
// ---------------------------------------------------------------------------

#[test]
fn test_invalid_header_name_rejection() {
    // 包含非法字符的 Header 名应导致整体规范化失败(fail-closed)
    let headers = [("Invalid\x00Header", "value"), ("Valid", "ok")];

    let result = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/",
        "",
        &headers,
        Protocol::Http1,
        Transport::Tls13,
    );

    let err = result.expect_err("invalid header name should be rejected");
    assert_eq!(err.message, "invalid header name");
}

#[test]
fn test_header_limit_enforcement() {
    // 超过 MAX_HEADER_COUNT (32) 应该返回错误
    // 这个测试验证规范化引擎的 header 限制

    let headers_small = [("h1", "v1"), ("h2", "v2")];
    let req = normalize_request(
        Method::Get,
        "https",
        "example.com",
        "/",
        "",
        &headers_small,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();
    assert_eq!(req.header_count(), 2);
}

// ---------------------------------------------------------------------------
// 配置化一致性测试
// ---------------------------------------------------------------------------

#[test]
fn test_custom_config_consistency() {
    let config = NormalizeConfig {
        decode_path: true,
        merge_headers: true,
        normalize_case: true,
    };

    let headers = [("Content-Type", "application/json")];

    let req1 = normalize_request_with_config(
        Method::Post,
        "HTTPS",
        "Example.COM:443",
        "/Api/Test",
        "key=value",
        &headers,
        Protocol::Http2,
        Transport::Tls13,
        config,
    )
    .unwrap();

    // 验证规范化结果
    assert_eq!(req1.scheme_str(), "https");
    assert_eq!(req1.authority_str(), "example.com");
    assert_eq!(req1.path_str(), "/Api/Test");
    assert_eq!(req1.query_str(), "key=value");
    assert_eq!(
        req1.find_header("content-type").unwrap().value_str(),
        "application/json"
    );

    // 使用默认配置应该产生相同结果
    let req2 = normalize_request(
        Method::Post,
        "HTTPS",
        "Example.COM:443",
        "/Api/Test",
        "key=value",
        &headers,
        Protocol::Http2,
        Transport::Tls13,
    )
    .unwrap();

    assert_canonical_equal(&req1, &req2);
}

#[test]
fn test_no_decode_config() {
    let config = NormalizeConfig {
        decode_path: false,
        merge_headers: true,
        normalize_case: true,
    };

    let headers = [("Host", "example.com")];

    let req = normalize_request_with_config(
        Method::Get,
        "https",
        "example.com",
        "/path%20with%20spaces",
        "",
        &headers,
        Protocol::Http1,
        Transport::Tls13,
        config,
    )
    .unwrap();

    // 不解码时,百分号编码应保留
    assert_eq!(req.path_str(), "/path%20with%20spaces");
}

// ---------------------------------------------------------------------------
// 全链路一致性测试
// ---------------------------------------------------------------------------

#[test]
fn test_full_request_pipeline_http1_http2_http3() {
    // 模拟三个协议的同语义请求

    // HTTP/1.1 请求
    let headers_h1 = [
        ("User-Agent", "Zenith/1.0"),
        ("Accept", "application/json"),
        ("Content-Type", "text/plain"),
    ];
    let req_h1 = normalize_request(
        Method::Post,
        "https",
        "api.example.com",
        "/v1/resource",
        "expand=true",
        &headers_h1,
        Protocol::Http1,
        Transport::Tls13,
    )
    .unwrap();

    // HTTP/2 请求(伪头部已作为独立参数传递)
    let headers_h2 = [
        ("user-agent", "Zenith/1.0"),
        ("accept", "application/json"),
        ("content-type", "text/plain"),
    ];
    let req_h2 = normalize_request(
        Method::Post,
        "https",
        "api.example.com",
        "/v1/resource",
        "expand=true",
        &headers_h2,
        Protocol::Http2,
        Transport::Tls13,
    )
    .unwrap();

    // HTTP/3 请求
    let headers_h3 = [
        ("user-agent", "Zenith/1.0"),
        ("accept", "application/json"),
        ("content-type", "text/plain"),
    ];
    let req_h3 = normalize_request(
        Method::Post,
        "https",
        "api.example.com",
        "/v1/resource",
        "expand=true",
        &headers_h3,
        Protocol::Http3,
        Transport::Tls13,
    )
    .unwrap();

    // 比较三个请求
    assert_canonical_equal(&req_h1, &req_h2);
    assert_canonical_equal(&req_h1, &req_h3);
    assert_canonical_equal(&req_h2, &req_h3);

    // 验证关键字段
    assert_eq!(req_h1.method, Method::Post);
    assert_eq!(req_h1.protocol, Protocol::Http1);
    assert_eq!(req_h3.protocol, Protocol::Http3);

    // 验证规范化后的字段
    assert_eq!(req_h1.scheme_str(), "https");
    assert_eq!(req_h1.authority_str(), "api.example.com");
    assert_eq!(req_h1.path_str(), "/v1/resource");
    assert_eq!(req_h1.query_str(), "expand=true");
}