fix: base64 encode details header (#345)

This commit is contained in:
Lucio Franco
2020-05-07 15:31:26 -04:00
committed by GitHub
parent 68d2ab3339
commit e683ffef1f
7 changed files with 100 additions and 3 deletions
+21
View File
@@ -0,0 +1,21 @@
[package]
name = "integration-tests"
version = "0.1.0"
authors = ["Lucio Franco <[email protected]>"]
edition = "2018"
publish = false
license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tonic = { path = "../../tonic" }
prost = "0.6"
futures-util = "0.3"
bytes = "0.5"
[dev-dependencies]
tokio = { version = "0.2", features = ["macros", "rt-core", "tcp"] }
[build-dependencies]
tonic-build = { path = "../../tonic-build" }
+3
View File
@@ -0,0 +1,3 @@
fn main() {
tonic_build::compile_protos("proto/test.proto").unwrap();
}
+10
View File
@@ -0,0 +1,10 @@
syntax = "proto3";
package test;
service Test {
rpc UnaryCall(Input) returns (Output);
}
message Input {}
message Output {}
+3
View File
@@ -0,0 +1,3 @@
pub mod pb {
tonic::include_proto!("test");
}
+52
View File
@@ -0,0 +1,52 @@
use bytes::Bytes;
use futures_util::FutureExt;
use integration_tests::pb::{test_client, test_server, Input, Output};
use std::time::Duration;
use tokio::sync::oneshot;
use tonic::{transport::Server, Code, Request, Response, Status};
#[tokio::test]
async fn status_with_details() {
struct Svc;
#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, _: Request<Input>) -> Result<Response<Output>, Status> {
Err(Status::with_details(
Code::ResourceExhausted,
"Too many requests",
Bytes::from_static(&[1]),
))
}
}
let svc = test_server::TestServer::new(Svc);
let (tx, rx) = oneshot::channel::<()>();
let jh = tokio::spawn(async move {
Server::builder()
.add_service(svc)
.serve_with_shutdown("127.0.0.1:1337".parse().unwrap(), rx.map(drop))
.await
.unwrap();
});
tokio::time::delay_for(Duration::from_millis(100)).await;
let mut channel = test_client::TestClient::connect("http://127.0.0.1:1337")
.await
.unwrap();
let err = channel
.unary_call(Request::new(Input {}))
.await
.unwrap_err();
assert_eq!(err.message(), "Too many requests");
assert_eq!(err.details(), &[1]);
tx.send(()).unwrap();
jh.await.unwrap();
}