Skip to content

Commit 0ca915a

Browse files
committed
add support to any method
- aslo fix panic when an invalid method is provided in the request
1 parent 95ead70 commit 0ca915a

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/hteapot.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub enum HttpMethod {
2222
OPTIONS,
2323
TRACE,
2424
CONNECT,
25+
Other(String),
2526
}
2627

2728
impl HttpMethod {
@@ -36,7 +37,7 @@ impl HttpMethod {
3637
"OPTIONS" => HttpMethod::OPTIONS,
3738
"TRACE" => HttpMethod::TRACE,
3839
"CONNECT" => HttpMethod::CONNECT,
39-
_ => panic!("Invalid HTTP method"),
40+
_ => Self::Other(method.to_string()),
4041
}
4142
}
4243
pub fn to_str(&self) -> &str {
@@ -50,6 +51,7 @@ impl HttpMethod {
5051
HttpMethod::OPTIONS => "OPTIONS",
5152
HttpMethod::TRACE => "TRACE",
5253
HttpMethod::CONNECT => "CONNECT",
54+
HttpMethod::Other(method) => method.as_str(),
5355
}
5456
}
5557
}
@@ -475,8 +477,9 @@ fn test_http_parser() {
475477
fn test_http_response_maker() {
476478
let response = Hteapot::response_maker(HttpStatus::IAmATeapot, "Hello, World!", None);
477479
let response = String::from_utf8(response).unwrap();
478-
let expected_response = "HTTP/1.1 418 I'm a teapot\r\nContent-Length: 13\r\nServer: HTeaPot/0.2.5\r\n\r\nHello, World!\r\n".split("\r\n");
479-
for item in expected_response.into_iter() {
480+
let expected_response = format!("HTTP/1.1 418 I'm a teapot\r\nContent-Length: 13\r\nServer: HTeaPot/{}\r\n\r\nHello, World!\r\n",VERSION);
481+
let expected_response_list = expected_response.split("\r\n");
482+
for item in expected_response_list.into_iter() {
480483
assert!(response.contains(item));
481484
}
482485
}

src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::time::SystemTime;
1212

1313
use brew::fetch;
1414
use hteapot::Hteapot;
15+
1516
use hteapot::HttpStatus;
1617
use logger::Logger;
1718

0 commit comments

Comments
 (0)