2019-08-20 23:53:00 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
2017-04-18 07:25:13 +00:00
|
|
|
|
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;
|
|
|
|
use rocket::config::{Environment, Config, Limits};
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::Client;
|
2017-04-18 07:25:13 +00:00
|
|
|
use rocket::http::{Status, ContentType};
|
|
|
|
|
|
|
|
fn rocket_with_forms_limit(limit: u64) -> rocket::Rocket {
|
|
|
|
let config = Config::build(Environment::Development)
|
2017-06-11 10:39:30 +00:00
|
|
|
.limits(Limits::default().limit("forms", limit))
|
2017-04-18 07:25:13 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2018-07-03 20:47:17 +00:00
|
|
|
rocket::custom(config).mount("/", routes![super::index])
|
2017-04-18 07:25:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
#[test]
|
|
|
|
fn large_enough() {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket_with_forms_limit(128)).unwrap();
|
|
|
|
let mut 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)
|
|
|
|
.dispatch();
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2019-06-30 16:45:17 +00:00
|
|
|
assert_eq!(response.body_string_wait(), Some("Hello world".into()));
|
2017-05-19 10:29:08 +00:00
|
|
|
}
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
#[test]
|
|
|
|
fn just_large_enough() {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket_with_forms_limit(17)).unwrap();
|
|
|
|
let mut 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)
|
|
|
|
.dispatch();
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2019-06-30 16:45:17 +00:00
|
|
|
assert_eq!(response.body_string_wait(), Some("Hello world".into()));
|
2017-05-19 10:29:08 +00:00
|
|
|
}
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
#[test]
|
|
|
|
fn much_too_small() {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket_with_forms_limit(4)).unwrap();
|
|
|
|
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)
|
|
|
|
.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
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn contracted() {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket_with_forms_limit(10)).unwrap();
|
|
|
|
let mut 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)
|
|
|
|
.dispatch();
|
2017-04-18 07:25:13 +00:00
|
|
|
|
2019-06-30 16:45:17 +00:00
|
|
|
assert_eq!(response.body_string_wait(), Some("Hell".into()));
|
2017-04-18 07:25:13 +00:00
|
|
|
}
|
|
|
|
}
|