shiguredo_http11 2026.5.0

HTTP/1.1 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
//! Cookie ヘッダーパース (RFC 6265)
//!
//! ## 概要
//!
//! RFC 6265 に基づいた Cookie および Set-Cookie ヘッダーのパースを提供します。
//!
//! ## 使い方
//!
//! ```rust
//! use shiguredo_http11::cookie::{Cookie, SetCookie, SameSite};
//!
//! // Cookie ヘッダーパース
//! let cookies = Cookie::parse("session=abc123; user=john").unwrap();
//! assert_eq!(cookies[0].name(), "session");
//! assert_eq!(cookies[0].value(), "abc123");
//!
//! // Set-Cookie ヘッダーパース
//! let set_cookie = SetCookie::parse("session=abc123; Path=/; HttpOnly; Secure", 2026).unwrap();
//! assert_eq!(set_cookie.name(), "session");
//! assert_eq!(set_cookie.value(), "abc123");
//! assert_eq!(set_cookie.path(), Some("/"));
//! assert!(set_cookie.http_only());
//! assert!(set_cookie.secure());
//! ```

use crate::date::{DateError, HttpDate};
use crate::validate::{is_token_char, trim_ows};
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt;

/// Cookie パースエラー
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CookieError {
    /// 空の Cookie
    Empty,
    /// 不正な形式
    InvalidFormat,
    /// 不正な名前
    InvalidName,
    /// 不正な値
    InvalidValue,
    /// 不正な属性
    InvalidAttribute,
    /// 不正な SameSite
    InvalidSameSite,
}

impl fmt::Display for CookieError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CookieError::Empty => write!(f, "empty cookie"),
            CookieError::InvalidFormat => write!(f, "invalid cookie format"),
            CookieError::InvalidName => write!(f, "invalid cookie name"),
            CookieError::InvalidValue => write!(f, "invalid cookie value"),
            CookieError::InvalidAttribute => write!(f, "invalid cookie attribute"),
            CookieError::InvalidSameSite => write!(f, "invalid SameSite attribute"),
        }
    }
}

impl core::error::Error for CookieError {}

/// Cookie (name=value ペア)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cookie {
    /// Cookie 名
    name: String,
    /// Cookie 値
    value: String,
}

impl Cookie {
    /// Cookie ヘッダー文字列をパース
    ///
    /// Cookie ヘッダーは複数の name=value ペアをセミコロンで区切って含みます。
    ///
    /// # 例
    ///
    /// ```rust
    /// use shiguredo_http11::cookie::Cookie;
    ///
    /// let cookies = Cookie::parse("session=abc123; user=john").unwrap();
    /// assert_eq!(cookies.len(), 2);
    /// assert_eq!(cookies[0].name(), "session");
    /// assert_eq!(cookies[1].name(), "user");
    /// ```
    pub fn parse(input: &str) -> Result<Vec<Cookie>, CookieError> {
        let input = trim_ows(input);
        if input.is_empty() {
            return Err(CookieError::Empty);
        }

        let mut cookies = Vec::new();

        for pair in input.split(';') {
            let pair = trim_ows(pair);
            if pair.is_empty() {
                continue;
            }

            let (name, value) = parse_cookie_pair(pair)?;
            cookies.push(Cookie {
                name: name.to_string(),
                value: value.to_string(),
            });
        }

        if cookies.is_empty() {
            return Err(CookieError::Empty);
        }

        Ok(cookies)
    }

    /// 新しい Cookie を作成
    pub fn new(name: &str, value: &str) -> Result<Self, CookieError> {
        if name.is_empty() || !is_valid_cookie_name(name) {
            return Err(CookieError::InvalidName);
        }
        if !is_valid_cookie_value(value) {
            return Err(CookieError::InvalidValue);
        }

        Ok(Cookie {
            name: name.to_string(),
            value: value.to_string(),
        })
    }

    /// Cookie 名を取得
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Cookie 値を取得
    pub fn value(&self) -> &str {
        &self.value
    }
}

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

/// SameSite 属性
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SameSite {
    /// Strict: 同一サイトリクエストのみ送信
    Strict,
    /// Lax: トップレベルナビゲーションでは送信
    #[default]
    Lax,
    /// None: すべてのリクエストで送信 (Secure 必須)
    None,
}

