Module rama::http::layer::header_config

Expand description

Extract a header config from a request or response and insert it into the Extensions of its Context.

§Example

use rama_http::layer::header_config::{HeaderConfigLayer, HeaderConfigService};
use rama_http::service::web::{WebService};
use rama_http::{Body, Request, StatusCode, HeaderName};
use rama_core::{Context, Service, Layer};
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone)]
struct Config {
    s: String,
    n: i32,
    m: Option<i32>,
    b: bool,
}

#[tokio::main]
async fn main() {
    let service = HeaderConfigLayer::<Config>::required(HeaderName::from_static("x-proxy-config"))
        .layer(WebService::default()
            .get("/", |ctx: Context<()>| async move {
                let cfg = ctx.get::<Config>().unwrap();
                assert_eq!(cfg.s, "E&G");
                assert_eq!(cfg.n, 1);
                assert!(cfg.m.is_none());
                assert!(cfg.b);
            }),
        );

    let request = Request::builder()
        .header("x-proxy-config", "s=E%26G&n=1&b=true")
        .body(Body::empty())
        .unwrap();

    let resp = service.serve(Context::default(), request).await.unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
}

Structs§

Functions§