DEV Community

Cover image for Rest Assured E2E Testing - Part 4
TaheraFirdose
TaheraFirdose

Posted on

Rest Assured E2E Testing - Part 4

Understanding HTTPS methods, status codes and syntax

What is HTTP?

The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. This is the foundation for data communication for the World Wide Web (i.e. internet) since 1990. HTTP is a generic and stateless protocol which can be used for other purposes as well using extensions of its request methods, error codes, and headers.

In Simple, HTTP works as a request-response protocol between a client and server.

Different Types of HTTP Methods:

GET:

GET requests should only be used to retrieve resource representation/information – not to modify it in any way. Because GET requests do not alter the state of the resource, they are considered safe methods.
For any given HTTP GET API, if the resource is found on the server, then it must return HTTP response code 200 (OK) – along with the response body, which is usually either XML or JSON content (due to their platform-independent nature).

POST:

A POST request is used to create a new resource into the collection of resources.
If a resource is created on the origin server, the response SHOULD be HTTP response code 201 (Created) and include an entity that describes the status of the request and refers to the new resource, as well as a Location header.

PUT:

PUT APIs are primarily used to update existing resources (if the resource does not exist, then API may decide to create a new resource or not). If the PUT API creates a new resource, the origin server MUST notify the user agent via the HTTP response code 201 (Created), and if an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful request completion.

DELETE

DELETE APIs, as the name implies, are used to delete resources (identified by the Request-URI).
A successful response of DELETE requests SHOULD be HTTP response code 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has been queued, or 204 (No Content) if the action has been performed but the response does not include an entity.

PATCH

PATCH requests are used to perform a partial update on a resource. PUT requests also modify a resource entity, so to be clear – the PATCH method is the correct choice for partially updating an existing resource, and PUT should only be used if you are completely replacing a resource.

HTTP Response codes

The HTTP response codes are divided broadly in five categories :
• 1xx – Informational codes
• 2xx – Success codes
• 3xx – Redirect codes
• 4xx – Client error codes
• 5xx – Server error codes

Some of the most common HTTP response codes used with REST are as follows :

•200 (OK): This response code indicates that the request was made successfully.
•201 (Created): This response code indicates that request was successful and a resource was created. It is used to confirm success of a PUT or POST request.
•400 (Bad Request) : This code indicates that the data is in incorrect format.
•401 (Unauthorized) : This code indicates an authentication error.
•405 (Method Not Allowed) : This code indicates that HTTP method used is not supported for this resource.
•409 (Conflict) : This code indicates that there is a conflict request to create the same resource twice.
•404 (Not Found) :This code indicates that the required resource could not be found.
•500 (Internal Server Error) : This code indicates that there is some error on Server side.

What is Payload?

The body of your request and response message is the Payload of an API Module. It holds the information that you send to the server when you make an API request. Payload can be sent and received in a variety of formats, including JSON.

JSON Syntax Rules

• Data is in name/value pairs
• Data is separated by commas
• Curly braces hold objects
• Square brackets hold arrays

WHAT is JSON?

JSON stands for JavaScript Object Notation and it is lightweight format for storing and transporting data and is often used when data is sent from a server to a web page.

Below is the sample JSON File.
image

What Is XML?

XML stands for extensible Markup Language. It plays an important role in many different IT systemsand is often used for distributing data over the Internet.

Below is the sample XML File.
image

Rest Assured is one of the most powerful libraries for testing RESTful API using Java language. This should be the first-choice when you need to test a REST. All tests should be written in the BDD (Behavior Driven Development) format and its framework syntax is very clean and easy to use. The framework sends a network request to an application under test and verifies the response based on the expectations.

image

Let’s have a look at REST-Assured Syntax:

image

Let's try to understand the above code:

  1. In the above example, no method has been passed to ‘given.' It means that there are no specific prerequisites.
  2. There is a ‘get' method that is passed to ‘when,' and it indicates that an event occurs when we visit the URL specified in the ‘get' method.
  3. When we hit the URL in ‘when', ‘then‘, we will receive output as a response, and then we will validate it. In this example, as shown in the above image, we are performing an assertion on the status code.

Top comments (0)