web services testing using apache http client
यह ट्यूटोरियल Apache HTTP क्लाइंट का उपयोग करके वेब सेवाओं और परीक्षण वेब सेवाओं पर विभिन्न CRUD संचालन करने के बारे में है:
इस में पूरा एपीआई परीक्षण ट्यूटोरियल श्रृंखला , हमने सीखा है कि वेब सेवा क्लाइंट और सर्वर मशीनों के बीच संचार के माध्यम के रूप में कार्य करती है जो एक नेटवर्क पर इंटरैक्ट करती है। हमने सब के बारे में समझाया एपीआई परीक्षण पोस्टमैन का उपयोग करना हमारे पिछले ट्यूटोरियल में।
इस लेख में, हम अपाचे HTTP क्लाइंट का उपयोग करके वेब सेवाओं का परीक्षण करने और वेब सेवाओं पर विभिन्न CRUD संचालन करने के तरीके पर जोर देंगे। बैकेंड परीक्षण के लिए उपलब्ध विभिन्न प्रकार के REST ग्राहकों पर भी चर्चा की जाएगी।
Android के लिए अच्छा मुफ्त एमपी 3 डाउनलोडर
आप क्या सीखेंगे:
एक वेब सेवा क्या है?
वेब सेवा क्लाइंट और सर्वर मशीनों के बीच संचार का एक माध्यम है जो HTTP प्रोटोकॉल की मदद से नेटवर्क पर इंटरैक्ट करता है। वेब सेवाएं आम तौर पर एपीआई हैं जो स्थानीय रूप से स्थापित या संग्रहीत नहीं हैं, लेकिन बादलों या कुछ दूरस्थ सर्वरों में उपलब्ध हैं।
वेब सेवा कैसे काम करती है, यह समझने के लिए नीचे दिए गए उदाहरण का संदर्भ लें।
MakeMyTrip और Goibibo.com कुछ प्रसिद्ध फ़्लाइट और होटल बुकिंग वेबसाइट हैं और इंडिगो, एयर इंडिया और इतिहाद, आदि जैसे विभिन्न फ़्लाइट वेंडर उपलब्ध हैं।
यदि कोई ग्राहक न्यूयॉर्क से लंदन के लिए फ्लाइट बुक करना चाहता है, तो वे सीधे फ्लाइट वेंडर पोर्टल पर ब्राउज़ कर सकते हैं या वे थर्ड-पार्टी विक्रेताओं के माध्यम से बुकिंग कर सकते हैं। यदि वे MakeMyTrip और अन्य बुकिंग साइटों जैसे थर्ड-पार्टी विक्रेताओं के माध्यम से बुकिंग कर रहे हैं, तो कुछ सेकंड के भीतर वे सबसे कम कीमत, उड़ान समय, और बहुत अधिक अन्य जानकारी जैसे उड़ान विवरण प्रदान करने वाले परिणामों की तुलना करेंगे और दिखाएंगे।
यहां सवाल उठता है कि कैसे, वास्तव में, सेकंड के भीतर वे हमें जानकारी प्रदान कर रहे हैं? वास्तव में वे क्या करते हैं?
UI से, वे सभी आवश्यक जानकारी लेते हैं और इसे JSON या XML फ़ाइल में संग्रहीत करते हैं और अपने विक्रेता API को प्रमाणीकरण टोकन के साथ कॉल करते हैं क्योंकि वे अपने API को उजागर करते हैं, और प्रतिक्रिया में, फ्लाइट विक्रेता की साइट MakeMTrip के लिए JSON / XML प्रतिक्रिया वापस भेजती है, और वे यूआई में प्राप्त प्रतिक्रिया और प्रदर्शन विवरण परिवर्तित करते हैं।
वेब सेवाओं के प्रकार
दो प्रकार की वेब सेवाएँ हैं, अर्थात्
- सोप एपीआई
- बाकी एपीआई
हमें इन दो वेब सेवाओं के बीच अंतर के रूप में नीचे की छवि में सूचीबद्ध देखें।
JSON / XML फ़ाइलें वेब सेवा में
S1 सिस्टम को J2EE भाषा में वापस कर दिया जाता है और S2 सिस्टम को .NET या पायथन में वापस कर दिया जाता है और हम जानते हैं कि दोनों प्रौद्योगिकियां सुरक्षा चिंता के साथ भी एक दूसरे से पूरी तरह अलग हैं। फिर S2 सिस्टम दूसरे सिस्टम के साथ कोड कैसे साझा करेगा?
तो, S2 सिस्टम, व्यापार तर्क को उजागर किए बिना S1 प्रणाली के लिए अपने APIs को उजागर करता है, और S2 प्रणाली अपने सिस्टम का उपयोग करने के लिए एपीआई नाम, एपीआई यूआरएल, एपीआई प्रारूप और प्रमाणीकरण कुंजी / टोकन कुंजी साझा करती है। JSON या XML फ़ाइलों का उपयोग करके दो प्रणालियों के बीच संचार होता है।
JSON / XML फ़ाइल ही क्यों?
JSON / XML फ़ाइलों का उपयोग किया जाता है क्योंकि ये डेटा संग्राहक हैं। किसी भी विशेष जानकारी को JSON या XML स्वरूपों में संग्रहीत किया जाता है, क्योंकि ये हल्के होते हैं और ये दो अलग-अलग इंटरफेस / प्लेटफॉर्म / सिस्टम के बीच संवाद करने के लिए मानक भाषा हैं।
इसलिए, एपीआई का उपयोग हमेशा किया जाता है जब दो स्वतंत्र सिस्टम एक दूसरे के साथ, स्थानीय स्तर पर, सिस्टम के भीतर या नेटवर्क पर बातचीत करते हैं।
बाकी ग्राहक क्या है?
REST Client एक उपकरण है जो API को कॉल करता है। APIs को कॉल करने के लिए UI नहीं होने पर बैकएंड टेस्टिंग में इसका उपयोग किया जाता है। कुछ लोकप्रिय REST क्लाइंट Apache HTTP क्लाइंट, POSTMAN, SOAP UI, स्वैगर और कई अन्य हैं।
यहां, इस लेख में, हम केवल Apache HTTP क्लाइंट पर चर्चा करेंगे और हम अपने भविष्य के लेखों में अन्य विभिन्न HTTP क्लाइंट को कवर करेंगे।
ग्रहण में क्लाइंट सेटअप
# 1) ग्रहण खोलें और एक नया मैवेन प्रोजेक्ट बनाएं।
#दो) MAVEN द्वारा प्रदान किए गए डमी पैकेज हटाएं अर्थात् 'Src / main / java' तथा 'Src / test / java'
# 3) Pom.xml फ़ाइल पर जाएं और JUnit निर्भरता को हटा दें क्योंकि हमें इसकी आवश्यकता नहीं है।
# 4) फिर, हमें HTTP क्लाइंट लाइब्रेरी, HTTP कोर लाइब्रेरी, JSON Parser लाइब्रेरी, TestNG लाइब्रेरी, जैक्सन-डेटा बाइंड लाइब्रेरी की आवश्यकता है
# 5) Pom.xml फ़ाइल में उपरोक्त निर्भरता (लाइब्रेरी) जोड़ें।
HTTP क्लाइंट लाइब्रेरी:
HTTP कोर लाइब्रेरी:
JSON पार्सर पुस्तकालय:
TestNG लाइब्रेरी:
# 6) नवीनतम और स्थिर संस्करण डाउनलोड करें। हम अपनी परियोजना में सेलेनियम जार नहीं जोड़ेंगे, क्योंकि हम पूर्ण बैकएंड परीक्षण कर रहे हैं। अंतिम pom.xml फ़ाइल नीचे दी गई छवि की तरह दिखाई देती है:
# 7) अगला, अपने एपीआई परीक्षण के लिए एक रूपरेखा बनाएं
सेवा मेरे) एक पैकेज बनाएं 'com.qa.config' -> एक फ़ाइल 'config.properties' बनाएँ और सभी URL को संग्रहीत करें।
बी) एक और पैकेज बनाएँ 'qa.com.base' -> एक क्लास बनाएं “testBase.java” जो सभी कक्षाओं के लिए एक मूल वर्ग होगा। इसमें सभी विधियों द्वारा उपयोग किए जाने वाले सामान्य कार्य शामिल हैं।
सी) एक और पैकेज बनाएँ 'com.qa.client' और वर्ग 'restClient.java'। इसमें GET, POST, DELETE, PUT कॉल लाने के लिए कोड है।
घ) एक और पैकेज बनाएँ 'com.qa.data' और वर्ग 'user.java' जो विभिन्न उपयोगकर्ता गुणों को परिभाषित करता है।
है) अंत में, 'src / test / java' के तहत एक पैकेज 'com.qa.Test' बनाएं और सभी GET, PUT, POST और Delete विधियों का परीक्षण करने के लिए एक मुख्य विधि और फ़ंक्शन की घोषणा करें।
च) अंतिम रूपरेखा संरचना नीचे दिखाए गए अनुसार दिखाई देगी:
जी) इस साइट द्वारा प्रदान की गई डमी एपीआई का उपयोग करें आरईक्यू आरईएस ।
HTTP मेथड्स या CRUD ऑपरेशंस
आइए हम विभिन्न HTTP तरीकों या सीआरयूडी कार्यों को देखते हैं जिन्हें हम स्वचालित करते हैं।
नीचे सूचीबद्ध कार्यों को CRUD संचालन कहा जाता है:
- सी : बनाएं (मतलब POST कॉल)
- आर : पुनः प्राप्त (मतलब जीईटी कॉल)
- यू : अपडेट (मतलब PUT कॉल)
- घ : डिलीट (मतलब डिलीट कॉल)
अन्य वेब सेवा में पैरामीटर
बाकी वेब सेवाओं में नीचे दिए गए मापदंडों को मान्य या जोर दें:
(i) यूआरआई: URI URL + Path पैरामीटर और क्वेरी पैरामीटर का संयोजन है।
उदाहरण: http://api.com/service/account/1
यहाँ, आपी। com S2 सर्वर के लिए URL है, सर्विस धारक है। इस सेवा में, धारक जाता है लेखा वर्ग, और इस खाता वर्ग से, यह विधि खाता = १ कहलाता है। प्रत्येक कॉल में हम URI पास करते हैं।
(ii) पेलोड: JSON / XML डेटा जिसे हम सिस्टम को फीड करते हैं।
(iii) स्थिति कोड: प्रत्येक प्रतिक्रिया के लिए, हमें स्थिति कोड मिलते हैं।
यहाँ, कुछ कोड नीचे सूचीबद्ध हैं:
- 200: ठीक है, सब कुछ ठीक काम कर रहा है।
- 201: जब भी आप POST कॉल कर रहे हैं या एक नई इकाई बना रहे हैं, सफलतापूर्वक बनाया गया।
- 400: पेलोड गलत है, अंतिम URL गलत है, खराब अनुरोध दिखाता है।
- 404: किसी इकाई को अपडेट या डिलीट करें और वह इकाई उपलब्ध नहीं है, तो हमें रिजल्ट नहीं मिला है।
- 500: मान लीजिए कि S2 सर्वर डाउन है, तो हमें आंतरिक सर्वर त्रुटि मिलती है।
- 401: प्रमाणीकरण त्रुटि
क्लिक यहाँ सभी स्थिति कोड प्राप्त करने के लिए।
(iv) शीर्षलेख: जैसे ऑथेंटिकेशन टोकन, यूजर आईडी / पासवर्ड, कंटेंट-टाइप आदि।
2 साल के अनुभव के लिए php साक्षात्कार प्रश्न और उत्तर
अपाचे HTTP क्लाइंट का उपयोग कर CRUD संचालन
# 1) कॉल प्राप्त करें
GET कॉल ऑपरेशन कैसे व्यवहार करता है?
गेट कॉल अनुरोध भेजता है और प्रतिक्रिया वापस प्राप्त करता है। हम यहां किसी JSON या पेलोड को पास नहीं करते हैं, हम एक यूआरआई पास करते हैं, जिसमें URL (समापन बिंदु पथ पैरामीटर, क्वेरी पैरामीटर) हेडर के साथ, यदि उपलब्ध हो तो।
विंडोज 8.1 के लिए मुफ्त बैकअप सॉफ्टवेयर
GET कॉल कोड लिखने से पहले, नीचे दी गई बातों को ध्यान में रखें:
- एक GET विधि की आवश्यकता है
- फिर एक URL की आवश्यकता है
- जैसे ही आप सेंड बटन को हिट करते हैं, आपको प्रतिक्रिया मिल जाएगी। फिर प्रतिक्रिया स्टोर करें।
- स्थिति कोड, हेडर की आवश्यकता है।
POSTMAN क्लाइंट के नीचे दिए गए स्क्रीनशॉट को देखें जो GET कॉल प्रतिक्रिया प्रदर्शित करता है:
RestClient.java वर्ग में,
(मैं) GET मेथड बनाएँ जो URL को कॉल करेगा और हेडर के बिना JSON ऑब्जेक्ट के रूप में प्रतिक्रिया प्राप्त करेगा।
package com.qa.Client; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import org.apache.http.Header; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; public class restClient { // create GET Method, which will call the URL and get the response in the form of JSON object without Header public CloseableHttpResponse getMethod(String Url) throws ClientProtocolException, IOException, JSONException{ /*Call HTTPClients class from HTTPClient library added in POM.xml. Call createDefault() method present in HTTPClients class, which will create a client connection. And this createDefault() method returns 'CloseableHttpClient' object which is an abstract class. And we are creating a reference to that abstract class */ CloseableHttpClient httpClient=HttpClients.createDefault(); /*create an object for HttpGet class, which is used for HTTP GET Request. And pass the URL which is to be loaded*/ HttpGet htttpGet = new HttpGet(Url); /*execute the HTTP GET Request, means it will hit the GET API call as we click SEND button from POSTMAN client. httpClient.execute() method returns the response 'CloseableHttpResponse' interface and store it in reference variable So the complete response is stored in CloseableHttpResponse And fetch all the details, in our test case/test method */ CloseableHttpResponse closeableHttpResponse = httpClient.execute(htttpGet); return closeableHttpResponse; } }
(ii) 'Src / test / java' के तहत मुख्य वर्ग 'getAPITest.java' बनाएं
उत्पादन
# 2) पोस्ट कॉल
POST Call एक खाता बनाता है या एक नई इकाई बनाता है।
उदाहरण - नाम, नौकरी और हेडर जैसे इन विवरणों को JSON पेलोड में पास करें। S2 सर्वर को Oracle कहे जाने वाले कुछ डेटाबेस से जोड़ा जाएगा और कुछ टेबल का नाम अकाउंट टेबल होगा। POST विधि डेटाबेस में एक प्रविष्टि बनाएगी और S2 सर्वर S1 क्लाइंट को सूचना पास करेगा। याद रखें कि, POST कॉल ऑपरेशन हमेशा एक नई इकाई बनाने के लिए उपयोग किया जाता है।
POST विधि में, हमें URL और पेलोड को पास करना होगा।
इस निर्भरता को डाउनलोड करें क्योंकि हमें जावा क्लास को जावा ऑब्जेक्ट से JSON ऑब्जेक्ट में बदलने की आवश्यकता है।
RestClient.java वर्ग में,
(मैं) POST Method बनाएँ, जो URL को कॉल करेगा और प्रतिक्रिया पोस्ट करेगा।
public class restClient { public CloseableHttpResponse POST(String url,String entityString,HashMap headermap) throwsClientProtocolException, IOException{ /*Call HTTPClients class from HTTPClient library added in POM.xml and createDefault() method present in HTTPClients class, which will create a client connection and this createDefault() method returns 'CloseableHttpClient' object which is an abstract class and we are creating reference to that abstract class */ CloseableHttpClient httpClient=HttpClients.createDefault(); /*create an object for HttpPost class, which is used for HTTP POST Request and pass the URL which is to be loaded */ HttpPost htttpPost = new HttpPost(url); /*define pay load, use setEnity method present in HTTPPOST class and pass the payload entity */ htttpPost.setEntity(new StringEntity(entityString)); //Create a for loop and iterate the hashmap, and store the header for(Map.Entry entry : headermap.entrySet()){ htttpPost.addHeader(entry.getKey(),entry.getValue()); } //Execute the POST request CloseableHttpResponse closeableHttpResponse = httpClient.execute(htttpPost); return closeableHttpResponse; } }
(ii) 'Src / test / java' के तहत मुख्य वर्ग 'postAPI_Test.java' बनाएँ।
//Inherit testBase class public class postAPI_Test extends testBase { //Create global methods testBase testbase; String serviceURL; String apiURL; String URL; restClient restClient; HttpResponse closeableHttpResponse; // In before method call the properties file @BeforeMethod public void setUp() throws ClientProtocolException, IOException, JSONException{ //call the constructor of base class and execute the properties file testbase = new testBase(); serviceURL = prop.getProperty('URL'); apiURL = prop.getProperty('serviceURL'); URL = serviceURL+apiURL; } //Main method which calls the GET method @Test public void POSTAPITest() throws ClientProtocolException, IOException, JSONException{ restClient = new restClient(); //Pass the Request Header HashMap headrMap = new HashMap(); headrMap.put('Content-Type', 'application/json'); /*Use Jackson API for doing marshaling, means converting java to java object to JSON Object and vice versa Use ObjectMapper class */ ObjectMapper mapper = new ObjectMapper(); //Create object of Users class, expected users users user = new users('John','Manager'); //Convert java object 'user' to JSON Object using writeValue(), pass the path where to store the JSON file and the object to be converted */ mapper.writeValue(new File('.\data\users.json'), user ); //convert java object to json in string String userJsonString = mapper.writeValueAsString(user); System.out.println(userJsonString); //Call the POST Method closeableHttpResponse = restClient.POST(URL, userJsonString, headrMap); //Fetches status, header, and JSON response from CloseableHttpResponse //1.Fetch Status Code int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();//Get the Status code System.out.println('Status Code --->' +statusCode); //Validate the status code using Assert class Assert.assertEquals(statusCode, response_Status_Code_201,'Status is not 200'); /*2.Fetch the JSON String use EntityUtils class and call to String method, where we have to pass entity and format entity is available in closeableHttpResponse and pass UTF-8 format because we want pure string so complete JSON will be stored in a String, so we need to convert an entire string into a JSON object */ String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(), 'UTF-8'); /*as we added one JSON library, from that library call JSON class and pass the response string. So this JSON object converts the string into JSON */ JSONObject responseJson = new JSONObject(responseString); System.out.println('Response JSONfrom API --->'+responseJson); /*Convert JSON to java actual User Object we are getting */ users userResObj = mapper.readValue(responseString, users.class); Assert.assertTrue(user.getName().equals(userResObj.getName())); Assert.assertTrue(user.getJob().equals(userResObj.getJob()));} }
आउटपुट:
# 3) PUT कॉल करें
PUT कॉल ऑपरेशन का उपयोग करके, आप एक नई इकाई बना सकते हैं और एक मौजूदा इकाई को अपडेट कर सकते हैं।
RestClient.java वर्ग में,
(मैं) PUT Method बनाएँ, जो URL को कॉल करेगा और प्रतिक्रिया को अपडेट करेगा।
public class restClient { public CloseableHttpResponse PUT(String url,String entityString,HashMap headermap) throwsClientProtocolException, IOException{ /*Call HTTPClients class from HTTPClient library added in POM.xml. Call createDefault() method present in HTTPClients class, which will create a client connection and this createDefault() method returns 'CloseableHttpClient' object which is an abstract class and we are creating reference to that abstract class*/ CloseableHttpClient httpClient=HttpClients.createDefault(); /*create an object for HttpPut class, which is used for HTTP PUT Request and pass the URL which is to be loaded */ HttpPut htttpPut = new HttpPut(url); /*define pay load, use setEnity method present in HTTPPUT class and pass the payload entity */ htttpPut.setEntity(new StringEntity(entityString)); /*create a for loop, iterate and store the header */ for(Map.Entry entry : headermap.entrySet()){ htttpPut.addHeader(entry.getKey(),entry.getValue()); } //Execute the PUT request CloseableHttpResponse closeableHttpResponse = httpClient.execute(htttpPut); return closeableHttpResponse; } }
(ii) 'Src / test / java' के तहत मुख्य वर्ग 'putAPI_Test.java' बनाएं
//Inherit testBase class public class putAPI_Test extends testBase { //Create global methods testBase testbase; String serviceURL; String apiURL; String URL; restClient restClient; HttpResponse closeableHttpResponse; // in before method call the properties file @BeforeMethod public void setUp() throws ClientProtocolException, IOException, JSONException{ //Call the constructor of the base class and execute the properties file testbase = new testBase(); serviceURL = prop.getProperty('URL'); apiURL = prop.getProperty('serviceURL'); URL = serviceURL+apiURL; } //Main method which calls PUT method @Test public void PUTAPITest() throws ClientProtocolException, IOException, JSONException{ restClient = new restClient(); //Pass the Request Header HashMap headrMap = new HashMap(); headrMap.put('Content-Type', 'application/json'); /*use Jackson API, for doing marshaling means converting java to java object to JSON Object and vice versa, use ObjectMapper class */ ObjectMapper mapper = new ObjectMapper(); //Create object of Users class, new users users user = new users('JohnMarry Dicosta','HRManager'); /*Convert java object 'user' to JASON Object using writeValue() and pass the path where to store the JSON file and the object to be converted */ mapper.writeValue(new File('.\data\users.json'), user ); //convert java object - > JSON - >String String userJsonString = mapper.writeValueAsString(user); System.out.println(userJsonString); //Call the PUT Method closeableHttpResponse = restClient.PUT(URL, userJsonString, headrMap); /*fetch status, header, JSON response from CloseableHttpResponse Fetch Status Code */ int statusCode = closeableHttpResponse.getStatusLine().getStatusCode(); System.out.println('Status Code --->' +statusCode); //Validate the status code using Assert class Assert.assertEquals(statusCode, response_Status_Code_200,'Status is 200'); /*2.Fetch the JSON String, use EntityUtils class and call to String method where we have to pass entity and format entity is available in closeableHttpResponse and pass UTF-8 format because we want a pure string. So complete JSON will be stored in a String, so we need to convert an entire string into a JSON object */ String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(), 'UTF-8'); /* From JSON library, call JSON class and pass the response string. This JSON object converts the string to JSON */ JSONObject responseJson = new JSONObject(responseString); System.out.println('Response JSONfrom API --->'+responseJson);}
उत्पादन
# 4) कॉल हटाएं
डिलीट कॉल ऑपरेशन सरल है, यानी खाता आईडी- 100 हटाएं और जानकारी को JSON फ़ाइल में पास करें।
RestClient.java वर्ग में,
(मैं) डिलीट मेथड बनाएँ, जो URL को कॉल करेगा और रिकॉर्ड को डिलीट करेगा।
public CloseableHttpResponse Delete(String url) throws ClientProtocolException, IOException{ /*Call HTTPClients class from HTTPClient library added in POM.xml and createDefault() method present in HTTPClients class, which will create a client connection and this createDefault() method returns 'CloseableHttpClient' object which is an abstract class and we are creating reference to that abstract class */ CloseableHttpClient httpClient=HttpClients.createDefault(); /*create an object for HttpDelete class, which is used for HTTP Delete Request, and pass the URL to be loaded*/ HttpDelete htttpDelete = new HttpDelete(url); //execute Delete request CloseableHttpResponse closeableHttpResponse =httpClient.execute(htttpDelete); return closeableHttpResponse; }
(ii) 'src / test / java' के तहत मुख्य वर्ग 'deleteAPI_Test.java' बनाएँ।
public class deleteAPI_Test extends testBase { //Create global methods testBase testbase; String serviceURL; String deleteuserUrl; String URL; restClient restClient; HttpResponse closeableHttpResponse; // In before method call the properties file @BeforeMethod public void setUp() throws ClientProtocolException, IOException, JSONException{ //call the constructor of the base class and execute the properties file testbase = new testBase(); serviceURL = prop.getProperty('URL'); deleteuserUrl = prop.getProperty('deleteuser'); URL = serviceURL+deleteuserUrl; } //The Main method which calls the Delete method @Test public void deleteAPI() throws ClientProtocolException, IOException, JSONException{ restClient = new restClient(); //Method returns closeableHttpResponse type closeableHttpResponse = restClient.Delete(URL); /*fetch status code, header, JSON response from CloseableHttpResponse -Fetch Status Code */ int statusCode = closeableHttpResponse.getStatusLine().getStatusCode(); System.out.println('Status Code --->' +statusCode); //Validate the status code using Assert class Assert.assertEquals(statusCode, response_Status_Code_204,'Status is 204 No Content'); }
उत्पादन
किसी भी प्रतिक्रिया को सत्यापित करने से पहले डेवलपर से सही URL प्राप्त करें, फिर जांचें कि क्या आपको सर्वर से अपेक्षित प्रतिक्रिया मिल रही है, प्रत्येक परिदृश्य के लिए परीक्षण के मामले तैयार करें और अनुक्रम w.r.t कार्यक्षमता में परीक्षण मामलों की व्यवस्था करें।
निष्कर्ष
इस लेख में, हमने पोस्ट उदाहरणों के साथ पोस्ट, पोस्ट, जीईटी और डिलीट कॉल्स को स्वचालित करने के लिए अपाचे HTTP क्लाइंट का उपयोग करने के तरीके को कवर किया है। हमने वेब सेवाओं के प्रकारों और JSON / XML फ़ाइलों के महत्व पर चर्चा की है और उनका उपयोग क्यों किया जाता है।
PREV ट्यूटोरियल | सबसे पहले ट्यूटोरियल
अनुशंसित पाठ
- वेब सेवा ट्यूटोरियल: घटक, वास्तुकला, प्रकार और उदाहरण
- 15+ साबुन ट्यूटोरियल: सर्वश्रेष्ठ वेब सेवा एपीआई परीक्षण उपकरण
- Amazon Web Services (AWS) साक्षात्कार प्रश्न और उत्तर
- शीर्ष 20 प्रतिष्ठित वेब सेवा साक्षात्कार प्रश्न और उत्तर
- शीर्ष 25 जावा वेब सेवाएँ साक्षात्कार प्रश्न और उत्तर
- शीर्ष 45 वेब सेवाएँ साक्षात्कार प्रश्न और उत्तर (प्रतिष्ठित, SOAP, सुरक्षा प्रश्न)
- लोडरनर VuGen स्क्रिप्टिंग का उपयोग करके वेब सेवा प्रदर्शन परीक्षण
- एपीआई परीक्षण ट्यूटोरियल: शुरुआती के लिए एक पूर्ण गाइड