Logging in Rest Assured ( Request Logging ) - Part 1
In the previous post, we learned about Using RequestSpecBuilder in Rest Assured. In this Rest Assured tutorial, we will learn about logging feature provided by Rest Assured.
Logging is very useful when you want to know what is send in the request and what you have received in response so that you can create the correct expectations and send the correct requests.
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.
Logging is very useful when you want to know what is send in the request and what you have received in response so that you can create the correct expectations and send the correct requests.
After the release of version 1.5, Rest Assured has started supporting logging of details which are sent in the request. Log method of rest assured supports various logging functions. Let us look at them one by one.
- log().all() : By adding log().all() to the request you’ll receive detailed information what the client sends to the server i.e. all the request specification details like parameters, header and body.
- log().params() : By adding log().params() to the request you'll receive information about what the client sends in the request parameters.
- log().body() : By adding log().body() to the request you'll receive information about what the client sends in the request body.
- log().headers() : By adding log().headers() to the request you'll receive information about what the client sends in the request headers.
- log().cookies() : By adding log().cookies() to the request you'll receive information about what the client sends in the request cookies.
- log().method() : By adding log().method() to the request you'll receive information about what the client sends in the request method.
- log().path() : By adding log().path() to the request you'll receive information about what the client sends in the request path.
Now let us look at an example where we will add log().all() 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.
import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import io.restassured.RestAssured; import static io.restassured.RestAssured.*; public class RequestLoggingTest { @BeforeClass public void setBaseUri () { RestAssured.baseURI="https://maps.googleapis.com"; } @Test public void logTest() { given() .param ("query", "Churchgate Station") .param ("key","Xyz" ) .when () .log () .all () .get ("/maps/api/place/textsearch/json"); } }
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 logTest 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 under which we add log().all() that will log all the request specification details like parameters, header and body.
Below is the output that will be printed on the console:
In above output, what is sent in Request path , Request parameters, etc gets logged on the console with the help of log().all().
In the next post, we will learn about Response logging feature of Rest Assured.
Comments
Post a Comment