Skip to content

Commit

Permalink
Add some basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed Apr 11, 2024
1 parent 9377c48 commit 8e87d26
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lychee-bin/src/formatters/response/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
71 changes: 71 additions & 0 deletions lychee-bin/src/formatters/response/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
}
2 changes: 2 additions & 0 deletions lychee-bin/src/formatters/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


78 changes: 78 additions & 0 deletions lychee-bin/src/formatters/response/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unknown status code>)"
);
}
}

0 comments on commit 8e87d26

Please sign in to comment.