in src/connection.rs [1234:1277]
fn test_parse_body() {
let (_, receiver) = UnixStream::pair().unwrap();
let mut conn = HttpConnection::new(receiver);
// Error case: end_cursor past buffer end.
assert_eq!(
conn.parse_body(&mut 0usize, conn.buffer.len() + 1)
.unwrap_err(),
ConnectionError::ParseError(RequestError::Overflow)
);
// Error case: line_start_index is past end_cursor.
assert_eq!(
conn.parse_body(&mut 1usize, 0usize).unwrap_err(),
ConnectionError::ParseError(RequestError::Underflow)
);
// OK case: consume the buffer.
conn.body_bytes_to_be_read = 1;
assert_eq!(conn.parse_body(&mut 0usize, 0usize), Ok(false));
// Error case: there's more body to be parsed, but no pending request set.
assert_eq!(
conn.parse_body(&mut 0, BUFFER_SIZE).unwrap_err(),
ConnectionError::ParseError(RequestError::BodyWithoutPendingRequest)
);
// Error case: read more bytes than we should have into the body of the request.
conn.pending_request = Some(Request {
request_line: RequestLine::new(Method::Get, "http://foo/bar", Version::Http11),
headers: Headers::new(0, true, true),
body: None,
files: Vec::new(),
});
conn.body_vec = vec![0xde, 0xad, 0xbe, 0xef];
assert_eq!(
conn.parse_body(&mut 0, BUFFER_SIZE).unwrap_err(),
ConnectionError::ParseError(RequestError::InvalidRequest)
);
// OK case.
conn.body_vec.clear();
assert_eq!(conn.parse_body(&mut 0, BUFFER_SIZE), Ok(true));
}