cookiebox 0.3.0

A type safe cookie management crate
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
//! cookiebox's core functionality  
use crate::attributes::{Attributes, AttributesSetter};
use crate::storage::Storage;
use biscotti::{RemovalCookie, ResponseCookie, ResponseCookieId};
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::{Value, json};
use std::any::type_name;
use thiserror::Error;

/// The error returned by [IncomingConfig] get methods
#[derive(Error, Debug, PartialEq)]
pub enum CookieBoxError {
    #[error("`{0}` does not exist")]
    NotFound(String),
    #[error("Failed to deserialize `{0}` to type `{1}`")]
    Deserialization(String, String),
}

/// Base struct for cookie generic types
pub struct Cookie<'c, T> {
    storage: Storage<'c>,
    attributes: Option<Attributes<'c>>,
    _marker: std::marker::PhantomData<T>,
}

impl<'c, T> Cookie<'c, T> {
    /// Create a cookie instance for any generic type parameter
    pub fn new(storage: &Storage<'c>) -> Self {
        Cookie {
            storage: storage.clone(),
            attributes: None,
            _marker: std::marker::PhantomData,
        }
    }
}
/// Provide methods to `get` data from a cookie instance for any generic type parameter that implements [IncomingConfig]
impl<T: IncomingConfig> Cookie<'_, T> {
    /// Retrieves the data from the [Storage] request collection using the cookie name specified by [CookieName].
    ///
    /// The deserialized date is returned as the associated type defined by the `Get` type from [IncomingConfig].
    /// # Example
    /// ```no_run
    /// use cookiebox::cookiebox_macros::{cookie, FromRequest};
    /// use cookiebox::cookies::{Cookie, CookieName, IncomingConfig};
    /// use actix_web::{HttpResponse, HttpMessage};
    ///
    /// // Set up a generic cookie type
    /// #[cookie(name = "my-cookie")]
    /// pub struct MyCookie;
    ///
    /// impl IncomingConfig for MyCookie {
    ///     type Get = String;
    /// }
    ///  
    /// // Use macro to implement `FromRequest` for cookie collection struct
    /// #[derive(FromRequest)]
    /// pub struct CookieCollection<'c>(Cookie<'c, MyCookie>);
    ///
    /// async fn get_cookie(cookie: CookieCollection<'_>) -> HttpResponse {
    ///     cookie.0.get();
    ///     HttpResponse::Ok().finish()
    /// }
    /// ```
    pub fn get(&self) -> Result<T::Get, CookieBoxError> {
        let data = &self
            .storage
            .request_storage
            .borrow()
            .get(T::COOKIE_NAME)
            .ok_or(CookieBoxError::NotFound(T::COOKIE_NAME.to_string()))?;

        let data = serde_json::from_str(data.value()).map_err(|_| {
            CookieBoxError::Deserialization(
                data.value().to_string(),
                type_name::<T::Get>().to_string(),
            )
        })?;
        Ok(data)
    }

    /// Retrieves a list of data items from the [Storage] request collection with the same name using the cookie name specified by [CookieName].
    ///
    /// Each item in the list is of the associated type `Get` from the [IncomingConfig].
    ///
    /// # Example
    /// ```no_run
    /// use cookiebox::cookiebox_macros::{cookie, FromRequest};
    /// use cookiebox::cookies::{Cookie, CookieName, IncomingConfig};
    /// use actix_web::{HttpResponse, HttpMessage};
    ///
    /// // Set up generic cookie type
    /// #[cookie(name = "my-cookie")]
    /// pub struct MyCookie;
    ///
    /// impl IncomingConfig for MyCookie {
    ///     type Get = String;
    /// }
    ///  
    /// // Use macro to implement `FromRequest` for cookie collection struct
    /// #[derive(FromRequest)]
    /// pub struct CookieCollection<'c>(Cookie<'c, MyCookie>);
    ///
    /// async fn get_all_cookies(cookie: CookieCollection<'_>) -> HttpResponse {
    ///     // return a Vec of set type
    ///     cookie.0.get_all();
    ///     HttpResponse::Ok().finish()
    /// }
    /// ```
    pub fn get_all(&self) -> Result<Vec<T::Get>, CookieBoxError> {
        let data = &self.storage.request_storage.borrow();

        let data = data
            .get_all(T::COOKIE_NAME)
            .ok_or(CookieBoxError::NotFound(T::COOKIE_NAME.to_string()))?;

        let mut result = Vec::new();

        for value in data.values() {
            let data = serde_json::from_str(value).map_err(|_| {
                CookieBoxError::Deserialization(
                    value.to_string(),
                    type_name::<T::Get>().to_string(),
                )
            })?;
            result.push(data);
        }

        Ok(result)
    }
}

