web analytics
Google + RSS Feed

Web Services Testing

0

May 9, 2017 by admin

Web Services Testing

Download – HTTP Client  from below link and extract the zip file and add all jars into project

http://hc.apache.org/downloads.cgi

Add

JSON Jar to read json format.

 

Web Services Testing

 

Before moving to test Web Services Testing, please go through the basics of Web Service.

Please check below article from W3School

Intro to WebServices

We will be testing Web Services in two ways

1- Using JSON File.

2-Using XML File.

 

How to verify HTTP Response for Web Service Testing

First we will verify HTTP response using response code below

Status codes

Most status codes which frequently used

200- Ok

404- Page not found

401- Unauthorized

 

Below is the piece of code which will verify HTTP Response

 

How to verify the response Content type for Web Services testing

public static void testMimeType(String restURL, String expectedMimeType) throws ClientProtocolException, IOException {

 

How to extract response from XML File for Web Services Testing

 

How to extract response from JSON File for Web Services Testing

 

Full program for Web Services Testing

 

import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class RESTTester {

public static void main (String args[]) {

// This is request which we are sending to server
String restURL_XML = “http://parabank.parasoft.com/parabank/services/bank/customers/12212/”;

// sending request and we are passing parameter in url itself
String restURL_JSON = “http://api.openweathermap.org/data/2.5/weather?q=Amsterdam”;

try {

testStatusCode(restURL_XML);
testStatusCode(restURL_JSON);
testMimeType(restURL_XML,”application/xml”);
testMimeType(restURL_JSON,”application/json”);
testContent(restURL_XML,”lastName”,”Smith”);
testContentJSON(restURL_JSON,”name”,”Amsterdam”);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void testStatusCode(String restURL) throws ClientProtocolException, IOException {

HttpUriRequest request = new HttpGet(restURL);
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);

Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(),HttpStatus.SC_OK);
}

public static void testMimeType(String restURL, String expectedMimeType) throws ClientProtocolException, IOException {

HttpUriRequest request = new HttpGet(restURL);
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);

Assert.assertEquals(expectedMimeType,ContentType.getOrDefault(httpResponse.getEntity()).getMimeType());
}

public static void testContent(String restURL, String element, String expectedValue) throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(restURL);
NodeList nodelist = doc.getElementsByTagName(element);

Assert.assertEquals(expectedValue,nodelist.item(0).getTextContent());
}

public static void testContentJSON(String restURL, String element, String expectedValue) throws ClientProtocolException, IOException, SAXException, ParserConfigurationException, JSONException {

HttpUriRequest request = new HttpGet(restURL);
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);

// Convert the response to a String format
String result = EntityUtils.toString(httpResponse.getEntity());

// Convert the result as a String to a JSON object
JSONObject jo = new JSONObject(result);

Assert.assertEquals(expectedValue, jo.getString(element));

}

}

If you find this useful then please share with friends. Comment below if any doubt, suggestion or feedback.


0 comments »

Leave a Reply

Your email address will not be published. Required fields are marked *


two + 1 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts