Skip to content

Commit

Permalink
feat: support adding a test context to the mock server to support gen…
Browse files Browse the repository at this point in the history
…erators
  • Loading branch information
rholshausen committed Dec 16, 2022
1 parent ac210b1 commit fc159a7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ maplit = "1.0.2"
pact_matching = "1.0.1"
pact_models = "1.0.2"
pact_verifier = "0.13.18"
pact-plugin-driver = "0.2.1"
pact-plugin-driver = "0.2.2"
anyhow = "1.0.42"
futures = "0.3.25"
async-trait = "0.1.58"
Expand Down
20 changes: 16 additions & 4 deletions src/mock_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use pact_models::prelude::v4::V4Pact;
use pact_models::v4::sync_message::SynchronousMessage;
use prost::Message;
use prost_types::{FileDescriptorSet, MethodDescriptorProto};
use serde_json::Value;
use serde_json::{json, Value};
use tokio::net::TcpListener;
use tokio::runtime::Handle;
use tokio::sync::oneshot::{channel, Sender};
Expand Down Expand Up @@ -54,19 +54,22 @@ pub struct GrpcMockServer {
descriptors: HashMap<String, FileDescriptorSet>,
routes: HashMap<String, (FileDescriptorSet, MethodDescriptorProto, SynchronousMessage)>,
/// Server key for this mock server
pub server_key: String
pub server_key: String,
/// test context pass in from the test framework
pub test_context: HashMap<String, Value>,
}

impl GrpcMockServer
{
/// Create a new mock server
pub fn new(pact: V4Pact, plugin_config: &PluginData) -> Self {
pub fn new(pact: V4Pact, plugin_config: &PluginData, test_context: HashMap<String, Value>) -> Self {
GrpcMockServer {
pact,
plugin_config: plugin_config.clone(),
descriptors: Default::default(),
routes: Default::default(),
server_key: Uuid::new_v4().to_string()
server_key: Uuid::new_v4().to_string(),
test_context
}
}

Expand Down Expand Up @@ -146,6 +149,8 @@ impl GrpcMockServer
let listener = TcpListener::bind(addr).await?;
let address = listener.local_addr()?;

self.update_mock_server_address(&address);

let handle = Handle::current();
// because Rust
let key = self.server_key.clone();
Expand Down Expand Up @@ -191,6 +196,13 @@ impl GrpcMockServer
Ok(address)
}
}

fn update_mock_server_address(&mut self, address: &SocketAddr) {
self.test_context.insert("mockServer".to_string(), json!({
"href": format!("http://{}:{}", address.ip(), address.port()),
"port": address.port()
}));
}
}

impl Service<Request<hyper::Body>> for GrpcMockServer {
Expand Down
13 changes: 12 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,16 @@ impl PactPlugin for ProtobufPactPlugin {
Some(config) => config.clone()
};

let grpc_mock_server = GrpcMockServer::new(pact, &plugin_config);
let test_context: HashMap<String, Value> = match request.test_context.as_ref()
.map(|tc| proto_struct_to_json(tc))
.unwrap_or_default() {
Value::Object(map) => map.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
_ => hashmap!{}
};

let grpc_mock_server = GrpcMockServer::new(pact, &plugin_config, test_context);
let server_key = grpc_mock_server.server_key.clone();
match grpc_mock_server.start_server(request.host_interface.as_str(), request.port, request.tls).await {
Ok(address) => {
Expand Down Expand Up @@ -1162,6 +1171,7 @@ mod tests {
port: 0,
tls: false,
pact: "I'm not JSON!".to_string(),
.. proto::StartMockServerRequest::default()
};
let result = plugin.start_mock_server(Request::new(request)).await;
let response = result.unwrap();
Expand All @@ -1180,6 +1190,7 @@ mod tests {
port: 0,
tls: false,
pact: "{}".to_string(),
.. proto::StartMockServerRequest::default()
};
let result = plugin.start_mock_server(Request::new(request)).await;
let response = result.unwrap();
Expand Down

0 comments on commit fc159a7

Please sign in to comment.