Skip to content

Commit ce22c81

Browse files
committed
Add some basic tests
1 parent 9377c48 commit ce22c81

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

lychee-bin/src/formatters/response/color.rs

+48
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,51 @@ fn format_status(status: &Status) -> String {
5050
width = status_code_or_text.len()
5151
)
5252
}
53+
54+
#[cfg(test)]
55+
mod tests {
56+
use super::*;
57+
use http::StatusCode;
58+
use lychee_lib::{ErrorKind, Status, Uri};
59+
60+
// Helper function to create a ResponseBody with a given status and URI
61+
fn mock_response_body(status: Status, uri: &str) -> ResponseBody {
62+
ResponseBody {
63+
uri: Uri::try_from(uri).unwrap(),
64+
status,
65+
}
66+
}
67+
68+
#[test]
69+
fn test_format_response_with_ok_status() {
70+
let formatter = ColorFormatter;
71+
let body = mock_response_body(Status::Ok(StatusCode::OK), "https://example.com");
72+
assert_eq!(
73+
formatter.format_response(&body),
74+
"\u{1b}[38;5;2m\u{1b}[1m [200]\u{1b}[0m https://example.com/"
75+
);
76+
}
77+
78+
#[test]
79+
fn test_format_response_with_error_status() {
80+
let formatter = ColorFormatter;
81+
let body = mock_response_body(
82+
Status::Error(ErrorKind::InvalidUrlHost),
83+
"https://example.com/404",
84+
);
85+
assert_eq!(
86+
formatter.format_response(&body),
87+
"\u{1b}[38;5;197m [ERROR]\u{1b}[0m https://example.com/404"
88+
);
89+
}
90+
91+
#[test]
92+
fn test_format_response_with_long_uri() {
93+
let formatter = ColorFormatter;
94+
let long_uri =
95+
"https://example.com/some/very/long/path/to/a/resource/that/exceeds/normal/lengths";
96+
let body = mock_response_body(Status::Ok(StatusCode::OK), long_uri);
97+
let formatted_response = formatter.format_response(&body);
98+
assert!(formatted_response.contains(long_uri));
99+
}
100+
}

lychee-bin/src/formatters/response/emoji.rs

+71
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,74 @@ impl ResponseBodyFormatter for EmojiFormatter {
2222
format!("{} {}", emoji, body.uri)
2323
}
2424
}
25+
26+
#[cfg(test)]
27+
mod emoji_tests {
28+
use super::*;
29+
use http::StatusCode;
30+
use lychee_lib::{ErrorKind, Status, Uri};
31+
32+
// Helper function to create a ResponseBody with a given status and URI
33+
fn mock_response_body(status: Status, uri: &str) -> ResponseBody {
34+
ResponseBody {
35+
uri: Uri::try_from(uri).unwrap(),
36+
status,
37+
}
38+
}
39+
40+
#[test]
41+
fn test_format_response_with_ok_status() {
42+
let formatter = EmojiFormatter;
43+
let body = mock_response_body(Status::Ok(StatusCode::OK), "https://example.com");
44+
assert_eq!(formatter.format_response(&body), "✅ https://example.com/");
45+
}
46+
47+
#[test]
48+
fn test_format_response_with_error_status() {
49+
let formatter = EmojiFormatter;
50+
let body = mock_response_body(
51+
Status::Error(ErrorKind::InvalidUrlHost),
52+
"https://example.com/404",
53+
);
54+
assert_eq!(
55+
formatter.format_response(&body),
56+
"❌ https://example.com/404"
57+
);
58+
}
59+
60+
#[test]
61+
fn test_format_response_with_excluded_status() {
62+
let formatter = EmojiFormatter;
63+
let body = mock_response_body(Status::Excluded, "https://example.com/not-checked");
64+
assert_eq!(
65+
formatter.format_response(&body),
66+
"🚫 https://example.com/not-checked"
67+
);
68+
}
69+
70+
#[test]
71+
fn test_format_response_with_redirect_status() {
72+
let formatter = EmojiFormatter;
73+
let body = mock_response_body(
74+
Status::Redirected(StatusCode::MOVED_PERMANENTLY),
75+
"https://example.com/redirect",
76+
);
77+
assert_eq!(
78+
formatter.format_response(&body),
79+
"↪️ https://example.com/redirect"
80+
);
81+
}
82+
83+
#[test]
84+
fn test_format_response_with_unknown_status_code() {
85+
let formatter = EmojiFormatter;
86+
let body = mock_response_body(
87+
Status::UnknownStatusCode(StatusCode::from_u16(999).unwrap()),
88+
"https://example.com/unknown",
89+
);
90+
assert_eq!(
91+
formatter.format_response(&body),
92+
"⚠️ https://example.com/unknown"
93+
);
94+
}
95+
}

