rust-mc-status 3.0.0

High-performance asynchronous Rust library for querying Minecraft server status (Java & Bedrock)
Documentation
//! Unit tests for HTTP CONNECT proxy response validation.
//!
//! Tests `validate_connect_response` logic through the pub(crate) test shim.
//! The actual TCP tunnel is not tested here (requires a live proxy) — those
//! belong in integration tests with a mock server.

#[cfg(feature = "proxy")]
mod http_connect {
    // ── Happy path ────────────────────────────────────────────────────────────

    #[test]
    fn http_200_is_ok() {
        let result = rust_mc_status::proxy::http::validate_response_test(
            "HTTP/1.1 200 Connection established\r\n\r\n"
        );
        assert!(result.is_ok(), "200 should succeed, got {result:?}");
    }

    #[test]
    fn http_200_bare_ok() {
        let result = rust_mc_status::proxy::http::validate_response_test(
            "HTTP/1.0 200 OK\r\n\r\n"
        );
        assert!(result.is_ok());
    }

    #[test]
    fn http_201_is_ok() {
        // Any 2xx should pass
        let result = rust_mc_status::proxy::http::validate_response_test(
            "HTTP/1.1 201 Created\r\n\r\n"
        );
        assert!(result.is_ok());
    }

    // ── Auth errors ───────────────────────────────────────────────────────────

    #[test]
    fn http_407_proxy_auth_required_is_error() {
        let result = rust_mc_status::proxy::http::validate_response_test(
            "HTTP/1.1 407 Proxy Authentication Required\r\n\r\n"
        );
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("407"), "error should mention 407, got: {msg}");
    }

    // ── Rejection errors ──────────────────────────────────────────────────────

    #[test]
    fn http_403_forbidden_is_error() {
        let result = rust_mc_status::proxy::http::validate_response_test(
            "HTTP/1.1 403 Forbidden\r\n\r\n"
        );
        assert!(result.is_err());
    }

    #[test]
    fn http_502_bad_gateway_is_error() {
        let result = rust_mc_status::proxy::http::validate_response_test(
            "HTTP/1.1 502 Bad Gateway\r\n\r\n"
        );
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("502"));
    }

    #[test]
    fn http_503_service_unavailable_is_error() {
        let result = rust_mc_status::proxy::http::validate_response_test(
            "HTTP/1.1 503 Service Unavailable\r\n\r\n"
        );
        assert!(result.is_err());
    }

    // ── Malformed responses ───────────────────────────────────────────────────

    #[test]
    fn empty_response_is_error() {
        let result = rust_mc_status::proxy::http::validate_response_test("");
        assert!(result.is_err());
    }

    #[test]
    fn response_without_status_code_is_error() {
        let result = rust_mc_status::proxy::http::validate_response_test(
            "HTTP/1.1 \r\n\r\n"
        );
        assert!(result.is_err());
    }

    #[test]
    fn non_http_response_is_error() {
        // Proxy sends garbage (e.g. a Minecraft packet)
        let result = rust_mc_status::proxy::http::validate_response_test(
            "\x00\x01\x02\x03"
        );
        assert!(result.is_err());
    }

    #[test]
    fn error_message_contains_status_code() {
        let result = rust_mc_status::proxy::http::validate_response_test(
            "HTTP/1.1 407 Proxy Auth Required\r\n\r\n"
        );
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("407"), "msg should contain 407: {msg}");
        assert!(msg.contains("Proxy Auth Required"), "msg should contain reason: {msg}");
    }
}