Getting started with Allure Java HTTP Client
Enrich your Allure Report test reports with the detailed information about the HTTP requests performed via the Java built-in HTTP Client, java.net.http.HttpClient.
Allure Java HTTP Client is available in Allure Java 3.0.0 and newer, which requires Java 17 or newer.
Allure Java HTTP Client is compatible with all JVM-based Allure integrations, including Allure JUnit Jupiter, Allure TestNG, Allure Cucumber-JVM, and more.
To enable the integration in your project:
Make sure you have the Allure Report integration enabled for the test framework you use.
See the instructions in the integration's documentation in Frameworks.
Add the Allure Java HTTP Client integration to your project's dependencies.
xml<!-- Define the version of Allure you want to use via the allure.version property --> <properties> <allure.version>VERSION</allure.version> </properties> <!-- Add allure-bom to dependency management to ensure correct versions of all the dependencies are used --> <dependencyManagement> <dependencies> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-bom</artifactId> <version>${allure.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-java-httpclient</artifactId> <scope>test</scope> </dependency> </dependencies>ktsval allureVersion = "VERSION" dependencies { testImplementation(platform("io.qameta.allure:allure-bom:$allureVersion")) testImplementation("io.qameta.allure:allure-java-httpclient") }groovydef allureVersion = "VERSION" dependencies { testImplementation platform("io.qameta.allure:allure-bom:$allureVersion") testImplementation "io.qameta.allure:allure-java-httpclient" }Wrap the HTTP client that your tests use with
AllureHttpClient.wrap(), and use the returned client for all requests.The example below shows usage of Allure Java HTTP Client in a JUnit Jupiter test.
javaimport io.qameta.allure.javahttpclient.AllureHttpClient; import org.junit.jupiter.api.Test; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import static org.junit.jupiter.api.Assertions.assertEquals; class TestMyWebsite { static HttpClient httpClient = AllureHttpClient.wrap(HttpClient.newHttpClient()); @Test void testSomeRequest() throws Exception { HttpRequest request = HttpRequest.newBuilder(URI.create("https://jsonplaceholder.typicode.com/todos/1")) .GET() .build(); HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); assertEquals(200, response.statusCode()); } }The wrapped client can be created once and shared between tests.
Run your tests and generate a test report the same way as you would do usually.
What the report shows
Each request made with send() or sendAsync() during a test or a fixture is added to the test results as a single HTTP exchange attachment called “HTTP exchange”. It contains:
- the request method, URL, headers and body;
- the response status, headers and body;
- the start and end time of the request;
- the error details, if the request fails, for example, because the server can't be reached.
Asynchronous requests are added when they complete, even if the test doesn't wait for the response.
The response body is captured as your body handler receives it. With streaming body handlers, such as BodyHandlers.ofInputStream() or BodyHandlers.ofLines(), the attachment contains only the part of the body that was received by the time send() returned.
WebSocket connections created via newWebSocketBuilder() are not added to the report. Requests made while no test or fixture is running are passed to the wrapped client without changes and are not added to the report.
Configuration
Configure the HTTP exchange attachment
configureHttpExchange(Consumer<HttpExchange.Builder> exchangeCustomizer)
Use configureHttpExchange() to control what the HTTP exchange attachment contains. The method returns the client, so you can call it right after wrap(). If you call it again, the new settings replace the previous ones.
The following methods are available on the builder:
redactHeader(String name)- replace the value of the given request or response header with a placeholder. The name is not case-sensitive.redactCookie(String name)- replace the value of the given cookie with a placeholder. Allure shows the cookies from theCookieandSet-Cookieheaders separately from the other headers.setMaxBodySize(long maxBodySize)- the maximum number of bytes of a request or response body to keep. Longer bodies are truncated and marked as truncated in the report. Defaults to 1 MiB.
By default, the values of the Authorization and Proxy-Authorization headers are replaced with a placeholder.
import io.qameta.allure.javahttpclient.AllureHttpClient;
import java.net.http.HttpClient;
static HttpClient httpClient = AllureHttpClient.wrap(HttpClient.newHttpClient())
.configureHttpExchange(exchange -> exchange
.redactHeader("X-Api-Key")
.redactCookie("SESSION")
.setMaxBodySize(64 * 1024));