Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestInfo in post middleware doesnt contain header added in pre middleware #119

Open
LizardWizzard opened this issue Feb 28, 2023 · 0 comments

Comments

@LizardWizzard
Copy link

An example:

use hyper::{header::HeaderValue, Body, Request, Response, Server};
// Import the routerify prelude traits.
use routerify::{Middleware, RequestInfo, Router, RouterService};
use std::io;
use std::net::SocketAddr;

// A handler for "/" page.
async fn home_handler(_: Request<Body>) -> Result<Response<Body>, io::Error> {
    Ok(Response::new(Body::from("Home page")))
}

async fn pre(mut req: Request<Body>) -> Result<Request<Body>, io::Error> {
    req.headers_mut()
        .insert("x-custom", HeaderValue::from_static("some value"));
    dbg!("pre", req.headers());
    Ok(req)
}

async fn post(res: Response<Body>, req_info: RequestInfo) -> Result<Response<Body>, io::Error> {
    req_info
        .headers()
        .get("x-custom")
        .expect("should see header added in pre");

    Ok(res)
}

fn router() -> Router<Body, io::Error> {
    // Create a router and specify the the handlers.
    Router::builder()
        .middleware(Middleware::pre(pre))
        .middleware(Middleware::post_with_info(post))
        .get("/", home_handler)
        .build()
        .unwrap()
}

#[tokio::main]
async fn main() {
    let router = router();

    // Create a Service from the router above to handle incoming requests.
    let service = RouterService::new(router).unwrap();

    // The address on which the server will be listening.
    let addr = SocketAddr::from(([127, 0, 0, 1], 3001));

    // Create a server by passing the created service to `.serve` method.
    let server = Server::bind(&addr).serve(service);

    println!("App is running on: {}", addr);
    if let Err(err) = server.await {
        eprintln!("Server error: {}", err);
    }
}

So it will panic with "should see header added in pre".

Is this an expected behavior?

Originally reported by @shanyp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant