Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Pact C Provider Example (c/provider)

This directory serves two purposes:

  1. Example for C Providers: Demonstrates how to use the Pact FFI (Foreign Function Interface) in C to verify a provider implementation against consumer contracts.
  2. Reference for FFI Maintainers: Provides a working example for maintainers of libraries in other languages that use the Pact FFI internally, showing how to implement provider verification.

It includes utilities, example provider implementation, and contract verification code for both use cases.

Directory Structure

  • include/: Header files for the provider code
  • src/: Source files for the provider implementation:
    • main.c: Main entry point that starts the HTTP server and runs verification
    • logging.c, http_server.c: Utility implementations
    • provider/: Provider-specific code including state handlers and HTTP endpoint handlers
  • CMakeLists.txt: CMake build configuration
  • justfile: Justfile for building and running the provider verification
  • conanfile.txt: Conan configuration for dependencies

Building and Running

This project uses Conan for dependency management and CMake for configuration and building. It also provides a justfile for convenience:

just run

The results of the verification tests are printed to stdout and will exit with a non-zero status if verification fails.

It assumes that the consumer contracts have already been generated by running the consumer tests in the c/consumer directory (or calling just run in the parent directory which runs both consumer and provider).

Prerequisites

The verification tests require:

  • A C and Rust compiler (Rust compiler requires nightly toolchain)
  • CMake (version 3.24 or higher)
  • Conan (version 2.0 or higher)

The CMake configuration will automatically build the Pact FFI library from the Rust source, hence the need for the Rust toolchain. In practice, you would typically download the pre-built binaries for your platform from the Pact FFI releases, and link against those instead.

All other dependencies (libmicrohttpd for HTTP server) will be automatically downloaded and/or built by Conan as needed.

Installing Conan

If you don't have Conan installed, you can install it via uv:

uv tool install conan

For more installation options, see the Conan installation documentation.

How Provider Verification Works

Provider verification in Pact ensures that the provider implementation satisfies the contracts defined by consumers. The process:

  1. Start Provider: Launch the actual HTTP server that implements the provider API
  2. Configure Verifier: Set up the Pact verifier with provider details and contract files
  3. Setup Provider States: For each interaction, configure the provider to be in the required state (e.g., "user exists")
  4. Run Verification: The verifier replays each interaction from the contract against the running provider
  5. Report Results: Output verification results and any mismatches

This example demonstrates all these steps using the Pact FFI in C.