OpenAPI mock server: specification से client development शुरू करें
API mock server एक test HTTP server है जो असली business service की जगह अस्थायी रूप से काम करता है। OpenAPI विवरण और उदाहरणों से routes और responses देकर यह backend तैयार होने से पहले client development शुरू करने और contract की कमियाँ जल्दी पहचानने में मदद करता है। Mock response से data persistence, authorization, transactions या असली service behavior साबित नहीं होता।
API mock server एक test HTTP server है जो असली business service की जगह अस्थायी रूप से काम करता है। OpenAPI विवरण और उदाहरणों से routes और responses देकर यह backend तैयार होने से पहले client development शुरू करने और contract की कमियाँ जल्दी पहचानने में मदद करता है। Mock response से data persistence, authorization, transactions या असली service behavior साबित नहीं होता।
क्या जाँचा जा सकता है?
Specification-आधारित mock API लागू होने से पहले client developers को request formats और response models दिखाता है। Stoplight Prism HTTP mock देने के लिए OpenAPI operations, request validation rules और response examples का उपयोग करता है। Validation proxy अलग सुविधा है, जो चल रहे implementation को जाँचती है।
| प्रश्न | Mock की भूमिका | आगे क्या जाँचना होगा |
|---|---|---|
| क्या client के लिए route मौजूद है? | विवरण में दिए path और method पर जवाब | वास्तविक server routing और deployment |
| क्या request contract के अनुरूप है? | विवरण के नियमों के अनुसार validation | Production code का validation और authorization |
| क्या client response model संभाल सकता है? | Example या schema से नकली data देता है | Implementation का contract से मेल |
| क्या error paths विकसित किए जा सकते हैं? | दस्तावेज़ित error status/example चुनता है | असली server में समान conditions और body हैं या नहीं |
Mock और sandbox में अंतर रखें। Mock आम तौर पर विवरण से response बनाता है और state नहीं रखता। Sandbox में वास्तविक implementation का कुछ हिस्सा, test data या accounts हो सकते हैं; सुविधाएँ और side effects अलग-अलग होते हैं। वास्तविक implementation से request/response अंतर जाँचने के लिए Prism जैसे validation proxy से traffic की तुलना करें।
अच्छे examples से बेहतर mock बनते हैं
परिणाम विवरण की गुणवत्ता पर निर्भर करता है। Prism के default static mode में स्पष्ट response example को प्राथमिकता मिलती है। Example न हो तो schema के default, examples, nullable और format के आधार पर values बनती हैं। Example के बिना string के लिए "string" जैसा placeholder आ सकता है; इससे type दिखता है, वास्तविक business data नहीं। उपयोगी UI के लिए success, empty result और error के प्रतिनिधि examples जोड़ें।
Static response बार-बार की जाँच को predictable बनाते हैं। Dynamic response schema के आधार पर values बदलते हैं और अलग आकार या मानों पर client behavior जाँच सकते हैं, लेकिन हर बार एक ही परिणाम चाहने वाले test के लिए उपयुक्त नहीं हो सकते। उद्देश्य के अनुसार mode चुनें; किसी खास example की जाँच में status और example दोनों चुनें।
Local mock चलाएँ
कमांड Prism OSS CLI 3.x दस्तावेज़ के अनुसार हैं। Repository README में Node.js 18.20.1 या बाद का version आवश्यक बताया गया है; install से पहले अपने Prism release की शर्तें जाँचें। ये दस्तावेज़ित उदाहरण हैं, इस लेख के लिए चलाए नहीं गए।
OpenAPI फ़ाइल तैयार करें और client के paths/methods, request schema, response code और examples जाँचें।
आधिकारिक installation guide के अनुसार @stoplight/prism-cli install करें और project directory से mock शुरू करें:
prism mock openapi.yamlPrism default रूप से http://127.0.0.1:4010 पर सुनता है और उपलब्ध operations दिखाता है।
curl या client का base URL local server पर रखें। Status, content type और body देखें। Response स्थानीय विवरण और examples से बनता है।
Error examples जोड़ें और दस्तावेज़ित selection options से client error handling जाँचें। Prism, status और example चुनने के लिए Prefer header विकल्प बताता है।
Docker में default localhost binding से container के बाहर access रुक सकता है। Container-to-container access आवश्यक हो तभी documented host option देखें और केवल जरूरी interface expose करें।
अपेक्षित परिणाम: client विवरण में घोषित request भेजता है और configured example या schema response संभालता है। 404 मिले तो Prism के route list की तुलना OpenAPI paths, servers और base path से करें। बदलते responses जाँच को अस्थिर करें तो static mode और स्पष्ट examples उपयोग करें।
असली API तैयार होने पर
सही mock यह सिद्ध नहीं करता कि provider contract का पालन करता है। API लागू होने पर base URL को वास्तविक development server पर बदलें और जरूरत हो तो validation proxy से requests/responses को विवरण के साथ तुलना करें। अंतर मिले तो पहले पुष्टि करें कि विवरण इच्छित contract है; फिर विवरण और provider समायोजित कर client checks दोहराएँ।
शुरू करने से पहले
क्या mock tool OpenAPI version और जरूरी features का समर्थन करता है?
क्या विवरण में success, error और empty-result examples हैं?
क्या client team जानती है कि values नकली हैं और state नहीं रखी जाती?
क्या local host, port और Docker port exposure जाँचे?
क्या वास्तविक API के साथ बाद की contract जाँच तय है?
स्रोत
[Prism: HTTP mocking और static/dynamic response generation](https://github.com/stoplightio/prism/blob/main/docs/guides/01-mocking.md)
[Prism: CLI, response examples और error selection](https://github.com/stoplightio/prism/blob/main/docs/getting-started/03-cli.md)
[Prism: वास्तविक API और OpenAPI तुलना के लिए validation proxy](https://github.com/stoplightio/prism/blob/main/docs/guides/03-validation-proxy.md)
[Prism: समर्थित specifications और installation/local use](https://github.com/stoplightio/prism)