Only override request methods via '_method' on POST.

This commit is contained in:
Greg Edwards 2016-12-30 21:33:51 -07:00 committed by Sergio Benitez
parent a1878ad080
commit d19cb0349c
2 changed files with 15 additions and 2 deletions

View File

@ -141,7 +141,7 @@ impl Rocket {
// field which we use to reinterpret the request's method.
let data_len = data.peek().len();
let (min_len, max_len) = ("_method=get".len(), "_method=delete".len());
if req.content_type().is_form() && data_len >= min_len {
if req.method() == Method::Post && req.content_type().is_form() && data_len >= min_len {
let form = unsafe {
from_utf8_unchecked(&data.peek()[..min(data_len, max_len)])
};

View File

@ -4,6 +4,7 @@
extern crate rocket;
use rocket::request::Form;
use rocket::http::Status;
#[derive(FromForm)]
struct FormData {
@ -24,7 +25,7 @@ use rocket::http::ContentType;
fn method_eval() {
let rocket = rocket::ignite().mount("/", routes![bug]);
let mut req = MockRequest::new(Patch, "/")
let mut req = MockRequest::new(Post, "/")
.header(ContentType::Form)
.body("_method=patch&form_data=Form+data");
@ -32,3 +33,15 @@ fn method_eval() {
let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some("OK".to_string()));
}
#[test]
fn get_passes_through() {
let rocket = rocket::ignite().mount("/", routes![bug]);
let mut req = MockRequest::new(Get, "/")
.header(ContentType::Form)
.body("_method=patch&form_data=Form+data");
let mut response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::NotFound);
}