API Testing using Apache HttpClient | Code Example

The Hyper-Text Transfer Protocol (HTTP) is perhaps the most significant protocol used on the Internet today. Web services, network-enabled appliances and the growth of network computing continue to expand the role of the HTTP protocol beyond user-driven web browsers, while increasing the number of applications that require HTTP support.

Although the java.net package provides basic functionality for accessing resources via HTTP, it doesn’t provide the full flexibility or functionality needed by many applications. HttpClient seeks to fill this void by providing an efficient, up-to-date, and feature-rich package implementing the client side of the most recent HTTP standards and recommendations.

Designed for extension while providing robust support for the base HTTP protocol, HttpClient may be of interest to anyone building HTTP-aware client applications such as web browsers, web service clients, or systems that leverage or extend the HTTP protocol for distributed communication.

Below are some Example of API Testing Using The Apache HTTPClient.

Code Example 1 | GET 

package io.fixer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Assert;

public class CheckResponseDates {

	public static void main(String[] args) throws ClientProtocolException, IOException {

		String url = "http://api.fixer.io/2000-01-03";
		
		HttpClient client = HttpClientBuilder.create().build();
		HttpGet request = new HttpGet(url);
		
		HttpResponse response = client.execute(request);
		int actualResponseCode = response.getStatusLine().getStatusCode();
		int expectedResponseCode = 200;
		
		Assert.assertEquals(expectedResponseCode, actualResponseCode);
		
		BufferedReader rd = new BufferedReader
				(new InputStreamReader(response.getEntity().getContent()));
		
		StringBuffer result = new StringBuffer();
		String line = "";
		while((line = rd.readLine()) !=null)
			result.append(line);
		System.out.println("Response Payload is " + result);
	}

}
 

Code Example 2 | GET 

package io.fixer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Assert;

public class CheckResponseDatesIncorrect {

	public static void main(String[] args) throws ClientProtocolException, IOException {

		String url = "http://api.fixer.io/2000-01-0";
		
		HttpClient client = HttpClientBuilder.create().build();
		HttpGet request = new HttpGet(url);
		
		HttpResponse response = client.execute(request);
		int actualResponseCode = response.getStatusLine().getStatusCode();
		int expectedResponseCode = 404;
		
		Assert.assertEquals(expectedResponseCode, actualResponseCode);
		
		BufferedReader rd = new BufferedReader
				(new InputStreamReader(response.getEntity().getContent()));
		
		StringBuffer result = new StringBuffer();
		String line = "";
		while((line = rd.readLine()) !=null)
			result.append(line);
		System.out.println("Response Payload is " + result);
	}
}

This is a open-source international community to improve software testing practices, send your post here

Advertisements

Leave a Reply