一、需求背景
在某个时间点,线上的某个接口不正常。开发说把接口名称以及其请求和响应发出来,我去后台查看具体的日志。那么我们的restassured接口框架有没有办法实现呢
二、解决方案
使用restassured的Filters
三、官网说明
过滤器允许您在实际提交之前检查和更改请求,并在返回到期望值之前检查和更改响应。 您可以将其视为 AOP 术语中的“环绕建议”。 过滤器可用于实现自定义身份验证方案、会话管理、日志记录等。要创建过滤器,您需要实现 io.restassured.filter.Filter 接口。 要使用过滤器,您可以执行以下操作:
given().filter(new MyFilter()). ..
REST Assured 提供了几个可供使用的过滤器:
- io.restassured.filter.log.RequestLoggingFilter: A filter that‘ll print the request specification details.
- io.restassured.filter.log.ResponseLoggingFilter: A filter that‘ll print the response details if the response matches a given status code.
- io.restassured.filter.log.ErrorLoggingFilter: A filter that‘ll print the response body if an error occurred (status code is between 400 and 500).
RequestLoggingFilter() 和 ResponseLoggingFilter() 可以将所有的请求和响应的 log 进行打印,而我们想要的是将 log 存入文件,因此还要借助方法 logRequestTo(PrintStream stream) ,指定 log 的格式化输出到文件中
四、案例---企业微信的登录接口为例
工程目录结构
五、具体步骤
1、引入commons-io库
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.10.0</version> </dependency>
2、实现自己的打印流,并且接口请求和响应写到对应的日志中
package com.wechat.utils; import io.restassured.RestAssured; import io.restassured.filter.log.RequestLoggingFilter; import io.restassured.filter.log.ResponseLoggingFilter; import org.apache.commons.io.output.WriterOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; public class MyPrintStream { public static void myPrintStream(){ FileWriter fileWriter = null; try{ fileWriter = new FileWriter("src/main/resources/test.log"); }catch (IOException e){ e.printStackTrace(); } PrintStream printStream = new PrintStream(new WriterOutputStream(fileWriter), true); RestAssured.filters( RequestLoggingFilter.logRequestTo (printStream), ResponseLoggingFilter.logResponseTo (printStream)); } }
3、实现把日志贴到Allure报告中
package com.wechat.utils; import io.qameta.allure.Allure; import java.io.FileInputStream; import java.io.FileNotFoundException; public class AllureAttachment { public static void addHttpLogToAllure(){ try { Allure.addAttachment("接口请求响应日志",new FileInputStream("src/main/resources/test.log")); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
4、在apiObject对象中加入这两个步骤
/** * projectName: WeChatWorkApiTest * fileName: TokenHelper.java * packageName: com.wechat.apiobject * date: 2020-07-18 5:42 下午 */ package com.wechat.apiobject; import com.wechat.utils.AllureAttachment; import com.wechat.utils.MyPrintStream; import static io.restassured.RestAssured.given; /** * @version: V1.0 * @author: kuohai * @className: TokenHelper * @packageName: com.wechat.apiobject * @description: Token相关工具类 * @data: 2020-07-18 5:42 下午 **/ public class TokenHelper { public static String getAccessToken() { MyPrintStream.myPrintStream (); String access_token = given() .log ().all () .when() .param("corpid", "wwc376242756245a87") .param("corpsecret", "LTnDiVdqHzzmUz8fj21-0kgxv6wEDs3krBnO-0g4MPw") .get("https://qyapi.weixin.qq.com/cgi-bin/gettoken") .then() .log().all ().extract ().path ( "access_token" ); AllureAttachment.addHttpLogToAllure (); return access_token; } }
5、测试类
/** * projectName: WeChatWorkApiTest * fileName: Demo_01.java * packageName: com.wechat.testcase * date: 2020-07-18 2:49 下午 */ package com.wechat.testcase; import com.wechat.apiobject.TokenHelper; import io.qameta.allure.*; import org.junit.jupiter.api.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * **/ @Epic("Epic企业微信接口测试用例0713") @Feature("Feature部门相关功能测试0713") public class Demo_09 { private static final Logger logger = LoggerFactory.getLogger( Demo_09.class); public static String accessToken; @Test void getAccessToken() { accessToken = TokenHelper.getAccessToken(); Assertions.assertNotNull ( accessToken ); } }
6、使用idea单独执行测试类,并且执行allure serve allure-results,得到测试报告,查看
restassured的filters官网:
https://github.com/rest-assured/rest-assured/wiki/Usage#filters