retina 0.4.19

high-level RTSP multimedia streaming library
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
// Copyright (C) The Retina Authors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! RTSP/1.0 message types.

use std::{borrow::Cow, collections::BTreeMap, fmt, ops::Deref};

use super::table::{is_valid_header_value, is_valid_token};
use crate::mostly_ascii::MostlyAscii;

// ---------------------------------------------------------------------------
// CaseInsensitive — ASCII-case-insensitive wrapper for Ord/Eq
// ---------------------------------------------------------------------------

/// Wrapper that compares the inner value using ASCII-case-insensitive ordering.
///
/// Works as both a sized wrapper (e.g. `CaseInsensitive<Cow<'static, str>>`)
/// and an unsized wrapper (`CaseInsensitive<str>`) for `BTreeMap` lookups via
/// the `Borrow` trait.
#[derive(Clone, Debug)]
#[repr(transparent)]
pub struct CaseInsensitive<T: ?Sized>(T);

impl CaseInsensitive<str> {
    fn new(s: &str) -> &Self {
        // SAFETY: `CaseInsensitive` is `repr(transparent)` over `T`.
        unsafe { &*(s as *const str as *const Self) }
    }
}

impl<T: AsRef<str> + ?Sized> PartialEq for CaseInsensitive<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0.as_ref().eq_ignore_ascii_case(other.0.as_ref())
    }
}
impl<T: AsRef<str> + ?Sized> Eq for CaseInsensitive<T> {}

impl<T: AsRef<str> + ?Sized> PartialOrd for CaseInsensitive<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: AsRef<str> + ?Sized> Ord for CaseInsensitive<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0
            .as_ref()
            .bytes()
            .map(|b| b.to_ascii_lowercase())
            .cmp(other.0.as_ref().bytes().map(|b| b.to_ascii_lowercase()))
    }
}

impl std::borrow::Borrow<CaseInsensitive<str>> for CaseInsensitive<Cow<'static, str>> {
    fn borrow(&self) -> &CaseInsensitive<str> {
        CaseInsensitive::new(self.0.as_ref())
    }
}

/// An RTSP/1.0 message head (parsed without body).
#[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
pub enum Message {
    Request(Request),
    Response(Response),
    Data(Data),
}

/// An RTSP/1.0 request head.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Request {
    pub method: Method,
    pub request_uri: Option<url::Url>,
    pub headers: Headers,
}

impl Request {
    pub fn write_head(&self, w: &mut impl std::io::Write) -> std::io::Result<()> {
        write!(
            w,
            "{method} {uri} RTSP/1.0\r\n",
            method = self.method,
            uri = self.request_uri.as_ref().map(|u| u.as_str()).unwrap_or("*"),
        )?;
        self.headers.write(w)?;
        Ok(())
    }
}

impl fmt::Display for Request {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{method} {uri} RTSP/1.0\r\n{headers}",
            method = self.method,
            uri = self.request_uri.as_ref().map(|u| u.as_str()).unwrap_or("*"),
            headers = self.headers,
        )
    }
}

/// Defines `pub const` token constants on `Self`, validated at compile time.
macro_rules! static_const_tokens {
    ($($NAME:ident => $s:literal),+ $(,)?) => {
        $(
            pub const $NAME: Self = Self(std::borrow::Cow::Borrowed(
                match $crate::rtsp::table::is_valid_token($s.as_bytes()) {
                    true => $s,
                    false => panic!("invalid token"),
                }
            ));
        )+
    };
}

// ---------------------------------------------------------------------------
// Method
// ---------------------------------------------------------------------------