impl SameSite {
    fn from_str(s: &str) -> Result<Self, CookieError> {
        match s.to_ascii_lowercase().as_str() {
            "strict" => Ok(SameSite::Strict),
            "lax" => Ok(SameSite::Lax),
            "none" => Ok(SameSite::None),
            _ => Err(CookieError::InvalidSameSite),
        }
    }
}

impl fmt::Display for SameSite {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SameSite::Strict => write!(f, "Strict"),
            SameSite::Lax => write!(f, "Lax"),
            SameSite::None => write!(f, "None"),
        }
    }
}

/// Set-Cookie ヘッダー
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetCookie {
    /// Cookie 名
    name: String,
    /// Cookie 値
    value: String,
    /// Expires 属性
    expires: Option<HttpDate>,
    /// Max-Age 属性 (秒)
    max_age: Option<i64>,
    /// Domain 属性
    domain: Option<String>,
    /// Path 属性
    path: Option<String>,
    /// Secure 属性
    secure: bool,
    /// HttpOnly 属性
    http_only: bool,
    /// SameSite 属性
    same_site: Option<SameSite>,
}

impl SetCookie {
    /// Set-Cookie ヘッダー文字列をパース
    ///
    /// `reference_year` は Expires 属性の RFC 850 形式 2 桁年解決に使う
    /// 現在年 (RFC 9110 §5.6.7)。
    ///
    /// # 例
    ///
    /// ```rust
    /// use shiguredo_http11::cookie::SetCookie;
    ///
    /// let cookie = SetCookie::parse("session=abc123; Path=/; HttpOnly; Secure", 2026).unwrap();
    /// assert_eq!(cookie.name(), "session");
    /// assert_eq!(cookie.value(), "abc123");
    /// assert_eq!(cookie.path(), Some("/"));
    /// assert!(cookie.http_only());
    /// assert!(cookie.secure());
    /// ```
    pub fn parse(input: &str, reference_year: u16) -> Result<Self, CookieError> {
        let input = trim_ows(input);
        if input.is_empty() {
            return Err(CookieError::Empty);
        }

        let mut parts = input.split(';');

        // 最初の部分は name=value
        let first = parts.next().ok_or(CookieError::InvalidFormat)?;
        let (name, value) = parse_cookie_pair(trim_ows(first))?;

        let mut set_cookie = SetCookie {
            name: name.to_string(),
            value: value.to_string(),
            expires: None,
            max_age: None,
            domain: None,
            path: None,
            secure: false,
            http_only: false,
            same_site: None,
        };

        // 属性をパース
        for part in parts {
            let part = trim_ows(part);
            if part.is_empty() {
                continue;
            }

            if let Some(eq_pos) = part.find('=') {
                let attr_name = part[..eq_pos].trim();
                let attr_value = part[eq_pos + 1..].trim();

                match attr_name.to_ascii_lowercase().as_str() {
                    "expires" => {
                        // RFC 6265 Section 5.2.1: 不正な Expires は無視
                        let parsed = HttpDate::parse(attr_value).or_else(|e| match e {
                            DateError::Rfc850Date => {
                                HttpDate::parse_rfc850(attr_value, reference_year)
                            }
                            other => Err(other),
                        });
                        if let Ok(date) = parsed {
                            set_cookie.expires = Some(date);
                        }
                    }
                    "max-age" => {
                        // RFC 6265 Section 5.2.2: 先頭が DIGIT または "-" 以外なら無視する。
                        // 残りの文字がすべて DIGIT でなければ無視する。
                        let bytes = attr_value.as_bytes();
                        if let Some(&first) = bytes.first()
                            && (first.is_ascii_digit() || first == b'-')
                            && bytes[1..].iter().all(|b| b.is_ascii_digit())
                            && let Ok(age) = attr_value.parse::<i64>()
                        {
                            // RFC 6265 Section 5.2.2: 負の Max-Age は 0 として扱う
                            set_cookie.max_age = Some(if age < 0 { 0 } else { age });
                        }
                    }
                    "domain" => {
                        // RFC 6265 Section 4.1.1:
                        //   domain-value = <subdomain> (RFC 1034 Section 3.5 + RFC 1123 Section 2.1)
                        //   = LDH (letter / digit / hyphen) を含む label を "." で連結したもの。
                        // RFC 6265 Section 5.2.3 / RFC 6265bis Section 5.6.3:
                        //   先頭の "." を 1 つだけ除去し、小文字に変換する。
                        // RFC 6265bis Section 5.1.2 (Canonicalized Host Names):
                        //   Domain attribute は全 label が punycode (LDH) でなければならず、
                        //   非 LDH を含む値は reject すべき (SHOULD)。
                        // 上記を統合し、strip 後の値が「LDH と "." のみで構成され、空でなく、
                        // 再び "." で始まらない」ことを検証する。これにより
                        // parse -> to_string -> parse の fixed-point 性も同時に担保される
                        // (`Display` が値をそのまま吐くため、validity が閉じた集合なら再 parse でも
                        // 同じ値が得られる)。
                        let d = attr_value.strip_prefix('.').unwrap_or(attr_value);
                        if d.is_empty() || d.starts_with('.') || !is_valid_domain_value(d) {
                            // 空 / leading dot 複数 / 非 LDH は無視する
                        } else {
                            set_cookie.domain = Some(d.to_ascii_lowercase());
                        }
                    }
                    "path" if !attr_value.is_empty() && attr_value.starts_with('/') => {
                        // RFC 6265 Section 5.2.4: 空の attribute-value や
                        // / 始まりでない値は default-path を使うべき (None を維持する)
                        set_cookie.path = Some(attr_value.to_string());
                    }
                    "samesite" => {
                        set_cookie.same_site = Some(SameSite::from_str(attr_value)?);
                    }
                    _ => {
                        // 未知の属性は無視 (RFC 6265 の推奨)
                    }
                }
            } else {
                // 値なし属性
                match part.to_ascii_lowercase().as_str() {
                    "secure" => set_cookie.secure = true,
                    "httponly" => set_cookie.http_only = true,
                    _ => {
                        // 未知の属性は無視
                    }
                }
            }
        }

        Ok(set_cookie)
    }

