Skip to content

Commit

Permalink
chore: Add example with consumer and provider #53
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Apr 10, 2024
1 parent 7414995 commit 0b9ac92
Show file tree
Hide file tree
Showing 9 changed files with 4,803 additions and 0 deletions.
3,176 changes: 3,176 additions & 0 deletions integrated_tests/new_fields/consumer/Cargo.lock

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions integrated_tests/new_fields/consumer/Cargo.toml
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"
4 changes: 4 additions & 0 deletions integrated_tests/new_fields/consumer/build.rs
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(())
}
55 changes: 55 additions & 0 deletions integrated_tests/new_fields/consumer/src/lib.rs
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"));
}
}
20 changes: 20 additions & 0 deletions integrated_tests/new_fields/new_fields.proto
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);
}
Loading

0 comments on commit 0b9ac92

Please sign in to comment.