/
mcmare
/
RustAPI
Обзор
Документация
Войти
/
mcmare
/
RustAPI
Код
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
engine/tests/dispatch_http.rs
101 строка
3 KB
mcmare
Add hot reload via file watcher; fix cross-cutting middleware to wrap merged router
18 июл 2026, 16:48
18 июл 2026, 16:48
104d8a6
Код
Авторство
О чём код?
use arc_swap::ArcSwap; use axum::{ body::Body, http::{Request, StatusCode}, }; use engine::{build_router, connect_all, ensure_tables, load_config, EngineState, RouteTable, ScriptEngine}; use http_body_util::BodyExt; use rustapi_core::AuthConfig; use serde_json::{json, Value}; use std::{path::Path, sync::Arc}; use tower::ServiceExt; async fn build_test_router() -> axum::Router { let config = load_config(Path::new("tests/fixtures/http")).expect("load config"); let pools = Arc::new(connect_all(&config.connections).await.expect("connect")); ensure_tables(&config.endpoints, &pools).await.expect("ensure tables"); let scripts = Arc::new(ScriptEngine::new(pools.clone(), "test-secret".to_string())); let routes = Arc::new(ArcSwap::from_pointee(RouteTable::build(&config).expect("build route table"))); let state = EngineState { pools, scripts, routes }; let auth = AuthConfig::new(vec!["secret-token".to_string()]); build_router(state, auth) } async fn body_json(resp: axum::response::Response) -> Value { let bytes = resp.into_body().collect().await.unwrap().to_bytes(); serde_json::from_slice(&bytes).unwrap() } #[tokio::test] async fn rejects_without_token() { let router = build_test_router().await; let resp = router .oneshot( Request::builder() .method("POST") .uri("/items") .header("content-type", "application/json") .body(Body::from(r#"{"name":"x"}"#)) .unwrap(), ) .await .unwrap(); assert_eq!(resp.status(), StatusCode::UNAUTHORIZED); } #[tokio::test] async fn crud_create_then_read_over_http() { let router = build_test_router().await; let create_resp = router .clone() .oneshot( Request::builder() .method("POST") .uri("/items") .header("authorization", "Bearer secret-token") .header("content-type", "application/json") .body(Body::from(json!({ "name": "widget" }).to_string())) .unwrap(), ) .await .unwrap(); assert_eq!(create_resp.status(), StatusCode::CREATED); let created = body_json(create_resp).await; let id = created["id"].as_str().expect("id in response").to_string(); let read_resp = router .oneshot( Request::builder() .method("GET") .uri(format!("/items/{id}")) .header("x-api-key", "secret-token") .body(Body::empty()) .unwrap(), ) .await .unwrap(); assert_eq!(read_resp.status(), StatusCode::OK); let read = body_json(read_resp).await; assert_eq!(read["name"], json!("widget")); } #[tokio::test] async fn script_endpoint_over_http() { let router = build_test_router().await; let resp = router .oneshot( Request::builder() .method("POST") .uri("/echo") .header("authorization", "Bearer secret-token") .header("content-type", "application/json") .body(Body::from(json!({ "hello": "world" }).to_string())) .unwrap(), ) .await .unwrap(); assert_eq!(resp.status(), StatusCode::OK); let body = body_json(resp).await; assert_eq!(body["received"]["hello"], json!("world")); }