francoisgib_webserver 1.0.3

HTTP Webserver
Documentation
#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use smallvec::smallvec;

    use crate::{
        http::{
            headers::{ContentType, HeaderEntry, HttpHeader, HttpHeaderValue},
            responses::HttpResponse,
            status::HttpStatus,
        },
        utils::buffer::Buffer,
    };

    #[test]
    fn test_find_existing_header() {
        let mut response = dummy_response();
        let header = response.find_header(HttpHeader::ContentType);
        assert!(header.is_some());
        assert_eq!(header.unwrap().name, HttpHeader::ContentType);
    }

    #[test]
    fn test_find_non_existent_header() {
        let mut response = dummy_response();
        let header = response.find_header(HttpHeader::Range);
        assert!(header.is_none());
    }

    #[test]
    fn test_response_to_string_contains_status_line_and_headers() {
        let response = dummy_response();
        let response_str = response.to_string();

        assert!(response_str.starts_with("HTTP/1.1 200 OK"));
        assert!(response_str.contains("Content-Length: 42"));
        assert!(response_str.contains("Content-Type: text/plain"));
        assert!(response_str.contains("Hello world!"));
    }

    fn dummy_response() -> HttpResponse {
        let headers = smallvec![
            HeaderEntry::new(
                HttpHeader::ContentLength,
                HttpHeaderValue::ContentLength(42)
            ),
            HeaderEntry::new(
                HttpHeader::ContentType,
                HttpHeaderValue::ContentType(ContentType::TextPlain)
            ),
        ];

        HttpResponse::new(
            (1, 1),
            HttpStatus::Ok,
            headers,
            Some(Buffer::from_str("Hello world!").unwrap()),
            None,
        )
    }
}