test invalid utf8

This commit is contained in:
Stefano Chiodino 2017-05-25 08:17:43 +01:00
parent 0f7ba4dcdd
commit 396d25de8d
1 changed files with 8 additions and 11 deletions

View File

@ -5,6 +5,7 @@ use rocket::http::ContentType;
use super::rocket; use super::rocket;
use super::FormInput; use super::FormInput;
use super::FormOption; use super::FormOption;
use super::INVALID_UTF8_ERROR_MESSAGE;
use std::fmt; use std::fmt;
@ -20,7 +21,7 @@ impl fmt::Display for FormOption {
} }
} }
fn test_post(request_body: &String) -> String { fn test_post<S: AsRef<[u8]>>(request_body: S) -> String {
let rocket = rocket(); let rocket = rocket();
let mut request = MockRequest::new(Post, "/") let mut request = MockRequest::new(Post, "/")
.header(ContentType::Form) .header(ContentType::Form)
@ -39,7 +40,7 @@ fn test_post_form(request_form: &FormInput) -> String {
request_form.text_area, request_form.text_area,
request_form.select); request_form.select);
test_post(&request_body) test_post(request_body)
} }
#[test] #[test]
@ -60,22 +61,18 @@ fn test_good_form() {
#[test] #[test]
fn test_invalid_form() { fn test_invalid_form() {
let input = "wrong form input".to_string(); let input = "wrong form input";
let response = test_post(&input); let response = test_post(input);
assert_eq!(response, format!("Invalid form input: {}", input)); assert_eq!(response, format!("Invalid form input: {}", input));
} }
#[test] #[test]
fn test_non_utf8_input() { fn test_non_utf8_input() {
let rocket = rocket(); let input = [0x99];
let mut request = MockRequest::new(Post, "/")
.header(ContentType::Form)
.body([0x99]);
let mut response = request.dispatch_with(&rocket); let response = test_post(&input);
assert_eq!(response, INVALID_UTF8_ERROR_MESSAGE.to_string());
assert_eq!(response.body_string().unwrap(), "Form input was invalid UTF8.".to_string());
} }