mirror of
https://github.com/rwf2/Rocket.git
synced 2025-01-19 07:59:05 +00:00
ec4cc3a293
The new 'FromData' trait allows an implementor to instruct the caller to maintain state on its stack and later pass a borrow for processing. Among other things, it greatly simplifies the 'Form' type, removing a use of unsafe, and allows references in deserialized data guards.
46 lines
1.1 KiB
Rust
46 lines
1.1 KiB
Rust
#![feature(plugin, decl_macro, proc_macro_non_items)]
|
|
#![plugin(rocket_codegen)]
|
|
|
|
#[macro_use] extern crate rocket;
|
|
|
|
use rocket::request::Form;
|
|
|
|
#[derive(FromForm)]
|
|
struct FormData {
|
|
form_data: String,
|
|
}
|
|
|
|
#[patch("/", data = "<form_data>")]
|
|
fn bug(form_data: Form<FormData>) -> &'static str {
|
|
assert_eq!("Form data", form_data.form_data);
|
|
"OK"
|
|
}
|
|
|
|
mod tests {
|
|
use super::*;
|
|
use rocket::local::Client;
|
|
use rocket::http::{Status, ContentType};
|
|
|
|
#[test]
|
|
fn method_eval() {
|
|
let client = Client::new(rocket::ignite().mount("/", routes![bug])).unwrap();
|
|
let mut response = client.post("/")
|
|
.header(ContentType::Form)
|
|
.body("_method=patch&form_data=Form+data")
|
|
.dispatch();
|
|
|
|
assert_eq!(response.body_string(), Some("OK".into()));
|
|
}
|
|
|
|
#[test]
|
|
fn get_passes_through() {
|
|
let client = Client::new(rocket::ignite().mount("/", routes![bug])).unwrap();
|
|
let response = client.get("/")
|
|
.header(ContentType::Form)
|
|
.body("_method=patch&form_data=Form+data")
|
|
.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
}
|
|
}
|