Testing GET Requests and their Responses using Rest Assured
What is a GET Request?
The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
Way to send GET Request using Rest Assured and Check Status Code:
For this exercise, we will use Google Places API ( Place Search ). You can view the API at below link :
We will learn about the Text Search GET request of Google Places API.
A Text Search request is an HTTP URL of the following form:
where
output
may be either of the following values:- json (recommended) indicates output in JavaScript Object Notation (JSON)
- xml indicates output as XML
&
) character.
Testing HTTP Error Codes
We will verify that after calling the URL, the response code that we get is 200, which indicates, the response is correct. HTTP status codes indicate whether the response was correct or wrong.
Explanation of status codes is given at below link:
HTTP Status Codes Wiki
Let's look at an example now:
import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.response.Response; import static io.restassured.RestAssured.*; public class GetStatusCodeTest { @BeforeClass public void setBaseUri () { RestAssured.baseURI = "https://maps.googleapis.com"; } @Test public void testStatusCode () { Response res = given() .param ("query", "restaurants in mumbai") .param ("key", "Xyz") .when() .get ("/maps/api/place/textsearch/json"); Assert.assertEquals (res.statusCode (), 200); } @Test public void testStatusCodeRestAssured () { given ().param ("query", "restaurants in mumbai") .param ("key", "Xyz") .when() .get ("/maps/api/place/textsearch/json") .then () .assertThat ().statusCode (200); } }
Explanation of setBaseUri Method:
The entire URL that we will hit is as follows:
The entire URL that we will hit is as follows:
So the BaseUri is https://maps.googleapis.com
Explanation of testStatusCode Method:
Rest Assured follows the BDD ( Behaviour Driven Development ) approach for writing tests i.e Given- When - Then structure. In Given, we will mention the parameters while making the API call. When, is used for making the API call.
Then, is where we perform the assertion or check if the response is as expected.
The query parameters can be passed via param and key to access an API should be also passed via param, wherein param we will pass query and key.
Rest Assured follows the BDD ( Behaviour Driven Development ) approach for writing tests i.e Given- When - Then structure. In Given, we will mention the parameters while making the API call. When, is used for making the API call.
Then, is where we perform the assertion or check if the response is as expected.
The query parameters can be passed via param and key to access an API should be also passed via param, wherein param we will pass query and key.
The path can be passed through GET, where path is
"/maps/api/place/textsearch/json"
"/maps/api/place/textsearch/json"
Under the When, we perform a GET request to the specified URL.
The Response will be saved in Response variable and calling status code method will return the code which we will assert using Test NG'S assertEquals method.
Explanation of testStatusCodeRestAssured Method:
The response can also be checked using Rest Assured rather than using Test Ng Assertions. Under THEN, using assertThat method we check if the response is equal to 200.
In next post, we will learn about how to send the GET Request and print the Response.
Comments
Post a Comment