mirror of https://github.com/rwf2/Rocket.git
Convert assertions in 'form_kitchen_sink' tests to use macros.
This commit is contained in:
parent
b0238e5110
commit
85b3259120
|
@ -14,24 +14,30 @@ impl fmt::Display for FormOption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn assert_form_eq(client: &Client, form_str: &str, expected: String) {
|
macro_rules! assert_form_eq {
|
||||||
let mut res = client.post("/")
|
($client:expr, $form_str:expr, $expected:expr) => {{
|
||||||
.header(ContentType::Form)
|
let mut res = $client.post("/")
|
||||||
.body(form_str)
|
.header(ContentType::Form)
|
||||||
.dispatch().await;
|
.body($form_str)
|
||||||
|
.dispatch().await;
|
||||||
|
|
||||||
assert_eq!(res.body_string().await, Some(expected));
|
assert_eq!(res.body_string().await, Some($expected));
|
||||||
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn assert_valid_form(client: &Client, input: &FormInput<'_>) {
|
macro_rules! assert_valid_form {
|
||||||
let f = format!("checkbox={}&number={}&type={}&password={}&textarea={}&select={}",
|
($client:expr, $input:expr) => {{
|
||||||
input.checkbox, input.number, input.radio, input.password,
|
let f = format!("checkbox={}&number={}&type={}&password={}&textarea={}&select={}",
|
||||||
input.text_area, input.select);
|
$input.checkbox, $input.number, $input.radio, $input.password,
|
||||||
assert_form_eq(client, &f, format!("{:?}", input)).await;
|
$input.text_area, $input.select);
|
||||||
|
assert_form_eq!($client, &f, format!("{:?}", $input));
|
||||||
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn assert_valid_raw_form(client: &Client, form_str: &str, input: &FormInput<'_>) {
|
macro_rules! assert_valid_raw_form {
|
||||||
assert_form_eq(client, form_str, format!("{:?}", input)).await;
|
($client:expr, $form_str:expr, $input:expr) => {{
|
||||||
|
assert_form_eq!($client, $form_str, format!("{:?}", $input));
|
||||||
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::async_test]
|
#[rocket::async_test]
|
||||||
|
@ -46,66 +52,71 @@ async fn test_good_forms() {
|
||||||
select: FormOption::B
|
select: FormOption::B
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
|
|
||||||
input.checkbox = false;
|
input.checkbox = false;
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
|
|
||||||
input.number = 0;
|
input.number = 0;
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
input.number = 120;
|
input.number = 120;
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
input.number = 133;
|
input.number = 133;
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
|
|
||||||
input.radio = FormOption::B;
|
input.radio = FormOption::B;
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
input.radio = FormOption::C;
|
input.radio = FormOption::C;
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
|
|
||||||
input.password = "".into();
|
input.password = "".into();
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
input.password = "----90138490285u2o3hndslkv".into();
|
input.password = "----90138490285u2o3hndslkv".into();
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
input.password = "hi".into();
|
input.password = "hi".into();
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
|
|
||||||
input.text_area = "".to_string();
|
input.text_area = "".to_string();
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
input.text_area = "----90138490285u2o3hndslkv".to_string();
|
input.text_area = "----90138490285u2o3hndslkv".to_string();
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
input.text_area = "hey".to_string();
|
input.text_area = "hey".to_string();
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
|
|
||||||
input.select = FormOption::A;
|
input.select = FormOption::A;
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
input.select = FormOption::C;
|
input.select = FormOption::C;
|
||||||
assert_valid_form(&client, &input).await;
|
assert_valid_form!(&client, &input);
|
||||||
|
|
||||||
// checkbox need not be present; defaults to false; accepts 'on' and 'off'
|
// checkbox need not be present; defaults to false; accepts 'on' and 'off'
|
||||||
assert_valid_raw_form(&client,
|
assert_valid_raw_form!(&client,
|
||||||
"number=133&type=c&password=hi&textarea=hey&select=c",
|
"number=133&type=c&password=hi&textarea=hey&select=c",
|
||||||
&input).await;
|
&input);
|
||||||
|
|
||||||
assert_valid_raw_form(&client,
|
assert_valid_raw_form!(&client,
|
||||||
"checkbox=off&number=133&type=c&password=hi&textarea=hey&select=c",
|
"checkbox=off&number=133&type=c&password=hi&textarea=hey&select=c",
|
||||||
&input).await;
|
&input);
|
||||||
|
|
||||||
input.checkbox = true;
|
input.checkbox = true;
|
||||||
assert_valid_raw_form(&client,
|
assert_valid_raw_form!(&client,
|
||||||
"checkbox=on&number=133&type=c&password=hi&textarea=hey&select=c",
|
"checkbox=on&number=133&type=c&password=hi&textarea=hey&select=c",
|
||||||
&input).await;
|
&input);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn assert_invalid_form(client: &Client, vals: &mut [&str; 6]) {
|
macro_rules! assert_invalid_form {
|
||||||
let s = format!("checkbox={}&number={}&type={}&password={}&textarea={}&select={}",
|
($client:expr, $vals:expr) => {{
|
||||||
vals[0], vals[1], vals[2], vals[3], vals[4], vals[5]);
|
let vals = $vals;
|
||||||
assert_form_eq(client, &s, format!("Invalid form input: {}", s)).await;
|
let s = format!("checkbox={}&number={}&type={}&password={}&textarea={}&select={}",
|
||||||
*vals = ["true", "1", "a", "hi", "hey", "b"];
|
vals[0], vals[1], vals[2], vals[3], vals[4], vals[5]);
|
||||||
|
assert_form_eq!($client, &s, format!("Invalid form input: {}", s));
|
||||||
|
*vals = ["true", "1", "a", "hi", "hey", "b"];
|
||||||
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn assert_invalid_raw_form(client: &Client, form_str: &str) {
|
macro_rules! assert_invalid_raw_form {
|
||||||
assert_form_eq(client, form_str, format!("Invalid form input: {}", form_str)).await;
|
($client:expr, $form_str:expr) => {{
|
||||||
|
assert_form_eq!($client, $form_str, format!("Invalid form input: {}", $form_str));
|
||||||
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::async_test]
|
#[rocket::async_test]
|
||||||
|
@ -114,62 +125,62 @@ async fn check_semantically_invalid_forms() {
|
||||||
let mut form_vals = ["true", "1", "a", "hi", "hey", "b"];
|
let mut form_vals = ["true", "1", "a", "hi", "hey", "b"];
|
||||||
|
|
||||||
form_vals[0] = "not true";
|
form_vals[0] = "not true";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[0] = "bing";
|
form_vals[0] = "bing";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[0] = "true0";
|
form_vals[0] = "true0";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[0] = " false";
|
form_vals[0] = " false";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
|
|
||||||
form_vals[1] = "-1";
|
form_vals[1] = "-1";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[1] = "1e10";
|
form_vals[1] = "1e10";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[1] = "-1-1";
|
form_vals[1] = "-1-1";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[1] = "NaN";
|
form_vals[1] = "NaN";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
|
|
||||||
form_vals[2] = "A?";
|
form_vals[2] = "A?";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[2] = " B";
|
form_vals[2] = " B";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[2] = "d";
|
form_vals[2] = "d";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[2] = "100";
|
form_vals[2] = "100";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[2] = "";
|
form_vals[2] = "";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
|
|
||||||
// password and textarea are always valid, so we skip them
|
// password and textarea are always valid, so we skip them
|
||||||
form_vals[5] = "A.";
|
form_vals[5] = "A.";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[5] = "b ";
|
form_vals[5] = "b ";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[5] = "d";
|
form_vals[5] = "d";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[5] = "-a";
|
form_vals[5] = "-a";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
form_vals[5] = "";
|
form_vals[5] = "";
|
||||||
assert_invalid_form(&client, &mut form_vals).await;
|
assert_invalid_form!(&client, &mut form_vals);
|
||||||
|
|
||||||
// now forms with missing fields
|
// now forms with missing fields
|
||||||
assert_invalid_raw_form(&client, "number=10&type=a&password=hi&textarea=hey").await;
|
assert_invalid_raw_form!(&client, "number=10&type=a&password=hi&textarea=hey");
|
||||||
assert_invalid_raw_form(&client, "number=10&radio=a&password=hi&textarea=hey&select=b").await;
|
assert_invalid_raw_form!(&client, "number=10&radio=a&password=hi&textarea=hey&select=b");
|
||||||
assert_invalid_raw_form(&client, "number=10&password=hi&select=b").await;
|
assert_invalid_raw_form!(&client, "number=10&password=hi&select=b");
|
||||||
assert_invalid_raw_form(&client, "number=10&select=b").await;
|
assert_invalid_raw_form!(&client, "number=10&select=b");
|
||||||
assert_invalid_raw_form(&client, "password=hi&select=b").await;
|
assert_invalid_raw_form!(&client, "password=hi&select=b");
|
||||||
assert_invalid_raw_form(&client, "password=hi").await;
|
assert_invalid_raw_form!(&client, "password=hi");
|
||||||
assert_invalid_raw_form(&client, "").await;
|
assert_invalid_raw_form!(&client, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::async_test]
|
#[rocket::async_test]
|
||||||
async fn check_structurally_invalid_forms() {
|
async fn check_structurally_invalid_forms() {
|
||||||
let client = Client::new(rocket()).await.unwrap();
|
let client = Client::new(rocket()).await.unwrap();
|
||||||
assert_invalid_raw_form(&client, "==&&&&&&==").await;
|
assert_invalid_raw_form!(&client, "==&&&&&&==");
|
||||||
assert_invalid_raw_form(&client, "a&=b").await;
|
assert_invalid_raw_form!(&client, "a&=b");
|
||||||
assert_invalid_raw_form(&client, "=").await;
|
assert_invalid_raw_form!(&client, "=");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::async_test]
|
#[rocket::async_test]
|
||||||
|
@ -177,6 +188,6 @@ async fn check_bad_utf8() {
|
||||||
let client = Client::new(rocket()).await.unwrap();
|
let client = Client::new(rocket()).await.unwrap();
|
||||||
unsafe {
|
unsafe {
|
||||||
let bad_str = std::str::from_utf8_unchecked(b"a=\xff");
|
let bad_str = std::str::from_utf8_unchecked(b"a=\xff");
|
||||||
assert_form_eq(&client, bad_str, "Form input was invalid UTF-8.".into()).await;
|
assert_form_eq!(&client, bad_str, "Form input was invalid UTF-8.".into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue