|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use integration_tests::pb::{test_client, test_server, Input, Output}; |
| 4 | +use tokio::sync::oneshot; |
| 5 | +use tonic::{ |
| 6 | + transport::{Endpoint, Server}, |
| 7 | + Request, Response, Status, |
| 8 | +}; |
| 9 | + |
| 10 | +/// This test checks that the max header list size is respected, and that |
| 11 | +/// it allows for error messages up to that size. |
| 12 | +#[tokio::test] |
| 13 | +async fn test_http_max_header_list_size_and_long_errors() { |
| 14 | + struct Svc; |
| 15 | + |
| 16 | + // The default value is 16k. |
| 17 | + const N: usize = 20_000; |
| 18 | + |
| 19 | + fn long_message() -> String { |
| 20 | + "a".repeat(N) |
| 21 | + } |
| 22 | + |
| 23 | + #[tonic::async_trait] |
| 24 | + impl test_server::Test for Svc { |
| 25 | + async fn unary_call(&self, _: Request<Input>) -> Result<Response<Output>, Status> { |
| 26 | + Err(Status::internal(long_message())) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + let svc = test_server::TestServer::new(Svc); |
| 31 | + |
| 32 | + let (tx, rx) = oneshot::channel::<()>(); |
| 33 | + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 34 | + let addr = format!("http://{}", listener.local_addr().unwrap()); |
| 35 | + |
| 36 | + let jh = tokio::spawn(async move { |
| 37 | + let (nodelay, keepalive) = (true, Some(Duration::from_secs(1))); |
| 38 | + let listener = |
| 39 | + tonic::transport::server::TcpIncoming::from_listener(listener, nodelay, keepalive) |
| 40 | + .unwrap(); |
| 41 | + Server::builder() |
| 42 | + .http2_max_pending_accept_reset_streams(Some(0)) |
| 43 | + .add_service(svc) |
| 44 | + .serve_with_incoming_shutdown(listener, async { drop(rx.await) }) |
| 45 | + .await |
| 46 | + .unwrap(); |
| 47 | + }); |
| 48 | + |
| 49 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 50 | + |
| 51 | + let channel = Endpoint::from_shared(addr) |
| 52 | + .unwrap() |
| 53 | + // Increase the max header list size to 2x the default value. If this is |
| 54 | + // not set, this test will hang. See |
| 55 | + // <https://github.com/hyperium/tonic/issues/1834>. |
| 56 | + .http2_max_header_list_size(u32::try_from(N * 2).unwrap()) |
| 57 | + .connect() |
| 58 | + .await |
| 59 | + .unwrap(); |
| 60 | + |
| 61 | + let mut client = test_client::TestClient::new(channel); |
| 62 | + |
| 63 | + let err = client.unary_call(Request::new(Input {})).await.unwrap_err(); |
| 64 | + |
| 65 | + assert_eq!(err.message(), long_message()); |
| 66 | + |
| 67 | + tx.send(()).unwrap(); |
| 68 | + |
| 69 | + jh.await.unwrap(); |
| 70 | +} |
0 commit comments