/// An RTSP method name, which must be a valid token as defined in
/// [RFC 7230 section 3.2.6](https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.6).
#[derive(Clone, PartialEq, Eq)]
pub struct Method(Cow<'static, str>);

impl Method {
    /// Returns a `Method` from a static string, or an error if it's not a valid token.
    pub const fn from_static(name: &'static str) -> Result<Self, &'static str> {
        if !is_valid_token(name.as_bytes()) {
            return Err("invalid RTSP method token");
        }
        Ok(Self(Cow::Borrowed(name)))
    }

    static_const_tokens! {
        DESCRIBE => "DESCRIBE",
        GET_PARAMETER => "GET_PARAMETER",
        OPTIONS => "OPTIONS",
        PLAY => "PLAY",
        SETUP => "SETUP",
        SET_PARAMETER => "SET_PARAMETER",
        TEARDOWN => "TEARDOWN",
    }
}

impl fmt::Display for Method {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl fmt::Debug for Method {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Method({:?})", &*self.0)
    }
}

impl std::borrow::Borrow<str> for Method {
    fn borrow(&self) -> &str {
        self.0.as_ref()
    }
}

impl Deref for Method {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        self.0.as_ref()
    }
}

impl TryFrom<&'_ [u8]> for Method {
    type Error = InvalidMethodError;
    fn try_from(v: &'_ [u8]) -> Result<Method, InvalidMethodError> {
        Self::try_from(v.to_owned())
    }
}

impl TryFrom<Vec<u8>> for Method {
    type Error = InvalidMethodError;
    fn try_from(v: Vec<u8>) -> Result<Method, InvalidMethodError> {
        if !is_valid_token(&v) {
            return Err(InvalidMethodError(v));
        }
        let v = String::from_utf8(v).expect("valid token => utf-8");
        Ok(Method(Cow::Owned(v)))
    }
}

impl TryFrom<&'_ str> for Method {
    type Error = InvalidMethodError;
    fn try_from(v: &'_ str) -> Result<Method, InvalidMethodError> {
        Self::try_from(v.as_bytes().to_owned())
    }
}

impl TryFrom<String> for Method {
    type Error = InvalidMethodError;
    fn try_from(v: String) -> Result<Method, InvalidMethodError> {
        if !is_valid_token(v.as_bytes()) {
            return Err(InvalidMethodError(v.into_bytes()));
        }
        Ok(Self(Cow::Owned(v)))
    }
}

#[derive(Clone, Debug, derive_more::Display, derive_more::Error)]
#[display("invalid RTSP method token {:?}", MostlyAscii { bytes: _0, escape_newline: true })]
pub struct InvalidMethodError(#[error(not(source))] Vec<u8>);

/// Compile-time-validated method literal.
#[allow(unused_macros)]
macro_rules! rtsp_method {
    ($s:literal) => {{
        const M: $crate::rtsp::msg::Method =
            $crate::rtsp::msg::Method(std::borrow::Cow::Borrowed(valid_token!($s)));
        M
    }};
}

// ---------------------------------------------------------------------------
// Response
// ---------------------------------------------------------------------------

/// An RTSP/1.0 response head.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Response {
    pub status_code: StatusCode,
    pub reason_phrase: String,
    pub headers: Headers,
}

impl Response {
    pub fn write_head(&self, w: &mut impl std::io::Write) -> std::io::Result<()> {
        write!(
            w,
            "RTSP/1.0 {:03} {}\r\n",
            self.status_code.0, &self.reason_phrase,
        )?;
        self.headers.write(w)?;
        Ok(())
    }
}

impl fmt::Display for Response {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "RTSP/1.0 {:03} {}\r\n{}",
            self.status_code.0, &self.reason_phrase, self.headers,
        )
    }
}

// ---------------------------------------------------------------------------
// StatusCode
// ---------------------------------------------------------------------------

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct StatusCode(u16);

impl StatusCode {
    pub const OK: StatusCode = StatusCode(200);
    pub const UNAUTHORIZED: StatusCode = StatusCode(401);
    pub const SESSION_NOT_FOUND: StatusCode = StatusCode(454);
    pub const INTERNAL_SERVER_ERROR: StatusCode = StatusCode(500);

    #[inline]
    pub fn as_u16(self) -> u16 {
        self.0
    }

    pub fn is_success(self) -> bool {
        (200..300).contains(&self.0)
    }
}

impl From<StatusCode> for u16 {
    fn from(s: StatusCode) -> u16 {
        s.0
    }
}

impl TryFrom<u16> for StatusCode {
    type Error = InvalidStatusCode;

    fn try_from(value: u16) -> Result<Self, Self::Error> {
        if !(100..600).contains(&value) {
            return Err(InvalidStatusCode(value));
        }
        Ok(StatusCode(value))
    }
}