    /// 新しい SetCookie を作成
    pub fn new(name: &str, value: &str) -> Result<Self, CookieError> {
        if name.is_empty() || !is_valid_cookie_name(name) {
            return Err(CookieError::InvalidName);
        }
        if !is_valid_cookie_value(value) {
            return Err(CookieError::InvalidValue);
        }

        Ok(SetCookie {
            name: name.to_string(),
            value: value.to_string(),
            expires: None,
            max_age: None,
            domain: None,
            path: None,
            secure: false,
            http_only: false,
            same_site: None,
        })
    }

    /// Cookie 名を取得
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Cookie 値を取得
    pub fn value(&self) -> &str {
        &self.value
    }

    /// Expires 属性を取得
    pub fn expires(&self) -> Option<&HttpDate> {
        self.expires.as_ref()
    }

    /// Max-Age 属性を取得 (秒)
    pub fn max_age(&self) -> Option<i64> {
        self.max_age
    }

    /// Domain 属性を取得
    pub fn domain(&self) -> Option<&str> {
        self.domain.as_deref()
    }

    /// Path 属性を取得
    pub fn path(&self) -> Option<&str> {
        self.path.as_deref()
    }

    /// Secure 属性を取得
    pub fn secure(&self) -> bool {
        self.secure
    }

    /// HttpOnly 属性を取得
    pub fn http_only(&self) -> bool {
        self.http_only
    }

    /// SameSite 属性を取得
    pub fn same_site(&self) -> Option<SameSite> {
        self.same_site
    }

    /// Expires を設定
    pub fn with_expires(mut self, expires: HttpDate) -> Self {
        self.expires = Some(expires);
        self
    }

    /// Max-Age を設定
    pub fn with_max_age(mut self, max_age: i64) -> Self {
        self.max_age = Some(max_age);
        self
    }

    /// Domain を設定
    pub fn with_domain(mut self, domain: &str) -> Self {
        self.domain = Some(domain.to_string());
        self
    }

    /// Path を設定
    pub fn with_path(mut self, path: &str) -> Self {
        self.path = Some(path.to_string());
        self
    }

    /// Secure を設定
    pub fn with_secure(mut self, secure: bool) -> Self {
        self.secure = secure;
        self
    }

    /// HttpOnly を設定
    pub fn with_http_only(mut self, http_only: bool) -> Self {
        self.http_only = http_only;
        self
    }

    /// SameSite を設定
    pub fn with_same_site(mut self, same_site: SameSite) -> Self {
        self.same_site = Some(same_site);
        self
    }
}

