Skip to content

Commit d99d36e

Browse files
committed
test(http): add unit tests for insert_header helper
Add 3 tests for the new insert_header() helper method: - test_insert_header_valid: verifies basic functionality - test_insert_header_invalid_value: verifies error handling for invalid headers - test_insert_header_multiple_headers: verifies multiple header insertions
1 parent ff58b6a commit d99d36e

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

crates/feedparser-rs-core/src/http/client.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,61 @@ mod tests {
276276
let err_msg = result.err().unwrap().to_string();
277277
assert!(err_msg.contains("Internal domain TLD not allowed"));
278278
}
279+
280+
#[test]
281+
fn test_insert_header_valid() {
282+
let mut headers = HeaderMap::new();
283+
let result = FeedHttpClient::insert_header(
284+
&mut headers,
285+
USER_AGENT,
286+
"TestBot/1.0",
287+
"User-Agent",
288+
);
289+
assert!(result.is_ok());
290+
assert_eq!(headers.get(USER_AGENT).unwrap(), "TestBot/1.0");
291+
}
292+
293+
#[test]
294+
fn test_insert_header_invalid_value() {
295+
let mut headers = HeaderMap::new();
296+
// Invalid header value with control characters
297+
let result = FeedHttpClient::insert_header(
298+
&mut headers,
299+
USER_AGENT,
300+
"Invalid\nHeader",
301+
"User-Agent",
302+
);
303+
assert!(result.is_err());
304+
match result {
305+
Err(FeedError::Http { message }) => {
306+
assert!(message.contains("Invalid User-Agent"));
307+
}
308+
_ => panic!("Expected Http error"),
309+
}
310+
}
311+
312+
#[test]
313+
fn test_insert_header_multiple_headers() {
314+
let mut headers = HeaderMap::new();
315+
316+
FeedHttpClient::insert_header(
317+
&mut headers,
318+
USER_AGENT,
319+
"TestBot/1.0",
320+
"User-Agent",
321+
)
322+
.unwrap();
323+
324+
FeedHttpClient::insert_header(
325+
&mut headers,
326+
ACCEPT,
327+
"application/xml",
328+
"Accept",
329+
)
330+
.unwrap();
331+
332+
assert_eq!(headers.len(), 2);
333+
assert_eq!(headers.get(USER_AGENT).unwrap(), "TestBot/1.0");
334+
assert_eq!(headers.get(ACCEPT).unwrap(), "application/xml");
335+
}
279336
}

0 commit comments

Comments
 (0)