Add field renaming to query params example.

This commit is contained in:
Giles Cope 2020-02-11 21:23:28 +00:00 committed by Sergio Benitez
parent afe3e71219
commit ff4b63c0be
2 changed files with 8 additions and 6 deletions

View File

@ -8,6 +8,8 @@ use rocket::request::{Form, LenientForm};
#[derive(FromForm)] #[derive(FromForm)]
struct Person { struct Person {
/// Use the `form` attribute to expect an invalid Rust identifier in the HTTP form.
#[form(field = "first-name")]
name: String, name: String,
age: Option<u8> age: Option<u8>
} }

View File

@ -11,12 +11,12 @@ macro_rules! run_test {
#[test] #[test]
fn age_and_name_params() { fn age_and_name_params() {
run_test!("?age=10&name=john", |mut response: Response<'_>| { run_test!("?age=10&first-name=john", |mut response: Response<'_>| {
assert_eq!(response.body_string(), assert_eq!(response.body_string(),
Some("Hello, 10 year old named john!".into())); Some("Hello, 10 year old named john!".into()));
}); });
run_test!("?age=20&name=john", |mut response: Response<'_>| { run_test!("?age=20&first-name=john", |mut response: Response<'_>| {
assert_eq!(response.body_string(), assert_eq!(response.body_string(),
Some("20 years old? Hi, john!".into())); Some("20 years old? Hi, john!".into()));
}); });
@ -37,7 +37,7 @@ fn age_param_only() {
#[test] #[test]
fn name_param_only() { fn name_param_only() {
run_test!("?name=John", |mut response: Response<'_>| { run_test!("?first-name=John", |mut response: Response<'_>| {
assert_eq!(response.body_string(), Some("Hello John!".into())); assert_eq!(response.body_string(), Some("Hello John!".into()));
}); });
} }
@ -57,12 +57,12 @@ fn no_params() {
#[test] #[test]
fn extra_params() { fn extra_params() {
run_test!("?age=20&name=Bob&extra", |mut response: Response<'_>| { run_test!("?age=20&first-name=Bob&extra", |mut response: Response<'_>| {
assert_eq!(response.body_string(), assert_eq!(response.body_string(),
Some("20 years old? Hi, Bob!".into())); Some("20 years old? Hi, Bob!".into()));
}); });
run_test!("?age=30&name=Bob&extra", |mut response: Response<'_>| { run_test!("?age=30&first-name=Bob&extra", |mut response: Response<'_>| {
assert_eq!(response.body_string(), assert_eq!(response.body_string(),
Some("We're gonna need a name, and only a name.".into())); Some("We're gonna need a name, and only a name.".into()));
}); });
@ -70,7 +70,7 @@ fn extra_params() {
#[test] #[test]
fn wrong_path() { fn wrong_path() {
run_test!("/other?age=20&name=Bob", |response: Response<'_>| { run_test!("/other?age=20&first-name=Bob", |response: Response<'_>| {
assert_eq!(response.status(), Status::NotFound); assert_eq!(response.status(), Status::NotFound);
}); });
} }