rust-auth-utils 1.0.0

A rust port of @better-auth/utils.
Documentation
use crate::base64::{Base64, Base64Url};

#[test]
fn test_base64_encode() {
    let test_cases: Vec<(&[u8], &str)> = vec![
        (b"Hello, World!", "SGVsbG8sIFdvcmxkIQ=="),
        (b"", ""),
        (b"f", "Zg=="),
        (b"fo", "Zm8="),
        (b"foo", "Zm9v"),
        (b"foob", "Zm9vYg=="),
        (b"fooba", "Zm9vYmE="),
        (b"foobar", "Zm9vYmFy"),
    ];

    for (input, expected) in test_cases {
        assert_eq!(Base64::encode(input, None), expected);
    }
}

#[test]
fn test_base64_decode() {
    let test_cases: Vec<(&str, &[u8])> = vec![
        ("SGVsbG8sIFdvcmxkIQ==", b"Hello, World!"),
        ("", b""),
        ("Zg==", b"f"),
        ("Zm8=", b"fo"),
        ("Zm9v", b"foo"),
        ("Zm9vYg==", b"foob"),
        ("Zm9vYmE=", b"fooba"),
        ("Zm9vYmFy", b"foobar"),
    ];

    for (input, expected) in test_cases {
        assert_eq!(Base64::decode(input).unwrap(), expected);
    }
}

#[test]
fn test_base64url_encode() {
    let test_cases: Vec<(&[u8], &str)> = vec![
        (b"Hello, World!", "SGVsbG8sIFdvcmxkIQ=="),
        (b"", ""),
        (b"f", "Zg=="),
        (b"fo", "Zm8="),
        (b"foo", "Zm9v"),
        (b"foob", "Zm9vYg=="),
        (b"fooba", "Zm9vYmE="),
        (b"foobar", "Zm9vYmFy"),
        // Special URL-safe test cases
        (b">>>???", "Pj4-Pz8_"),
        (b"+/+/", "Ky8rLw=="),
    ];

    for (input, expected) in test_cases {
        let output = Base64Url::encode(input, None);
        assert_eq!(
            output.replace("+", "-").replace("/", "_"),
            expected.replace("+", "-").replace("/", "_")
        );
    }
}

#[test]
fn test_base64url_decode() {
    let test_cases: Vec<(&str, &[u8])> = vec![
        ("SGVsbG8sIFdvcmxkIQ==", b"Hello, World!"),
        ("", b""),
        ("Zg==", b"f"),
        ("Zm8=", b"fo"),
        ("Zm9v", b"foo"),
        ("Zm9vYg==", b"foob"),
        ("Zm9vYmE=", b"fooba"),
        ("Zm9vYmFy", b"foobar"),
        // Special URL-safe test cases
        ("Pj4-Pz8_", b">>>???"),
        ("Ky8rLw==", b"+/+/"),
    ];

    for (input, expected) in test_cases {
        assert_eq!(Base64Url::decode(input).unwrap(), expected);
    }
}

#[test]
fn test_padding_options() {
    let input = b"Hello";
    assert_eq!(Base64::encode(input, Some(true)), "SGVsbG8=");
    assert_eq!(Base64::encode(input, Some(false)), "SGVsbG8");
}

#[test]
fn test_string_encoding() {
    let test_str = "Hello, World!";
    let encoded = Base64::encode_string(test_str, None);
    let decoded = Base64::decode_string(&encoded).unwrap();
    assert_eq!(test_str, decoded);
}

#[test]
fn test_invalid_input() {
    let result = Base64::decode("!@#$%^&*");
    assert!(result.is_err());

    let error = result.unwrap_err();
    assert!(error.contains("Invalid Base64 character"));
}

#[test]
fn test_url_safe_detection() {
    let regular = "SGVsbG8sIFdvcmxkIQ==";
    let url_safe = "SGVsbG8sIFdvcmxkIQ==".replace("+", "-").replace("/", "_");

    assert_eq!(
        Base64::decode(regular).unwrap(),
        Base64::decode(&url_safe).unwrap()
    );
}