mirror of
https://github.com/rwf2/Rocket.git
synced 2025-01-05 17:22:36 +00:00
5d9035ddc1
In brief, this commit: * Updates to the latest upstream 'cookie', fixing a memory leak. * Make changes to 'CookieJar' observable only through 'pending()'. * Deprecates 'Client::new()' in favor of 'Client::tracked()'. * Makes 'dispatch()' on tracked 'Client's synchronize on cookies. * Makes 'Client::untracked()' actually untracked. This commit updates to the latest 'cookie' which removes support for 'Sync' cookie jars. Instead of relying on 'cookie', this commit implements an op-log based 'CookieJar' which internally keeps track of changes. The API is such that changes are only observable through specialized '_pending()' methods.
48 lines
1.5 KiB
Rust
48 lines
1.5 KiB
Rust
#[macro_use] extern crate rocket;
|
|
|
|
use rocket::request::Request;
|
|
use rocket::http::{Cookie, CookieJar};
|
|
|
|
#[catch(404)]
|
|
fn not_found(request: &Request) -> &'static str {
|
|
request.cookies().add(Cookie::new("not_found", "404"));
|
|
"404 - Not Found"
|
|
}
|
|
|
|
#[get("/")]
|
|
fn index(cookies: &CookieJar<'_>) -> &'static str {
|
|
cookies.add(Cookie::new("index", "hi"));
|
|
"Hello, world!"
|
|
}
|
|
|
|
mod tests {
|
|
use super::*;
|
|
use rocket::local::blocking::Client;
|
|
use rocket::fairing::AdHoc;
|
|
|
|
#[test]
|
|
fn error_catcher_sets_cookies() {
|
|
let rocket = rocket::ignite()
|
|
.mount("/", routes![index])
|
|
.register(catchers![not_found])
|
|
.attach(AdHoc::on_request("Add Cookie", |req, _| Box::pin(async move {
|
|
req.cookies().add(Cookie::new("fairing", "woo"));
|
|
})));
|
|
|
|
let client = Client::tracked(rocket).unwrap();
|
|
|
|
// Check that the index returns the `index` and `fairing` cookie.
|
|
let response = client.get("/").dispatch();
|
|
let cookies = response.cookies();
|
|
assert_eq!(cookies.iter().count(), 2);
|
|
assert_eq!(cookies.get("index").unwrap().value(), "hi");
|
|
assert_eq!(cookies.get("fairing").unwrap().value(), "woo");
|
|
|
|
// Check that the catcher returns only the `not_found` cookie.
|
|
let response = client.get("/not-existent").dispatch();
|
|
let cookies = response.cookies();
|
|
assert_eq!(cookies.iter().count(), 1);
|
|
assert_eq!(cookies.get("not_found").unwrap().value(), "404");
|
|
}
|
|
}
|