Skip to content

Commit

Permalink
unit test reproducing google panic
Browse files Browse the repository at this point in the history
Add failing test case

Introduce TryBufRead, clean up
  • Loading branch information
lulf authored and bugadani committed Nov 11, 2023
1 parent c54b610 commit 0341925
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,20 @@ mod tests {
assert!(conn.is_exhausted());
}

#[tokio::test]
async fn can_read_to_end_with_chunked_encoding() {
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\n0\r\n\r\n",
);
let mut header_buf = [0; 200];
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", 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
21 changes: 21 additions & 0 deletions tests/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,24 @@ async fn write_without_base_path() {

assert!(from_utf8(&buf).unwrap().starts_with("GET /hello HTTP/1.1"));
}

#[tokio::test]
async fn google_panic() {
use std::net::SocketAddr;
let google_ip = [142, 250, 74, 110];
let addr = SocketAddr::from((google_ip, 80));

let conn = tokio::net::TcpStream::connect(addr).await.unwrap();
let mut conn = TokioStream(FromTokio::new(conn));

let request = Request::get("/")
.host("www.google.com")
.content_type(ContentType::TextPlain)
.build();
request.write(&mut conn).await.unwrap();

let mut rx_buf = [0; 8 * 1024];
let resp = Response::read(&mut conn, Method::GET, &mut rx_buf).await.unwrap();
let body = resp.body().read_to_end().await.unwrap();
println!("{} -> {}", body.len(), core::str::from_utf8(&body).unwrap());
}

0 comments on commit 0341925

Please sign in to comment.