Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,54 @@ This provides complete transparency into test flow and helps debug failures quic

---

## HTTP Request Reporting

The Testomat HTTP filter integrates with RestAssured and automatically reports HTTP requests as test steps.

Each request is displayed as a parent step with detailed substeps containing:

- HTTP method
- URL
- Status code
- Request duration
- Request headers
- Response headers
- Request body
- Response body

### Basic usage

```java
given()
.filter(TestomatHttpFilter.create())
.when()
.get("/users");
```

### Custom configuration

```java
given()
.filter(
TestomatHttpFilter.builder()
.headers(false)
.requestBody(false)
.onlyFailures(true)
.build()
);
```

### Available options

| Option | Description | Default |
| -------------- | ------------------------------------------------- | ------- |
| `headers` | Include request and response headers | `true` |
| `requestBody` | Include request body | `true` |
| `responseBody` | Include response body | `true` |
| `onlyFailures` | Report only requests with HTTP status code >= 400 | `false` |

---

## Test Filtering by ID

**JUnit & TestNG only**
Expand Down
10 changes: 8 additions & 2 deletions java-reporter-core/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>coretest</groupId>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<name>Testomat.io Reporter Core</name>
<version>coretest</version>
<version>0.13.1</version>
<description>Core functionality for Testomat.io Java test reporting library</description>
<url>https://github.com/testomatio/java-reporter</url>
<developers>
Expand Down Expand Up @@ -271,6 +271,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.5.7</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
8 changes: 7 additions & 1 deletion java-reporter-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.15.0</version>
<version>0.16.0</version>
<packaging>jar</packaging>

<name>Testomat.io Reporter Core</name>
Expand Down Expand Up @@ -110,6 +110,12 @@
<version>2.22.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.5.7</version>
<scope>provided</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.testomat.core.constants;

public class CommonConstants {
public static final String REPORTER_VERSION = "0.15.0";
public static final String REPORTER_VERSION = "0.16.0";

public static final String TESTS_STRING = "tests";
public static final String API_KEY_STRING = "api_key";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public static void step(String stepName, Runnable action) {
}
}

static void step(String stepName, String passedLog, Runnable action) {

}

