browsing 0.1.7

Browser automation: navigate, click, extract, screenshot. Standalone browser control via CDP.
Documentation
//! Cookie management tests

use browsing::browser::views::{Cookie, CookieParam};

#[test]
fn test_cookie_struct_creation() {
    let cookie = Cookie {
        name: "session".to_string(),
        value: "abc123".to_string(),
        domain: ".example.com".to_string(),
        path: "/".to_string(),
        secure: true,
        http_only: true,
        expires: Some(1893456000.0),
        same_site: Some("Lax".to_string()),
        size: None,
        third_party: None,
    };

    assert_eq!(cookie.name, "session");
    assert_eq!(cookie.value, "abc123");
    assert!(cookie.secure);
    assert!(cookie.http_only);
}

#[test]
fn test_cookie_param_default() {
    let param = CookieParam {
        name: "foo".to_string(),
        value: "bar".to_string(),
        ..Default::default()
    };

    assert_eq!(param.name, "foo");
    assert_eq!(param.value, "bar");
    assert!(param.domain.is_none());
    assert!(param.path.is_none());
    assert!(param.secure.is_none());
    assert!(param.http_only.is_none());
    assert!(param.expires.is_none());
    assert!(param.same_site.is_none());
    assert!(param.url.is_none());
}

#[test]
fn test_cookie_param_full() {
    let param = CookieParam {
        name: "auth".to_string(),
        value: "token123".to_string(),
        domain: Some("example.com".to_string()),
        path: Some("/api".to_string()),
        secure: Some(true),
        http_only: Some(true),
        expires: Some(2000000000.0),
        same_site: Some("Strict".to_string()),
        url: Some("https://example.com".to_string()),
    };

    assert_eq!(param.name, "auth");
    assert_eq!(param.domain, Some("example.com".to_string()));
    assert_eq!(param.same_site, Some("Strict".to_string()));
}

#[test]
fn test_cookie_serialization() {
    let cookie = Cookie {
        name: "test".to_string(),
        value: "val".to_string(),
        domain: "localhost".to_string(),
        path: "/".to_string(),
        secure: false,
        http_only: false,
        expires: None,
        same_site: None,
        size: None,
        third_party: None,
    };

    let json = serde_json::to_value(&cookie).unwrap();
    assert_eq!(json["name"], "test");
    assert_eq!(json["value"], "val");
    assert_eq!(json["httpOnly"], false);
}

#[test]
fn test_cookie_deserialization() {
    let json = r#"{
        "name": "sid",
        "value": "xyz",
        "domain": ".test.com",
        "path": "/",
        "secure": true,
        "httpOnly": true,
        "expires": 1893456000,
        "sameSite": "None"
    }"#;

    let cookie: Cookie = serde_json::from_str(json).unwrap();
    assert_eq!(cookie.name, "sid");
    assert_eq!(cookie.value, "xyz");
    assert!(cookie.secure);
    assert!(cookie.http_only);
    assert_eq!(cookie.expires, Some(1893456000.0));
    assert_eq!(cookie.same_site, Some("None".to_string()));
}