impl fmt::Display for StatusCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[derive(Copy, Clone, Debug, derive_more::Display, derive_more::Error)]
#[display("status code {_0} outside the valid range 100..=599")]
pub struct InvalidStatusCode(#[error(not(source))] u16);

// ---------------------------------------------------------------------------
// Headers
// ---------------------------------------------------------------------------

#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct Headers(BTreeMap<HeaderName, HeaderValue>);

impl Headers {
    /// Looks up a header by name (case-insensitive).
    pub fn get(&self, name: &str) -> Option<&HeaderValue> {
        self.0.get(CaseInsensitive::new(name))
    }

    /// Appends the given value; if there is an existing value for the same key,
    /// the new value is appended with `", "` between.
    pub fn append(&mut self, name: HeaderName, value: HeaderValue) {
        match self.0.entry(name) {
            std::collections::btree_map::Entry::Occupied(mut e) => {
                e.get_mut().0.extend([", ", &value]);
            }
            std::collections::btree_map::Entry::Vacant(e) => {
                e.insert(value);
            }
        }
    }

    /// Inserts the given value, overwriting any existing value.
    pub fn insert(&mut self, name: HeaderName, value: HeaderValue) {
        self.0.insert(name, value);
    }

    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)> {
        self.0.iter()
    }

    /// Writes all header lines followed by the blank termination line.
    pub fn write(&self, w: &mut impl std::io::Write) -> std::io::Result<()> {
        for (name, value) in self.0.iter() {
            write!(w, "{name}: {value}\r\n")?;
        }
        w.write_all(b"\r\n")
    }
}

impl fmt::Display for Headers {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (name, value) in self.0.iter() {
            write!(f, "{name}: {value}\r\n")?;
        }
        f.write_str("\r\n")
    }
}

impl<I: IntoIterator<Item = (HeaderName, HeaderValue)>> From<I> for Headers {
    fn from(i: I) -> Self {
        let mut out = Self::default();
        for (name, value) in i {
            out.append(name, value);
        }
        out
    }
}

// ---------------------------------------------------------------------------
// HeaderName
// ---------------------------------------------------------------------------

/// An RTSP header name, which must be a valid token.
/// Compared case-insensitively.
#[derive(Clone)]
pub struct HeaderName(Cow<'static, str>);

impl HeaderName {
    /// Returns a `HeaderName` from a static string, or an error if it's not a valid token.
    pub const fn from_static(name: &'static str) -> Result<Self, &'static str> {
        if !is_valid_token(name.as_bytes()) {
            return Err("invalid RTSP header name token");
        }
        Ok(Self(Cow::Borrowed(name)))
    }

    static_const_tokens! {
        ACCEPT => "Accept",
        AUTHORIZATION => "Authorization",
        CONTENT_TYPE => "Content-Type",
        CSEQ => "CSeq",
        PUBLIC => "Public",
        RANGE => "Range",
        RTP_INFO => "RTP-Info",
        SESSION => "Session",
        TRANSPORT => "Transport",
        USER_AGENT => "User-Agent",
        WWW_AUTHENTICATE => "WWW-Authenticate",
    }
}

impl fmt::Display for HeaderName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl fmt::Debug for HeaderName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "HeaderName({:?})", &*self.0)
    }
}

impl std::borrow::Borrow<CaseInsensitive<str>> for HeaderName {
    fn borrow(&self) -> &CaseInsensitive<str> {
        CaseInsensitive::new(self.0.as_ref())
    }
}

impl Deref for HeaderName {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        self.0.as_ref()
    }
}

impl PartialEq for HeaderName {
    fn eq(&self, other: &Self) -> bool {
        self.0.eq_ignore_ascii_case(&other.0)
    }
}
impl Eq for HeaderName {}

impl PartialOrd for HeaderName {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for HeaderName {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        CaseInsensitive::new(self.0.as_ref()).cmp(CaseInsensitive::new(other.0.as_ref()))
    }
}

impl TryFrom<&'_ [u8]> for HeaderName {
    type Error = InvalidHeaderNameError;
    fn try_from(v: &'_ [u8]) -> Result<HeaderName, InvalidHeaderNameError> {
        Self::try_from(v.to_owned())
    }
}

