#Need help understanding why my format is incorrect.

5 messages · Page 1 of 1 (latest)

wide musk
#

here is my HtttpResponse struct:

pub struct HttpResponse<'a> {
    version: &'a str,
    status_code: &'a str,
    status_text: &'a str,
    headers: Option<HashMap<&'a str, &'a str>>,
    body: Option<String>,
}

here is my From impl for my HttpResponse struct

impl<'a> From<HttpResponse<'a>> for String {
    fn from(res: HttpResponse<'a>) -> Self {
        let res1 = res.clone();
        format!(
            "{}{}{}\r\n{} Content-Lenght: {}\r\n\r\n{}",
            &res1.version(),
            &res1.status_code(),
            &res1.status_text(),
            &res1.headers(),
            &res1.body.as_ref().unwrap().len(),
            &res1.body()
        )
    }
}

and here is my test:

    #[test]
    fn test_http_response_creation() {
        let response_expected = HttpResponse {
            version: "HTTP/1.1",
            status_code: "404",
            status_text: "Not Found",
            headers: {
                let mut h = HashMap::new();
                h.insert("Content-Type", "text/html");
                Some(h)
            },
            body: Some("this is a body text".into()),
        };
        let http_string: String = response_expected.into();
        let response_actual= "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nContent-Length: 19\r\n\r\nthis is a body text";
        assert_eq!(response_actual, http_string);
    }

my test results:
left: `"HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nContent-Length: 19\r\n\r\nthis is a body text"`, right: `"HTTP/1.1404Not Found\r\nContent-Type:text/html\r\n Content-Lenght: 19\r\n\r\nthis is a body text"`',

I don't know what could be causing the spacing problem. any help is appreciated. Thank you.

calm vector
#

Look at the format string in your From impl. There are no spaces ther, so neither they are in result.

wide musk
#

ok let me check. thank yu

wide musk