Skip to main content

Interested in sponsoring the site? [find out more]

blog title image

2 minute read - Test Automation RestAssured Tips

RestAssured Tips for Proxies and Logging

Jul 6, 2020

TLDR; Some tips for seeing traffic when using RestAssured.

Logging

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

When the above code is used then you will see logging messages to the console which show you the basic requests and responses that your messages send out.

Log to File

The Filters above can be instantiated with a PrintStream to write to files.

e.g.

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

And here is a contextual example showing creating the file, the FileOutputStream and the PrintStream

// create a file in the project dir 
// named using current time in milliseconds
final String currentDir = System.getProperty("user.dir");
File outputFile = new File(currentDir,
            "restassured" + System.currentTimeMillis()+".log");

System.out.println("log to file:" + 
    outputFile.getAbsolutePath().toString());

Then, using the file to write.

FileOutputStream fileOutput = new FileOutputStream(outputFile);
PrintStream printToFile = new PrintStream(fileOutput);

// configuring RestAssured to use the file
RestAssured.filters(new RequestLoggingFilter(printToFile),
        new ResponseLoggingFilter(printToFile));

Proxies

I have found the logging approach useful for simple debugging and for seeing the output.

But when I really want to have a detailed look at the traffic, I prefer to go through a proxy.

RestAssured has a very simple proxy configuration.

RestAssured.proxy("localhost",8080);
RestAssured.useRelaxedHTTPSValidation();

I find I need to set the relaxed HTTPS validation when going through a proxy.

Code

You can find the example code for this in:

Specifically:

If you are interested in learning how to Automate and Test APIs, then you might find my book “Automating and Testing a REST API” useful

If you like this content then you might be interested in my Patreon Community. I create exclusive content multiple times a week. Gain access to Patreon only content and online training courses for as little as $1 per month. Learn more about the EvilTester Patreon Community.

<< Fluent Helper Classes for SlowLoadableComponent Page Objects
Ordering Headers In HTTP Requests with RestAssured and UniRest >>