2018-08-07 02:58:07 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2017-04-18 07:25:13 +00:00
|
|
|
|
|
|
|
use rocket::request::Form;
|
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct Simple {
|
|
|
|
value: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/", data = "<form>")]
|
|
|
|
fn index(form: Form<Simple>) -> String {
|
|
|
|
form.into_inner().value
|
|
|
|
}
|
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
mod limits_tests {
|
2017-04-18 07:25:13 +00:00
|
|
|
use rocket;
|
2020-07-05 18:35:36 +00:00
|
|
|
use rocket::local::blocking::Client;
|
2017-04-18 07:25:13 +00:00
|
|
|
use rocket::http::{Status, ContentType};
|
2020-07-30 11:17:38 +00:00
|
|
|
use rocket::data::Limits;
|
2017-04-18 07:25:13 +00:00
|
|
|
|
|
|
|
fn rocket_with_forms_limit(limit: u64) -> rocket::Rocket {
|
2020-09-03 05:41:31 +00:00
|
|
|
let limits = Limits::default().limit("forms", limit.into());
|
|
|
|
let config = rocket::Config::figment().merge(("limits", limits));
|
2018-07-03 20:47:17 +00:00
|
|
|
rocket::custom(config).mount("/", routes![super::index])
|
2017-04-18 07:25:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn large_enough() {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(rocket_with_forms_limit(128)).unwrap();
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client.post("/")
|
2017-05-19 10:29:08 +00:00
|
|
|
.body("value=Hello+world")
|
2017-06-06 20:41:04 +00:00
|
|
|
.header(ContentType::Form)
|
2020-07-05 18:35:36 +00:00
|
|
|
.dispatch();
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
assert_eq!(response.into_string(), Some("Hello world".into()));
|
2017-05-19 10:29:08 +00:00
|
|
|
}
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn just_large_enough() {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(rocket_with_forms_limit(17)).unwrap();
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client.post("/")
|
2017-05-19 10:29:08 +00:00
|
|
|
.body("value=Hello+world")
|
2017-06-06 20:41:04 +00:00
|
|
|
.header(ContentType::Form)
|
2020-07-05 18:35:36 +00:00
|
|
|
.dispatch();
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
assert_eq!(response.into_string(), Some("Hello world".into()));
|
2017-05-19 10:29:08 +00:00
|
|
|
}
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn much_too_small() {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(rocket_with_forms_limit(4)).unwrap();
|
2017-06-06 20:41:04 +00:00
|
|
|
let response = client.post("/")
|
2017-05-19 10:29:08 +00:00
|
|
|
.body("value=Hello+world")
|
2017-06-06 20:41:04 +00:00
|
|
|
.header(ContentType::Form)
|
2020-07-05 18:35:36 +00:00
|
|
|
.dispatch();
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2018-02-14 22:02:56 +00:00
|
|
|
assert_eq!(response.status(), Status::UnprocessableEntity);
|
2017-05-19 10:29:08 +00:00
|
|
|
}
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn contracted() {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(rocket_with_forms_limit(10)).unwrap();
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client.post("/")
|
2017-04-18 07:25:13 +00:00
|
|
|
.body("value=Hello+world")
|
2017-06-06 20:41:04 +00:00
|
|
|
.header(ContentType::Form)
|
2020-07-05 18:35:36 +00:00
|
|
|
.dispatch();
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
assert_eq!(response.into_string(), Some("Hell".into()));
|
2017-04-18 07:25:13 +00:00
|
|
|
}
|
|
|
|
}
|