silent 2.16.1

Silent Web Framework
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
use std::fmt;
use std::fmt::{Display, Formatter};

use crate::core::res_body::{ResBody, full};
use crate::headers::{ContentType, Header, HeaderMap, HeaderMapExt};
use crate::{Result, SilentError, State, StatusCode, header};
use http::{Extensions, Version};
use http_body::{Body, SizeHint};
use serde::Serialize;
use serde_json::Value;

/// 响应体
/// ```
/// use silent::Response;
/// let req = Response::empty();
/// ```
pub struct Response<B: Body = ResBody> {
    /// The HTTP status code.
    pub(crate) status: StatusCode,
    /// The HTTP version.
    pub(crate) version: Version,
    /// The HTTP headers.
    pub(crate) headers: HeaderMap,
    pub(crate) body: B,
    pub(crate) extensions: Extensions,
    pub(crate) state: State,
}

impl fmt::Debug for Response {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        writeln!(f, "{:?} {}\n{:?}", self.version, self.status, self.headers)
    }
}

impl Display for Response {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl Response {
    /// 创建空响应体
    pub fn empty() -> Self {
        Self {
            status: StatusCode::OK,
            headers: HeaderMap::new(),
            version: Version::default(),
            body: ResBody::None,
            extensions: Extensions::default(),
            state: State::default(),
        }
    }
    /// 获取响应状态码
    #[inline]
    pub fn status(&self) -> StatusCode {
        self.status
    }
    /// 取出响应体(将内部body置为空)
    #[inline]
    pub fn take_body(&mut self) -> ResBody {
        std::mem::replace(&mut self.body, ResBody::None)
    }
    #[inline]
    /// 设置响应重定向
    pub fn redirect(url: &str) -> Result<Self> {
        let mut res = Self::empty();
        res.status = StatusCode::MOVED_PERMANENTLY;
        res.headers.insert(
            header::LOCATION,
            url.parse().map_err(|e| {
                SilentError::business_error(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("redirect error: {e}"),
                )
            })?,
        );
        Ok(res)
    }
    #[inline]
    /// 生成文本响应
    pub fn text(text: &str) -> Self {
        let mut res = Self::empty();
        res.set_typed_header(ContentType::text_utf8());
        res.set_body(full(text.as_bytes().to_vec()));
        res
    }
    #[inline]
    /// 生成html响应
    pub fn html(html: &str) -> Self {
        let mut res = Self::empty();
        res.set_typed_header(ContentType::html());
        res.set_body(full(html.as_bytes().to_vec()));
        res
    }
    #[inline]
    /// 生成json响应
    pub fn json<T: Serialize>(json: &T) -> Self {
        let mut res = Self::empty();
        res.set_typed_header(ContentType::json());
        res.set_body(full(serde_json::to_vec(json).unwrap()));
        res
    }
}

impl<B: Body> Response<B> {
    /// 设置响应状态
    #[inline]
    pub fn set_status(&mut self, status: StatusCode) {
        self.status = status;
    }
    /// 包含响应状态
    #[inline]
    pub fn with_status(mut self, status: StatusCode) -> Self {
        self.status = status;
        self
    }
    /// 设置响应body
    #[inline]
    pub fn set_body(&mut self, body: B) {
        self.body = body;
    }
    /// 包含响应body
    #[inline]
    pub fn with_body(mut self, body: B) -> Self {
        self.body = body;
        self
    }
    /// 获取响应体
    #[inline]
    pub fn body(&self) -> &B {
        &self.body
    }
    /// 设置响应header
    #[inline]
    pub fn set_header(&mut self, key: header::HeaderName, value: header::HeaderValue) {
        self.headers.insert(key, value);
    }
    /// 包含响应header
    #[inline]
    pub fn with_header(mut self, key: header::HeaderName, value: header::HeaderValue) -> Self {
        self.headers.insert(key, value);
        self
    }
    #[inline]
    /// 获取extensions
    pub fn extensions(&self) -> &Extensions {
        &self.extensions
    }
    #[inline]
    /// 获取extensions_mut
    pub fn extensions_mut(&mut self) -> &mut Extensions {
        &mut self.extensions
    }

    /// 获取状态
    #[inline]
    pub fn get_state<T: Send + Sync + 'static>(&self) -> Result<&T> {
        self.state.get::<T>().ok_or(SilentError::ConfigNotFound)
    }