lychee-bin/src/formatters/response/plain.rs

+78
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,81 @@ impl ResponseBodyFormatter for PlainFormatter {
1717
body.to_string()
1818
}
1919
}
20+
21+
#[cfg(test)]
22+
mod plain_tests {
23+
use super::*;
24+
use http::StatusCode;
25+
use lychee_lib::{ErrorKind, Status, Uri};
26+
27+
// Helper function to create a ResponseBody with a given status and URI
28+
fn mock_response_body(status: Status, uri: &str) -> ResponseBody {
29+
ResponseBody {
30+
uri: Uri::try_from(uri).unwrap(),
31+
status,
32+
}
33+
}
34+
35+
#[test]
36+
fn test_format_response_with_ok_status() {
37+
let formatter = PlainFormatter;
38+
let body = mock_response_body(Status::Ok(StatusCode::OK), "https://example.com");
39+
assert_eq!(
40+
formatter.format_response(&body),
41+
"[200] https://example.com/"
42+
);
43+
}
44+
45+
#[test]
46+
fn test_format_response_with_error_status() {
47+
let formatter = PlainFormatter;
48+
let body = mock_response_body(
49+
Status::Error(ErrorKind::InvalidUrlHost),
50+
"https://example.com/404",
51+
);
52+
assert_eq!(
53+
formatter.format_response(&body),
54+
"[ERROR] https://example.com/404 | Failed: URL is missing a host"
55+
);
56+
}
57+
58+
#[test]
59+
fn test_format_response_with_excluded_status() {
60+
let formatter = PlainFormatter;
61+
let body = mock_response_body(Status::Excluded, "https://example.com/not-checked");
62+
assert_eq!(formatter.format_response(&body), body.to_string());
63+
assert_eq!(
64+
formatter.format_response(&body),
65+
"[EXCLUDED] https://example.com/not-checked | Excluded"
66+
);
67+
}
68+
69+
#[test]
70+
fn test_format_response_with_redirect_status() {
71+
let formatter = PlainFormatter;
72+
let body = mock_response_body(
73+
Status::Redirected(StatusCode::MOVED_PERMANENTLY),
74+
"https://example.com/redirect",
75+
);
76+
assert_eq!(formatter.format_response(&body), body.to_string());
77+
assert_eq!(
78+
formatter.format_response(&body),
79+
"[301] https://example.com/redirect | Redirect (301 Moved Permanently): Moved Permanently"
80+
);
81+
}
82+
83+
#[test]
84+
fn test_format_response_with_unknown_status_code() {
85+
let formatter = PlainFormatter;
86+
let body = mock_response_body(
87+
Status::UnknownStatusCode(StatusCode::from_u16(999).unwrap()),
88+
"https://example.com/unknown",
89+
);
90+
assert_eq!(formatter.format_response(&body), body.to_string());
91+
// Check the actual string representation of the status code
92+
assert_eq!(
93+
formatter.format_response(&body),
94+
"[999] https://example.com/unknown | Unknown status (999 <unknown status code>)"
95+
);
96+
}
97+
}

0 commit comments

Comments
 (0)