impl fmt::Display for SetCookie {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}={}", self.name, self.value)?;

        if let Some(expires) = &self.expires {
            write!(f, "; Expires={}", expires)?;
        }

        if let Some(max_age) = self.max_age {
            write!(f, "; Max-Age={}", max_age)?;
        }

        if let Some(domain) = &self.domain {
            write!(f, "; Domain={}", domain)?;
        }

        if let Some(path) = &self.path {
            write!(f, "; Path={}", path)?;
        }

        if self.secure {
            write!(f, "; Secure")?;
        }

        if self.http_only {
            write!(f, "; HttpOnly")?;
        }

        if let Some(same_site) = self.same_site {
            write!(f, "; SameSite={}", same_site)?;
        }

        Ok(())
    }
}

/// Cookie name=value ペアをパース
fn parse_cookie_pair(pair: &str) -> Result<(&str, &str), CookieError> {
    let eq_pos = pair.find('=').ok_or(CookieError::InvalidFormat)?;

    let name = pair[..eq_pos].trim();
    let value = pair[eq_pos + 1..].trim();

    if name.is_empty() {
        return Err(CookieError::InvalidName);
    }

    if !is_valid_cookie_name(name) {
        return Err(CookieError::InvalidName);
    }

    // 値の引用符を除去
    let value = if value.starts_with('"') && value.ends_with('"') && value.len() >= 2 {
        &value[1..value.len() - 1]
    } else {
        value
    };

    // RFC 6265 Section 4.1.1: cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
    if !is_valid_cookie_value(value) {
        return Err(CookieError::InvalidValue);
    }

    Ok((name, value))
}

/// 有効な Cookie 名かどうか
/// RFC 6265 Section 4.1.1: cookie-name = token
fn is_valid_cookie_name(s: &str) -> bool {
    !s.is_empty() && s.bytes().all(is_token_char)
}

/// 有効な Cookie 値かどうか
/// RFC 6265 Section 4.1.1
fn is_valid_cookie_value(s: &str) -> bool {
    s.bytes().all(is_cookie_octet)
}

/// 有効な domain-value かどうか
///
/// RFC 6265 Section 4.1.1 は `domain-value = <subdomain>` と定義し、`<subdomain>` は
/// RFC 1034 Section 3.5 + RFC 1123 Section 2.1 の構文に従う。すなわち letter / digit /
/// hyphen を含む label を "." で連結した形である。RFC 6265bis Section 5.1.2 は IDN を
/// punycode (LDH) で表現することを要求しているため、本実装も LDH + "." のみを許容する。
///
/// RFC 1034 Section 3.5 の DNS ラベル制約 (先頭/末尾ハイフン禁止、空ラベル禁止、
/// ラベル長 1-63、全体長 253) は現時点では検証していない。
/// 今後の RFC 改訂や erratum で厳格化が必要になった場合に追加する。
fn is_valid_domain_value(s: &str) -> bool {
    s.bytes()
        .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'.')
}

