Skip to content

Commit

Permalink
Refactor code and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
theoforger authored and hurl-bot committed Jan 21, 2025
1 parent f9f81ce commit 1f14a73
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions packages/hurl/src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,15 +551,7 @@ impl Client {
implicit_content_type: Option<&str>,
options: &ClientOptions,
) -> Result<(), HttpError> {
let mut list = List::new();

for header in headers {
if header.value.is_empty() {
list.append(&format!("{};", header.name))?;
} else {
list.append(&format!("{}: {}", header.name, header.value))?;
}
}
let mut list = headers.to_curl_headers()?;

// If request has no `Content-Type` header, we set it if the content type has been set
// implicitly on this request.
Expand Down Expand Up @@ -883,6 +875,21 @@ impl Header {
}
}

impl HeaderVec {
/// Converts this list of [`Header`] to a lib curl header list.
fn to_curl_headers(&self) -> Result<List, HttpError> {
let mut curl_headers = List::new();
for header in self {
if header.value.is_empty() {
curl_headers.append(&format!("{};", header.name))?;
} else {
curl_headers.append(&format!("{}: {}", header.name, header.value))?;
}
}
Ok(curl_headers)
}
}

/// Splits an array of bytes into HTTP lines (\r\n separator).
fn split_lines(data: &[u8]) -> Vec<String> {
let mut lines = vec![];
Expand Down Expand Up @@ -1242,4 +1249,18 @@ mod tests {
("foo\\".to_string(), Some("toto\\:tata:tutu".to_string()))
);
}

#[test]
fn test_to_curl_headers() {
let mut headers = HeaderVec::new();
headers.push(Header::new("foo", "a"));
headers.push(Header::new("bar", "b"));
headers.push(Header::new("baz", ""));

let list = headers.to_curl_headers().unwrap();
assert_eq!(
list.iter().collect::<Vec<_>>(),
vec!["foo: a".as_bytes(), "bar: b".as_bytes(), "baz;".as_bytes()]
)
}
}

0 comments on commit 1f14a73

Please sign in to comment.