impl TryFrom<Vec<u8>> for HeaderName {
    type Error = InvalidHeaderNameError;
    fn try_from(v: Vec<u8>) -> Result<HeaderName, InvalidHeaderNameError> {
        if !is_valid_token(&v) {
            return Err(InvalidHeaderNameError(v));
        }
        let v = String::from_utf8(v).expect("valid token => utf-8");
        Ok(HeaderName(Cow::Owned(v)))
    }
}

impl TryFrom<&'_ str> for HeaderName {
    type Error = InvalidHeaderNameError;
    fn try_from(v: &'_ str) -> Result<HeaderName, InvalidHeaderNameError> {
        Self::try_from(v.as_bytes().to_owned())
    }
}

impl TryFrom<String> for HeaderName {
    type Error = InvalidHeaderNameError;
    fn try_from(v: String) -> Result<HeaderName, InvalidHeaderNameError> {
        if !is_valid_token(v.as_bytes()) {
            return Err(InvalidHeaderNameError(v.into_bytes()));
        }
        Ok(Self(Cow::Owned(v)))
    }
}

#[derive(Clone, Debug, derive_more::Display, derive_more::Error)]
#[display("invalid RTSP header name token {:?}", MostlyAscii { bytes: _0, escape_newline: true })]
pub struct InvalidHeaderNameError(#[error(not(source))] Vec<u8>);

/// Compile-time-validated header name literal.
#[allow(unused_macros)]
macro_rules! rtsp_header {
    ($s:literal) => {{
        const H: $crate::rtsp::msg::HeaderName =
            $crate::rtsp::msg::HeaderName(std::borrow::Cow::Borrowed(valid_token!($s)));
        H
    }};
}

// ---------------------------------------------------------------------------
// HeaderValue
// ---------------------------------------------------------------------------

#[derive(Clone, PartialEq, Eq)]
pub struct HeaderValue(String);

impl fmt::Display for HeaderValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl fmt::Debug for HeaderValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", &self.0)
    }
}

impl Deref for HeaderValue {
    type Target = str;
    fn deref(&self) -> &str {
        &self.0
    }
}

impl TryFrom<String> for HeaderValue {
    type Error = InvalidHeaderValueError;
    fn try_from(v: String) -> Result<HeaderValue, InvalidHeaderValueError> {
        if !is_valid_header_value(v.as_bytes()) {
            return Err(InvalidHeaderValueError);
        }
        Ok(Self(v))
    }
}

impl TryFrom<&str> for HeaderValue {
    type Error = InvalidHeaderValueError;
    fn try_from(v: &str) -> Result<HeaderValue, InvalidHeaderValueError> {
        if !is_valid_header_value(v.as_bytes()) {
            return Err(InvalidHeaderValueError);
        }
        Ok(Self(v.to_owned()))
    }
}

impl TryFrom<Vec<u8>> for HeaderValue {
    type Error = InvalidHeaderValueError;
    fn try_from(v: Vec<u8>) -> Result<HeaderValue, InvalidHeaderValueError> {
        if !is_valid_header_value(&v) {
            return Err(InvalidHeaderValueError);
        }
        Ok(Self(
            String::from_utf8(v).expect("valid header value => UTF-8"),
        ))
    }
}

#[derive(Clone, Debug, derive_more::Display, derive_more::Error)]
#[display("invalid RTSP header value")]
pub struct InvalidHeaderValueError;

// ---------------------------------------------------------------------------
// Data
// ---------------------------------------------------------------------------

/// An RTSP interleaved data frame header (the `$` prefix).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Data {
    pub channel_id: u8,
    pub body_len: u16,
}

impl Data {
    pub fn write(&self, w: &mut impl std::io::Write) -> std::io::Result<()> {
        let body_len = self.body_len.to_be_bytes();
        w.write_all(&[b'$', self.channel_id, body_len[0], body_len[1]])
    }
}

// ---------------------------------------------------------------------------
// OwnedMessage — for sending over the wire
// ---------------------------------------------------------------------------

/// A complete message (head + body) for encoding/sending.
#[derive(Clone, Debug)]
pub enum OwnedMessage {
    Request { head: Request, body: bytes::Bytes },
    Response { head: Response, body: bytes::Bytes },
    Data { channel_id: u8, body: bytes::Bytes },
}

