QA
Intermediate LevelREST APIs & Web Services

What is Consumer-Driven Contract Testing (Pact) for REST microservices?

❓ Interview Prompt: Explain Consumer-Driven Contract Testing, Pact framework, verifying API contracts without running full microservice clusters, and CI/CD pipeline integration.

Consumer-Driven Contract Testing (Pact) verifies that microservices can communicate without requiring expensive, brittle end-to-end integration test environments:

How Pact Works:

1
Consumer Defines Contract: The frontend / API consumer defines expected HTTP requests and responses in a Pact file.
2
Provider Verifies Contract: The backend provider runs the Pact contract file against its actual endpoints in CI/CD, guaranteeing backend edits never break consumer assumptions!

Code Implementation Example

typescript
import { PactV3, MatchersV3 } from '@pact-foundation/pact';

const provider = new PactV3({ consumer: 'FrontendApp', provider: 'UserService' });

provider.addInteraction({
  states: [{ description: 'User 42 exists' }],
  uponReceiving: 'A request for user 42',
  withRequest: { method: 'GET', path: '/api/v1/users/42' },
  willRespondWith: {
    status: 200,
    body: { id: '42', name: MatchersV3.string('Alice') }
  }
});
💡Defining consumer contract expectations using Pact framework in TypeScript.
🎯 Interview Key Takeaway

Pact Consumer-Driven Contract Testing verifies frontend and backend API expectations in CI/CD without running full microservice clusters.

#Pact#Contract Testing#Microservices#Intermediate
Back to all REST APIs & Web Services Questions→

More REST APIs & Web Services Interview Questions

Beginner LevelREST APIs & Web Services

What is HTTP Method Idempotency and how do you implement Idempotency Keys for REST APIs?

Beginner LevelREST APIs & Web Services

What are standard HTTP Status Codes (200, 201, 204, 400, 401, 403, 404, 409, 429, 500, 503)?

Beginner LevelREST APIs & Web Services

What are HTTP Verbs (GET, POST, PUT, PATCH, DELETE) and when to use PUT vs PATCH?