public class EasyHttp extends Object implements AddPlaceholder
文件名:EasyHttp.java
用途:根据接口信息,对接口进行请求的工具,亦可通过类中的静态方法,对接口进行快速请求。 工具类允许对参数设置公式和提取词语等方法,并可以设置自动断言,方便接口自动化运行
编码时间:2020年6月18日上午7:02:54
修改时间:2023年5月23日 上午11:28:54
Modifier and Type | Field and Description |
---|---|
static String |
ASSERT_RESULT_JSON_INTER_INFO
定义断言结果json的接口信息字段名
|
static String |
ASSERT_RESULT_JSON_RESULT
定义断言结果json的结果字段名
|
static Entry<Long,TimeUnit> |
connectTime
定义默认连接超时时间,仅对使用默认时间的静态方法生效
|
Constructor and Description |
---|
EasyHttp() |
Modifier and Type | Method and Description |
---|---|
EasyHttp |
addFunction(DataDriverFunction functions)
该方法用于添加数据处理函数
|
void |
addReplaceFunction(String regex,
DataFunction function)
用于添加待替换的词语及相应的替换方法
|
void |
addReplaceWord(String word,
String replaceWord)
该方法用于添加被替换的词语以及替换的内容
|
Set<String> |
getAssertResult()
该方法用于返回请求接口后自动断言的结果集合
|
String |
getReplaceKey(String key)
该方法用于返回指定待替换关键词的内容
|
EasyResponse |
requst(InterfaceInfo interInfo)
该方法用于根据接口信息,对接口进行请求,并返回响应内容
|
static EasyResponse |
requst(RequestType requestType,
String url)
该方法用于对接口进行快速请求
|
static EasyResponse |
requst(RequestType requestType,
String url,
Map<String,String> requestHead,
MessageType messageType,
Object body)
该方法用于对接口进行快速请求
|
static EasyResponse |
requst(RequestType requestType,
String url,
MessageType messageType,
Object body)
该方法用于对接口进行快速请求
|
static EasyResponse |
requst(String url)
该方法用于以get请求的方式对接口进行快速请求
|
EasyHttp |
setAssertFailThrowException(boolean isAssertFailThrowException)
该方法用于设置自动断言失败时,是否需要抛出异常
|
EasyHttp |
setAutoBeforeOperation(boolean isAutoBeforeOperation)
该方法用于设置在请求接口时,是否自动调用接口的前置操作
|
public static final String ASSERT_RESULT_JSON_INTER_INFO
public static final String ASSERT_RESULT_JSON_RESULT
public EasyHttp addFunction(DataDriverFunction functions)
可通过lambda添加公式对数据处理的方式,例如,将文本中的存在的"a()"全部替换为文本“test”,则可按如下写法:
addFunction(new DataDriverFunction("a\\(\\)", text -> "test"));
可添加Functions
类中预设的函数
functions
- 数据处理函数public void addReplaceFunction(String regex, DataFunction function)
AddPlaceholder
该方法允许添加待替换词语的处理方式,在写入用例时,若指定的待替换内容符合此方法指定的正则时,则会使用存储的替换方式,
对词语进行替换。例如,占位符前后标志均为“#”,则:
@Test
public void addReplaceWordTest_DataDriverFunction() {
// 定义词语匹配规则和处理方式,当匹配到正则后,将获取“随机:”后的字母
// 若字母为“N”,则随机生成两位数字字符串
// 若字母为“Y”,则随机生成两位中文字符串
test.addReplaceWord(new DataDriverFunction("随机:[NC]", text -> {
return "N".equals(text.split(":")[1]) ? RandomString.randomString(2, 2, StringMode.NUM)
: RandomString.randomString(2, 2, StringMode.CH);
}));
}
部分定义方法可调用工具类Functions
类获取,以其中一个方法为例,其传参方法为如下:
DataDriverFunction
driverFunction = Functions.randomCarId()
;
addReplaceFunction(DataDriverFunction.getRegex()
, DataDriverFunction.getFunction()
);
addReplaceFunction
in interface AddPlaceholder
regex
- 需要替换的内容正则表达式function
- 替换词语使用的函数public void addReplaceWord(String word, String replaceWord)
AddPlaceholder
addReplaceWord
in interface AddPlaceholder
word
- 需要替换的内容replaceWord
- 替换后的内容public String getReplaceKey(String key)
key
- 待替换关键词public EasyHttp setAssertFailThrowException(boolean isAssertFailThrowException)
isAssertFailThrowException
- 断言失败是否抛出异常public EasyHttp setAutoBeforeOperation(boolean isAutoBeforeOperation)
isAutoBeforeOperation
- 是否自动调用接口的前置操作public Set<String> getAssertResult()
public EasyResponse requst(InterfaceInfo interInfo)
interInfo
- 接口信息类对象public static EasyResponse requst(RequestType requestType, String url, Map<String,String> requestHead, MessageType messageType, Object body)
注意: 当请求体为表单类型时,其body必须是“List<Entry<String, Object>>”类型;当请求体为文件类型时,则body必须是“File”类型
requestType
- 请求类型url
- 接口url地址requestHead
- 请求头集合messageType
- 请求体内容格式类型body
- 请求体内容public static EasyResponse requst(RequestType requestType, String url, MessageType messageType, Object body)
requestType
- 请求类型url
- 接口url地址messageType
- 请求体内容格式类型body
- 请求体内容public static EasyResponse requst(RequestType requestType, String url)
requestType
- 请求类型url
- 接口url地址public static EasyResponse requst(String url)
url
- 接口url地址Copyright © 2024. All rights reserved.