|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use futures::{channel::oneshot, FutureExt}; |
| 4 | +use http::{header::HeaderName, HeaderValue}; |
| 5 | +use integration_tests::pb::{test_client::TestClient, test_server, Input, Output}; |
| 6 | +use tonic::{ |
| 7 | + transport::{Endpoint, Server}, |
| 8 | + Request, Response, Status, |
| 9 | +}; |
| 10 | +use tower::ServiceBuilder; |
| 11 | +use tower_http::{set_header::SetRequestHeaderLayer, trace::TraceLayer}; |
| 12 | + |
| 13 | +#[tokio::test] |
| 14 | +async fn connect_supports_standard_tower_layers() { |
| 15 | + struct Svc; |
| 16 | + |
| 17 | + #[tonic::async_trait] |
| 18 | + impl test_server::Test for Svc { |
| 19 | + async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> { |
| 20 | + match req.metadata().get("x-test") { |
| 21 | + Some(_) => Ok(Response::new(Output {})), |
| 22 | + None => Err(Status::internal("user-agent header is missing")), |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + let (tx, rx) = oneshot::channel(); |
| 28 | + let svc = test_server::TestServer::new(Svc); |
| 29 | + |
| 30 | + // Start the server now, second call should succeed |
| 31 | + let jh = tokio::spawn(async move { |
| 32 | + Server::builder() |
| 33 | + .add_service(svc) |
| 34 | + .serve_with_shutdown("127.0.0.1:1340".parse().unwrap(), rx.map(drop)) |
| 35 | + .await |
| 36 | + .unwrap(); |
| 37 | + }); |
| 38 | + |
| 39 | + let channel = Endpoint::from_static("http://127.0.0.1:1340").connect_lazy(); |
| 40 | + |
| 41 | + // prior to https://github.com/hyperium/tonic/pull/974 |
| 42 | + // this would not compile. (specifically the `TraceLayer`) |
| 43 | + let mut client = TestClient::new( |
| 44 | + ServiceBuilder::new() |
| 45 | + .layer(SetRequestHeaderLayer::overriding( |
| 46 | + HeaderName::from_static("x-test"), |
| 47 | + HeaderValue::from_static("test-header"), |
| 48 | + )) |
| 49 | + .layer(TraceLayer::new_for_grpc()) |
| 50 | + .service(channel), |
| 51 | + ); |
| 52 | + |
| 53 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 54 | + client.unary_call(Request::new(Input {})).await.unwrap(); |
| 55 | + |
| 56 | + tx.send(()).unwrap(); |
| 57 | + jh.await.unwrap(); |
| 58 | +} |
0 commit comments