3 min read

APIs (Part 2)

HTTP Status Codes
HTTP Status Codes

REST API (Rest Assured) Basics

What is REST?

REST stands for Representation State Transfer. REST is a set of constraints that were put forth by Roy Fielding.
Rest Architectural style defines parts of Restful service as key conceptual elements. For e.g. while we make a Rest request to a Server, the information returned by the Server is called a Resource. Let us take a quick look at the key conceptual elements in a RESTful Web Service.

ElementExample
ResourceInformation stored on a Server, which can be requested by a client. It could be Weather info or may be employee details
Resource IdentifierNow that we have a resource defined, we need to uniquely identify the resource. That is actually the complete URL
RepresentationA resource is the actual data. Now this data can be represented as an XML, HTML or may be simple text. That is what is called a Representation
Representation MetadataIn order for the Clients to specify and process a resources given in a particular Representation (XML or HTML etc) some extra data (Metadata) needs to be passed in the request.

Resource

A resource in the Restful architecture is an asset that is available on a Server. For e.g. the weather API returns the weather information about the city specified. The returned weather data is a resource on the Server. A Resource can be

  • Temporal
  • Static

Temporal resource is one that keeps changing with time. For e.g. the Weather. A Static resource is one that stays constant over longer time durations. For e.g. a webpage containing some static text. Like the one you are reading now can just be a Static resource on the server.

Resource Identifier

Every Resource on the Server needs to be uniquely identifiable. To do that every resource has a unique URL that can be used to get to that Resource. Taking an example of it, ToolsQA has hosted a web service that can return you to the location from which Client is making a call.

You can see that to access your location you have to hit the above URL. Here the Resource is your location while the unique way to access it is the Resource Identifier (URL). Important point to note here is that a resource is not necessarily a static piece of information. For example, this URL when hit by someone from the United States will give their location while when hit from India will give some location from India. The location is calculated at run time and sent back to the Client. Even dynamic information is also called a Resource. A similar example would be the weather API exposed by ToolsQA.

Representation and Representation Metadata

Now that we have our heads cleared about a Resource and a Resource Identifier, it is time to understand what a Resource Representation means. A Representation of resources is Data plus Meta Data explaining the data. Meta Data in the strict sense of Request and Response will be some header explaining the format type of the Response body. For e.g. Resource metadata may say that the content in the Response body is in JSON format. This will inform Clients about how to interpret the data. Let us take an example of a Sample API. Let us try to see what the Request and Response headers look like for the Weather API exposed by ToolsQA.


About RestAssured

REST Assured is a Java library for testing RESTful web services. It provides a simple way to interact with HTTP-based APIs, allowing you to send HTTP requests (GET, POST, PUT, DELETE, etc.), validate the response status code, headers, and body, and specify expectations using a fluent and concise API.

To get started with REST Assured, you'll need to add the library to your project's dependencies (for example, using Maven):

<dependency>
  <groupId>io.rest-assured</groupId>
  <artifactId>rest-assured</artifactId>
  <version>4.3.1</version>
  <scope>test</scope>
</dependency>

Here's an example of how you can use REST Assured to test a simple API endpoint:

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.Before;
import org.junit.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

public class ExampleTest {

  @Before
  public void setUp() {
    RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
  }

  @Test
  public void testGetRequest() {
    Response response = 
      given().
        when().
        get("/posts/1").
        then().
        statusCode(200).
        and().
        body("id", equalTo(1)).
        extract().
        response();
  }
}

In this example, RestAssured.baseURI is set to the base URL of the API under test. The given() method is used to specify the request details, while the when() method is used to send the request. The then() method is used to validate the response, using a combination of status code checks and body validations using JUnit and Hamcrest matchers.