Thursday, July 11, 2019

API testing by using Fluent API HTTP client

some important imports https://hulagabal.blogspot.com/2019/07/rest-assured-pom-file-and-imports.html
Dependency to be used
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.1</version>
</dependency>
package newresrapi;
import java.io.IOException;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.restassured.path.json.JsonPath;
public class HTTPTest {
@Test
public void getFluently() throws ClientProtocolException, IOException {
// Get request
String entity = Request.Get("http://dummy.restapiexample.com/api/v1/employee/5928").execute().returnContent()
.asString();
System.out.println("---------Get started---------");
System.out.println(entity);
System.out.println(new JsonPath(entity).get("id"));
System.out.println("---------Get End---------");
}
@Test
public void putFluently() throws ClientProtocolException, IOException {
// Put request
String updateresponse = Request.Put("http://dummy.restapiexample.com/api/v1/update/5928")
.bodyString("{\"name\":\"muu222\",\"salary\":\"1123\",\"age\":\"14\"}", ContentType.APPLICATION_JSON)
.execute().returnContent().asString();
System.out.println("--------------Put started-------------");
System.out.println(updateresponse);
System.out.println("--------------Put End-------------");
}
}
view raw HTTPTest.java hosted with ❤ by GitHub

No comments:

Post a Comment