Skip to content

Commit

Permalink
Feat(pact_consumer): Upgrade pact_mock_server to the new 2.0.0 version
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jul 18, 2024
1 parent fd1ba0c commit 111a18c
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 243 deletions.
84 changes: 81 additions & 3 deletions rust/Cargo.lock

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

4 changes: 2 additions & 2 deletions rust/pact_consumer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pact_consumer"
version = "1.2.4"
version = "1.3.0"
authors = ["Ronald Holshausen <ronald.holshausen@gmail.com>", "Eric Kidd <git@randomhacks.net>"]
edition = "2021"
description = "Pact-Rust module that provides support for writing consumer pact tests"
Expand Down Expand Up @@ -31,7 +31,7 @@ itertools = "0.13.0"
lazy_static = "1.5.0"
maplit = "1.0.2"
pact_matching = { version = "~1.2.5", path = "../pact_matching", default-features = false }
pact_mock_server = { version = "~1.2.9", default-features = false }
pact_mock_server = { version = "~2.0.0", default-features = false }
pact_models = { version = "~1.2.2", default-features = false }
pact-plugin-driver = { version = "~0.7.0", optional = true, default-features = false }
regex = "1.10.5"
Expand Down
5 changes: 1 addition & 4 deletions rust/pact_consumer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ To use it, add it to your dev-dependencies in your cargo manifest:

```toml
[dev-dependencies]
pact_consumer = "1.1"
pact_consumer = "1.3"
```

You can now write a pact test using the consumer DSL.

```rust
use pact_consumer::prelude::*;
use pact_consumer::*;

#[tokio::test]
async fn a_service_consumer_side_of_a_pact_goes_a_little_something_like_this() {
Expand Down Expand Up @@ -76,7 +75,6 @@ file.

```rust
use pact_consumer::prelude::*;
use pact_consumer::*;

