#testing #pact #cdc

pact_consumer

Pact-Rust module that provides support for writing consumer pact tests

26 releases

new 0.8.0-beta.3 Oct 19, 2021
0.7.8 Sep 28, 2021
0.7.7 Jul 23, 2021
0.7.5 Mar 14, 2021
0.2.0 Oct 9, 2016

#3 in #cdc

Download history 132/week @ 2021-06-30 139/week @ 2021-07-07 118/week @ 2021-07-14 87/week @ 2021-07-21 113/week @ 2021-07-28 89/week @ 2021-08-04 95/week @ 2021-08-11 99/week @ 2021-08-18 130/week @ 2021-08-25 205/week @ 2021-09-01 162/week @ 2021-09-08 58/week @ 2021-09-15 51/week @ 2021-09-22 123/week @ 2021-09-29 228/week @ 2021-10-06 278/week @ 2021-10-13

571 downloads per month
Used in 3 crates

MIT AND Apache-2.0

1.5MB
27K SLoC

Pact test DSL for writing consumer pact tests in Rust

This library provides a test DSL for writing consumer pact tests in Rust. It is based on the V3 pact specification.

Online rust docs

To use it

To use it, add it to your dev-dependencies in your cargo manifest:

[dev-dependencies]
pact_consumer = "0.6.3"

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

use pact_consumer::prelude::*;
use pact_consumer::*;

#[test]
fn a_service_consumer_side_of_a_pact_goes_a_little_something_like_this() {

    // Define the Pact for the test (you can setup multiple interactions by chaining the given or upon_receiving calls)
    let pact_runner = ConsumerPactBuilder::consumer(s!("Consumer")) // Define the service consumer by name
        .has_pact_with(s!("Alice Service"))                         // Define the service provider that it has a pact with
        .given("there is some good mallory".to_string())            // defines a provider state. It is optional.
        .upon_receiving("a retrieve Mallory request".to_string())   // upon_receiving starts a new interaction
            .path(s!("/mallory"))                                   // define the request, a GET (default) request to '/mallory'
        .will_respond_with()                                        // define the response we want returned
            .status(200)
            .headers(hashmap!{ s!("Content-Type") => s!("text/html") })
            .body(OptionalBody::Present(s!("That is some good Mallory.")))
        .build();

    // Execute the run method to have the mock server run (the URL to the mock server will be passed in).
    // It takes a closure to execute your requests and returns a Pact VerificationResult.
    let result = pact_runner.run(&|url| {
        let client = Client { url: url.clone(), .. Client::default() }; // You would use your actual client code here
        let result = client.fetch("/mallory"); // we get our client code to execute the request
        expect!(result).to(be_ok().value("That is some good Mallory."));
        Ok(())
    });
    expect!(result).to(be_equal_to(VerificationResult::PactVerified)); // This means it is all good
}

Changing the output directory

By default, the pact files will be written to target/pacts. To change this, set the environment variable PACT_OUTPUT_DIR.

Forcing pact files to be overwritten

Pacts are merged with existing pact files when written. To change this behaviour so that the files are always overwritten, set the environment variable PACT_OVERWRITE to true.

Dependencies

~28MB
~667K SLoC