Logging in Rest Assured ( Response Logging ) - Part 2

In the previous post, we studied about Request Logging Feature of Rest Assured. In this post, we will learn about Response Logging feature of Rest Assured and logging on various conditions like if validation fails.

As we have studied in the previous post that log method supports various Request logging methods. So now we will study the multiple Response logging methods.
  • log().body() : By adding log().body() to the response you'll receive information about what the server sends in the response body. It will log the response even if error has occured.
  • log().all().ifError() : By adding log().ifError().body() to the response you'll receive information about what the sever sends in the response body only if error occurs.
  • log().status() : By adding log().status() to the response you'll receive information about what the sever sends as the status.
  • log().headers() : By adding log().headers() to the response you'll receive information about what headers the server send.
  • log().cookies() : By adding log().cookies() to the response you'll receive information about cookies.
  • log().ifStatusCodeIsEqualTo(status code) : By adding log().ifStatusCodeIsEqualTo(status code) to the response you can configure to log the response only if status code matches some value.
  • log().ifStatusCodeMatches(matcher) : By adding     log().ifStatusCodeMatches(matcher) to the response you can configure to log the response only if status code matches the value supplied by Hamcrest matcher.
  • log().ifValidationFails() : By adding log().ifValidationFails() to the response you can configure to log the response only if the added validation to response fails like ContentType returned is xml instead of JSON.
  • RestAssured.enableLoggingOfRequestAndResponseIfValidationFails() : By adding above method in beforeClass you can enable logs for both Request and Response if the validation fails. It will act as a shortcut for multiple tests.
  • given().config(RestAssured.config().logConfig (LogConfig.logConfig ().enableLoggingOfRequestAndResponseIfValidationFails (LogDetail.HEADERS))) By adding above method you can configure to log both request and response if the validation fails, where HEADERS can be replaced with ALL, body, Cookies, Method, Params, Status and Uri. In this case it will log only headers. 
Now let us look at an example where we will add log().body() to the request. Here we are making use of Google Search API that we studied in Testing GET Requests and their Responses using Rest Assured.

package com.googlerestapi;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;


public class ResponseLoggingTest {

  @BeforeClass
  public void responseSpec () {
    
    RestAssured.baseURI="https://maps.googleapis.com";
    
 }
  
  @Test
  public void loggingTest() {
      
      given()
         .param ("query", "Churchgate Station")
         .param ("key","XYZ" )
         .when ()
         .get ("/maps/api/place/textsearch/json")
         .then ()
         .log ().ifError ().assertThat ().statusCode (200);
                 
  }
  
}

In the setBaseUri method, We are simply setting the default URI  to use in all of our tests. Now when we write out a test that calls an API (as we will do in the next step), we don’t have to type in the full address of the API.

In loggingTest method, we start with Given method under which we specify the parameters that are to be sent with the request ( query and key ) then we use When method we specify the base path in GET method and under then we specify log().ifError() method which won't log anything in this case since status code returned will be 200.


Now we will change the base path "/maps/api/place/textsearch/json" to incorrect one i.e.
"/maps/api/place/textsearch/jso" and run the test again.




















So it has logged only when status code returned is not equal to 200 .

Comments

Popular posts from this blog

Extracting an XML Response with Rest-Assured