如何拦截放心响应验证失败
How to intercept rest-assured response validation failure
放心,当我们做的时候
given().get("/api").then().statusCode(SC_OK).content(JSON);
抛出的错误始终是连续的,即如果状态代码失败,则不会检查 contentType 是否为 JSON。
此外,抛出的错误始终是 AssertionError(预期:200,实际:404)
无法从中了解实际响应是什么,如果我们启用日志记录,它将打印在 STDOUT 上,但它无法通过任何其他方式获得。
有没有什么方法可以构建或设置类似我们如何实现的东西Filter
,我们可以在请求构建时提供它以在实际调用之前拦截设置
我有一个休息 api 框架,其中大部分验证是使用 ValidatableResponse
完成的
即使用 .then()...;
而不是通过在请求构建时创建对响应的期望。
我希望拦截特定类型的失败,即如果状态代码失败是 50_ 而我们预期是 200 或其他任何东西,那么实际失败可以在测试失败原因中发布
我确实加注了
github issue
但我不确定这是否会很快实施。我也无法在文档中找到任何内容。
我能够通过委派 Response
和 ValidatableResponse
接口实现来实现这一点
基本上
public class DelegateResponse implements Response {
Response response;
DelegateResponse(Response response){
this.response = response;
}
.
.
.
// Override and delegate other functions normally, but in the below call, delegate further
@Override
ValidatableResponse then(){
return new DelegateValidatableResponse(response.then(), response);
}
}
public class DelegateValidatableResponse implements ValidatableResponse {
ValidatableResponse validatableResponse;
Response response;
DelegateValidatableResponse(ValidatableResponse validatableResponse,Response response) {
this.validatableResponse = validatableResponse;
this.response = response;
}
.
.
.
// Override and delegate other functions similarly and wrap with try catch
// This gives us access to Response object if an exception is thrown and we have more details
// than just the String "Expected status code 200 but was 500"
@Override
public ValidatableResponse statusCode(Matcher<? super Integer> expectedStatusCode) {
try {
return validatableResponse.statusCode(expectedStatusCode);
} catch (Throwable exception) {
throw new ApiValidationError(response, requestSpec, responseSpec, exception);
}
}
}
放心,当我们做的时候
given().get("/api").then().statusCode(SC_OK).content(JSON);
抛出的错误始终是连续的,即如果状态代码失败,则不会检查 contentType 是否为 JSON。
此外,抛出的错误始终是 AssertionError(预期:200,实际:404) 无法从中了解实际响应是什么,如果我们启用日志记录,它将打印在 STDOUT 上,但它无法通过任何其他方式获得。
有没有什么方法可以构建或设置类似我们如何实现的东西Filter
,我们可以在请求构建时提供它以在实际调用之前拦截设置
我有一个休息 api 框架,其中大部分验证是使用 ValidatableResponse
完成的
即使用 .then()...;
而不是通过在请求构建时创建对响应的期望。
我希望拦截特定类型的失败,即如果状态代码失败是 50_ 而我们预期是 200 或其他任何东西,那么实际失败可以在测试失败原因中发布
我确实加注了 github issue
但我不确定这是否会很快实施。我也无法在文档中找到任何内容。
我能够通过委派 Response
和 ValidatableResponse
接口实现来实现这一点
基本上
public class DelegateResponse implements Response {
Response response;
DelegateResponse(Response response){
this.response = response;
}
.
.
.
// Override and delegate other functions normally, but in the below call, delegate further
@Override
ValidatableResponse then(){
return new DelegateValidatableResponse(response.then(), response);
}
}
public class DelegateValidatableResponse implements ValidatableResponse {
ValidatableResponse validatableResponse;
Response response;
DelegateValidatableResponse(ValidatableResponse validatableResponse,Response response) {
this.validatableResponse = validatableResponse;
this.response = response;
}
.
.
.
// Override and delegate other functions similarly and wrap with try catch
// This gives us access to Response object if an exception is thrown and we have more details
// than just the String "Expected status code 200 but was 500"
@Override
public ValidatableResponse statusCode(Matcher<? super Integer> expectedStatusCode) {
try {
return validatableResponse.statusCode(expectedStatusCode);
} catch (Throwable exception) {
throw new ApiValidationError(response, requestSpec, responseSpec, exception);
}
}
}