extern crate rkt;
use rkt::{fairing::AdHoc, http::ContentType, local::blocking::Client};
use rkt::{Build, Rocket};
#[rkt::post("/", data = "<_data>", format = "json")]
fn index(_data: rkt::Data<'_>) -> &'static str {
"json"
}
#[rkt::post("/", data = "<_data>", rank = 2)]
fn other_index(_data: rkt::Data<'_>) -> &'static str {
"other"
}
fn rocket() -> Rocket<Build> {
rkt::build()
.mount("/", rkt::routes![index, other_index])
.attach(AdHoc::on_request("Change CT", |req, _| {
Box::pin(async move {
let need_ct = req.content_type().is_none();
if req.uri().path().starts_with("/add") {
req.set_uri(rkt::uri!(index));
if need_ct {
req.add_header(ContentType::JSON);
}
} else if need_ct {
req.replace_header(ContentType::JSON);
}
})
}))
}
#[test]
fn check_fairing_changes_content_type() {
let client = Client::debug(rocket()).unwrap();
let response = client.post("/").header(ContentType::PNG).dispatch();
assert_eq!(response.into_string().unwrap(), "other");
let response = client.post("/").dispatch();
assert_eq!(response.into_string().unwrap(), "json");
let response = client.post("/add").dispatch();
assert_eq!(response.into_string().unwrap(), "json");
let response = client.post("/add").header(ContentType::HTML).dispatch();
assert_eq!(response.into_string().unwrap(), "other");
}