tor-dirclient: Extend format test to check the body is formatted too.

This commit is contained in:
Gabriela Moldovan 2023-08-14 11:07:45 +01:00
parent e71703ad90
commit 966150f9b2
No known key found for this signature in database
GPG Key ID: 3946E0ADE72BAC99
1 changed files with 21 additions and 7 deletions

View File

@ -38,6 +38,15 @@ mod test {
//! <!-- @@ end test lint list maintained by maint/add_warning @@ --> //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
use super::*; use super::*;
#[derive(Copy, Clone, Debug)]
struct TestBody;
impl StringBody for TestBody {
fn str(&self) -> &str {
"hello"
}
}
fn build_request<B: StringBody>(body: B, headers: &[(&str, &str)]) -> http::Request<B> { fn build_request<B: StringBody>(body: B, headers: &[(&str, &str)]) -> http::Request<B> {
let mut builder = http::Request::builder().method("GET").uri("/index.html"); let mut builder = http::Request::builder().method("GET").uri("/index.html");
@ -50,13 +59,18 @@ mod test {
#[test] #[test]
fn format() { fn format() {
let req = build_request((), &[]); fn chk_format<B: StringBody + Clone>(body: B, expected_body: &str) {
assert_eq!(encode_request(&req), "GET /index.html HTTP/1.0\r\n\r\n"); let req = build_request(body.clone(), &[]);
assert_eq!(encode_request(&req), format!("GET /index.html HTTP/1.0\r\n\r\n{expected_body}"));
let req = build_request((), &[("X-Marsupial", "Opossum")]); let req = build_request(body, &[("X-Marsupial", "Opossum")]);
assert_eq!( assert_eq!(
encode_request(&req), encode_request(&req),
"GET /index.html HTTP/1.0\r\nx-marsupial: Opossum\r\n\r\n" format!("GET /index.html HTTP/1.0\r\nx-marsupial: Opossum\r\n\r\n{expected_body}")
); );
}
chk_format((), "");
chk_format(TestBody, "hello");
} }
} }