2017-08-29 03:14:59 +00:00
|
|
|
#![feature(plugin, decl_macro, custom_derive)]
|
2017-04-18 07:25:13 +00:00
|
|
|
#![plugin(rocket_codegen)]
|
|
|
|
|
|
|
|
extern crate rocket;
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
assert_eq!(response.body_string(), Some("Hello world".into()));
|
|
|
|
}
|
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
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
assert_eq!(response.body_string(), Some("Hello world".into()));
|
|
|
|
}
|
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
|
|
|
|
|
|
|
assert_eq!(response.body_string(), Some("Hell".into()));
|
|
|
|
}
|
|
|
|
}
|