http_test_server/
http.rs

1//! HTTP constants
2
3/// HTTP methods
4#[derive(Debug, Clone, PartialEq)]
5pub enum Method {
6    GET,
7    POST,
8    PUT,
9    DELETE,
10    PATCH
11}
12
13impl Method {
14    fn value(&self) -> &'static str {
15        match self {
16            Method::GET => "GET",
17            Method::POST => "POST",
18            Method::PUT => "PUT",
19            Method::DELETE => "DELETE",
20            Method::PATCH => "PATCH"
21        }
22    }
23
24    /// compares with string equivalent
25    /// ```
26    /// # use http_test_server::http::Method;
27    /// let method = Method::POST;
28    ///
29    /// assert!(method.equal("POST"));
30    /// ```
31    pub fn equal(&self, value: &str) -> bool {
32        self.value() == value
33    }
34}
35
36/// HTTP status
37///
38/// Can be converted to its numeral equivalent.
39/// ```
40/// # use http_test_server::http::Status;
41/// let status_code = Status::NotFound as u16;
42///
43/// assert_eq!(status_code, 404);
44/// ```
45#[derive(Debug)]
46pub enum Status {
47    Continue = 100,
48    SwitchingProtocols = 101,
49    Processing = 102,
50    OK = 200,
51    Created = 201,
52    Accepted = 202,
53    NonAuthoritativeInformation = 203,
54    NoContent = 204,
55    ResetContent = 205,
56    PartialContent = 206,
57    MultiStatus= 207,
58    MultipleChoices= 300,
59    MovedPermanently = 301,
60    Found = 302,
61    SeeOther = 303,
62    NotModified= 304,
63    UseProxy = 305,
64    TemporaryRedirect = 307,
65    PermanentRedirect = 308,
66    BadRequest = 400,
67    Unauthorized = 401,
68    PaymentRequired= 402,
69    Forbidden = 403,
70    NotFound = 404,
71    MethodNotAllowed = 405,
72    NotAcceptable = 406,
73    ProxyAuthenticationRequired = 407,
74    RequestTimeout = 408,
75    Conflict = 409,
76    Gone = 410,
77    LengthRequired = 411,
78    PreconditionFailed = 412,
79    PayloadTooLarge = 413,
80    UriTooLong = 414,
81    UnsupportedMediaType  = 415,
82    RangeNotSatisfiable = 416,
83    ExpectationFailed = 417,
84    ImATeapot = 418,
85    UnprocessableEntity= 422,
86    Locked = 423,
87    FailedDependency = 424,
88    UpgradeRequired = 426,
89    PreconditionRequired   = 428,
90    TooManyRequests = 429,
91    RequestHeaderFieldsTooLarge = 431,
92    InternalServerError = 500,
93    NotImplemented = 501,
94    BadGateway = 502,
95    ServiceUnavailable = 503,
96    GatewayTimeout = 504,
97    HttpVersionNotSupported = 505,
98    InsufficientStorage = 507,
99    NetworkAuthenticationRequired = 511
100}
101
102impl Status {
103    /// Returns status' full description
104    /// ```
105    /// use http_test_server::http::Status;
106    ///
107    /// let description = Status::MultipleChoices.description();
108    ///
109    /// assert_eq!(description, "300 Multiple Choices");
110    /// ```
111    pub fn description(&self) -> &'static str {
112        match self {
113            Status::Continue => "100 Continue",
114            Status::SwitchingProtocols => "101 Switching Protocols",
115            Status::Processing => "102 Processing",
116            Status::OK => "200 Ok",
117            Status::Created => "201 Created",
118            Status::Accepted => "202 Accepted",
119            Status::NonAuthoritativeInformation => "203 Non Authoritative Information",
120            Status::NoContent => "204 No Content",
121            Status::ResetContent => "205 Reset Content",
122            Status::PartialContent => "206 Partial Content",
123            Status::MultiStatus=> "207 Multi Status",
124            Status::MultipleChoices=> "300 Multiple Choices",
125            Status::MovedPermanently => "301 Moved Permanently",
126            Status::Found => "302 Found",
127            Status::SeeOther => "303 See Other",
128            Status::NotModified=> "304 Not Modified",
129            Status::UseProxy => "305 Use Proxy",
130            Status::TemporaryRedirect => "307 Temporary Redirect",
131            Status::PermanentRedirect => "308 Permanent Redirect",
132            Status::BadRequest => "400 Bad Request",
133            Status::Unauthorized => "401 Unauthorized",
134            Status::PaymentRequired=> "402 Payment Required",
135            Status::Forbidden => "403 Forbidden",
136            Status::NotFound => "404 Not Found",
137            Status::MethodNotAllowed => "405 Method Not Allowed",
138            Status::NotAcceptable => "406 Not Acceptable",
139            Status::ProxyAuthenticationRequired => "407 Proxy Authentication Required",
140            Status::RequestTimeout => "408 Request Timeout",
141            Status::Conflict => "409 Conflict",
142            Status::Gone => "410 Gone",
143            Status::LengthRequired => "411 Length Required",
144            Status::PreconditionFailed => "412 Precondition Failed",
145            Status::PayloadTooLarge => "413 Payload Too Large",
146            Status::UriTooLong => "414 URI Too Long",
147            Status::UnsupportedMediaType  => "415 Unsupported Media Type",
148            Status::RangeNotSatisfiable => "416 Range Not Satisfiable",
149            Status::ExpectationFailed => "417 Expectation Failed",
150            Status::ImATeapot => "418 I'm A Teapot",
151            Status::UnprocessableEntity=> "422 Unprocessable Entity",
152            Status::Locked => "423 Locked",
153            Status::FailedDependency => "424 Failed Dependency",
154            Status::UpgradeRequired => "426 Upgrade Required",
155            Status::PreconditionRequired   => "428 Precondition Required",
156            Status::TooManyRequests => "429 Too Many Requests",
157            Status::RequestHeaderFieldsTooLarge => "431 Request Header Fields Too Large",
158            Status::InternalServerError => "500 Internal Server Error",
159            Status::NotImplemented => "501 Not Implemented",
160            Status::BadGateway => "502 Bad Gateway",
161            Status::ServiceUnavailable => "503 Service Unavailable",
162            Status::GatewayTimeout => "504 Gateway Timeout",
163            Status::HttpVersionNotSupported => "505 Http Version Not Supported",
164            Status::InsufficientStorage => "507 Insufficient Storage",
165            Status::NetworkAuthenticationRequired => "511 Network Authentication Required",
166        }
167    }
168}
169