设置restAssured以全局记录所有请求和响应 [英] Set restAssured to log all requests and responses globally
问题描述
默认情况下,我想为所有restAssured响应和请求启用日志记录.
I want to enable logging for all restAssured responses and requests by default.
这就是我要做的:
RestAssured.requestSpecification = new RequestSpecBuilder().
setBaseUri("api").
setContentType(ContentType.JSON).
build().
log().all();
RestAssured.responseSpecification = new ResponseSpecBuilder().
build().
log().all();
requestSpecification可以正常运行,但是通过responseSpecification我得到:
requestSpecification works alright, but with responseSpecification I get:
由于未定义请求规范,因此无法配置日志记录. 您可能会滥用该API.
Cannot configure logging since request specification is not defined. You may be misusing the API.
我真的不想每次都使用log().all().
I really don't want to use log().all() after each then.
推荐答案
将日志记录过滤器添加到RestAssured默认值,请参见过滤器和默认值.
Add logging filters to RestAssured defaults, see filters and defaults.
要创建过滤器,您需要实现io.restassured.filter.Filter接口.要使用过滤器,您可以执行以下操作:
给定().过滤器(新的MyFilter()). ..
To create a filter you need to implement the io.restassured.filter.Filter interface. To use a filter you can do:
given().filter(new MyFilter()). ..
REST Assured提供了一些可以使用的过滤器:
1. io.restassured.filter.log.RequestLoggingFilter:一个过滤器,将打印请求规范的详细信息.
2. io.restassured.filter.log.ResponseLoggingFilter:一个过滤器,如果响应与给定的状态码匹配,它将打印响应详细信息.
3. io.restassured.filter.log.ErrorLoggingFilter:如果发生错误(状态代码在400到500之间),将打印响应正文的过滤器
There are a couple of filters provided by REST Assured that are ready to use:
1. io.restassured.filter.log.RequestLoggingFilter: A filter that'll print the request specification details.
2. io.restassured.filter.log.ResponseLoggingFilter: A filter that'll print the response details if the response matches a given status code.
3. io.restassured.filter.log.ErrorLoggingFilter: A filter that'll print the response body if an error occurred (status code is between 400 and 500)
可以将任何过滤器添加到请求,规格或全局默认值:
Any filter could be added to request, spec or global defaults:
RestAssured.filters(..); //默认过滤器列表
RestAssured.filters(..); // List of default filters
这篇关于设置restAssured以全局记录所有请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!