/// Cookie 値に使える文字
fn is_cookie_octet(b: u8) -> bool {
    b == 0x21
        || (0x23..=0x2B).contains(&b)
        || (0x2D..=0x3A).contains(&b)
        || (0x3C..=0x5B).contains(&b)
        || (0x5D..=0x7E).contains(&b)
}

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

    #[test]
    fn test_cookie_parse_single() {
        let cookies = Cookie::parse("session=abc123").unwrap();
        assert_eq!(cookies.len(), 1);
        assert_eq!(cookies[0].name(), "session");
        assert_eq!(cookies[0].value(), "abc123");
    }

    #[test]
    fn test_cookie_parse_multiple() {
        let cookies = Cookie::parse("session=abc123; user=john").unwrap();
        assert_eq!(cookies.len(), 2);
        assert_eq!(cookies[0].name(), "session");
        assert_eq!(cookies[0].value(), "abc123");
        assert_eq!(cookies[1].name(), "user");
        assert_eq!(cookies[1].value(), "john");
    }

    #[test]
    fn test_cookie_parse_with_spaces() {
        let cookies = Cookie::parse("  session = abc123 ; user = john  ").unwrap();
        assert_eq!(cookies.len(), 2);
        assert_eq!(cookies[0].name(), "session");
        assert_eq!(cookies[0].value(), "abc123");
    }

    #[test]
    fn test_cookie_parse_empty() {
        assert!(Cookie::parse("").is_err());
    }

    #[test]
    fn test_cookie_display() {
        let cookie = Cookie::new("session", "abc123").unwrap();
        assert_eq!(cookie.to_string(), "session=abc123");
    }

    #[test]
    fn test_set_cookie_parse_simple() {
        let cookie = SetCookie::parse("session=abc123", 2026).unwrap();
        assert_eq!(cookie.name(), "session");
        assert_eq!(cookie.value(), "abc123");
        assert!(!cookie.secure());
        assert!(!cookie.http_only());
    }

    #[test]
    fn test_set_cookie_parse_with_attributes() {
        let cookie = SetCookie::parse("session=abc123; Path=/; HttpOnly; Secure", 2026).unwrap();
        assert_eq!(cookie.name(), "session");
        assert_eq!(cookie.value(), "abc123");
        assert_eq!(cookie.path(), Some("/"));
        assert!(cookie.http_only());
        assert!(cookie.secure());
    }

    #[test]
    fn test_set_cookie_parse_with_domain() {
        let cookie = SetCookie::parse("session=abc123; Domain=example.com", 2026).unwrap();
        assert_eq!(cookie.domain(), Some("example.com"));
    }

    #[test]
    fn test_set_cookie_parse_with_max_age() {
        let cookie = SetCookie::parse("session=abc123; Max-Age=3600", 2026).unwrap();
        assert_eq!(cookie.max_age(), Some(3600));
    }

    #[test]
    fn test_set_cookie_parse_with_expires() {
        let cookie = SetCookie::parse(
            "session=abc123; Expires=Sun, 06 Nov 1994 08:49:37 GMT",
            2026,
        )
        .unwrap();
        assert!(cookie.expires().is_some());
    }

    #[test]
    fn test_set_cookie_parse_with_samesite() {
        let cookie = SetCookie::parse("session=abc123; SameSite=Strict", 2026).unwrap();
        assert_eq!(cookie.same_site(), Some(SameSite::Strict));

        let cookie = SetCookie::parse("session=abc123; SameSite=Lax", 2026).unwrap();
        assert_eq!(cookie.same_site(), Some(SameSite::Lax));

        let cookie = SetCookie::parse("session=abc123; SameSite=None", 2026).unwrap();
        assert_eq!(cookie.same_site(), Some(SameSite::None));
    }

    #[test]
    fn test_set_cookie_display() {
        let cookie = SetCookie::new("session", "abc123")
            .unwrap()
            .with_path("/")
            .with_secure(true)
            .with_http_only(true);
        let s = cookie.to_string();
        assert!(s.contains("session=abc123"));
        assert!(s.contains("Path=/"));
        assert!(s.contains("Secure"));
        assert!(s.contains("HttpOnly"));
    }

    #[test]
    fn test_set_cookie_builder() {
        let cookie = SetCookie::new("session", "abc123")
            .unwrap()
            .with_domain("example.com")
            .with_path("/app")
            .with_max_age(3600)
            .with_secure(true)
            .with_http_only(true)
            .with_same_site(SameSite::Strict);

        assert_eq!(cookie.name(), "session");
        assert_eq!(cookie.value(), "abc123");
        assert_eq!(cookie.domain(), Some("example.com"));
        assert_eq!(cookie.path(), Some("/app"));
        assert_eq!(cookie.max_age(), Some(3600));
        assert!(cookie.secure());
        assert!(cookie.http_only());
        assert_eq!(cookie.same_site(), Some(SameSite::Strict));
    }

    #[test]
    fn test_cookie_parse_quoted_value() {
        let cookies = Cookie::parse("session=\"abc123\"").unwrap();
        assert_eq!(cookies[0].value(), "abc123");
    }

    #[test]
    fn test_same_site_default() {
        assert_eq!(SameSite::default(), SameSite::Lax);
    }

    #[test]
    fn test_set_cookie_invalid_expires_ignored() {
        // RFC 6265 Section 5.2.1: 不正な Expires は無視される
        let cookie = SetCookie::parse("session=abc123; Expires=invalid-date", 2026).unwrap();
        assert_eq!(cookie.name(), "session");
        assert_eq!(cookie.value(), "abc123");
        assert!(cookie.expires().is_none());
    }

    #[test]
    fn test_set_cookie_invalid_max_age_ignored() {
        // RFC 6265 Section 5.2.2: 不正な Max-Age は無視される
        let cookie = SetCookie::parse("session=abc123; Max-Age=not-a-number", 2026).unwrap();
        assert_eq!(cookie.name(), "session");
        assert_eq!(cookie.value(), "abc123");
        assert!(cookie.max_age().is_none());
    }
}