Reputation: 3606
I'm using test assured to load a reasonable large set of test data into my application under test.
I want to log the request and response details for ONLY the requests that generate an error response (HTTP 4xx or 5xx)
I've tried these snippets but end up with ALL requests logged and only the error responses logged. The problem is that my log files become really large and it's causing issues in Jenkins. I just want to see the errors and the request that caused them.
RequestSpecBuilder build = new RequestSpecBuilder();
build.addFilter(new ErrorLoggingFilter()).log(LogDetail.ALL);
requestSpec = build.build();
RequestSpecBuilder build = new RequestSpecBuilder();
build.log(LogDetail.ALL).addFilter(new ErrorLoggingFilter());
requestSpec = build.build();
Upvotes: 0
Views: 7105
Reputation: 177
You have .log(LogDetail.ALL)
in your RequestSpecBuilder. This method adds new RequestLoggingFilter
to your filter chain. It causes your request to be logged.
See log
method source code
Upvotes: 0
Reputation: 1
I created an optional config parameter to enable failure logging for debugging failing tests:
public static Response sendRequest(Method method, String url, RequestSpecification spec, Integer expectedStatus) {
try {
ValidatableResponse response = given().spec(spec).request(method, new URL(url)).then();
if (getConfig().restAssuredDebuggingEnabled()) {
response.log().all();
}
if (expectedStatus != null) {
response.statusCode(expectedStatus);
}
return response.extract().response();
}
catch (Exception ex) {
throw new RuntimeException(method + " call to " + url + " failed", ex);
}
}
Upvotes: 0
Reputation: 1390
You can create your own filter by implementing io.restassured.filter.Filter
interface:
public class FailedRequestFilter implements Filter {
private static final Logger logger = Logger.getLogger(FailedRequestFilter.class.getName());
@Override
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
Response response = ctx.next(requestSpec, responseSpec);
if (response.statusCode() >= 400) {
logger.log(Level.INFO, requestSpec.getMethod() + " " + requestSpec.getURI() + " => " +
response.getStatusCode() + " " + response.getStatusLine());
}
return response;
}
}
And then use it in your requests:
RestAssured.given()
.filter(new FailedRequestFilter())
.when()
.get("http://www.example.com");
Using Java 8 lambda expression can also work:
Logger logger = Logger.getLogger("SomeLoggerName");
RestAssured.given()
.filter(
(request, response, ctx) -> {
Response resp = ctx.next(request, response);
if (resp.statusCode() >= 400) {
logger.log(Level.INFO, request.getMethod() + " " + request.getURI() + " => "
+ response.getStatusCode() + " " + response.getStatusLine());
}
return resp;
})
.when()
.get("http://wwww.example.com");
Upvotes: 4