servlet 1.2.0

A small application runtime with an explicit operational contract
Documentation
//! Environment tests use snapshots, never process-global mutation.
use super::*;

#[test]
fn parses_all_settings_and_preserves_defaults() {
    let config = AppConfig::from_env_pairs([
        ("SERVLET_BIND", "0.0.0.0:9000"),
        ("SERVLET_BODY_IDLE_TIMEOUT", "250ms"),
        ("SERVLET_REQUEST_TIMEOUT", "15s"),
        ("PATH", "ignored"),
    ])
    .unwrap();
    assert_eq!(config.bind.port(), 9000);
    assert_eq!(config.body_idle_timeout, Duration::from_millis(250));
    assert_eq!(config.request_timeout, Duration::from_secs(15));
    assert_eq!(config.max_in_flight, 128);
}

#[test]
fn supplied_environment_is_validated_for_the_selected_role() {
    let values = [
        ("SERVLET_BIND", "127.0.0.1:9090"),
        ("SERVLET_ADMIN_BIND", "127.0.0.1:9090"),
    ];
    assert_eq!(
        AppConfig::from_env_pairs(values).unwrap_err(),
        ConfigError::ConflictingListeners
    );
    assert_eq!(
        AppConfig::from_env_pairs_for_role(values, Role::Api).unwrap_err(),
        ConfigError::ConflictingListeners
    );
    let config = AppConfig::from_env_pairs_for_role(values, Role::Worker).unwrap();
    assert_eq!(config.bind.port(), 9090);
    assert_eq!(config.admin_bind, Some(config.bind));
}

#[test]
fn invalid_or_unknown_settings_fail_without_exposing_values() {
    for (key, value) in [
        ("SERVLET_SECRET", "do-not-leak"),
        ("SERVLET_EXAMPLE_SERVICE_HOST", "do-not-leak"),
        ("SERVLET_EXAMPLE_SERVICE_PORT", "do-not-leak"),
        ("SERVLET_REQUEST_TIMEOUT", "do-not-leak"),
        ("SERVLET_MAX_CONNECTIONS", "do-not-leak"),
    ] {
        let error = AppConfig::from_env_pairs([(key, value)]).unwrap_err();
        assert!(!error.to_string().contains(value));
        assert!(error.to_string().contains(key));
    }
    assert!(AppConfig::from_env_pairs([("SERVLET_REQUEST_TIMEOUT", "0s")]).is_err());
    assert!(AppConfig::from_env_pairs([("SERVLET_ADMIN_BIND", "0.0.0.0:9001")]).is_err());
}