public static void meta(String key, String value) {
MetaStorage.TEMP_META_STORAGE.get().put(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -74,7 +75,11 @@ public AwsService(ArtifactKeyGenerator keyGenerator, AwsClient awsClient,
public void uploadAllArtifactsForTest(String testName, String rid, String testId) {

List<String> artifactDirectories = TempArtifactDirectoriesStorage.DIRECTORIES.get();
Map<UUID, StepData> stepArtifactDirectories = TempArtifactDirectoriesStorage.STEP_DATA.get(Thread.currentThread().getId());
Map<UUID, StepData> stepArtifactDirectories =
TempArtifactDirectoriesStorage.STEP_DATA.getOrDefault(
Thread.currentThread().getId(),
Collections.emptyMap())
;

if (artifactDirectories.isEmpty() && stepArtifactDirectories.isEmpty()) {
log.debug("Artifact list is empty for test: {}", testName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package io.testomat.core.restassured;

import io.restassured.filter.Filter;
import io.restassured.filter.FilterContext;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
import io.testomat.core.facade.Testomatio;
import io.testomat.core.step.StepLifecycle;
import io.testomat.core.step.TestStep;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestomatHttpFilter implements Filter {
private static final Logger log = LoggerFactory.getLogger(TestomatHttpFilter.class);

private final boolean requestBody;
private final boolean responseBody;
private final boolean headers;
private final boolean onlyFailures;

/**
* Creates filter with default settings.
*/
public static TestomatHttpFilter create() {
return new Builder().build();
}

/**
* Creates builder for custom configuration.
*/
public static Builder builder() {
return new Builder();
}

private TestomatHttpFilter(Builder builder) {
this.requestBody = builder.requestBody;
this.responseBody = builder.responseBody;
this.headers = builder.headers;
this.onlyFailures = builder.onlyFailures;
}

@Override
public Response filter(
FilterableRequestSpecification request,
FilterableResponseSpecification responseSpec,
FilterContext ctx) {

long startTime = System.currentTimeMillis();

Response response = ctx.next(request, responseSpec);

long duration = System.currentTimeMillis() - startTime;

if (onlyFailures && response.statusCode() < 400) {
return response;
}

TestStep testStep = new TestStep();
testStep.setCategory("user");
StepLifecycle.start(testStep);

String summary = buildSummary(
request,
response,
duration
);

reportHttpData("Method: " + request.getMethod(), null);
reportHttpData("URL: " + request.getURI(), null);
reportHttpData("Status: " + response.statusCode(), null);
reportHttpData("Duration: " + duration + " ms", null);

if (headers) {
reportHttpData("Request headers", request.getHeaders().toString());
reportHttpData("Response headers", response.getHeaders().toString());
}

if (requestBody) {
reportHttpData("Request body",
request.getBody() == null ? "<empty>" : request.getBody().toString());
}

if (responseBody) {
reportHttpData("Response body", response.getBody().asPrettyString());
}

testStep.setStepTitle(summary);
StepLifecycle.finish();

return response;
}

private String buildSummary(FilterableRequestSpecification request, Response response, long duration) {
String path = URI.create(request.getURI()).getPath();

return String.format("%s %s → %d (%d ms)",
request.getMethod(),
path,
response.statusCode(),
duration
);
}

public static final class Builder {

private boolean requestBody = true;
private boolean responseBody = true;
private boolean headers = true;
private boolean onlyFailures = false;

private Builder() {
}

public Builder requestBody(boolean requestBody) {
this.requestBody = requestBody;
return this;
}

public Builder responseBody(boolean responseBody) {
this.responseBody = responseBody;
return this;
}

public Builder headers(boolean headers) {
this.headers = headers;
return this;
}

public Builder onlyFailures(boolean onlyFailures) {
this.onlyFailures = onlyFailures;
return this;
}

public TestomatHttpFilter build() {
return new TestomatHttpFilter(this);
}
}

private void reportHttpData(String summary, String details) {
TestStep testStep = new TestStep();
testStep.setCategory("user");
StepLifecycle.start(testStep);
testStep.setStepTitle(summary);

try {
if (details != null && !details.isBlank()) {
Path dir = Paths.get("target", "testomat", "http");
Files.createDirectories(dir);
Path json = Files.createTempFile(dir, "http-", ".json");
Files.writeString(json, details, StandardCharsets.UTF_8);
Testomatio.stepArtifact(json.toString());
}
} catch (IOException e) {
log.error("Failed to create HTTP attachment", e);
} finally {
StepLifecycle.finish();
}
}
}
4 changes: 2 additions & 2 deletions java-reporter-cucumber/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.testomat</groupId>
<artifactId>java-reporter-cucumber</artifactId>
<version>0.8.0</version>
<version>0.8.1</version>
<packaging>jar</packaging>

<name>Testomat.io Java Reporter Cucumber</name>
Expand Down Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.15.0</version>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
4 changes: 2 additions & 2 deletions java-reporter-junit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.testomat</groupId>
<artifactId>java-reporter-junit</artifactId>
<version>0.9.0</version>
<version>0.9.1</version>
<packaging>jar</packaging>

<name>Testomat.io Java Reporter JUnit</name>
Expand Down Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.15.0</version>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
4 changes: 2 additions & 2 deletions java-reporter-karate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.testomat</groupId>
<artifactId>java-reporter-karate</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
<packaging>jar</packaging>

<name>Testomat.io Java Reporter Karate</name>
Expand Down Expand Up @@ -52,7 +52,7 @@
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.15.0</version>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>io.karatelabs</groupId>
Expand Down
4 changes: 2 additions & 2 deletions java-reporter-testng/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.testomat</groupId>
<artifactId>java-reporter-testng</artifactId>
<version>0.8.0</version>
<version>0.8.1</version>
<packaging>jar</packaging>

<name>Testomat.io Java Reporter TestNG</name>
Expand Down Expand Up @@ -47,7 +47,7 @@
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.15.0</version>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Loading
Loading