API contract: request-response व्यवहार पर सहमति और सत्यापन
API contract provider और consumer के बीच किसी interaction की साझा अपेक्षा है। इसमें path और payload schema से अधिक भी शामिल हो सकता है: HTTP method, status code, header और वे error responses जिन पर client निर्भर है। OpenAPI API surface का वर्णन करता है; Pact जैसे consumer-driven tests ठोस interactions दोबारा चलाकर जाँचते हैं कि provider consumer की अपेक्षाएँ पूरी करता है या नहीं। दोनों अलग प्रमाण हैं, एक-दूसरे का स्थान नहीं लेते।
API contract provider और consumer के बीच किसी interaction की साझा अपेक्षा है। इसमें path और payload schema से अधिक भी शामिल हो सकता है: HTTP method, status code, header और वे error responses जिन पर client निर्भर है। OpenAPI API surface का वर्णन करता है; Pact जैसे consumer-driven tests ठोस interactions दोबारा चलाकर जाँचते हैं कि provider consumer की अपेक्षाएँ पूरी करता है या नहीं। दोनों अलग प्रमाण हैं, एक-दूसरे का स्थान नहीं लेते।
Contract में क्या रखें?
POST /orders से order बनाने वाले client के लिए भेजा गया method/path, जरूरी authentication और content type, उपयोग किए गए request fields, success status और पढ़े गए response fields, तथा संभाले जाने वाले प्रमुख errors दर्ज करें। Duplicate order पर 409 Conflict आता है तो client उसे कैसे संभालता है, यह भी समझौते का हिस्सा हो सकता है।
Consumer contract में provider के हर validation rule या storage detail की सूची जरूरी नहीं। Pact message exchange को provider side effects—जैसे order सचमुच सही तरह store हुआ या नहीं—से अलग करता है; इसकी जाँच provider functional tests करते हैं। बहुत बारीक scenarios सुरक्षित बदलाव भी रोक सकते हैं।
Schema description और contract test अलग हैं
OpenAPI HTTP API का language-agnostic विवरण है और documentation, code generation तथा test tools में काम आता है। दस्तावेज़ होने से implementation का पालन सिद्ध नहीं होता; provider verification व्यवहार की तुलना दस्तावेज़ से कर सकता है।
Pact code-first consumer-driven तरीका है। Consumer tests वही interactions बताते हैं जिनका client वास्तव में उपयोग करता है और जाँचते हैं कि वह अपेक्षित request बनाता और response संभालता है। Pact को जरूरी provider state तैयार करके provider पर replay किया जा सकता है। यह दोनों पूरे services का end-to-end test नहीं और हर business outcome का प्रमाण भी नहीं है।
| प्रश्न | उपयोगी प्रमाण |
|---|---|
| पूरी API surface और data structure क्या है? | OpenAPI या अन्य schema/specification |
| Consumer कौन-सा request-response interaction उपयोग करता है? | Consumer interaction और provider verification |
| Request ने सही side effect किया? | Provider functional/integration test |
| Deploy हो रही version pair compatible है? | ताज़ा verification results और deployment policy |
Contract में केवल consumer की निर्भरता शामिल करें: status, headers, पढ़े गए fields और महत्वपूर्ण error handling। आकस्मिक internal details और अत्यधिक business rules को स्थिर न करें। Success के साथ महत्त्वपूर्ण errors भी जाँचें; बदलाव पर दोनों पक्ष verify करें और deployment के लिए provider verification results साझा करें।
जाँच सूची
कौन-से consumer और provider यह contract साझा करते हैं?
Method/path, authentication, content type, status और प्रयुक्त payload स्पष्ट हैं?
क्या अप्रासंगिक implementation details और बहुत कड़े validation rules छोड़े हैं?
Consumer जिन महत्त्वपूर्ण errors को संभालता है, वे शामिल हैं?
Schema conformance और interaction tests अलग रखे हैं?
वर्तमान verification deploy होने वाली versions को कवर करता है?
Side effects और business rules provider अलग से जाँचता है?
स्रोत
[OpenAPI Specification 3.1.1](https://spec.openapis.org/oas/v3.1.1.html)
[Pact: contract testing और schema से अंतर](https://docs.pact.io/)
[Pact: interactions और verification प्रक्रिया](https://docs.pact.io/getting_started/how_pact_works)
[Pact: contract और functional tests का अंतर](https://docs.pact.io/consumer/contract_tests_not_functional_tests)
[Pact: provider verification results](https://docs.pact.io/getting_started/verifying_pacts)