Skip to content

Commit

Permalink
Fix using the entire available buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 21, 2023
1 parent 0941b61 commit 0bb1d22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/response/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ where
};

let mut len = 0;
while len < reader.raw_body.buffer.buffer.len() {
while !reader.raw_body.buffer.buffer.is_empty() {
// Read some
let read = reader.fill_buf().await?.len();
len += read;
Expand Down
15 changes: 15 additions & 0 deletions src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,21 @@ mod tests {
assert!(conn.is_exhausted());
}

#[tokio::test]
async fn can_read_to_end_into_a_small_buffer() {
let mut conn = FakeSingleReadConnection::new(
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHELLO\r\n6\r\n WORLD\r\n1\r\n \r\n5\r\nHELLO\r\n6\r\n WORLD\r\n1\r\n \r\n5\r\nHELLO\r\n6\r\n WORLD\r\n0\r\n\r\n",
);
conn.read_length = 10;
let mut header_buf = [0; 50]; // buffer is long enough to hold the complete response
let response = Response::read(&mut conn, Method::GET, &mut header_buf).await.unwrap();

let body = response.body().read_to_end().await.unwrap();

assert_eq!(b"HELLO WORLD HELLO WORLD HELLO WORLD", body);
assert!(conn.is_exhausted());
}

#[tokio::test]
async fn can_read_to_end_of_connection_with_same_buffer() {
let mut conn = FakeSingleReadConnection::new(b"HTTP/1.1 200 OK\r\n\r\nHELLO WORLD");
Expand Down

0 comments on commit 0bb1d22

Please sign in to comment.