impl more robust test for HTTP Request

This commit is contained in:
landrigun 2022-09-13 17:17:09 +01:00
parent ce7e49af47
commit 6e994f1b90

View File

@ -10,14 +10,12 @@ type RequestParts = (String, String, String);
#[derive(Debug)] #[derive(Debug)]
pub enum HTTPVersion { pub enum HTTPVersion {
Http1, Http1,
//Http2,
} }
impl Into<String> for HTTPVersion { impl Into<String> for HTTPVersion {
fn into(self) -> String { fn into(self) -> String {
match self { match self {
Self::Http1 => "HTTP/1.1".to_string(), Self::Http1 => "HTTP/1.1".to_string(),
//Self::Http2 => "HTTP/2.2".to_string(),
} }
} }
} }
@ -55,7 +53,7 @@ impl HTTPStartLine {
)) ))
} }
fn is_valid(self) -> bool { fn is_valid(&self) -> bool {
return self.method != "" && self.target != ""; return self.method != "" && self.target != "";
} }
} }
@ -189,7 +187,7 @@ impl HTTPRequest {
} }
} }
fn is_valid(self) -> bool { fn is_valid(&self) -> bool {
return self.start_line.is_valid(); return self.start_line.is_valid();
} }
} }
@ -221,23 +219,88 @@ pub fn handle_request(request: &str) -> HTTPRequest {
#[test] #[test]
fn test_handle_request() { fn test_handle_request() {
let test_cases: [(&str, bool); 7] = [ struct expect {
("GET / HTTP/1.1\r\n\r\n", true), start_line: String,
body: Option<String>,
is_valid: bool,
}
let test_cases: [(&str, expect); 7] = [
(
"GET / HTTP/1.1\r\n\r\n",
expect {
start_line: "GET / HTTP/1.1".to_string(),
body: None,
is_valid: true,
},
),
// intentionally add HTTP with no version number
( (
"OPTIONS /admin/2 HTTP/\r\nContent-Type: application/json\r\n", "OPTIONS /admin/2 HTTP/\r\nContent-Type: application/json\r\n",
true, expect {
), // intentionally add HTTP with no version number start_line: "OPTIONS /admin/2 HTTP/1.1".to_string(),
("POST HTTP/1.1", false), body: None,
("", false), is_valid: true,
("fjlqskjd /oks?id=65 HTTP/2\r\n\r\n", true), },
(" ", false), ),
("lm //// skkss\r\ndkldklkdl\r\n", true), (
"POST HTTP",
expect {
start_line: " HTTP/1.1".to_string(),
body: None,
is_valid: false,
}
),
(
"",
expect {
start_line: " HTTP/1.1".to_string(),
body: None,
is_valid: false,
}
),
(
"fjlqskjd /oks?id=65 HTTP/2\r\n\r\n",
expect {
start_line: "fjlqskjd /oks?id=65 HTTP/1.1".to_string(),
body: None,
is_valid: false,
}
),
(
" ",
expect {
start_line: " HTTP/1.1".to_string(),
body: None,
is_valid: false,
}
),
(
r#"lm //// skkss\r\ndkldklkdl\r\n"{"access_token":"AAAAAAAAAAAA.BBBBBBBBBB.CCCCCCCCCC","refresh_token": "DDDDDDDDDDD.EEEEEEEEEEE.FFFFF"}""#,
expect {
start_line: " HTTP/1.1".to_string(),
body: Some(r#"{"access_token":"AAAAAAAAAAAA.BBBBBBBBBB.CCCCCCCCCC","refresh_token": "DDDDDDDDDDD.EEEEEEEEEEE.FFFFF"}"#.to_string()),
is_valid: false,
}
),
]; ];
for (request, is_valid) in test_cases { for (request, expect) in test_cases {
let http_request = HTTPRequest::from(request); let http_request = HTTPRequest::from(request);
println!("{:?}", http_request); println!("{:?}", http_request);
assert_eq!(http_request.is_valid(), is_valid); assert_eq!(expect.is_valid, http_request.is_valid());
let start_line: String = http_request.start_line.into();
assert_eq!(expect.start_line, start_line);
match http_request.body {
Some(v) => {
assert_eq!(expect.body.unwrap(), v.data)
}
None => {
assert!(expect.body.is_none())
}
}
} }
} }