footing 0.6.6

Feature-gated foundations for Rust backend services
Documentation
#![cfg(feature = "http")]

use footing::http::{ApiProblem, MAX_PROBLEM_CODE_LENGTH, ProblemCode, ProblemIdentifierError};
use serde_json::json;

const STATIC_TEXT: &str = "Account:Already.Exists-2";
const STATIC_CODE: ProblemCode = ProblemCode::from_static(STATIC_TEXT);
const MAX_TEXT: &str = match std::str::from_utf8(&[b'a'; MAX_PROBLEM_CODE_LENGTH]) {
    Ok(value) => value,
    Err(_) => panic!("ASCII must be valid UTF-8"),
};
const MAX_CODE: ProblemCode = ProblemCode::from_static(MAX_TEXT);
const OVERSIZED_TEXT: &str = match std::str::from_utf8(&[b'a'; MAX_PROBLEM_CODE_LENGTH + 1]) {
    Ok(value) => value,
    Err(_) => panic!("ASCII must be valid UTF-8"),
};

#[test]
fn static_codes_and_clones_borrow_the_original_text() {
    let code = STATIC_CODE;
    let cloned = code.clone();
    assert_eq!(code.as_str().as_ptr(), STATIC_TEXT.as_ptr());
    assert_eq!(cloned.as_str().as_ptr(), STATIC_TEXT.as_ptr());
    assert_eq!(code.to_string(), STATIC_TEXT);
    assert_eq!(cloned.into_string(), STATIC_TEXT);
    assert_eq!(MAX_CODE.as_str().len(), MAX_PROBLEM_CODE_LENGTH);
}

#[test]
fn static_and_dynamic_codes_share_validation_boundaries() {
    for value in ["a", "Z", "9", "-_.:", STATIC_TEXT, MAX_TEXT] {
        assert_eq!(
            ProblemCode::from_static(value),
            ProblemCode::try_new(value).unwrap()
        );
    }

    for (value, expected) in [
        ("", ProblemIdentifierError::Empty),
        (
            OVERSIZED_TEXT,
            ProblemIdentifierError::TooLong {
                max: MAX_PROBLEM_CODE_LENGTH,
            },
        ),
        ("not valid", ProblemIdentifierError::InvalidCharacters),
        ("forbidden/path", ProblemIdentifierError::InvalidCharacters),
        ("invalid\0code", ProblemIdentifierError::InvalidCharacters),
        ("échec", ProblemIdentifierError::InvalidCharacters),
    ] {
        assert_eq!(ProblemCode::try_new(value), Err(expected));
        assert!(std::panic::catch_unwind(|| ProblemCode::from_static(value)).is_err());
        assert!(serde_json::from_value::<ProblemCode>(json!(value)).is_err());
    }
}

#[test]
fn only_dynamic_codes_trim_whitespace() {
    for value in [" code ", "\t\ncode\r\n", "\u{2003}code\u{3000}"] {
        assert_eq!(ProblemCode::try_new(value).unwrap().as_str(), "code");
        assert!(std::panic::catch_unwind(|| ProblemCode::from_static(value)).is_err());
    }
    for value in [" ", "\t\r\n", "\u{2003}\u{3000}"] {
        assert_eq!(
            ProblemCode::try_new(value),
            Err(ProblemIdentifierError::Empty)
        );
        assert!(std::panic::catch_unwind(|| ProblemCode::from_static(value)).is_err());
    }
}

#[test]
fn dynamic_codes_reuse_normalized_owned_input() {
    let value = String::from("user_exists");
    let original_storage = value.as_ptr();
    let code = ProblemCode::try_new(value).unwrap();
    assert_eq!(code.as_str().as_ptr(), original_storage);
    let owned = code.into_string();
    assert_eq!(owned.as_ptr(), original_storage);
}

#[test]
fn static_codes_keep_the_existing_problem_and_serde_protocol() {
    assert_eq!(
        serde_json::to_value(STATIC_CODE).unwrap(),
        json!(STATIC_TEXT)
    );
    assert_eq!(
        serde_json::from_value::<ProblemCode>(json!(STATIC_TEXT)).unwrap(),
        STATIC_CODE
    );
    let problem = ApiProblem::conflict(STATIC_CODE, "Account already exists");
    let body = serde_json::to_value(problem.body()).unwrap();
    assert_eq!(body["code"], STATIC_TEXT);
    assert_eq!(body["status"], 409);
}