-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add GrpcMethod extension into request for client (#1275)
* feat: add GrpcMethod extension into request for client * refactor: change GrpcMethod fields into private and expose methods instead * refactor: hide GrpcMethod::new in doc --------- Co-authored-by: Lucio Franco <luciofranco14@gmail.com>
- Loading branch information
1 parent
1547f96
commit 7a6b20d
Showing
9 changed files
with
207 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::time::Duration; | ||
|
||
use futures::{channel::oneshot, FutureExt}; | ||
use integration_tests::pb::{test_client::TestClient, test_server, Input, Output}; | ||
use tonic::{ | ||
transport::{Endpoint, Server}, | ||
GrpcMethod, Request, Response, Status, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn interceptor_retrieves_grpc_method() { | ||
use test_server::Test; | ||
|
||
struct Svc; | ||
|
||
#[tonic::async_trait] | ||
impl Test for Svc { | ||
async fn unary_call(&self, _: Request<Input>) -> Result<Response<Output>, Status> { | ||
Ok(Response::new(Output {})) | ||
} | ||
} | ||
|
||
let svc = test_server::TestServer::new(Svc); | ||
|
||
let (tx, rx) = oneshot::channel(); | ||
// Start the server now, second call should succeed | ||
let jh = tokio::spawn(async move { | ||
Server::builder() | ||
.add_service(svc) | ||
.serve_with_shutdown("127.0.0.1:1340".parse().unwrap(), rx.map(drop)) | ||
.await | ||
.unwrap(); | ||
}); | ||
|
||
let channel = Endpoint::from_static("http://127.0.0.1:1340").connect_lazy(); | ||
|
||
fn client_intercept(req: Request<()>) -> Result<Request<()>, Status> { | ||
println!("Intercepting client request: {:?}", req); | ||
|
||
let gm = req.extensions().get::<GrpcMethod>().unwrap(); | ||
assert_eq!(gm.service(), "test.Test"); | ||
assert_eq!(gm.method(), "UnaryCall"); | ||
|
||
Ok(req) | ||
} | ||
let mut client = TestClient::with_interceptor(channel, client_intercept); | ||
|
||
tokio::time::sleep(Duration::from_millis(100)).await; | ||
client.unary_call(Request::new(Input {})).await.unwrap(); | ||
|
||
tx.send(()).unwrap(); | ||
jh.await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.