/// Provide methods to `insert` and `remove` a cookie instance for any generic type parameter that implements [OutgoingConfig]
impl<T: OutgoingConfig> Cookie<'_, T> {
    /// Add a cookie to the [Storage] response collection which later attached to the HTTP response using the `Set-Cookie` header.
    ///
    /// # Example
    /// ```no_run
    /// use cookiebox::cookiebox_macros::{cookie, FromRequest};
    /// use cookiebox::cookies::{Cookie, CookieName, OutgoingConfig};
    /// use actix_web::{HttpResponse, HttpMessage};
    ///
    /// // Set up generic cookie type
    /// #[cookie(name = "my-cookie")]
    /// pub struct MyCookie;
    ///
    /// impl OutgoingConfig for MyCookie {
    ///     type Insert = String;
    /// }
    ///  
    /// // Use macro to implement `FromRequest` for cookie collection struct
    /// #[derive(FromRequest)]
    /// pub struct CookieCollection<'c>(Cookie<'c, MyCookie>);
    ///
    /// async fn insert_cookie(cookie: CookieCollection<'_>) -> HttpResponse {
    ///     cookie.0.insert("cookie value".to_string());
    ///     HttpResponse::Ok().finish()
    /// }
    /// ```
    pub fn insert(&self, value: T::Insert) {
        let data = T::serialize(value);

        let response_cookie = ResponseCookie::new(T::COOKIE_NAME, data.to_string());

        let attributes = match &self.attributes {
            Some(attributes) => attributes,
            None => &T::attributes(),
        };

        let response_cookie = response_cookie.set_attributes(attributes);

        self.storage
            .response_storage
            .borrow_mut()
            .insert(response_cookie);
    }
    /// Add a removal cookie to the [Storage] response collection, which later attached to the HTTP response using the `Set-Cookie` header.
    ///
    /// Cookie removal is determined by name, path, and domain
    ///
    /// # Example
    /// ```no_run
    /// use cookiebox::cookiebox_macros::{cookie, FromRequest};
    /// use cookiebox::cookies::{Cookie, CookieName, OutgoingConfig};
    /// use actix_web::{HttpResponse, HttpMessage};
    ///
    /// // Set up generic cookie type
    /// #[cookie(name = "my-cookie")]
    /// pub struct MyCookie;
    ///
    /// impl OutgoingConfig for MyCookie {
    ///     type Insert = String;
    /// }
    ///  
    /// // Use macro to implement `FromRequest` for cookie collection struct
    /// #[derive(FromRequest)]
    /// pub struct CookieCollection<'c>(Cookie<'c, MyCookie>);
    ///
    /// async fn remove_cookie(cookie: CookieCollection<'_>) -> HttpResponse {
    ///     cookie.0.remove();
    ///     HttpResponse::Ok().finish()
    /// }
    /// ```
    pub fn remove(&self) {
        let attributes = match &self.attributes {
            Some(attributes) => attributes,
            None => &T::attributes(),
        };

        let removal_cookie = RemovalCookie::new(T::COOKIE_NAME);

        // Sets the domain and path only
        let removal_cookie = removal_cookie.set_attributes(attributes);

        // Inserting the removal cookie will replace any cookie with the same name, path, and domain
        self.storage
            .response_storage
            .borrow_mut()
            .insert(removal_cookie);
    }
    /// Discard a cookie from the response collection [Storage] only
    ///
    /// Discarding a cookie is determined by name, path, and domain
    ///
    /// # Example
    /// ```no_run
    /// use cookiebox::cookiebox_macros::{cookie, FromRequest};
    /// use cookiebox::cookies::{Cookie, CookieName, OutgoingConfig};
    /// use actix_web::{HttpResponse, HttpMessage};
    ///
    /// // Set up generic cookie type
    /// #[cookie(name = "my-cookie")]
    /// pub struct MyCookie;
    ///
    /// impl OutgoingConfig for MyCookie {
    ///     type Insert = String;
    /// }
    ///  
    /// // Use macro to implement `FromRequest` for cookie collection struct
    /// #[derive(FromRequest)]
    /// pub struct CookieCollection<'c>(Cookie<'c, MyCookie>);
    ///
    /// async fn discard_cookie(cookie: CookieCollection<'_>) -> HttpResponse {
    ///     cookie.0.insert("Stephanie".to_string());
    ///     cookie.0.discard();
    ///     HttpResponse::Ok().finish()
    /// }
    /// ```
    pub fn discard(&self) {
        let discard_id = ResponseCookieId::new(T::COOKIE_NAME);

        let attributes = match &self.attributes {
            Some(attributes) => attributes,
            None => &T::attributes(),
        };

        // This sets the path and domain only
        let discard_id = discard_id.set_attributes(attributes);

        self.storage
            .response_storage
            .borrow_mut()
            .discard(discard_id);
    }
}

