85 releases (15 stable)

new 2.0.0 Jul 18, 2024
2.0.0-beta.4 Jun 25, 2024
1.2.9 Jul 17, 2024
1.2.5 Mar 15, 2024
0.2.0 Jul 13, 2016

#62 in Network programming

Download history 750/week @ 2024-03-28 953/week @ 2024-04-04 1301/week @ 2024-04-11 940/week @ 2024-04-18 930/week @ 2024-04-25 1258/week @ 2024-05-02 1357/week @ 2024-05-09 1523/week @ 2024-05-16 1485/week @ 2024-05-23 2562/week @ 2024-05-30 2263/week @ 2024-06-06 2745/week @ 2024-06-13 2858/week @ 2024-06-20 1810/week @ 2024-06-27 2255/week @ 2024-07-04 1452/week @ 2024-07-11

9,183 downloads per month
Used in 7 crates (6 directly)

MIT license

140KB
3K SLoC

Pact Mock Server library

This library implements the in-process mock server for matching HTTP requests and generating responses from a pact file. It implements the V3 Pact specification and V4 Pact specification.

Online rust docs

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.

tokio_test::block_on(async {
    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);
    
    Ok::<(), anyhow::Error>(())
});

Legacy functions

The following deprecated functions from the 1.x version exist in the legacy module.

create_mock_server

Creates a mock server. Requires the pact JSON as a string as well as the port for the mock server to run on. A value of 0 for the port will result in a port being allocated by the operating system. The port of the mock server is returned.

mock_server_matched

Simple function that returns a boolean value given the port number of the mock service. This value will be true if all the expectations of the pact that the mock server was created with have been met. It will return false if any request did not match, an un-recognised request was received or an expected request was not received.

mock_server_mismatches

This returns all the mismatches, un-expected requests and missing requests in JSON format, given the port number of the mock server.

shutdown_mock_server

Shuts down the mock server with the provided port. Returns a boolean value to indicate if the mock server was successfully shut down.

write_pact_file

Trigger a mock server to write out its pact file. This function should be called if all the consumer tests have passed. The directory to write the file to is passed as the second parameter. If None is passed in, the current working directory is used. If overwrite is true, the file will be overwritten with the contents of the current pact. Otherwise it will be merged with any existing pact file.

Returns Ok if the pact file was successfully written. Returns an Err if the file can not be written, or there is no mock server running on that port.

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.

Dependencies

~21–60MB
~1M SLoC