Ferrit Explore
中文·繁體·EN·日本語 Sign in Register
cielxl / veld / tests / integration_test.rs
use std::io::{Read, Write};
use std::net::TcpStream;
use std::time::Duration;

// Integration tests for the veld server.
// These tests require the server to be running on localhost:8080.

fn make_request(host: &str, port: u16, request: &str) -> String {
    let addr = format!("{}:{}", host, port);
    match TcpStream::connect_timeout(&addr.parse().unwrap(), Duration::from_secs(5)) {
        Ok(mut stream) => {
            stream
                .set_read_timeout(Some(Duration::from_secs(5)))
                .unwrap();
            stream
                .set_write_timeout(Some(Duration::from_secs(5)))
                .unwrap();
            stream.write_all(request.as_bytes()).unwrap();

            let mut response = String::new();
            stream.read_to_string(&mut response).unwrap();
            response
        }
        Err(e) => {
            // Server not running, skip test
            eprintln!("Could not connect to {}: {}", addr, e);
            String::new()
        }
    }
}

#[test]
fn test_server_starts() {
    // Placeholder: in a full end-to-end test we would start the server and
    // verify it accepts connections. Reaching this point is the assertion.
}

#[test]
fn test_http_get_request() {
    let response = make_request(
        "127.0.0.1",
        8080,
        "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n",
    );
    if response.is_empty() {
        return; // Server not running
    }
    assert!(response.contains("200 OK") || response.contains("HTTP/1.1 200"));
}

#[test]
fn test_http_404() {
    let response = make_request(
        "127.0.0.1",
        8080,
        "GET /nonexistent HTTP/1.1\r\nHost: localhost\r\n\r\n",
    );
    if response.is_empty() {
        return;
    }
    assert!(response.contains("404") || response.contains("Not Found"));
}