    /// 获取状态(Uncheck)
    #[inline]
    pub fn get_state_uncheck<T: Send + Sync + 'static>(&self) -> &T {
        self.state.get::<T>().unwrap()
    }

    /// 获取状态容器
    #[inline]
    pub fn state(&self) -> &State {
        &self.state
    }

    /// 获取可变状态容器
    #[inline]
    pub fn state_mut(&mut self) -> &mut State {
        &mut self.state
    }

    /// 获取配置
    #[deprecated(since = "2.16.0", note = "请使用 get_state 代替")]
    #[inline]
    pub fn get_config<T: Send + Sync + 'static>(&self) -> Result<&T> {
        self.get_state::<T>()
    }

    /// 获取配置(Uncheck)
    #[deprecated(since = "2.16.0", note = "请使用 get_state_uncheck 代替")]
    #[inline]
    pub fn get_config_uncheck<T: Send + Sync + 'static>(&self) -> &T {
        self.get_state_uncheck::<T>()
    }

    /// 获取全局配置
    #[deprecated(since = "2.16.0", note = "请使用 state() 代替")]
    #[inline]
    pub fn configs(&self) -> &State {
        self.state()
    }

    /// 获取可变全局配置
    #[deprecated(since = "2.16.0", note = "请使用 state_mut() 代替")]
    #[inline]
    pub fn configs_mut(&mut self) -> &mut State {
        self.state_mut()
    }
    #[inline]
    /// 设置响应header
    pub fn headers(&self) -> &HeaderMap {
        &self.headers
    }
    #[inline]
    /// 设置响应header
    pub fn headers_mut(&mut self) -> &mut HeaderMap {
        &mut self.headers
    }
    #[inline]
    /// 获取响应体长度
    pub fn content_length(&self) -> SizeHint {
        self.body.size_hint()
    }
    #[inline]
    /// 设置响应header
    pub fn set_typed_header<H>(&mut self, header: H)
    where
        H: Header,
    {
        self.headers.typed_insert(header);
    }
    #[inline]
    /// 包含响应header
    pub fn with_typed_header<H>(mut self, header: H) -> Self
    where
        H: Header,
    {
        self.headers.typed_insert(header);
        self
    }

    /// move response to from another response
    pub fn copy_from_response(&mut self, res: Response<B>) {
        self.headers.extend(res.headers);
        self.status = res.status;
        self.extensions.extend(res.extensions);
        self.set_body(res.body);
    }
}

