help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
rust-web-server
/
tests
RSK World
rust-web-server
Rust Web Server - High-Performance Async Web Server + WebSocket Support + JWT Authentication + File Upload + Memory Safety + Educational Design
tests
  • integration_tests.rs5.2 KB
integration_tests.rs
tests/integration_tests.rs
Raw Download
Find: Go to:
/*
 * Integration Tests - Rust Web Server
 *
 * Created by RSK World (https://rskworld.in)
 * Founder: Molla Samser
 * Designer & Tester: Rima Khatun
 *
 * Contact:
 * - Email: hello@rskworld.in, support@rskworld.in
 * - Phone: +91 93305 39277
 * - Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147
 *
 * © 2026 RSK World. All rights reserved.
 * Content used for educational purposes only.
 */

use hyper::{Body, Method, Request, StatusCode};
use rust_web_server::config::ServerConfig;
use rust_web_server::handlers::SharedState;
use rust_web_server::static_files::StaticFileHandler;
use std::path::PathBuf;
use tokio;

mod common;

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::SocketAddr;

    #[tokio::test]
    async fn test_server_config_creation() {
        let config = ServerConfig::new(
            "127.0.0.1".to_string(),
            8080,
            PathBuf::from("static"),
            4,
        );

        assert_eq!(config.host, "127.0.0.1");
        assert_eq!(config.port, 8080);
        assert_eq!(config.static_dir, PathBuf::from("static"));
        assert_eq!(config.workers, 4);
        assert_eq!(config.max_connections, 1000);
        assert_eq!(config.request_timeout_secs, 30);
        assert_eq!(config.buffer_size, 8192);
    }

    #[tokio::test]
    async fn test_bind_address() {
        let config = ServerConfig::new(
            "0.0.0.0".to_string(),
            3000,
            PathBuf::from("public"),
            8,
        );

        assert_eq!(config.bind_address(), "0.0.0.0:3000");
        assert_eq!(config.server_url(), "http://0.0.0.0:3000");
    }

    #[tokio::test]
    async fn test_static_file_handler_creation() {
        let static_dir = PathBuf::from("static");
        let handler = StaticFileHandler::new(static_dir.clone());

        // Test that the handler was created successfully
        assert_eq!(handler.base_dir, static_dir);
    }

    #[tokio::test]
    async fn test_shared_state_creation() {
        let static_handler = StaticFileHandler::new(PathBuf::from("static"));
        let state = SharedState::new(static_handler);

        // Test that counters start at 0
        assert_eq!(*state.request_count.read().await, 0);
        assert_eq!(*state.active_connections.read().await, 0);
    }

    #[tokio::test]
    async fn test_config_from_default() {
        let config = ServerConfig::default();

        assert_eq!(config.host, "127.0.0.1");
        assert_eq!(config.port, 8080);
        assert_eq!(config.workers, 4);
        assert!(config.static_dir.exists() || config.static_dir == PathBuf::from("static"));
    }

    #[tokio::test]
    async fn test_error_handling() {
        use rust_web_server::error::ServerError;

        let error = ServerError::NotFound("Test resource not found".to_string());
        let response = error.to_response();

        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_error_status_codes() {
        use rust_web_server::error::ServerError;

        assert_eq!(ServerError::NotFound("test".to_string()).status_code(), StatusCode::NOT_FOUND);
        assert_eq!(ServerError::BadRequest("test".to_string()).status_code(), StatusCode::BAD_REQUEST);
        assert_eq!(ServerError::Unauthorized("test".to_string()).status_code(), StatusCode::UNAUTHORIZED);
        assert_eq!(ServerError::Forbidden("test".to_string()).status_code(), StatusCode::FORBIDDEN);
        assert_eq!(ServerError::Internal("test".to_string()).status_code(), StatusCode::INTERNAL_SERVER_ERROR);
    }
}

#[cfg(test)]
mod api_tests {
    use super::*;
    use hyper::body;
    use serde_json::{json, Value};

    // Note: These tests would require a running server instance
    // For now, they serve as templates for integration testing

    async fn make_request(method: Method, path: &str) -> Result<Value, Box<dyn std::error::Error>> {
        // This would make an actual HTTP request to a running server
        // For now, return mock data
        let mock_response = match path {
            "/api/info" => json!({
                "name": "Rust Web Server",
                "version": "0.1.0",
                "description": "High-performance web server built with Rust"
            }),
            "/health" => json!({
                "status": "healthy",
                "timestamp": "2026-01-23T10:00:00Z"
            }),
            _ => json!({"error": "Not found"})
        };

        Ok(mock_response)
    }

    #[tokio::test]
    async fn test_health_endpoint_structure() {
        let response = make_request(Method::GET, "/health").await.unwrap();

        assert!(response.get("status").is_some());
        assert!(response.get("timestamp").is_some());
        assert_eq!(response["status"], "healthy");
    }

    #[tokio::test]
    async fn test_info_endpoint_structure() {
        let response = make_request(Method::GET, "/api/info").await.unwrap();

        assert!(response.get("name").is_some());
        assert!(response.get("version").is_some());
        assert!(response.get("description").is_some());
        assert_eq!(response["name"], "Rust Web Server");
    }
}
159 lines•5.2 KB
rust

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer