-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add example with consumer and provider #53
- Loading branch information
1 parent
7414995
commit 0b9ac92
Showing
9 changed files
with
4,803 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,26 @@ | ||
[workspace] | ||
|
||
[package] | ||
name = "new_fields_consumer" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tokio = { version = "1", features = ["full"] } | ||
anyhow = "1.0.43" | ||
tonic = "0.11.0" | ||
prost = "0.12.4" | ||
prost-types = "0.12.4" | ||
tracing = { version = "0.1", features = [ "log-always" ] } | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
|
||
[dev-dependencies] | ||
expectest = "0.12.0" | ||
env_logger = "0.11.3" | ||
pact-plugin-driver = "0.5.1" | ||
pact_consumer = "1.1.2" | ||
serde_json = "1.0.66" | ||
maplit = "1.0.2" | ||
|
||
[build-dependencies] | ||
tonic-build = "0.11.0" |
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,4 @@ | ||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
tonic_build::compile_protos("../new_fields.proto")?; | ||
Ok(()) | ||
} |
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,55 @@ | ||
tonic::include_proto!("pactissue"); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::path::Path; | ||
|
||
use expectest::prelude::*; | ||
use pact_consumer::prelude::*; | ||
use pact_consumer::mock_server::StartMockServerAsync; | ||
use serde_json::json; | ||
|
||
use super::*; | ||
|
||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] | ||
async fn test_proto_client() { | ||
let _ = env_logger::builder().is_test(true).try_init(); | ||
|
||
let mut pact_builder = PactBuilderAsync::new_v4("grpc-consumer-rust", "new_fields"); | ||
let mock_server = pact_builder | ||
.using_plugin("protobuf", None).await | ||
.synchronous_message_interaction("initial values request", |mut i| async move { | ||
let proto_file = Path::new("../new_fields.proto") | ||
.canonicalize().unwrap().to_string_lossy().to_string(); | ||
i.contents_from(json!({ | ||
"pact:proto": proto_file, | ||
"pact:content-type": "application/protobuf", | ||
"pact:proto-service": "UserService/GetUser", | ||
|
||
"request": { | ||
"id": "matching(regex, '\\d+', '1234')" | ||
}, | ||
"response": { | ||
"id": "matching(type, '89b9475a-09d0-47a9-a4bc-2b6b9d361db6')", | ||
"display_name": "matching(type, 'xX5im0n-P3ggXx')", | ||
"first_name": "matching(type, 'Simon')", | ||
"last_name": "matching(type, 'Pegg')", | ||
} | ||
})).await; | ||
i | ||
}) | ||
.await | ||
.start_mock_server_async(Some("protobuf/transport/grpc")) | ||
.await; | ||
|
||
let url = mock_server.url(); | ||
let mut client = user_service_client::UserServiceClient::connect(url.to_string()).await.unwrap(); | ||
|
||
let request_message = GetUserRequest { | ||
id: "1234".to_string() | ||
}; | ||
let response = client.get_user(tonic::Request::new(request_message)).await; | ||
let response_message = response.unwrap(); | ||
expect!(response_message.get_ref().id.as_str()).to(be_equal_to("89b9475a-09d0-47a9-a4bc-2b6b9d361db6")); | ||
} | ||
} |
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,20 @@ | ||
syntax = "proto3"; | ||
|
||
package pactissue; | ||
|
||
message GetUserResponse { | ||
string id = 1; | ||
string display_name = 2; | ||
string first_name = 3; | ||
string last_name = 4; | ||
string created_at = 5; | ||
string updated_at = 6; | ||
} | ||
|
||
message GetUserRequest { | ||
string id = 1; | ||
} | ||
|
||
service UserService { | ||
rpc GetUser(GetUserRequest) returns (GetUserResponse); | ||
} |
Oops, something went wrong.