Crate pact_consumer [] [src]

The pact_consumer crate provides the test DSL for writing consumer pact tests. It implements the V1.1 Pact specification (https://github.com/pact-foundation/pact-specification/tree/version-1.1).

To use it

To use it, add it to your dev-dependencies in your cargo manifest and add an extern crate definition for it.

[dev-dependencies]
pact_consumer = "0.1.0"Run

Then in your main module

#[cfg(test)]
#[macro_use]
extern crate pact_consumer;Run

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

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)
    // Define the service consumer by name
    let pact_runner = ConsumerPactBuilder::consumer(s!("Consumer"))
        // Define the service provider that it has a pact with
        .has_pact_with(s!("Alice Service"))
        // defines a provider state. It is optional.
        .given("there is some good mallory".to_string())
        // upon_receiving starts a new interaction
        .upon_receiving("a retrieve Mallory request".to_string())
            // define the request, a GET (default) request to '/mallory'
            .path(s!("/mallory"))
        // define the response we want returned
        .will_respond_with()
            .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| {
        // You would use your actual client code here
        let client = Client { url: url.clone(), .. Client::default() };
        // we get our client code to execute the request
        let result = client.fetch("/mallory");
        expect!(result).to(be_ok().value("That is some good Mallory."));
        Ok(())
    });
    // This means it is all good
    expect!(result).to(be_equal_to(VerificationResult::PactVerified));
}Run

Structs

ConsumerPactBuilder

Struct to setup the consumer pact test expectations

ConsumerPactRunner

Runner for a consumer pact test

Enums

VerificationResult

Result of running the pact test