Replies: 1 comment
-
This works just fine, which is morally equivalent to (but much simpler than) what you're doing: #[macro_use] extern crate rocket;
use std::num::ParseIntError;
use rocket::http::{CookieJar, Cookie};
use rocket::response::Debug;
#[get("/")]
fn index(cookies: &CookieJar<'_>) -> Result<String, Debug<ParseIntError>> {
let cookie = cookies.get_private("counter").unwrap_or(Cookie::new("counter", "0"));
let count = cookie.value().parse::<usize>()? + 1;
cookies.add_private(("counter", count.to_string()));
Ok(format!("Count: {count}"))
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}
#[test]
fn test_counter() {
use rocket::local::blocking::Client;
let client = Client::tracked(rocket()).unwrap();
let response = client.get("/").dispatch();
let first_cookie = response.cookies().get("counter").unwrap().to_owned();
assert_eq!(response.into_string(), Some("Count: 1".into()));
let response = client.get("/")
.cookie(("counter", first_cookie.value()))
.dispatch();
assert_eq!(response.into_string(), Some("Count: 2".into()));
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I wrote a simple example using private counters and I added a test. I found out that in order to set the cookie from the client (in the test I need to use the private_cookie and provide the real data.
That's fine, but I am surprised why providing the encrypted cookie-string as regular cookie does not work?
If I use
curl
I can send back the cookie string and that works well. As expected.Here is the code. The server does not recognize the cookie as I set in the second request
Full example can be found here
Beta Was this translation helpful? Give feedback.
All reactions