2019-08-20 23:53:00 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
2018-09-20 04:14:30 +00:00
|
|
|
|
|
|
|
#[macro_use] extern crate rocket;
|
|
|
|
|
|
|
|
use rocket::{Request, Data, Outcome::*};
|
2020-06-22 11:54:34 +00:00
|
|
|
use rocket::local::asynchronous::Client;
|
2018-09-20 04:14:30 +00:00
|
|
|
use rocket::request::Form;
|
|
|
|
use rocket::data::{self, FromDataSimple};
|
|
|
|
use rocket::http::{RawStr, ContentType, Status};
|
|
|
|
|
|
|
|
// Test that the data parameters works as expected.
|
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct Inner<'r> {
|
|
|
|
field: &'r RawStr
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Simple(String);
|
|
|
|
|
|
|
|
impl FromDataSimple for Simple {
|
|
|
|
type Error = ();
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
fn from_data(_: &Request<'_>, data: Data) -> data::FromDataFuture<'static, Self, ()> {
|
|
|
|
Box::pin(async {
|
2019-12-11 00:34:23 +00:00
|
|
|
use tokio::io::AsyncReadExt;
|
2019-08-24 17:27:10 +00:00
|
|
|
|
|
|
|
let mut string = String::new();
|
|
|
|
let mut stream = data.open().take(64);
|
|
|
|
if let Err(_) = stream.read_to_string(&mut string).await {
|
|
|
|
return Failure((Status::InternalServerError, ()));
|
|
|
|
}
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
Success(Simple(string))
|
|
|
|
})
|
2018-09-20 04:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/f", data = "<form>")]
|
2019-06-13 01:59:25 +00:00
|
|
|
fn form(form: Form<Inner<'_>>) -> String { form.field.url_decode_lossy() }
|
2018-09-20 04:14:30 +00:00
|
|
|
|
|
|
|
#[post("/s", data = "<simple>")]
|
|
|
|
fn simple(simple: Simple) -> String { simple.0 }
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_data() {
|
2018-09-20 04:14:30 +00:00
|
|
|
let rocket = rocket::ignite().mount("/", routes![form, simple]);
|
2020-06-14 15:57:53 +00:00
|
|
|
let client = Client::new(rocket).await.unwrap();
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client.post("/f")
|
2018-09-20 04:14:30 +00:00
|
|
|
.header(ContentType::Form)
|
|
|
|
.body("field=this%20is%20here")
|
2019-08-24 17:27:10 +00:00
|
|
|
.dispatch().await;
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-06-22 11:54:34 +00:00
|
|
|
assert_eq!(response.into_string().await.unwrap(), "this is here");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client.post("/s").body("this is here").dispatch().await;
|
|
|
|
assert_eq!(response.into_string().await.unwrap(), "this is here");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client.post("/s").body("this%20is%20here").dispatch().await;
|
|
|
|
assert_eq!(response.into_string().await.unwrap(), "this%20is%20here");
|
2018-09-20 04:14:30 +00:00
|
|
|
}
|