/// Provide internal customization for `insert` and `remove` methods in [Cookie].
///
/// The `insert` and `remove` will be available when types that implement this trait is used as generic parameters for `Cookie`.
/// ```no_run
/// use cookiebox::cookiebox_macros::cookie;
/// use cookiebox::cookies::{CookieName, OutgoingConfig};
///
/// // Define a generic cookie type
/// #[cookie(name = "__my-cookie")]
/// pub struct MyCookie;
///
/// impl OutgoingConfig for MyCookie {
///    // Configure the insert type
///    type Insert = String;
///    
///    // The default serialization is used here, if customization is needed, implement the `serialize` method.
///    
///    // The default attributes is used here which consists of http-only: true, SameSite: Lax, and
///    // path: "/"
/// }
/// ```
pub trait OutgoingConfig: CookieName {
    /// The serialization type when inserting a cookie to storage
    type Insert: Serialize;

    /// Provides default serialization for a cookie. This can be overwriting
    fn serialize(values: Self::Insert) -> Value {
        json!(values)
    }

    /// Provides preset attributes for a cookie. This can be overwriting
    fn attributes<'c>() -> Attributes<'c> {
        Attributes::default()
    }
}

/// Provide internal customization for `get` and `get_all` methods in [Cookie].
///
/// The `get` and `get_all` will be available when types that implement this trait is used as generic parameters for `Cookie`.
/// ```no_run
/// use cookiebox::cookiebox_macros::cookie;
/// use cookiebox::cookies::{CookieName, IncomingConfig};
/// // Define a generic cookie type struct
/// #[cookie(name = "__my-cookie")]
/// pub struct MyCookie;
///
/// impl IncomingConfig for MyCookie {
///     // Configure the get return type
///     type Get = String;
/// }
/// ```
pub trait IncomingConfig: CookieName {
    /// The deserialization type when getting a cookie from storage
    type Get: DeserializeOwned;
}

/// This is the base implementation of a cookie type
///
/// This is either implemented manually or with macro `#[Cookie(name = "...")]`
pub trait CookieName {
    const COOKIE_NAME: &'static str;
}

