impl more robust test for HTTP Request
This commit is contained in:
parent
ce7e49af47
commit
6e994f1b90
@ -10,14 +10,12 @@ type RequestParts = (String, String, String);
|
||||
#[derive(Debug)]
|
||||
pub enum HTTPVersion {
|
||||
Http1,
|
||||
//Http2,
|
||||
}
|
||||
|
||||
impl Into<String> for HTTPVersion {
|
||||
fn into(self) -> String {
|
||||
match self {
|
||||
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 != "";
|
||||
}
|
||||
}
|
||||
@ -189,7 +187,7 @@ impl HTTPRequest {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_valid(self) -> bool {
|
||||
fn is_valid(&self) -> bool {
|
||||
return self.start_line.is_valid();
|
||||
}
|
||||
}
|
||||
@ -221,23 +219,88 @@ pub fn handle_request(request: &str) -> HTTPRequest {
|
||||
|
||||
#[test]
|
||||
fn test_handle_request() {
|
||||
let test_cases: [(&str, bool); 7] = [
|
||||
("GET / HTTP/1.1\r\n\r\n", true),
|
||||
struct expect {
|
||||
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",
|
||||
true,
|
||||
), // intentionally add HTTP with no version number
|
||||
("POST HTTP/1.1", false),
|
||||
("", false),
|
||||
("fjlqskjd /oks?id=65 HTTP/2\r\n\r\n", true),
|
||||
(" ", false),
|
||||
("lm //// skkss\r\ndkldklkdl\r\n", true),
|
||||
expect {
|
||||
start_line: "OPTIONS /admin/2 HTTP/1.1".to_string(),
|
||||
body: None,
|
||||
is_valid: 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);
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user