feat: middleware

This commit is contained in:
DavidOnTop 2024-07-05 10:11:17 +02:00
parent 7c76a4a04e
commit 4e65532679
No known key found for this signature in database
GPG key ID: 5D05538A45D5149F
11 changed files with 138 additions and 39 deletions

34
tests/middleware.rs Normal file
View file

@ -0,0 +1,34 @@
use axum::{response::IntoResponse, routing::get, Json, Router};
use axum_test::TestServer;
use http::{HeaderMap, HeaderName, HeaderValue};
use leptos_reactive_axum::extract;
use serde_json::{json, Value};
async fn handler(Json(json): Json<Value>) -> impl IntoResponse {
let headers: HeaderMap = extract().await.unwrap();
assert_eq!(
headers.get("macrosareawesome"),
Some(&HeaderValue::from_static("YehTheyAre"))
);
assert_eq!(json.as_str(), Some("hello there"));
""
}
#[tokio::test]
async fn test_handlers() {
let router = Router::new()
.route("/", get(handler))
.layer(leptos_reactive_axum::middleware::ReactiveLayer);
let server = TestServer::new(router).unwrap();
let _ = server
.get("/")
.add_header(
HeaderName::from_static("macrosareawesome"),
HeaderValue::from_static("YehTheyAre"),
)
.json(&json!("hello there"));
}