#[test]
fn a_message_consumer_side_of_a_pact_goes_a_little_something_like_this() {
Expand Down Expand Up @@ -120,7 +118,6 @@ one or more response messages are returned. Examples of this would be things lik

```rust
use pact_consumer::prelude::*;
use pact_consumer::*;
use expectest::prelude::*;
use serde_json::{Value, from_slice};

Expand Down
11 changes: 8 additions & 3 deletions rust/pact_consumer/src/builders/pact_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::{Debug, Formatter};
use std::panic::RefUnwindSafe;
use std::path::PathBuf;
use pact_mock_server::mock_server::MockServerConfig;

use pact_models::{Consumer, Provider};
use pact_models::interaction::Interaction;
Expand Down Expand Up @@ -282,7 +283,11 @@ impl PactBuilder {
}

impl StartMockServer for PactBuilder {
fn start_mock_server(&self, _catalog_entry: Option<&str>) -> Box<dyn ValidatingMockServer> {
fn start_mock_server(
&self,
_catalog_entry: Option<&str>,
mock_server_config: Option<MockServerConfig>
) -> Box<dyn ValidatingMockServer> {
#[cfg(feature = "plugins")]
{
match _catalog_entry {
Expand All @@ -295,13 +300,13 @@ impl StartMockServer for PactBuilder {
}
None => panic!("Did not find a catalogue entry for key '{}'", entry_name)
}
None => ValidatingHttpMockServer::start(self.build(), self.output_dir.clone())
None => ValidatingHttpMockServer::start(self.build(), self.output_dir.clone(), mock_server_config)
}
}

#[cfg(not(feature = "plugins"))]
{
ValidatingHttpMockServer::start(self.build(), self.output_dir.clone())
ValidatingHttpMockServer::start(self.build(), self.output_dir.clone(), mock_server_config)
}
}
}
Expand Down
21 changes: 15 additions & 6 deletions rust/pact_consumer/src/builders/pact_builder_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::future::Future;
use std::path::PathBuf;

use async_trait::async_trait;
use pact_mock_server::mock_server::MockServerConfig;
use pact_models::{Consumer, Provider};
use pact_models::interaction::Interaction;
use pact_models::pact::Pact;
Expand Down Expand Up @@ -251,7 +252,11 @@ impl PactBuilderAsync {
}

impl StartMockServer for PactBuilderAsync {
fn start_mock_server(&self, _catalog_entry: Option<&str>) -> Box<dyn ValidatingMockServer> {
fn start_mock_server(
&self,
_catalog_entry: Option<&str>,
mock_server_config: Option<MockServerConfig>
) -> Box<dyn ValidatingMockServer> {
#[cfg(feature = "plugins")]
{
match _catalog_entry {
Expand All @@ -264,20 +269,24 @@ impl StartMockServer for PactBuilderAsync {
}
None => panic!("Did not find a catalogue entry for key '{}'", entry_name)
}
None => ValidatingHttpMockServer::start(self.build(), self.output_dir.clone())
None => ValidatingHttpMockServer::start(self.build(), self.output_dir.clone(), mock_server_config)
}
}

#[cfg(not(feature = "plugins"))]
{
ValidatingHttpMockServer::start(self.build(), self.output_dir.clone())
ValidatingHttpMockServer::start(self.build(), self.output_dir.clone(), mock_server_config)
}
}
}

#[async_trait]
impl StartMockServerAsync for PactBuilderAsync {
async fn start_mock_server_async(&self, _catalog_entry: Option<&str>) -> Box<dyn ValidatingMockServer> {
async fn start_mock_server_async(
&self,
_catalog_entry: Option<&str>,
mock_server_config: Option<MockServerConfig>
) -> Box<dyn ValidatingMockServer> {
#[cfg(feature = "plugins")]
{
match _catalog_entry {
Expand All @@ -290,13 +299,13 @@ impl StartMockServerAsync for PactBuilderAsync {
}
None => panic!("Did not find a catalogue entry for key '{}'", entry_name)
}
None => ValidatingHttpMockServer::start_async(self.build(), self.output_dir.clone()).await
None => ValidatingHttpMockServer::start_async(self.build(), self.output_dir.clone(), mock_server_config).await
}
}

#[cfg(not(feature = "plugins"))]
{
ValidatingHttpMockServer::start_async(self.build(), self.output_dir.clone()).await
ValidatingHttpMockServer::start_async(self.build(), self.output_dir.clone(), mock_server_config).await
}
}
}
Expand Down
18 changes: 12 additions & 6 deletions rust/pact_consumer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! // Return the interaction builder back to the pact framework
//! i
//! })
//! .start_mock_server(None);
//! .start_mock_server(None, None);
//! ```
//!
//! You can than use an HTTP client like `reqwest` to make requests against your
Expand All @@ -82,7 +82,7 @@
//! # .body("That is some good Mallory.");
//! # // Return the interaction builder back to the pact framework
//! # i
//! # }).start_mock_server(None);
//! # }).start_mock_server(None, None);
//!
//! // You would use your actual client code here.
//! let mallory_url = provider_service.path("/mallory");
Expand All @@ -104,7 +104,6 @@
//!
//! ```
//! use pact_consumer::prelude::*;
//! use pact_consumer::*;
//!
//! PactBuilder::new("quotes client", "quotes service")
//! .interaction("add a new quote to the database", "", |mut i| {
Expand Down Expand Up @@ -156,7 +155,6 @@
//!
//! ```
//! use pact_consumer::prelude::*;
//! use pact_consumer::{each_like, each_like_helper, json_pattern};
//! use serde::{Deserialize, Serialize};
//!
//! /// Our application's domain object representing a user.
Expand Down Expand Up @@ -210,7 +208,6 @@
//!
//! ```rust
//! use pact_consumer::prelude::*;
//! use pact_consumer::*;
//! use expectest::prelude::*;
//! use serde_json::{Value, from_slice};
//!
Expand Down Expand Up @@ -348,7 +345,7 @@
//! })
//! .await
//! // Now start the mock server
//! .start_mock_server_async(None)
//! .start_mock_server_async(None, None)
//! .await;
//!
//! // Now we can make our actual request for the CSV file and validate the response
Expand Down Expand Up @@ -392,6 +389,14 @@ pub mod util;
/// use pact_consumer::prelude::*;
/// ```
pub mod prelude {
pub use crate::{
like,
each_like,
each_like_helper,
term,
json_pattern,
json_pattern_internal
};
pub use crate::builders::{HttpPartBuilder, PactBuilder, PactBuilderAsync};
#[cfg(feature = "plugins")] pub use crate::builders::plugin_builder::PluginInteractionBuilder;
pub use crate::mock_server::{StartMockServer, ValidatingMockServer};
Expand All @@ -410,6 +415,7 @@ pub mod prelude {
};
#[cfg(feature = "datetime")] pub use crate::patterns::{DateTime};
pub use crate::util::strip_null_fields;
pub use pact_mock_server::mock_server::MockServerConfig;
}

/// Consumer version
Expand Down
Loading

0 comments on commit 111a18c

Please sign in to comment.