Rocket/core/lib/tests/catcher-cookies-1213.rs
Sergio Benitez 02011a1307 Clean up codebase: fix reasonable clippy warnings.
This commit is a codebase-wide cleanup driven by clippy warnings. In
addition to fixing every reasonable warning, the following new
functionality was introduced:

  * `Accept::new()` now takes any `T: Into<QMediaType>` iterator.
  * `TempFile::is_empty()` was added.
  * `HeaderMap` now implements `IntoIterator`.

This commit makes the following breaking changes:

  * The `IntoCollection` trait is gone. Generics previously bound by the
    trait are now bound by `IntoIterator`. This affects:
    - `Accept::new()`
    - `ContentType::with_params()`
    - `Permission::{allow, allowed}()`
  * `MediaType`, `QMediaType`, and `Allow` implement `IntoIterator`,
    enabling most existing code to continue working without change.
  * The inherent `HeaderMap::into_iter()` method was removed.
  * The `Ok` variant in ErrorKind::Liftoff` is now `Box<Rocket<Orbit>>`.
2024-03-20 00:47:38 -07:00

48 lines
1.5 KiB
Rust

#[macro_use] extern crate rocket;
use rocket::request::Request;
use rocket::http::CookieJar;
#[catch(404)]
fn not_found(request: &Request<'_>) -> &'static str {
request.cookies().add(("not_found", "404"));
"404 - Not Found"
}
#[get("/")]
fn index(cookies: &CookieJar<'_>) -> &'static str {
cookies.add(("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::build()
.mount("/", routes![index])
.register("/", catchers![not_found])
.attach(AdHoc::on_request("Add Cookie", |req, _| Box::pin(async move {
req.cookies().add(("fairing", "woo"));
})));
let client = Client::debug(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");
}
}