impl<S: Serialize> From<S> for Response {
    fn from(value: S) -> Self {
        match serde_json::to_value(&value).unwrap() {
            Value::String(value) => Response::empty()
                .with_typed_header(ContentType::json())
                .with_body(full(value.as_bytes().to_vec())),
            Value::Null => Response::empty().with_status(StatusCode::NO_CONTENT),
            _ => Self::json(&value),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::response::Response;

    // 基础构造函数测试

    #[test]
    fn test_response_empty() {
        let res = Response::empty();
        assert_eq!(res.status(), StatusCode::OK);
        assert_eq!(res.version, Version::default());
        assert_eq!(res.headers().len(), 0);
    }

    #[test]
    fn test_response_text() {
        let res = Response::text("Hello, World!");
        assert_eq!(res.status(), StatusCode::OK);
        assert!(res.headers().get("content-type").is_some());
        let content_type = res.headers().get("content-type").unwrap();
        assert!(content_type.to_str().unwrap().contains("text/plain"));
    }

    #[test]
    fn test_response_html() {
        let res = Response::html("<html><body>Test</body></html>");
        assert_eq!(res.status(), StatusCode::OK);
        assert!(res.headers().get("content-type").is_some());
        let content_type = res.headers().get("content-type").unwrap();
        assert!(content_type.to_str().unwrap().contains("text/html"));
    }

    #[test]
    fn test_response_json() {
        let data = serde_json::json!({"key": "value"});
        let res = Response::json(&data);
        assert_eq!(res.status(), StatusCode::OK);
        assert!(res.headers().get("content-type").is_some());
        let content_type = res.headers().get("content-type").unwrap();
        assert!(content_type.to_str().unwrap().contains("application/json"));
    }

    #[test]
    fn test_response_json_with_struct() {
        #[derive(Serialize)]
        struct TestData {
            name: String,
            count: i32,
        }
        let data = TestData {
            name: "test".to_string(),
            count: 42,
        };
        let res = Response::json(&data);
        assert_eq!(res.status(), StatusCode::OK);
        assert!(res.headers().get("content-type").is_some());
    }

    #[test]
    fn test_response_redirect_valid_url() {
        let res = Response::redirect("https://example.com");
        assert!(res.is_ok());
        let res = res.unwrap();
        assert_eq!(res.status(), StatusCode::MOVED_PERMANENTLY);
        assert!(res.headers().get("location").is_some());
        let location = res.headers().get("location").unwrap();
        assert_eq!(location.to_str().unwrap(), "https://example.com");
    }

    #[test]
    fn test_response_redirect_relative_url() {
        let res = Response::redirect("/new-location");
        assert!(res.is_ok());
        let res = res.unwrap();
        assert_eq!(res.status(), StatusCode::MOVED_PERMANENTLY);
        assert!(res.headers().get("location").is_some());
    }

    #[test]
    fn test_response_redirect_empty_url() {
        let res = Response::redirect("");
        // 空字符串可以被 HeaderValue 接受
        assert!(res.is_ok());
        let res = res.unwrap();
        assert_eq!(res.status(), StatusCode::MOVED_PERMANENTLY);
    }

    #[test]
    fn test_response_redirect_invalid_url() {
        let res = Response::redirect("not a valid url");
        // HeaderValue 可以接受任何字符串
        assert!(res.is_ok());
        let res = res.unwrap();
        assert_eq!(res.status(), StatusCode::MOVED_PERMANENTLY);
    }

    // 状态管理测试

    #[test]
    fn test_response_status() {
        let res = Response::empty();
        assert_eq!(res.status(), StatusCode::OK);
    }

    #[test]
    fn test_response_set_status() {
        let mut res = Response::empty();
        res.set_status(StatusCode::NOT_FOUND);
        assert_eq!(res.status(), StatusCode::NOT_FOUND);
    }

    #[test]
    fn test_response_set_status_multiple() {
        let mut res = Response::empty();
        res.set_status(StatusCode::NOT_FOUND);
        assert_eq!(res.status(), StatusCode::NOT_FOUND);
        res.set_status(StatusCode::INTERNAL_SERVER_ERROR);
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[test]
    fn test_response_with_status() {
        let res = Response::empty().with_status(StatusCode::CREATED);
        assert_eq!(res.status(), StatusCode::CREATED);
    }

    #[test]
    fn test_response_with_status_chain() {
        let res = Response::empty()
            .with_status(StatusCode::CREATED)
            .with_status(StatusCode::ACCEPTED);
        assert_eq!(res.status(), StatusCode::ACCEPTED);
    }

    // 主体管理测试

    #[test]
    fn test_response_body() {
        let res = Response::text("test");
        assert!(!res.body().is_end_stream());
    }

    #[test]
    fn test_response_set_body() {
        let mut res = Response::empty();
        let new_body = full(b"new body".to_vec());
        res.set_body(new_body);
        assert!(!res.body().is_end_stream());
    }

    #[test]
    fn test_response_with_body() {
        let body = full(b"test body".to_vec());
        let res = Response::empty().with_body(body);
        assert!(!res.body().is_end_stream());
    }

    #[test]
    fn test_response_take_body() {
        let mut res = Response::text("test");
        let body = res.take_body();
        assert!(!body.is_end_stream());
        assert!(res.body().is_end_stream()); // After take, body should be None
    }

    #[test]
    fn test_response_take_body_twice() {
        let mut res = Response::text("test");
        let _body1 = res.take_body();
        let body2 = res.take_body();
        assert!(body2.is_end_stream());
    }

    #[test]
    fn test_response_content_length() {
        let res = Response::text("Hello, World!");
        let hint = res.content_length();
        assert!(hint.lower() > 0);
    }

    #[test]
    fn test_response_content_length_empty() {
        let res = Response::empty();
        let hint = res.content_length();
        assert_eq!(hint.lower(), 0);
    }

    // 头部管理测试

    #[test]
    fn test_response_headers() {
        let res = Response::text("test");
        assert!(res.headers().get("content-type").is_some());
        assert_eq!(res.headers().len(), 1);
    }

    #[test]
    fn test_response_headers_empty() {
        let res = Response::empty();
        assert_eq!(res.headers().len(), 0);
    }

    #[test]
    fn test_response_headers_mut() {
        let mut res = Response::empty();
        res.headers_mut()
            .insert("x-custom-header", "custom-value".parse().unwrap());
        assert_eq!(res.headers().len(), 1);
        assert!(res.headers().get("x-custom-header").is_some());
    }

    #[test]
    fn test_response_set_header() {
        let mut res = Response::empty();
        res.set_header(header::CONTENT_TYPE, "application/json".parse().unwrap());
        assert!(res.headers().get("content-type").is_some());
    }

    #[test]
    fn test_response_set_header_multiple() {
        let mut res = Response::empty();
        res.set_header(header::CONTENT_TYPE, "text/plain".parse().unwrap());
        res.set_header(header::CACHE_CONTROL, "no-cache".parse().unwrap());
        assert_eq!(res.headers().len(), 2);
    }

    #[test]
    fn test_response_with_header() {
        let res = Response::empty().with_header(header::CONTENT_TYPE, "text/html".parse().unwrap());
        assert!(res.headers().get("content-type").is_some());
    }

    #[test]
    fn test_response_with_header_chain() {
        let res = Response::empty()
            .with_header(header::CONTENT_TYPE, "text/plain".parse().unwrap())
            .with_header(header::CACHE_CONTROL, "no-cache".parse().unwrap());
        assert_eq!(res.headers().len(), 2);
    }

    #[test]
    fn test_response_set_typed_header() {
        let mut res = Response::empty();
        res.set_typed_header(ContentType::json());
        assert!(res.headers().get("content-type").is_some());
    }

    #[test]
    fn test_response_with_typed_header() {
        let res = Response::empty().with_typed_header(ContentType::text_utf8());
        assert!(res.headers().get("content-type").is_some());
    }

    // 扩展测试

    #[test]
    fn test_response_extensions() {
        let res = Response::empty();
        assert_eq!(res.extensions().len(), 0);
    }

    #[test]
    fn test_response_extensions_mut() {
        let mut res = Response::empty();
        res.extensions_mut().insert("test_key");
        assert_eq!(res.extensions().len(), 1);
    }

    #[test]
    fn test_response_extensions_insert_and_get() {
        let mut res = Response::empty();
        res.extensions_mut().insert(42i32);
        assert!(res.extensions().get::<i32>().is_some());
    }

    // 状态测试

    #[test]
    fn test_response_state() {
        let res = Response::empty();
        assert_eq!(res.state().len(), 0);
    }

    #[test]
    fn test_response_state_mut() {
        let mut res = Response::empty();
        res.state_mut().insert(42i32);
        assert_eq!(res.state().len(), 1);
    }

    #[test]
    fn test_response_get_state_not_found() {
        let res = Response::empty();
        let result: Result<&i32> = res.get_state();
        assert!(result.is_err());
    }

    #[test]
    fn test_response_get_state_uncheck_panics() {
        // 测试 get_state_uncheck 在状态不存在时会 panic
        // Response 包含 ResBody,而 ResBody 包含非 UnwindSafe 的 trait object
        // 因此不能使用 catch_unwind 捕获 panic
        // 这个测试仅作为文档说明该方法在状态不存在时会 panic
    }

    #[test]
    fn test_response_get_state_success() {
        let mut res = Response::empty();
        res.state_mut().insert(42i32);
        let state: Result<&i32> = res.get_state();
        assert!(state.is_ok());
        assert_eq!(*state.unwrap(), 42);
    }

    #[test]
    fn test_response_get_state_uncheck_success() {
        let mut res = Response::empty();
        res.state_mut().insert(100i32);
        let state: &i32 = res.get_state_uncheck();
        assert_eq!(*state, 100);
    }

    // copy_from_response 测试

    #[test]
    fn test_response_copy_from_response() {
        let mut dest = Response::empty();
        let src = Response::text("source content")
            .with_status(StatusCode::CREATED)
            .with_header(header::CACHE_CONTROL, "no-cache".parse().unwrap());

        dest.copy_from_response(src);
        assert_eq!(dest.status(), StatusCode::CREATED);
        assert!(dest.headers().get("cache-control").is_some());
        assert!(!dest.body().is_end_stream());
    }

    #[test]
    fn test_response_copy_from_response_preserves_some_headers() {
        let mut dest =
            Response::empty().with_header(header::CONTENT_TYPE, "text/plain".parse().unwrap());
        let src = Response::html("<html>source</html>");

        dest.copy_from_response(src);
        // Source headers should extend destination, not replace
        assert!(dest.headers().get("content-type").is_some());
    }

    #[test]
    fn test_response_copy_from_response_empty_source() {
        let mut dest = Response::text("destination");
        let src = Response::empty();

        dest.copy_from_response(src);
        assert_eq!(dest.status(), StatusCode::OK);
    }

    // From trait 测试

    #[test]
    fn test_response_from_string() {
        let res: Response = "test string".to_string().into();
        assert_eq!(res.status(), StatusCode::OK);
        assert!(res.headers().get("content-type").is_some());
    }

    #[test]
    fn test_response_from_integer() {
        let res: Response = 42.into();
        assert_eq!(res.status(), StatusCode::OK);
        assert!(res.headers().get("content-type").is_some());
    }

    #[test]
    fn test_response_from_struct() {
        #[derive(Serialize)]
        struct TestData {
            field: String,
        }
        let data = TestData {
            field: "value".to_string(),
        };
        let res: Response = data.into();
        assert_eq!(res.status(), StatusCode::OK);
    }

    #[test]
    fn test_response_from_null_value() {
        let value: Option<i32> = None;
        let res: Response = value.into();
        assert_eq!(res.status(), StatusCode::NO_CONTENT);
    }

    #[test]
    fn test_response_from_json_object() {
        #[derive(Serialize)]
        struct Person {
            name: String,
            age: i32,
        }
        let person = Person {
            name: "Alice".to_string(),
            age: 30,
        };
        let res: Response = person.into();
        assert_eq!(res.status(), StatusCode::OK);
    }

    // Debug and Display trait 测试

    #[test]
    fn test_response_debug_format() {
        let res = Response::text("test");
        let debug_str = format!("{:?}", res);
        assert!(debug_str.contains("HTTP"));
        assert!(debug_str.contains("200 OK"));
    }

    #[test]
    fn test_response_display_format() {
        let res = Response::text("test");
        let display_str = format!("{}", res);
        assert!(!display_str.is_empty());
    }

    #[test]
    fn test_response_display_equals_debug() {
        let res = Response::empty();
        let debug_str = format!("{:?}", res);
        let display_str = format!("{}", res);
        // Display delegates to Debug
        assert_eq!(debug_str, display_str);
    }

    // 边界条件和特殊情况测试

    #[test]
    fn test_response_empty_text() {
        let res = Response::text("");
        assert_eq!(res.status(), StatusCode::OK);
        assert!(res.headers().get("content-type").is_some());
    }

    #[test]
    fn test_response_empty_html() {
        let res = Response::html("");
        assert_eq!(res.status(), StatusCode::OK);
        assert!(res.headers().get("content-type").is_some());
    }

    #[test]
    fn test_response_empty_json() {
        let data: serde_json::Value = serde_json::json!({});
        let res = Response::json(&data);
        assert_eq!(res.status(), StatusCode::OK);
    }

    #[test]
    fn test_response_unicode_text() {
        let text = "你好,世界!🌍";
        let res = Response::text(text);
        assert_eq!(res.status(), StatusCode::OK);
    }

    #[test]
    fn test_response_large_body() {
        let large_text = "x".repeat(1000000);
        let res = Response::text(&large_text);
        let hint = res.content_length();
        assert!(hint.lower() >= 1000000);
    }

    #[test]
    fn test_response_set_body_after_take() {
        let mut res = Response::text("original");
        let _ = res.take_body();
        res.set_body(full(b"new body".to_vec()));
        assert!(!res.body().is_end_stream());
    }

    #[test]
    fn test_response_status_code_range() {
        let mut res = Response::empty();
        for code in [100, 200, 301, 404, 500] {
            res.set_status(StatusCode::from_u16(code).unwrap());
            assert_eq!(res.status().as_u16(), code);
        }
    }

    #[test]
    fn test_response_version_default() {
        let res = Response::empty();
        assert_eq!(res.version, Version::default());
    }

    #[test]
    fn test_response_multiple_headers_same_name() {
        let mut res = Response::empty();
        res.headers_mut()
            .append("x-custom", "value1".parse().unwrap());
        res.headers_mut()
            .append("x-custom", "value2".parse().unwrap());
        let values: Vec<_> = res.headers().get_all("x-custom").iter().collect();
        assert_eq!(values.len(), 2);
    }
}