pact_consumer/
mock_server.rs

1//! Support for mock HTTP servers that verify pacts.
2
3use async_trait::async_trait;
4use pact_models::pact::Pact;
5use pact_models::sync_pact::RequestResponsePact;
6use url::Url;
7
8use pact_mock_server::matching::MatchResult;
9use pact_mock_server::mock_server::{MockServerConfig, MockServerMetrics};
10
11use crate::mock_server::http_mock_server::ValidatingHttpMockServer;
12
13pub(crate) mod http_mock_server;
14#[cfg(feature = "plugins")] pub(crate) mod plugin_mock_server;
15
16/// A mock server that handles the requests described in a `Pact`, intended
17/// for use in tests, and validates that the requests made to that server are
18/// correct.
19///
20/// Because this is intended for use in tests, it will panic if something goes
21/// wrong.
22pub trait ValidatingMockServer {
23  /// The base URL of the mock server. You can make normal HTTP requests using this
24  /// as the base URL (if it is a HTTP-based mock server).
25  fn url(&self) -> Url;
26
27  /// Given a path string, return a URL pointing to that path on the mock
28  /// server. If the `path` cannot be parsed as URL, **this function will
29  /// panic**. For a non-panicking version, call `.url()` instead and build
30  /// this path yourself.
31  fn path(&self, path: &str) -> Url;
32
33  /// Returns the current status of the mock server. Note that with some mock server
34  /// implementations, the status will only be available once the mock server has shutdown.
35  fn status(&self) -> Vec<MatchResult>;
36
37  /// Returns the metrics collected by the mock server
38  fn metrics(&self) -> MockServerMetrics;
39}
40
41/// This trait is implemented by types which allow us to start a mock server.
42pub trait StartMockServer {
43  /// Start a mock server running in a background thread. If the catalog entry is omitted,
44  /// then a standard HTTP mock server will be started.
45  fn start_mock_server(
46    &self,
47    catalog_entry: Option<&str>,
48    mock_server_config: Option<MockServerConfig>
49  ) -> Box<dyn ValidatingMockServer>;
50}
51
52/// This trait is implemented by types which allow us to start a mock server (async version).
53#[async_trait]
54pub trait StartMockServerAsync {
55  /// Start a mock server running in a task (requires a Tokio runtime to be already setup)
56  async fn start_mock_server_async(
57    &self,
58    catalog_entry: Option<&str>,
59    mock_server_config: Option<MockServerConfig>
60  ) -> Box<dyn ValidatingMockServer>;
61}
62
63impl StartMockServer for RequestResponsePact {
64  fn start_mock_server(
65    &self,
66    _catalog_entry: Option<&str>,
67    mock_server_config: Option<MockServerConfig>
68  ) -> Box<dyn ValidatingMockServer> {
69    ValidatingHttpMockServer::start(self.boxed(), None, mock_server_config)
70  }
71}
72
73#[async_trait]
74impl StartMockServerAsync for RequestResponsePact {
75  async fn start_mock_server_async(
76    &self,
77    _catalog_entry: Option<&str>,
78    mock_server_config: Option<MockServerConfig>
79  ) -> Box<dyn ValidatingMockServer> {
80    ValidatingHttpMockServer::start_async(self.boxed(), None, mock_server_config).await
81  }
82}