thruster-rs / Thruster

A fast, middleware based, web framework written in Rust
MIT License
1.06k stars 47 forks source link

reading cookies/headers from context #252

Open Tronikelis opened 1 year ago

Tronikelis commented 1 year ago

Hi, how do I read the headers and the cookies from the response?

I am testing a middleware that needs cookies like this:

let response = Testable::get(
    &app,
    "/",
    vec![("cookie".to_string(), cookie_header.clone())],
)
.await
.unwrap()
.expect_status(200, "OK");

And I am printing out everything like this:

println!("headers: {:#?}", context.headers);
println!("cookies: {:#?}", context.cookies);
println!("cookies__ {:#?}", context.get_cookies());

But every one of those 3 variables return empty:

running 3 tests
headers: {
    "server": "Thruster",
}
cookies: {}
headers: {
    "server": "Thruster",
}
cookies: {}
cookies__ []
headers: {
    "server": "Thruster",
}
cookies: {}
test hello_world ... cookies__ []
cookies__ []
ok
headers: {
    "server": "Thruster",
}
cookies: {}
cookies__ []
Tronikelis commented 1 year ago

@trezm and I am confused about the headers and cookies, I have some suggestions:

I think clear differentiation between request and response variables is needed

So something like this:

// from
context.set("foo", "bar");
// to
context.set_header("foo", "bar");

// from
context.cookie("foo", "bar", &CookieOptions::default());
// to
context.set_cookie("foo", "bar", &CookieOptions::default());

// from
context.headers;
// to
context.res_headers;

// I am confused about the methods below,
// the `context.cookie()` sets the header and thats it
// so these methods won't return anything?

// from
context.get_cookies();
// to
context.get_res_cookies();

// not sure what this does
context.set_cookies(cookies);