#[cfg(test)]
mod tests {
    use crate::cookiebox_macros::cookie;
    use crate::cookies::{Cookie, CookieName, IncomingConfig, OutgoingConfig};
    use crate::time::{SignedDuration, Zoned, civil::date, tz::TimeZone};
    use crate::{Attributes, Expiration, SameSite, Storage};
    use biscotti::{RequestCookie, ResponseCookie};
    use serde::{Deserialize, Serialize};
    use serde_json::json;

    // Cookie types
    #[cookie(name = "type_a")]
    pub struct TypeA;
    #[cookie(name = "type_b")]
    pub struct TypeB;
    #[cookie(name = "type_c")]
    pub struct TypeC;
    #[cookie(name = "type_d")]
    pub struct TypeD;

    #[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
    pub struct GetType {
        name: String,
    }

    // read and write for type a
    impl OutgoingConfig for TypeA {
        type Insert = GetType;
    }
    impl IncomingConfig for TypeA {
        type Get = GetType;
    }

    // read and write for type b
    impl OutgoingConfig for TypeB {
        type Insert = (String, i32);

        fn serialize(values: Self::Insert) -> serde_json::Value {
            json!({
                "name": format!("{} is {}", values.0, values.1)
            })
        }
    }
    impl IncomingConfig for TypeB {
        type Get = GetType;
    }

    // read and write for type c
    impl OutgoingConfig for TypeC {
        type Insert = GetType;

        fn attributes<'c>() -> Attributes<'c> {
            // Expiration has an internal From impl for Into<Option<Zoned>
            let date = date(2024, 1, 15)
                .at(0, 0, 0, 0)
                .to_zoned(TimeZone::UTC)
                .unwrap();

            Attributes::new()
                .path("/some-path")
                .domain("..example.com")
                .same_site(SameSite::Lax)
                .secure(true)
                .http_only(true)
                .partitioned(true)
                .expires(date)
                .max_age(SignedDuration::from_hours(10))
        }
    }
    impl IncomingConfig for TypeC {
        type Get = GetType;
    }

    // read and write for type d
    impl OutgoingConfig for TypeD {
        type Insert = GetType;

        fn attributes<'c>() -> Attributes<'c> {
            Attributes::new().permanent(true)
        }
    }
    impl IncomingConfig for TypeD {
        type Get = GetType;
    }

    #[test]
    fn get() {
        // Set up
        // Initialize storage
        let storage = Storage::new();
        let incoming_cookie = RequestCookie::new("type_a", r#"{ "name": "some value" }"#);
        let get_type_value = GetType {
            name: "some value".to_string(),
        };

        storage.request_storage.borrow_mut().append(incoming_cookie);

        // Use generic type parameter to create a cookie instance
        let cookie = Cookie::<TypeA>::new(&storage);

        let typed_request_value = cookie.get();

        assert_eq!(typed_request_value.is_ok(), true);
        assert_eq!(typed_request_value, Ok(get_type_value));
    }
    #[test]
    fn get_all() {
        // Set up
        // Initialize storage
        let storage = Storage::new();
        let incoming_cookie_a = RequestCookie::new("type_a", r#"{ "name": "some value 1" }"#);
        let incoming_cookie_b = RequestCookie::new("type_a", r#"{ "name": "some value 2" }"#);
        let get_type_values = vec![
            GetType {
                name: "some value 1".to_string(),
            },
            GetType {
                name: "some value 2".to_string(),
            },
        ];

        storage
            .request_storage
            .borrow_mut()
            .append(incoming_cookie_a);
        storage
            .request_storage
            .borrow_mut()
            .append(incoming_cookie_b);

        // Use generic type parameter to create a cookie instance
        let cookie = Cookie::<TypeA>::new(&storage);

        let typed_request_value = cookie.get_all();

        assert_eq!(typed_request_value.is_ok(), true);
        assert_eq!(typed_request_value, Ok(get_type_values));
    }
    #[test]
    fn insert_cookie() {
        // Set up
        // Initialize storage
        let storage = Storage::new();
        let outgoing_cookie = ResponseCookie::new("type_a", r#"{ "name": "some value" }"#);
        // The id determined by name path and domain
        let outgoing_cookie_id = outgoing_cookie.id().set_path("/");
        let get_type_value = GetType {
            name: "some value ".to_string(),
        };

        // Use generic type parameter to create a cookie instance
        let cookie = Cookie::<TypeA>::new(&storage);

        cookie.insert(get_type_value);

        let binding = storage.response_storage.borrow();
        let response_cookie = binding.get(outgoing_cookie_id);

        assert_eq!(response_cookie.is_some(), true);
        assert_eq!(
            response_cookie.unwrap().name_value(),
            ("type_a", r#"{"name":"some value "}"#)
        );
    }
    #[test]
    fn insert_cookie_with_custom_serialize_impl() {
        // Set up
        // Initialize storage
        let storage = Storage::new();
        let outgoing_cookie = ResponseCookie::new("type_b", r#"{ "name": "some value is 32" }"#);
        // The id determined by name path and domain
        let outgoing_cookie_id = outgoing_cookie.id().set_path("/");
        let get_type_value = ("some value".to_string(), 32);

        // Use generic type parameter to create a cookie instance
        let cookie = Cookie::<TypeB>::new(&storage);

        cookie.insert(get_type_value);

        let binding = storage.response_storage.borrow();
        let response_cookie = binding.get(outgoing_cookie_id);

        assert_eq!(response_cookie.is_some(), true);
        assert_eq!(
            response_cookie.unwrap().name_value(),
            ("type_b", r#"{"name":"some value is 32"}"#)
        );
    }
    #[test]
    fn insert_cookie_with_custom_attributes() {
        // Set up
        // Initialize storage
        let storage = Storage::new();
        let outgoing_cookie = ResponseCookie::new("type_c", r#"{ "name": "some value" }"#);
        // The id determined by name path and domain
        let outgoing_cookie_id = outgoing_cookie
            .id()
            .set_path("/some-path")
            .set_domain("..example.com");
        let get_type_value = GetType {
            name: "some value".to_string(),
        };

        // Expiration cookie set up
        let date = date(2024, 1, 15)
            .at(0, 0, 0, 0)
            .to_zoned(TimeZone::UTC)
            .unwrap();

        // Use generic type parameter to create a cookie instance
        let cookie = Cookie::<TypeC>::new(&storage);

        cookie.insert(get_type_value);

        let binding = storage.response_storage.borrow();
        let response_cookie = binding.get(outgoing_cookie_id);

        assert_eq!(response_cookie.is_some(), true);
        assert_eq!(
            response_cookie.unwrap().name_value(),
            ("type_c", r#"{"name":"some value"}"#)
        );
        assert_eq!(response_cookie.unwrap().path(), Some("/some-path"));
        assert_eq!(response_cookie.unwrap().domain(), Some(".example.com"));
        assert_eq!(response_cookie.unwrap().same_site(), Some(SameSite::Lax));
        assert_eq!(response_cookie.unwrap().http_only(), Some(true));
        assert_eq!(response_cookie.unwrap().secure(), Some(true));
        assert_eq!(response_cookie.unwrap().partitioned(), Some(true));
        assert_eq!(
            response_cookie.unwrap().expires(),
            Some(&Expiration::from(date))
        );
        assert_eq!(
            response_cookie.unwrap().max_age(),
            Some(SignedDuration::from_hours(10))
        );
    }
    #[test]
    fn double_insert_cookie_with_custom_attributes_should_not_change_attributes_values() {
        // Set up
        // Initialize storage
        let storage = Storage::new();
        let outgoing_cookie = ResponseCookie::new("type_c", r#"{ "name": "some value" }"#);
        // The id determined by name path and domain
        let outgoing_cookie_id = outgoing_cookie
            .id()
            .set_path("/some-path")
            .set_domain("..example.com");
        let get_type_value = GetType {
            name: "some value".to_string(),
        };

        // Expiration cookie set up
        let date = date(2024, 1, 15)
            .at(0, 0, 0, 0)
            .to_zoned(TimeZone::UTC)
            .unwrap();

        // Use generic type parameter to create a cookie instance
        let cookie = Cookie::<TypeC>::new(&storage);

        cookie.insert(get_type_value.clone());
        cookie.insert(get_type_value);

        let binding = storage.response_storage.borrow();
        let response_cookie = binding.get(outgoing_cookie_id);

        assert_eq!(response_cookie.is_some(), true);
        assert_eq!(
            response_cookie.unwrap().name_value(),
            ("type_c", r#"{"name":"some value"}"#)
        );
        assert_eq!(response_cookie.unwrap().path(), Some("/some-path"));
        assert_eq!(response_cookie.unwrap().domain(), Some(".example.com"));
        assert_eq!(response_cookie.unwrap().same_site(), Some(SameSite::Lax));
        assert_eq!(response_cookie.unwrap().http_only(), Some(true));
        assert_eq!(response_cookie.unwrap().secure(), Some(true));
        assert_eq!(response_cookie.unwrap().partitioned(), Some(true));
        assert_eq!(
            response_cookie.unwrap().expires(),
            Some(&Expiration::from(date))
        );
        assert_eq!(
            response_cookie.unwrap().max_age(),
            Some(SignedDuration::from_hours(10))
        );
    }
    #[test]
    fn insert_cookie_with_permanent() {
        // Set up
        // Initialize storage
        let storage = Storage::new();
        let outgoing_cookie = ResponseCookie::new("type_d", r#"{ "name": "some value" }"#);
        // The id determined by name path and domain
        let outgoing_cookie_id = outgoing_cookie.id();
        let get_type_value = GetType {
            name: "some value".to_string(),
        };

        // Use generic type parameter to create a cookie instance
        let cookie = Cookie::<TypeD>::new(&storage);

        cookie.insert(get_type_value);

        let binding = storage.response_storage.borrow();
        let response_cookie = binding.get(outgoing_cookie_id);

        assert_eq!(response_cookie.is_some(), true);
        assert_eq!(
            response_cookie.unwrap().name_value(),
            ("type_d", r#"{"name":"some value"}"#)
        );
        assert_eq!(
            response_cookie.unwrap().max_age(),
            Some(SignedDuration::from_hours(24 * 20 * 365))
        );
    }
    #[test]
    fn remove_cookie() {
        // Set up
        // Initialize storage
        let storage = Storage::new();
        let outgoing_cookie = ResponseCookie::new("type_b", r#"{ "name": "some value is 32" }"#);
        // The id determined by name path and domain
        let outgoing_cookie_id = outgoing_cookie.id().set_path("/");

        // Use generic type parameter to create a cookie instance
        let cookie = Cookie::<TypeB>::new(&storage);

        cookie.remove();

        let binding = storage.response_storage.borrow();
        let response_cookie = binding.get(outgoing_cookie_id);

        assert_eq!(response_cookie.is_some(), true);
        assert_eq!(response_cookie.unwrap().name_value(), ("type_b", ""));
        assert!(
            response_cookie
                .unwrap()
                .expires()
                .unwrap()
                .datetime()
                .unwrap()
                < Zoned::now()
        );
    }
    #[test]
    fn discard_cookie() {
        // Set up
        // Initialize storage
        let storage = Storage::new();
        let outgoing_cookie = ResponseCookie::new("type_b", r#"{ "name": "some value is 32" }"#);
        // The id determined by name path and domain
        let outgoing_cookie_id = outgoing_cookie.id().set_path("/");

        // Use generic type parameter to create a cookie instance
        let cookie = Cookie::<TypeB>::new(&storage);

        cookie.discard();

        let binding = storage.response_storage.borrow();
        let response_cookie = binding.get(outgoing_cookie_id);

        assert_eq!(response_cookie.is_some(), false);
    }
}