@@ -17,3 +17,81 @@ impl ResponseBodyFormatter for PlainFormatter {
17
17
body. to_string ( )
18
18
}
19
19
}
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