Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Improve RestAssured integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndimer committed Jun 13, 2022
1 parent bd51ce4 commit 367a933
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.cloudbeat</groupId>
<artifactId>cb-common</artifactId>
<version>0.12.8</version>
<version>0.12.9</version>
<build>
<plugins>
<plugin>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/cloudbeat/common/CloudBeatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.cloudbeat.common.reporter.model.LogEntryResult;
import io.cloudbeat.common.reporter.wrapper.webdriver.WebDriverEventHandler;
import io.cloudbeat.common.restassured.RestAssuredFailureListener;
import io.cloudbeat.common.restassured.RestAssuredRequestLogger;
import io.restassured.RestAssured;
import io.restassured.config.FailureConfig;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -69,9 +68,10 @@ protected String guessCurrentTestPackage() {
}

public void setupRestAssured() {
RestAssured.filters(new RestAssuredRequestLogger(this));
/*RestAssured.filters(new RestAssuredRequestLogger(this));
RestAssured.config = RestAssured.config()
.failureConfig(FailureConfig.failureConfig().with().failureListeners(new RestAssuredFailureListener(this)));
*/
}

public WebDriver setupWebDriver() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ public void failStep(final String stepId, Throwable throwable, String screenshot
endStep(stepId, TestStatus.FAILED, throwable, screenshot);
}

public StepResult endStep(final String stepId) {
return endStep(stepId, TestStatus.PASSED, null);
}
public StepResult endStep(final String stepId, TestStatus status, Throwable throwable) {
return endStep(stepId, status, throwable, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.cloudbeat.common.restassured;

import io.cloudbeat.common.CbTestContext;
import io.restassured.filter.FilterContext;
import io.restassured.filter.OrderedFilter;
import io.restassured.internal.NameAndValue;
import io.restassured.internal.support.Prettifier;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class CbRestAssuredFilter implements OrderedFilter {
final CbTestContext ctx;
public CbRestAssuredFilter(CbTestContext ctx) {
this.ctx = ctx;
}

@Override
public Response filter(final FilterableRequestSpecification requestSpec,
final FilterableResponseSpecification responseSpec,
final FilterContext filterContext) {
//public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
final Prettifier prettifier = new Prettifier();
final String url = requestSpec.getURI();
final String method = requestSpec.getMethod();
final Map<String, String> requestHeaders = toMap(requestSpec.getHeaders());
String requestBody = null;
if (Objects.nonNull(requestSpec.getBody())) {
requestBody = prettifier.getPrettifiedBodyIfPossible(requestSpec);
}
final Response response = filterContext.next(requestSpec, responseSpec);
final String statusText = response.getStatusLine();
final int statusCode = response.getStatusCode();
final String responseBody = prettifier.getPrettifiedBodyIfPossible(response, response.getBody());
final Map<String, String> responseHeaders = toMap(response.getHeaders());
final String stepName = String.format("%s %s", method, url);
final String stepId = ctx.getReporter().startStep(stepName);
ctx.getReporter().endStep(stepId);
return response;
}

@Override
public int getOrder() {
return Integer.MAX_VALUE;
}

private static Map<String, String> toMap(final Iterable<? extends NameAndValue> items) {
final Map<String, String> result = new HashMap<>();
items.forEach(h -> result.put(h.getName(), h.getValue()));
return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.cloudbeat.common.restassured;

import io.cloudbeat.common.CbTestContext;
import io.cloudbeat.common.CloudBeatTest;
import io.restassured.listener.ResponseValidationFailureListener;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;

public class RestAssuredFailureListener implements ResponseValidationFailureListener {
private final CloudBeatTest currentTest;

public RestAssuredFailureListener(CloudBeatTest test) {
this.currentTest = test;
final CbTestContext ctx;
public RestAssuredFailureListener(CbTestContext ctx) {
this.ctx = ctx;
}

@Override
Expand Down

This file was deleted.

0 comments on commit 367a933

Please sign in to comment.