diff --git a/lychee-bin/src/formatters/response/color.rs b/lychee-bin/src/formatters/response/color.rs index 1c4bc1516e..ff5df2c349 100644 --- a/lychee-bin/src/formatters/response/color.rs +++ b/lychee-bin/src/formatters/response/color.rs @@ -50,3 +50,51 @@ fn format_status(status: &Status) -> String { width = status_code_or_text.len() ) } + +#[cfg(test)] +mod tests { + use super::*; + use http::StatusCode; + use lychee_lib::{ErrorKind, Status, Uri}; + + // Helper function to create a ResponseBody with a given status and URI + fn mock_response_body(status: Status, uri: &str) -> ResponseBody { + ResponseBody { + uri: Uri::try_from(uri).unwrap(), + status, + } + } + + #[test] + fn test_format_response_with_ok_status() { + let formatter = ColorFormatter; + let body = mock_response_body(Status::Ok(StatusCode::OK), "https://example.com"); + assert_eq!( + formatter.format_response(&body), + "\u{1b}[38;5;2m\u{1b}[1m [200]\u{1b}[0m https://example.com/" + ); + } + + #[test] + fn test_format_response_with_error_status() { + let formatter = ColorFormatter; + let body = mock_response_body( + Status::Error(ErrorKind::InvalidUrlHost), + "https://example.com/404", + ); + assert_eq!( + formatter.format_response(&body), + "\u{1b}[38;5;197m [ERROR]\u{1b}[0m https://example.com/404" + ); + } + + #[test] + fn test_format_response_with_long_uri() { + let formatter = ColorFormatter; + let long_uri = + "https://example.com/some/very/long/path/to/a/resource/that/exceeds/normal/lengths"; + let body = mock_response_body(Status::Ok(StatusCode::OK), long_uri); + let formatted_response = formatter.format_response(&body); + assert!(formatted_response.contains(long_uri)); + } +} diff --git a/lychee-bin/src/formatters/response/emoji.rs b/lychee-bin/src/formatters/response/emoji.rs index 253dc9673d..ea70fc0ff9 100644 --- a/lychee-bin/src/formatters/response/emoji.rs +++ b/lychee-bin/src/formatters/response/emoji.rs @@ -22,3 +22,74 @@ impl ResponseBodyFormatter for EmojiFormatter { format!("{} {}", emoji, body.uri) } } + +#[cfg(test)] +mod emoji_tests { + use super::*; + use http::StatusCode; + use lychee_lib::{ErrorKind, Status, Uri}; + + // Helper function to create a ResponseBody with a given status and URI + fn mock_response_body(status: Status, uri: &str) -> ResponseBody { + ResponseBody { + uri: Uri::try_from(uri).unwrap(), + status, + } + } + + #[test] + fn test_format_response_with_ok_status() { + let formatter = EmojiFormatter; + let body = mock_response_body(Status::Ok(StatusCode::OK), "https://example.com"); + assert_eq!(formatter.format_response(&body), "✅ https://example.com/"); + } + + #[test] + fn test_format_response_with_error_status() { + let formatter = EmojiFormatter; + let body = mock_response_body( + Status::Error(ErrorKind::InvalidUrlHost), + "https://example.com/404", + ); + assert_eq!( + formatter.format_response(&body), + "❌ https://example.com/404" + ); + } + + #[test] + fn test_format_response_with_excluded_status() { + let formatter = EmojiFormatter; + let body = mock_response_body(Status::Excluded, "https://example.com/not-checked"); + assert_eq!( + formatter.format_response(&body), + "🚫 https://example.com/not-checked" + ); + } + + #[test] + fn test_format_response_with_redirect_status() { + let formatter = EmojiFormatter; + let body = mock_response_body( + Status::Redirected(StatusCode::MOVED_PERMANENTLY), + "https://example.com/redirect", + ); + assert_eq!( + formatter.format_response(&body), + "↪️ https://example.com/redirect" + ); + } + + #[test] + fn test_format_response_with_unknown_status_code() { + let formatter = EmojiFormatter; + let body = mock_response_body( + Status::UnknownStatusCode(StatusCode::from_u16(999).unwrap()), + "https://example.com/unknown", + ); + assert_eq!( + formatter.format_response(&body), + "⚠️ https://example.com/unknown" + ); + } +} diff --git a/lychee-bin/src/formatters/response/mod.rs b/lychee-bin/src/formatters/response/mod.rs index 5d0e668b5f..7332494d5a 100644 --- a/lychee-bin/src/formatters/response/mod.rs +++ b/lychee-bin/src/formatters/response/mod.rs @@ -25,3 +25,5 @@ pub(crate) const MAX_RESPONSE_OUTPUT_WIDTH: usize = 10; pub(crate) trait ResponseBodyFormatter: Send + Sync { fn format_response(&self, body: &ResponseBody) -> String; } + + diff --git a/lychee-bin/src/formatters/response/plain.rs b/lychee-bin/src/formatters/response/plain.rs index d5e68cb644..bd2d0e4c5e 100644 --- a/lychee-bin/src/formatters/response/plain.rs +++ b/lychee-bin/src/formatters/response/plain.rs @@ -17,3 +17,81 @@ impl ResponseBodyFormatter for PlainFormatter { body.to_string() } } + +#[cfg(test)] +mod plain_tests { + use super::*; + use http::StatusCode; + use lychee_lib::{ErrorKind, Status, Uri}; + + // Helper function to create a ResponseBody with a given status and URI + fn mock_response_body(status: Status, uri: &str) -> ResponseBody { + ResponseBody { + uri: Uri::try_from(uri).unwrap(), + status, + } + } + + #[test] + fn test_format_response_with_ok_status() { + let formatter = PlainFormatter; + let body = mock_response_body(Status::Ok(StatusCode::OK), "https://example.com"); + assert_eq!( + formatter.format_response(&body), + "[200] https://example.com/" + ); + } + + #[test] + fn test_format_response_with_error_status() { + let formatter = PlainFormatter; + let body = mock_response_body( + Status::Error(ErrorKind::InvalidUrlHost), + "https://example.com/404", + ); + assert_eq!( + formatter.format_response(&body), + "[ERROR] https://example.com/404 | Failed: URL is missing a host" + ); + } + + #[test] + fn test_format_response_with_excluded_status() { + let formatter = PlainFormatter; + let body = mock_response_body(Status::Excluded, "https://example.com/not-checked"); + assert_eq!(formatter.format_response(&body), body.to_string()); + assert_eq!( + formatter.format_response(&body), + "[EXCLUDED] https://example.com/not-checked | Excluded" + ); + } + + #[test] + fn test_format_response_with_redirect_status() { + let formatter = PlainFormatter; + let body = mock_response_body( + Status::Redirected(StatusCode::MOVED_PERMANENTLY), + "https://example.com/redirect", + ); + assert_eq!(formatter.format_response(&body), body.to_string()); + assert_eq!( + formatter.format_response(&body), + "[301] https://example.com/redirect | Redirect (301 Moved Permanently): Moved Permanently" + ); + } + + #[test] + fn test_format_response_with_unknown_status_code() { + let formatter = PlainFormatter; + let body = mock_response_body( + Status::UnknownStatusCode(StatusCode::from_u16(999).unwrap()), + "https://example.com/unknown", + ); + assert_eq!(formatter.format_response(&body), body.to_string()); + // Check the actual string representation of the status code + assert_eq!( + formatter.format_response(&body), + "[999] https://example.com/unknown | Unknown status (999 )" + ); + } +}