REST API Automation Testing Tutorial Part 2:In this tutorial we are going to focus on testing the HTTP POST method using REST ASSURED API. To know more details about the REST and REST ASSURED API and testing HTTP GET method. Please refer to this link.
1. Understanding Post Request
REST Endpoint URL : https://api.covid19api.com/auth/access_token
AUTHORIZATION: Basic Auth
Username:go-corona-admin
Password:5577YvzU4bK63a1WIQ3Z043H
Body:
{“Email”:”test1@covid19api.com”, “Subscription”:”basic”}
Response:
{ ‘result’ : ‘success’ }
Response Code:
200
Response Headers:
Server: nginx/1.14.0 (Ubuntu)
Date: Sun, 22 Mar 2020 08:07:30 GMT
Content-Type: application/json
Content-Length: 24
Connection: keep-alive
Vary: Origin
2. Design Test Cases
3. Pre-requisites
io.restassured
maven dependency
add the dependency in the pom.xml
4. Post Method Example
- Validate Response Code
- Validate Response Headers
- Validate Response Body
- Validate 401 unauthorized response
Java Logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
package com.covidtracking.tests; import java.util.LinkedHashMap; import java.util.Map; import org.apache.commons.codec.binary.Base64; import org.testng.Assert; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.Headers; import io.restassured.path.json.JsonPath; import io.restassured.response.ResponseBody; import io.restassured.specification.RequestSpecification; //blog details : http://total-qa.com public class Covid19APIPostMethodTests { @Test(description="TC_001 Validate the response code for covid19 api" ) public void TC_001_validatePostRequestRespCode() { //Username:go-corona-admin //Password:5577YvzU4bK63a1WIQ3Z043H //AUTHORIZATION: Basic Auth String userCreds = ("go-corona-admin"+":"+"5577YvzU4bK63a1WIQ3Z043H"); String token = new String(Base64.encodeBase64(userCreds.getBytes())); System.out.println("Token Value :" + token); RequestSpecification request = RestAssured.given(); Map<String,Object> headersMap = new LinkedHashMap<String,Object>(); headersMap.put("Content-Type", "application/json"); headersMap.put("AUTHORIZATION", "Basic "+ token); request.headers(headersMap); int actualRespCode = request.when(). urlEncodingEnabled(true). body("{\"Email\":\"test1@covid19api.com\", \"Subscription\":\"basic\"}"). post("https://api.covid19api.com/auth/access_token").getStatusCode(); System.out.println("ResponseCode:: "+ actualRespCode); int expectedRespCode = 200; Assert.assertEquals(actualRespCode, expectedRespCode); } @Test(description="TC_002 Validate the response body contains Status:active" ) public void TC_002_validatePostRequestRespBody() { //Username:go-corona-admin //Password:5577YvzU4bK63a1WIQ3Z043H //AUTHORIZATION: Basic Auth String userCreds = ("go-corona-admin"+":"+"5577YvzU4bK63a1WIQ3Z043H"); String token = new String(Base64.encodeBase64(userCreds.getBytes())); System.out.println("Token Value :" + token); RequestSpecification request = RestAssured.given(); Map<String,Object> headersMap = new LinkedHashMap<String,Object>(); headersMap.put("Content-Type", "application/json"); headersMap.put("AUTHORIZATION", "Basic "+ token); request.headers(headersMap); ResponseBody respBody = request.when(). urlEncodingEnabled(true). body("{\"Email\":\"test1@covid19api.com\", \"Subscription\":\"basic\"}"). post("https://api.covid19api.com/auth/access_token").body(); System.out.println("ResponseCode:: "+ respBody.asString()); JsonPath jsonPath = new JsonPath(respBody.asString()); String actual = jsonPath.get("Status"); String expected ="active"; Assert.assertEquals(actual, expected); } @Test(description="TC_003 Validate the response Headers contains Content-Length:132") public void TC_003_validatePostRequestRespHeaders() { String userCreds = ("go-corona-admin"+":"+"5577YvzU4bK63a1WIQ3Z043H"); String token = new String(Base64.encodeBase64(userCreds.getBytes())); System.out.println("Token Value :" + token); RequestSpecification request = RestAssured.given(); Map<String,Object> headersMap = new LinkedHashMap<String,Object>(); headersMap.put("Content-Type", "application/json"); headersMap.put("AUTHORIZATION", "Basic "+ token); request.headers(headersMap); Headers respHeaders = request.when(). urlEncodingEnabled(true). body("{\"Email\":\"test1@covid19api.com\", \"Subscription\":\"basic\"}"). post("https://api.covid19api.com/auth/access_token").getHeaders(); System.out.println("Response Header Value:: "+ respHeaders.get("Content-Length")); int actual = Integer.parseInt(respHeaders.get("Content-Length").toString().split("=")[1]); int expected =132; Assert.assertEquals(actual, expected); } @Test(description="TC_004 Validate the 401 Unauthorized http code in the response") public void TC_004_validatePostRequest401Unauthorized() { RequestSpecification request = RestAssured.given(); Map<String,Object> headersMap = new LinkedHashMap<String,Object>(); headersMap.put("Content-Type", "application/json"); request.headers(headersMap); int actual = request.when(). urlEncodingEnabled(true). body("{\"Email\":\"test1@covid19api.com\", \"Subscription\":\"basic\"}"). post("https://api.covid19api.com/auth/access_token").statusCode(); int expected=401; Assert.assertEquals(actual, expected); } } |
Conclusion:
Validated Response Code
Validated Response Headers
Validated Response Body
Validated 401 unauthorized response