Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Default Host and Port
2.1.
Setting Default Host and Port in REST Assured
2.2.
Benefits of Using Default Host and Port
3.
Get Requests
3.1.
Making GET Requests with REST Assured
4.
Assertions 
4.1.
Making Assertions with REST Assured
4.2.
Types of Assertions in REST Assured
5.
Frequently Asked Questions
5.1.
What is REST Assured?
5.2.
What are the key features of REST Assured?
5.3.
What is a GET request in REST Assured?
5.4.
What are assertions in REST Assured?
6.
Conclusions
Last Updated: Mar 27, 2024
Medium

Default Host And Port, Get and Assert in Rest Assured

Author Vivek Tiwari
2 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

REST Assured is a JAVA library that makes testing RESTful web services easier. It provides a simple and efficient way to test RESTful APIs and validate their responses. 

Default Host And Port, Get and Assert in Rest Assured

In this blog, we will focus on some important concepts in depth and develop a clear understanding of the implementation in REST Assured. Whether you are a beginner or experienced, this blog will provide information to help you work with REST Assured more effectively.

Default Host and Port

The default host and port in REST Assured refer to the default base URI of an API, which is the starting point for all API requests. The Default host and port in REST Assured are used as Base URI for all API requests unless a different base URI is explicitly specified for the particular request. For example, if the default host of (“http://api.example.com”) and the default port of 8080 is set, then all API requests made will start with “http://api.example.com:8080” unless and until a different base URI is specified. This eliminates the problem of specifying the host and port for each API request, saving time and effort.

given().
  baseUri("http://api.example.com").
  port(8080).
when().
  // ...

Setting Default Host and Port in REST Assured

Default host and port in REST Assured can be set up using baseUri() and port()  methods of given() REST method Assured. For example:

given().
  baseUri("http://api.example.com").
  port(8080).
when().
  // ...


In the above example, the baseUri (“http://api.example.com”) sets the default host to “http://api.example.com” and the port(8080) sets the default port to 8080. Once this is done, all API requests made will start with “http://api.example.com:8080” unless and until a different base URI is specified.

RestAssured.baseURI = "http://api.example.com";
RestAssured.port = 8080;


when().
  // ...


In the above-mentioned example RestAssured.baseURI field sets the default host to “http://api.example.com” and the RestAssured.port field sets the default port to 8080. These values will be used for all API requests using REST Assured in the current JVM.  

Benefits of Using Default Host and Port

There are numerous benefits of using default host and port in REST Assured, some of which are mentioned below:

  1. Reduce code duplication. 
     
  2. Readable code.
     
  3. Organized code.
     
  4. High Productivity.
     
  5. Low Maintenance cost.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Get Requests

GET requests to retrieve information from the server or from a specific resource, which can be either a user or a product, or an article, get() method is used to make a GET request. The below code shows how to make a GET request to retrieve information about the specific user:

given().
  baseUri("http://api.example.com").
  port(8080).
when().
  get("/users/1").
then().
  // ...


In the above example, the get(“/user/1”) method makes a GET request to the URL “http://api.example.com:8080/users/1”. then() method can be used to verify the response, like checking the status code, the response body, or the response headers. 

Making GET Requests with REST Assured

Making GET requests is simple, and the below code shows how to make a GET request to retrieve information about specific users:

given().
  baseUri("http://api.example.com").
  port(8080).
when().
  get("/users/1").
then().
  statusCode(200).
  body("id", equalTo(1)).
  body("name", equalTo("vivek")).
  header("Content-Type", equalTo("application/json")).


In this example, the get("/users/1") method makes a GET request to the URL “http://api.example.com:8080/users/1”. The then() method is used to verify the response, such as checking the status code, the response body, and
the response headers.

The statusCode(200) method verifies that the HTTP status code of the response is 200 (OK). The body("id", equalTo(1)) method verifies that the "id" field in the response body is equal to 1. The body("name", equaITo("vivek")) method verifies that the "name" field in the response body is equal to "vivek". The header("Content-Type", equalTo("application/json")) method verifies that the "Content-Type" header in the response is equal to "application/json".

This is a basic example of making a GET request with REST Assured. REST

Assured provides many more features and options for making GET requests, such as support for query parameters, headers, and cookies. It is a powerful tool for testing RESTful web services and can be easily integrated into your existing testing workflow.

Assertions 

To verify the behavior and output of the application, Assertions are used; in addition, they are used to verify the response of a REST API, such as verifying the response status code, response body, and response headers. For example: using statuscode(), you can verify the status code of a response.


given().
  baseUri("http://api.example.com").
  port(8080).
when().
  get("/users/1").
then().
  statusCode(200).


In this example, the statusCode(200) method verifies that the HTTP status code of the response is 200 (OK). If the status code is not 200, the test will fail.

REST Assured provides a wide range of assertions for verifying the response of a REST API, making it a powerful tool for testing RESTful web services. By using assertions, you can ensure that the API behaves as expected and that the data returned by the API is accurate and valid.

Making Assertions with REST Assured

In REST Assured, then() method is used to make assertions. It verifies the response of REST API to make the assertions. 

given().
  baseUri("http://api.example.com").
  port(8080).
when().
  get("/users/1").
then().
  statusCode(200).


In this example, the statusCode(200) method verifies that the HTTP status code of the response is 200 (OK). If the status code is not 200, the test will fail.

REST also provides methods for verifying the response body and header, respectively. You can use the body() and header() methods for that purpose.

given().
  baseUri("http://api.example.com").
  port(8080).
when().
  get("/users/1").
then().
  statusCode(200).
  body("id", equalTo(1)).
  body("name", equalTo("vivek")).


In this example, the body("id", equaITo(1)) method verifies that the "id" field in the response body is equal to 1. The body("name", equalTo("vivek")) method verifies that the "name" field in the response body is equal to "vivek".

Types of Assertions in REST Assured

Various assertions are used to verify the response of REST APIs. Some of them include

  1. Status code assertions: Verify the HTTP status code of the response. statuscode() method can be used to verify the status code is 200(OK).
     
  2. Body assertions: Verify the contents of the response body. For example, the body() method.
     
  3. Header assertions: Verify the contents of the response headers. For example, the header() method.
     
  4. Time-related assertions: Verify the time taken for the response to be returned. For example, the time() method.
     
  5. JSON and XML assertions: verify the content of JSON and XML response. For example, jsonPath() and xmlPath().

Frequently Asked Questions

What is REST Assured?

It is a JAVA library for testing REST APIs and provides a simple and intuitive interface for testing REST APIs and making assertions about the responses received.

What are the key features of REST Assured?

It includes the ability to set a default host and port, make GET requests, and assertions about the responses received. 

What is a GET request in REST Assured?

These are requests made using the HTTP GET method to retrieve information from a REST API. Provides a Simple interface for making requests and examining responses.

What are assertions in REST Assured?

These statements tell the expected behavior of REST API and are used for validating responses from REST APIs.

Conclusions

In the blog, we discussed the default host and port concept and how to set them using the baseUri() and port() methods. The benefit of using a default host and port include consistency and ease of use. Basics of GET request and how to make GET request using the when() and get(). The importance of understanding the response code in GET requests and how to verify the response's status code using the statuscode() method.  The importance of making assertions to verify the response of REST API. In a nutshell, we have discussed the features and functionalities of REST Assured and how to use them for testing REST APIs.

You can also refer to the below articles:

You can learn the Basics of Java and data structures and algorithms in Java on Coding Ninjas. Refer to our guided path on code studio to learn more about DSA Competitive ProgrammingJavascriptSystem Design, etc. Enroll in our courses and refer to the mock test and Problems available. Also, look at the Interview Experiences for placement preparations.

Happy Learning, Ninjas.

Previous article
REST Assured –RequestSpecification – How the request will look like
Next article
REST Assured – Multiple Ways of Calling HTTP Methods on a RequestSpecification
Live masterclass