Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set restAssured to log all requests and responses globally

I want to enable logging for all RestAssured responses and requests by default.

Here's what I do:

RestAssured.requestSpecification = new RequestSpecBuilder().
        setBaseUri("api").
        setContentType(ContentType.JSON).
        build().
        log().all();
RestAssured.responseSpecification = new ResponseSpecBuilder().
        build().
        log().all();

requestSpecification works alright, but with responseSpecification I get:

Cannot configure logging since request specification is not defined. You may be misusing the API.

I really don't want to use log().all() after each then.

like image 248
MuchHelping Avatar asked May 30 '17 10:05

MuchHelping


People also ask

How do you log a response in Restassured?

We can log response specification using log() method provided by ValidatableResponse interface.

What is log all in Restassured?

all() – Logs everything in the specification, including e.g. headers, cookies, body with the option to pretty-print the body if the content-type is either XML, JSON or HTML, Same job is done by a method named “everything()”. body() – Logs only the content of the body.

What is RequestSpecification in Rest assured?

'RequestSpecification' interface provided by Rest Assured is used to club and extract repetitive actions like setting up base URL, headers, HTTP verbs etc which may be common for multiple Rest calls.

What are filters in Rest assured?

A filter allows you to inspect and alter a request before it's actually committed and also inspect and alter the response before it's returned to the expectations. You can regard it as an "around advice" in AOP terms. Filters can be used to implement custom authentication schemes, session management, logging etc.


4 Answers

Put this line of code on your @BeforeClass method and every given call will create a log just like using log.all() after every given:

RestAssured.filters(new RequestLoggingFilter(), new ResponseLoggingFilter());


Rest-Assured project:
https://github.com/rest-assured/rest-assured/blob/master/rest-assured/src/main/java/io/restassured/filter/log/RequestLoggingFilter.java

like image 20
Luiz Gustavo Avatar answered Oct 24 '22 08:10

Luiz Gustavo


I think you need to see the logs then test fails, in this case just use this configuration from rest assured:

RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();

like image 170
Ștefan Teslari Avatar answered Oct 24 '22 08:10

Ștefan Teslari


Add logging filters to RestAssured defaults, see filters and defaults.

To create a filter you need to implement the io.restassured.filter.Filter interface. To use a filter you can do:
given().filter(new MyFilter()). ..

There are a couple of filters provided by REST Assured that are ready to use:
1. io.restassured.filter.log.RequestLoggingFilter: A filter that'll print the request specification details.
2. io.restassured.filter.log.ResponseLoggingFilter: A filter that'll print the response details if the response matches a given status code.
3. io.restassured.filter.log.ErrorLoggingFilter: A filter that'll print the response body if an error occurred (status code is between 400 and 500)

Any filter could be added to request, spec or global defaults:

RestAssured.filters(..); // List of default filters

like image 24
RocketRaccoon Avatar answered Oct 24 '22 08:10

RocketRaccoon


You can define filters at the class level

public ClassName() {
    filters(new RequestLoggingFilter(), new ResponseLoggingFilter(), new ErrorLoggingFilter());
}
like image 1
J.Klimov Avatar answered Oct 24 '22 07:10

J.Klimov