1.0.1 • Published 9 months ago

contract-proxy v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

ContractTesting

ContractTesting is a framework designed to streamline contract testing in a microservices architecture. In distributed systems, where multiple services communicate, it’s critical to ensure consistent and predictable interactions as services evolve independently. This framework helps validate that the communication between services remains stable, ensuring that the consumer-provider agreements (contracts) are upheld.

Features

  • Microservices Contract Validation: Ensure services continue to communicate according to agreed-upon contracts (e.g., API specifications).
  • OpenAPI Contract Testing: Validate service interactions using OpenAPI specifications to ensure adherence to the expected data formats and communication protocols.
  • Prism Framework Integration: Leverage Prism, a powerful tool for contract testing, to simulate and validate provider responses against consumer expectations.

Why Contract Testing?

In a microservices architecture, independent service evolution can lead to broken integrations if service interfaces change unexpectedly. Contract testing mitigates this by ensuring:

  • Services (providers) adhere to contracts defined by the consumer.
  • Early detection of breaking changes in services.
  • Increased confidence in the reliability of your system’s inter-service communication.

How It Works

  1. Consumer-Driven Contracts: The consumer service defines the expectations in the form of contracts (API specifications), such as an OpenAPI document.
  2. Provider Validation: Using the Prism framework, the provider’s responses are validated against the predefined contract.
  3. Test Automation: Integrate into CI/CD pipelines for continuous contract verification and instant feedback on breaking changes.

Getting Started

  1. Clone the Repository:

    git clone https://github.com/dipjyotimetia/ContractTesting.git
    cd ContractTesting
  2. Install Dependencies: Ensure you have Node.js installed, then run:

    npm install
  3. Run Contract Tests: You can start running your contract tests with:

    npm run test:contract
  4. Prism Integration: Prism can be used to mock and validate API interactions. Follow the Prism documentation for advanced setup and usage.

Example Use Case

For a consumer service that expects a specific API response format from a provider, this framework will:

  • Define the API contract using OpenAPI.
  • Run contract tests using Prism to ensure the provider complies with the API contract.
  • Provide immediate feedback on any contract violations.

CI/CD Integration

Add contract tests to your CI/CD pipelines to continuously verify that your microservices' interactions remain valid as services evolve:

# Example GitHub Actions workflow
name: Contract Testing
on:
  push:
    branches:
      - main
jobs:
  contract-test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Install dependencies
        run: npm install
      - name: Run contract tests
        run: npm run test:contract