Rocket/core/lib/tests/derive-reexports.rs
Sergio Benitez 03127f4dae Add blocking variant of 'local'.
This commit adds the 'local::blocking' module and moves the existing
asynchronous testing to 'local::asynchronous'. It also includes several
changes to improve the local API, bringing it to parity (and beyond)
with master. These changes are:

  * 'LocalRequest' implements 'Clone'.
  * 'LocalResponse' doesn't implement 'DerefMut<Target=Response>'.
    Instead, direct methods on the type, such as 'into_string()', can
    be used to read the 'Response'.
  * 'Response::body()' returns an '&ResponseBody' as opposed to '&mut
    ResponseBody', which is returned by a new 'Response::body_mut()'.
  * '&ResponseBody' implements 'known_size()` to retrieve a body's size,
    if it is known.

Co-authored-by: Jeb Rosen <jeb@jebrosen.com>
2020-07-11 09:24:30 -07:00

59 lines
1.3 KiB
Rust

#![feature(proc_macro_hygiene)]
use rocket;
use rocket::{get, routes};
use rocket::request::{Form, FromForm, FromFormValue};
use rocket::response::Responder;
#[derive(FromFormValue)]
enum Thing {
A,
B,
C,
}
impl std::fmt::Display for Thing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Thing::A => write!(f, "a"),
Thing::B => write!(f, "b"),
Thing::C => write!(f, "c"),
}
}
}
#[derive(FromForm)]
struct ThingForm {
thing: Thing,
}
#[derive(Responder)]
struct DerivedResponder {
data: String,
}
#[get("/")]
fn index() -> DerivedResponder {
DerivedResponder { data: "hello".to_string() }
}
#[get("/?<params..>")]
fn number(params: Form<ThingForm>) -> DerivedResponder {
DerivedResponder { data: params.thing.to_string() }
}
#[rocket::async_test]
async fn test_derive_reexports() {
use rocket::local::asynchronous::Client;
let rocket = rocket::ignite().mount("/", routes![index, number]);
let client = Client::new(rocket).await.unwrap();
let response = client.get("/").dispatch().await;
assert_eq!(response.into_string().await.unwrap(), "hello");
let response = client.get("/?thing=b").dispatch().await;
assert_eq!(response.into_string().await.unwrap(), "b");
}