impl OwnedMessage {
    /// Returns the method of a Request message.
    /// Panics if this is not a Request.
    pub fn method(&self) -> &Method {
        match self {
            OwnedMessage::Request { head, .. } => &head.method,
            _ => panic!("not a request"),
        }
    }

    /// Returns the method name as a `&str`. Panics if not a Request.
    pub fn method_str(&self) -> &str {
        self.method()
    }

    /// Returns a mutable reference to the request headers.
    /// Panics if this is not a Request.
    pub fn headers_mut(&mut self) -> &mut Headers {
        match self {
            OwnedMessage::Request { head, .. } => &mut head.headers,
            _ => panic!("not a request"),
        }
    }

    /// Returns the request URI as a string, or "*" if none.
    pub fn request_uri_str(&self) -> &str {
        match self {
            OwnedMessage::Request { head, .. } => {
                head.request_uri.as_ref().map(|u| u.as_str()).unwrap_or("*")
            }
            _ => panic!("not a request"),
        }
    }

    pub fn write(&self, w: &mut impl std::io::Write) -> std::io::Result<()> {
        match self {
            OwnedMessage::Request { head, body } => {
                head.write_head(w)?;
                w.write_all(body)?;
            }
            OwnedMessage::Response { head, body } => {
                head.write_head(w)?;
                w.write_all(body)?;
            }
            OwnedMessage::Data { channel_id, body } => {
                let len = body.len() as u16;
                w.write_all(&[b'$', *channel_id, (len >> 8) as u8, len as u8])?;
                w.write_all(body)?;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn hdr_name(s: &str) -> HeaderName {
        HeaderName::try_from(s).unwrap()
    }

    fn hdr_value(s: &str) -> HeaderValue {
        HeaderValue::try_from(s).unwrap()
    }

    #[test]
    fn get_case_insensitive() {
        let mut h = Headers::default();
        h.insert(HeaderName::CSEQ, hdr_value("1"));
        assert_eq!(h.get("CSeq").unwrap().deref(), "1");
        assert_eq!(h.get("cseq").unwrap().deref(), "1");
        assert_eq!(h.get("CSEQ").unwrap().deref(), "1");
        assert!(h.get("Content-Type").is_none());
    }

    #[test]
    fn insert_overwrites() {
        let mut h = Headers::default();
        h.insert(HeaderName::SESSION, hdr_value("old"));
        h.insert(HeaderName::SESSION, hdr_value("new"));
        assert_eq!(h.get("Session").unwrap().deref(), "new");
    }

    #[test]
    fn append_joins_with_comma() {
        let mut h = Headers::default();
        h.append(HeaderName::PUBLIC, hdr_value("DESCRIBE"));
        h.append(HeaderName::PUBLIC, hdr_value("SETUP"));
        h.append(HeaderName::PUBLIC, hdr_value("PLAY"));
        assert_eq!(h.get("Public").unwrap().deref(), "DESCRIBE, SETUP, PLAY");
    }

    #[test]
    fn append_case_insensitive_merges() {
        let mut h = Headers::default();
        h.append(hdr_name("Accept"), hdr_value("a"));
        h.append(hdr_name("accept"), hdr_value("b"));
        // Both should be merged under a single key.
        assert_eq!(h.get("ACCEPT").unwrap().deref(), "a, b");
        assert_eq!(h.iter().count(), 1);
    }

    #[test]
    fn from_iterator() {
        let h = Headers::from([
            (HeaderName::CSEQ, hdr_value("1")),
            (HeaderName::SESSION, hdr_value("abc")),
        ]);
        assert_eq!(h.get("cseq").unwrap().deref(), "1");
        assert_eq!(h.get("session").unwrap().deref(), "abc");
    }

    #[test]
    fn display_format() {
        let h = Headers::from([
            (HeaderName::CSEQ, hdr_value("1")),
            (HeaderName::SESSION, hdr_value("abc")),
        ]);
        let s = h.to_string();
        assert!(s.contains("CSeq: 1\r\n"));
        assert!(s.contains("Session: abc\r\n"));
        assert!(s.ends_with("\r\n\r\n"));
    }
}