Skip to content

Commit 179e3c3

Browse files
committed
Implement display on partial requests
This returns the underlying message string
1 parent abdd7b0 commit 179e3c3

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/models/partial_request.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::fmt;
12
use std::{ops::Range, str::FromStr};
23

34
use crate::{error::Error, span::get_line_spans};
@@ -15,6 +16,12 @@ pub struct PartialHttpRequest {
1516
body: Option<Range<usize>>,
1617
}
1718

19+
impl fmt::Display for PartialHttpRequest {
20+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21+
write!(f, "{}", self.message())
22+
}
23+
}
24+
1825
impl PartialHttpRequest {
1926
pub fn new(
2027
message: &str,

tests/display_tests.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::{fs, str::FromStr};
2+
3+
use http_message::PartialHttpRequest;
4+
5+
use pretty_assertions::assert_eq;
6+
7+
test!(display_empty_request, "./tests/fixtures/empty.request");
8+
test!(
9+
display_get_with_headers_request,
10+
"./tests/fixtures/get_with_headers.request"
11+
);
12+
test!(
13+
display_get_with_multiple_spaces_request,
14+
"./tests/fixtures/get_with_multiple_spaces.request"
15+
);
16+
test!(
17+
display_get_without_http_version_request,
18+
"./tests/fixtures/get_without_http_version.request"
19+
);
20+
test!(display_get_request, "./tests/fixtures/get.request");
21+
test!(
22+
display_post_with_body_request,
23+
"./tests/fixtures/post_with_body.request"
24+
);
25+
test!(
26+
display_post_with_headers_and_body_request,
27+
"./tests/fixtures/post_with_headers_and_body.request"
28+
);
29+
test!(
30+
display_whitespace_request,
31+
"./tests/fixtures/whitespace.request"
32+
);
33+
34+
#[macro_export]
35+
macro_rules! test {
36+
($name:ident, $path:expr) => {
37+
#[test]
38+
fn $name() {
39+
let path: &str = $path;
40+
let content = fs::read_to_string(path).expect("should read test fixture");
41+
42+
let partial = PartialHttpRequest::from_str(&content).expect("should be parsable");
43+
44+
assert_eq!(content, format!("{partial}"));
45+
}
46+
};
47+
}

0 commit comments

Comments
 (0)