mirror of
https://github.com/rwf2/Rocket.git
synced 2025-02-01 14:22:05 +00:00
b34085392d
This commit allow routes to be declared for methods outside of the standard HTTP method set. Specifically, it enables declaring routes for any method in the IANA Method Registry: ```rust #[route(LINK, uri = "/<foo>")] fn link() { ... } #[route("VERSION-CONTROL", uri = "/<foo>")] fn version_control() { ... } ``` The `Method` type has gained variants for each registered method. Breaking changes: - `Method::from_str()` no longer parses mixed-case method names. - `Method` is marked as non-exhaustive. - `Method::supports_payload()` removed in favor of `Method::allows_request_body()`. Resolves #232.
76 lines
2.2 KiB
Rust
76 lines
2.2 KiB
Rust
#[macro_use] extern crate rocket;
|
|
|
|
use rocket::form::Form;
|
|
|
|
#[derive(FromForm)]
|
|
struct FormData {
|
|
form_data: String,
|
|
}
|
|
|
|
#[patch("/", data = "<form_data>")]
|
|
fn patch(form_data: Form<FormData>) -> &'static str {
|
|
assert_eq!("Form data", form_data.into_inner().form_data);
|
|
"PATCH OK"
|
|
}
|
|
|
|
#[route(UPDATEREDIRECTREF, uri = "/", data = "<form_data>")]
|
|
fn urr(form_data: Form<FormData>) -> &'static str {
|
|
assert_eq!("Form data", form_data.into_inner().form_data);
|
|
"UPDATEREDIRECTREF OK"
|
|
}
|
|
|
|
#[route("VERSION-CONTROL", uri = "/", data = "<form_data>")]
|
|
fn vc(form_data: Form<FormData>) -> &'static str {
|
|
assert_eq!("Form data", form_data.into_inner().form_data);
|
|
"VERSION-CONTROL OK"
|
|
}
|
|
|
|
mod tests {
|
|
use super::*;
|
|
use rocket::local::blocking::Client;
|
|
use rocket::http::{Status, ContentType, Method};
|
|
|
|
#[test]
|
|
fn method_eval() {
|
|
let client = Client::debug_with(routes![patch, urr, vc]).unwrap();
|
|
let response = client.post("/")
|
|
.header(ContentType::Form)
|
|
.body("_method=patch&form_data=Form+data")
|
|
.dispatch();
|
|
|
|
assert_eq!(response.into_string(), Some("PATCH OK".into()));
|
|
|
|
let response = client.post("/")
|
|
.header(ContentType::Form)
|
|
.body("_method=updateredirectref&form_data=Form+data")
|
|
.dispatch();
|
|
|
|
assert_eq!(response.into_string(), Some("UPDATEREDIRECTREF OK".into()));
|
|
|
|
let response = client.req(Method::UpdateRedirectRef, "/")
|
|
.header(ContentType::Form)
|
|
.body("form_data=Form+data")
|
|
.dispatch();
|
|
|
|
assert_eq!(response.into_string(), Some("UPDATEREDIRECTREF OK".into()));
|
|
|
|
let response = client.post("/")
|
|
.header(ContentType::Form)
|
|
.body("_method=version-control&form_data=Form+data")
|
|
.dispatch();
|
|
|
|
assert_eq!(response.into_string(), Some("VERSION-CONTROL OK".into()));
|
|
}
|
|
|
|
#[test]
|
|
fn get_passes_through() {
|
|
let client = Client::debug_with(routes![patch, urr, vc]).unwrap();
|
|
let response = client.get("/")
|
|
.header(ContentType::Form)
|
|
.body("_method=patch&form_data=Form+data")
|
|
.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
}
|
|
}
|