2019-08-20 23:53:00 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
2018-10-05 04:44:42 +00:00
|
|
|
|
2018-08-07 02:58:07 +00:00
|
|
|
use rocket::local::Client;
|
|
|
|
use rocket::response::Responder;
|
|
|
|
use rocket::http::{Status, ContentType, Cookie};
|
|
|
|
|
|
|
|
#[derive(Responder)]
|
|
|
|
pub enum Foo<'r> {
|
|
|
|
First(String),
|
|
|
|
#[response(status = 500)]
|
|
|
|
Second(Vec<u8>),
|
|
|
|
#[response(status = 404, content_type = "html")]
|
|
|
|
Third {
|
|
|
|
responder: &'r str,
|
2019-06-13 01:59:25 +00:00
|
|
|
ct: rocket::http::ContentType,
|
2018-08-07 02:58:07 +00:00
|
|
|
},
|
|
|
|
#[response(status = 105)]
|
|
|
|
Fourth {
|
|
|
|
string: &'r str,
|
2019-06-13 01:59:25 +00:00
|
|
|
ct: rocket::http::ContentType,
|
2018-08-07 02:58:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn responder_foo() {
|
2020-06-14 15:57:53 +00:00
|
|
|
let client = Client::new(rocket::ignite()).await.expect("valid rocket");
|
2018-08-07 02:58:07 +00:00
|
|
|
let local_req = client.get("/");
|
|
|
|
let req = local_req.inner();
|
|
|
|
|
|
|
|
let mut response = Foo::First("hello".into())
|
2020-02-03 08:30:22 +00:00
|
|
|
.respond_to(req).await
|
2018-08-07 02:58:07 +00:00
|
|
|
.expect("response okay");
|
|
|
|
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert_eq!(response.content_type(), Some(ContentType::Plain));
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_eq!(response.body_string().await, Some("hello".into()));
|
2018-08-07 02:58:07 +00:00
|
|
|
|
|
|
|
let mut response = Foo::Second("just a test".into())
|
2020-02-03 08:30:22 +00:00
|
|
|
.respond_to(req).await
|
2018-08-07 02:58:07 +00:00
|
|
|
.expect("response okay");
|
|
|
|
|
|
|
|
assert_eq!(response.status(), Status::InternalServerError);
|
|
|
|
assert_eq!(response.content_type(), Some(ContentType::Binary));
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_eq!(response.body_string().await, Some("just a test".into()));
|
2018-08-07 02:58:07 +00:00
|
|
|
|
|
|
|
let mut response = Foo::Third { responder: "well, hi", ct: ContentType::JSON }
|
2020-02-03 08:30:22 +00:00
|
|
|
.respond_to(req).await
|
2018-08-07 02:58:07 +00:00
|
|
|
.expect("response okay");
|
|
|
|
|
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
assert_eq!(response.content_type(), Some(ContentType::HTML));
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_eq!(response.body_string().await, Some("well, hi".into()));
|
2018-08-07 02:58:07 +00:00
|
|
|
|
|
|
|
let mut response = Foo::Fourth { string: "goodbye", ct: ContentType::JSON }
|
2020-02-03 08:30:22 +00:00
|
|
|
.respond_to(req).await
|
2018-08-07 02:58:07 +00:00
|
|
|
.expect("response okay");
|
|
|
|
|
|
|
|
assert_eq!(response.status(), Status::raw(105));
|
|
|
|
assert_eq!(response.content_type(), Some(ContentType::JSON));
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_eq!(response.body_string().await, Some("goodbye".into()));
|
2018-08-07 02:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Responder)]
|
|
|
|
#[response(content_type = "plain")]
|
|
|
|
pub struct Bar<'r> {
|
|
|
|
responder: Foo<'r>,
|
|
|
|
other: ContentType,
|
|
|
|
third: Cookie<'static>,
|
|
|
|
#[response(ignore)]
|
|
|
|
_yet_another: String,
|
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn responder_bar() {
|
2020-06-14 15:57:53 +00:00
|
|
|
let client = Client::new(rocket::ignite()).await.expect("valid rocket");
|
2018-08-07 02:58:07 +00:00
|
|
|
let local_req = client.get("/");
|
|
|
|
let req = local_req.inner();
|
|
|
|
|
|
|
|
let mut response = Bar {
|
|
|
|
responder: Foo::Second("foo foo".into()),
|
|
|
|
other: ContentType::HTML,
|
|
|
|
third: Cookie::new("cookie", "here!"),
|
|
|
|
_yet_another: "uh..hi?".into()
|
2019-08-24 17:27:10 +00:00
|
|
|
}.respond_to(req).await.expect("response okay");
|
2018-08-07 02:58:07 +00:00
|
|
|
|
|
|
|
assert_eq!(response.status(), Status::InternalServerError);
|
|
|
|
assert_eq!(response.content_type(), Some(ContentType::Plain));
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_eq!(response.body_string().await, Some("foo foo".into()));
|
2018-08-07 02:58:07 +00:00
|
|
|
assert_eq!(response.headers().get_one("Set-Cookie"), Some("cookie=here!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Responder)]
|
|
|
|
#[response(content_type = "application/x-custom")]
|
|
|
|
pub struct Baz {
|
|
|
|
responder: &'static str,
|
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn responder_baz() {
|
2020-06-14 15:57:53 +00:00
|
|
|
let client = Client::new(rocket::ignite()).await.expect("valid rocket");
|
2018-08-07 02:58:07 +00:00
|
|
|
let local_req = client.get("/");
|
|
|
|
let req = local_req.inner();
|
|
|
|
|
|
|
|
let mut response = Baz { responder: "just a custom" }
|
2020-02-03 08:30:22 +00:00
|
|
|
.respond_to(req).await
|
2018-08-07 02:58:07 +00:00
|
|
|
.expect("response okay");
|
|
|
|
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert_eq!(response.content_type(), Some(ContentType::new("application", "x-custom")));
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_eq!(response.body_string().await, Some("just a custom".into()));
|
2018-08-07 02:58:07 +00:00
|
|
|
}
|