Crate pact_mock_server

Source
Expand description

The pact_mock_server crate provides the in-process mock server for mocking HTTP requests and generating responses based on a pact file. It implements the V3 Pact specification and V4 Pact specification.

§Crate features

All features are enabled by default

  • datetime: Enables support of date and time expressions and generators.
  • xml: Enables support for parsing XML documents.
  • plugins: Enables support for using plugins.
  • multipart: Enables support for MIME multipart bodies.
  • tls: Enables support for mock servers using TLS. This will add the following dependencies: hyper-rustls, rustls, rustls-pemfile, tokio-rustls.

§Creating a mock server

Mock servers can be created by using the mock server builder in the builder package. The builder can create both standard HTTP and HTTPS servers.

The following example loads a Pact file, starts the mock server and then shuts it down later.

use pact_models::prelude::{Pact, RequestResponsePact};
use pact_mock_server::builder::MockServerBuilder;

// Setup a Pact file for the mock server
let pact_json = r#"
    {
        "provider": {
            "name": "Example Provider"
        },
        "consumer": {
            "name": "Example Consumer"
        },
        "interactions": [
          {
              "description": "a GET request",
              "request": {
                "method": "GET",
                "path": "/path"
              },
              "response": {
                "status": 200,
                "headers": {
                  "Content-Type": "text/plain"
                },
                "body": "Hello from the mock server"
              }
          }
        ]
    }
    "#;
 let pact = RequestResponsePact::from_json(&"JSON sample".to_string(), &serde_json::from_str(pact_json)?)?;

// Create the mock server. Note that the async version requires a Tokio runtime.
let mut mock_server = MockServerBuilder::new()
  .bind_to("127.0.0.1:0")
  .with_pact(pact.boxed())
  .start()
  .await?;

// We can now make any requests to the mock server
let http_client = reqwest::Client::new();
let response = http_client.get(format!("http://127.0.0.1:{}/path", mock_server.port()).as_str())
  .send()
  .await?;
assert_eq!(response.text().await?, "Hello from the mock server");

// Shut the mock server down. This will dispose of the running background tasks.
mock_server.shutdown()?;

// Finally we can now check the status of the mock server.
assert_eq!(mock_server.all_matched(), true);

Modules§

builder
Provides a builder for constructing mock servers
hyper_server
Mock server implementation using Hyper
legacy
Deprecated 1.x mock server functions
matching
The matching module defines how a request is matched against a list of potential interactions.
mock_server
This module defines the external interface for controlling one particular instance of a mock server.
server_manager
This module defines a manager for holding multiple instances of mock servers.

Structs§

MANAGERDeprecated
A global thread-safe, “init-on-demand” reference to a server manager. When the server manager is initialized, it starts a separate thread on which to serve requests.

Enums§

MockServerError
Mock server errors
WritePactFileErr
Write Pact File Errors

Statics§

LOG_IDDeprecated
Log ID to accumulate logs against

Functions§

configure_core_catalogue
Sets up all the core catalogue entries for mock servers