1use http::*;
2
3#[test]
4fn from_bytes() {
5 for ok in &[
6 "100", "101", "199", "200", "250", "299", "321", "399", "499", "599", "600", "999"
7 ] {
8 assert!(StatusCode::from_bytes(ok.as_bytes()).is_ok());
9 }
10
11 for not_ok in &[
12 "0", "00", "10", "40", "99", "000", "010", "099", "1000", "1999",
13 ] {
14 assert!(StatusCode::from_bytes(not_ok.as_bytes()).is_err());
15 }
16}
17
18#[test]
19fn equates_with_u16() {
20 let status = StatusCode::from_u16(200u16).unwrap();
21 assert_eq!(200u16, status);
22 assert_eq!(status, 200u16);
23}
24
25#[test]
26fn roundtrip() {
27 for s in 100..1000 {
28 let sstr = s.to_string();
29 let status = StatusCode::from_bytes(sstr.as_bytes()).unwrap();
30 assert_eq!(s, u16::from(status));
31 assert_eq!(sstr, status.as_str());
32 }
33}
34
35#[test]
36fn is_informational() {
37 assert!(status_code(100).is_informational());
38 assert!(status_code(199).is_informational());
39
40 assert!(!status_code(200).is_informational());
41}
42
43#[test]
44fn is_success() {
45 assert!(status_code(200).is_success());
46 assert!(status_code(299).is_success());
47
48 assert!(!status_code(199).is_success());
49 assert!(!status_code(300).is_success());
50}
51
52#[test]
53fn is_redirection() {
54 assert!(status_code(300).is_redirection());
55 assert!(status_code(399).is_redirection());
56
57 assert!(!status_code(299).is_redirection());
58 assert!(!status_code(400).is_redirection());
59}
60
61#[test]
62fn is_client_error() {
63 assert!(status_code(400).is_client_error());
64 assert!(status_code(499).is_client_error());
65
66 assert!(!status_code(399).is_client_error());
67 assert!(!status_code(500).is_client_error());
68}
69
70#[test]
71fn is_server_error() {
72 assert!(status_code(500).is_server_error());
73 assert!(status_code(599).is_server_error());
74
75 assert!(!status_code(499).is_server_error());
76 assert!(!status_code(600).is_server_error());
77}
78
79/// Helper method for readability
80fn status_code(status_code: u16) -> StatusCode {
81 StatusCode::from_u16(status_code).unwrap()
82}
83