aoc-toolkit 0.1.0-alpha.2

A toolkit for solving Advent of Code problems
Documentation
use super::*;

#[test]
fn creates_sensitive_session_cookie() {
    let cookie = session_cookie("abc123").unwrap();

    assert_eq!(cookie.to_str().unwrap(), "session=abc123");

    assert!(cookie.is_sensitive());
}

#[test]
fn rejects_invalid_session_tokens() {
    for token in ["", "invalid token", "abc\n123", "abc;other=123", "abc,123"] {
        assert!(matches!(
            session_cookie(token),
            Err(ClientError::InvalidSession)
        ));
    }
}

#[test]
fn accepts_successful_response() {
    assert!(validate_status(StatusCode::OK).is_ok());
}

#[test]
fn rejects_unauthorized_response() {
    assert!(matches!(
        validate_status(StatusCode::BAD_REQUEST),
        Err(ClientError::Unauthorized)
    ));
}

#[test]
fn rejects_rate_limited_response() {
    assert!(matches!(
        validate_status(StatusCode::TOO_MANY_REQUESTS),
        Err(ClientError::RateLimited)
    ));
}

#[test]
fn rejects_redirects() {
    assert!(matches!(
        validate_status(StatusCode::FOUND),
        Err(ClientError::UnexpectedStatus(_))
    ));
}

#[test]
fn accepts_plain_text_response() {
    for value in ["text/plain", "text/plain; charset=utf-8"] {
        assert!(validate_content_type(Some(value)).is_ok());
    }
}

#[test]
fn rejects_html_response() {
    assert!(matches!(
        validate_content_type(Some("text/html")),
        Err(ClientError::InvalidResponse)
    ));
}

#[test]
fn rejects_missing_content_type() {
    assert!(matches!(
        validate_content_type(None),
        Err(ClientError::InvalidResponse)
    ));
}

#[cfg(feature = "cli")]
#[test]
fn detects_logout_link() {
    for path in [
        "/auth/logout",
        "/2025/auth/logout",
        "/2026/auth/logout",
        "/2027/auth/logout",
    ] {
        assert!(has_logout_link(&format!(r#"<a href="{path}">Log Out</a>"#)));
    }
}

#[cfg(feature = "cli")]
#[test]
fn rejects_missing_logout_link() {
    for body in [
        "",
        r#"<a href="/auth/login">Log In</a>"#,
        r#"<a href="/invalid/auth/logout">Log Out</a>"#,
        r#"<a href="https://example.com/auth/logout">Log Out</a>"#,
    ] {
        assert!(!has_logout_link(body));
    }
}