rust-web-server 18.10.0

Single-binary web server and reverse proxy: automatic HTTPS (Let's Encrypt), HTTP/1.1, HTTP/2 and HTTP/3, load balancing, auth and rate limiting, configured in one TOML file. Also a Rust library: routing, middleware, MCP server, ORM, background jobs. No hyper, no Tower; HTTP/2 and HTTP/3 framing via h2 and quinn.
Documentation
use std::sync::atomic::Ordering;

use super::*;
use crate::app::App;
use crate::core::New;
use crate::test_client::TestClient;

// Serialize all tests that touch the global MAINTENANCE_MODE static (the
// admin API's tests switch it too).
fn lock() -> std::sync::MutexGuard<'static, ()> {
    crate::test_env::lock()
}

#[test]
fn normal_mode_passes_through() {
    let _g = lock();
    MAINTENANCE_MODE.store(false, Ordering::SeqCst);
    let app = App::new().wrap(MaintenanceLayer);
    let client = TestClient::new(app);
    let res = client.get("/healthz").send();
    MAINTENANCE_MODE.store(false, Ordering::SeqCst);
    assert_eq!(200, res.status());
}

#[test]
fn maintenance_mode_returns_503() {
    let _g = lock();
    MAINTENANCE_MODE.store(true, Ordering::SeqCst);
    let app = App::new().wrap(MaintenanceLayer);
    let client = TestClient::new(app);
    let res = client.get("/api/users").send();
    MAINTENANCE_MODE.store(false, Ordering::SeqCst);
    assert_eq!(503, res.status());
}

#[test]
fn healthz_passes_in_maintenance_mode() {
    let _g = lock();
    MAINTENANCE_MODE.store(true, Ordering::SeqCst);
    let app = App::new().wrap(MaintenanceLayer);
    let client = TestClient::new(app);
    let res = client.get("/healthz").send();
    MAINTENANCE_MODE.store(false, Ordering::SeqCst);
    assert_eq!(200, res.status());
}

#[test]
fn readyz_passes_in_maintenance_mode() {
    let _g = lock();
    MAINTENANCE_MODE.store(true, Ordering::SeqCst);
    let app = App::new().wrap(MaintenanceLayer);
    let client = TestClient::new(app);
    let res = client.get("/readyz").send();
    MAINTENANCE_MODE.store(false, Ordering::SeqCst);
    // readyz returns its own 503 "not ready" — maintenance layer let it through.
    assert_ne!(b"Service Temporarily Unavailable".as_ref(), res.body_bytes());
}

#[test]
fn passes_through_only_while_off() {
    let _g = lock();
    let request = crate::request::Request::parse(b"GET / HTTP/1.1\r\nHost: x\r\n\r\n").unwrap();
    let connection = crate::server::ConnectionInfo::default();
    MAINTENANCE_MODE.store(false, Ordering::SeqCst);
    assert!(MaintenanceLayer.is_passthrough(&request, &connection));
    MAINTENANCE_MODE.store(true, Ordering::SeqCst);
    assert!(!MaintenanceLayer.is_passthrough(&request, &connection));
    MAINTENANCE_MODE.store(false, Ordering::SeqCst);
}