public class EasyResponse extends Object
文件名:EasyResponse.java
用途: 对接口响应参数进行处理,可根据不同的返回,对响应结果进行输出,或以格式化的形式输出响应内容
编码时间:2020年6月26日下午7:09:07
修改时间:2020年6月26日下午7:09:07
Modifier | Constructor and Description |
---|---|
protected |
EasyResponse(okhttp3.Response response,
InterfaceInfo interInfo)
构造对象,指定OKHttp响应类
|
Modifier and Type | Method and Description |
---|---|
boolean |
assertResponse(String assertRegex,
SearchType searchType,
String paramName)
该方法用于对接口响应报文的指定部分内容进行断言
|
boolean |
assertResponse(String assertRegex,
SearchType searchType,
String paramName,
String xpath)
该方法用于对接口响应报文的指定部分内容进行断言
|
boolean |
assertResponse(String assertRegex,
SearchType searchType,
String leftBoundary,
String rightBoundary,
int index)
该方法用于对接口响应报文的指定部分内容进行断言
|
boolean |
assertResponse(String assertRegex,
SearchType searchType,
String paramName,
String xpath,
String leftBoundary,
String rightBoundary,
int index)
该方法用于对接口响应报文的指定部分内容进行断言
|
boolean |
assertResponse(String assertRegex,
String paramName)
该方法用于对接口响应报文的指定部分内容进行断言
|
String |
extractKey(SearchType searchType,
String paramName)
该方法用于通过指定的搜索参数对接口响应报文指定内容进行提取,返回提取到的内容
|
String |
extractKey(SearchType searchType,
String paramName,
String xpath)
该方法用于通过指定的搜索参数对接口响应报文指定内容进行提取,返回提取到的内容
|
String |
extractKey(SearchType searchType,
String leftBoundary,
String rightBoundary,
int index)
该方法用于通过指定的边界条件对接口响应报文指定内容进行提取,返回提取到的内容
|
String |
extractKey(SearchType searchType,
String paramName,
String xpath,
String leftBoundary,
String rightBoundary,
int index)
该方法用于通过指定的条件对接口响应报文指定内容进行提取,返回提取到的内容
|
String |
extractKey(String paramName)
该方法用于通过指定的搜索参数对响应体进行提取,返回提取到的内容
|
String |
getMessage()
该方法用于返回接口响应消息
|
long |
getReceivedResponseAtMillis()
该方法用于返回客户端接收到服务端返回的请求头时的时间戳
|
InterfaceInfo |
getRequestInterfaceInfo()
该方法用于返回接口的实际请求信息
|
byte[] |
getResponseBody()
该方法用于返回响应体
|
String |
getResponseBodyText()
该方法用于以
setCharsetName(String) 方法设定的编码格式,返回接口的响应体字符串 |
Map<String,String> |
getResponseHeaderMap()
该方法用于返回接口返回的响应头
|
long |
getResponseTimeDifferenceAtMillis()
该方法用于对客户端发送请求的时间戳与客户端接收到返回的时间戳做差,返回其差值,即接口从请求到响应的时间,单位为毫秒
|
long |
getSentRequestAtMillis()
该方法用于返回服务端收到客户端发出请求时的时间戳
|
int |
getStatus()
该方法用于返回接口响应状态码
|
void |
setCharsetName(String charsetName)
该方法用于设置字符集名称
|
void |
setMessageType(int status,
Set<MessageType> messageSet)
该方法用于添加响应体的内容格式
|
protected EasyResponse(okhttp3.Response response, InterfaceInfo interInfo)
response
- OKHttp响应类interInfo
- public void setCharsetName(String charsetName)
charsetName
- 字符集名称public void setMessageType(int status, Set<MessageType> messageSet)
status
- 状态messageSet
- 内容格式集合public String getResponseBodyText()
setCharsetName(String)
方法设定的编码格式,返回接口的响应体字符串public byte[] getResponseBody()
public Map<String,String> getResponseHeaderMap()
public int getStatus()
public String getMessage()
public long getSentRequestAtMillis()
public long getReceivedResponseAtMillis()
public long getResponseTimeDifferenceAtMillis()
public InterfaceInfo getRequestInterfaceInfo()
注意:返回的信息类仅存储请求接口的地址、请求类型、请求头和请求体信息,若通过EasyHttp.requst(InterfaceInfo)
请求接口时,其除前面提到的信息外,
其他在InterfaceInfo
参数中的信息将不会被存储
public boolean assertResponse(String assertRegex, SearchType searchType, String paramName, String xpath, String leftBoundary, String rightBoundary, int index)
注意:断言规则为正则表达式,若断言非正则表达式的内容,需要自行进行转义,例如断言“(”符号,则需要传入“\\(”。方法中各个参数的解释,可参考extractKey(SearchType, String, String, String, String, int)
方法
assertRegex
- 断言规则searchType
- 搜索范围枚举paramName
- 搜索变量xpath
- 提取内容xpathleftBoundary
- 断言内容左边界rightBoundary
- 断言内容右边界index
- 指定下标内容public boolean assertResponse(String assertRegex, SearchType searchType, String leftBoundary, String rightBoundary, int index)
注意:断言规则为正则表达式,若断言非正则表达式的内容,需要自行进行转义,例如断言“(”符号,则需要传入“\\(”。
方法中各个参数的解释,可参考extractKey(SearchType, String, String, int)
方法
assertRegex
- 断言规则searchType
- 搜索范围枚举leftBoundary
- 断言内容左边界rightBoundary
- 断言内容右边界index
- 指定下标内容public boolean assertResponse(String assertRegex, SearchType searchType, String paramName, String xpath)
注意:断言规则为正则表达式,若断言非正则表达式的内容,需要自行进行转义,例如断言“(”符号,则需要传入“\\(”。
方法中各个参数的解释,可参考extractKey(SearchType, String, String)
方法
assertRegex
- 断言规则searchType
- 搜索范围枚举paramName
- 搜索变量xpath
- 提取内容xpathpublic boolean assertResponse(String assertRegex, SearchType searchType, String paramName)
注意:断言规则为正则表达式,若断言非正则表达式的内容,需要自行进行转义,例如断言“(”符号,则需要传入“\\(”。
方法中各个参数的解释,可参考extractKey(SearchType, String)
方法
assertRegex
- 断言规则searchType
- 搜索范围枚举paramName
- 搜索变量public boolean assertResponse(String assertRegex, String paramName)
注意:断言规则为正则表达式,若断言非正则表达式的内容,需要自行进行转义,例如断言“(”符号,则需要传入“\\(”。
方法中各个参数的解释,可参考extractKey(String)
方法
assertRegex
- 断言规则paramName
- 搜索变量public String extractKey(SearchType searchType, String paramName, String xpath, String leftBoundary, String rightBoundary, int index)
提取规则如下:
SearchType
枚举,否则抛出HttpResponseException
异常SearchType.MESSAGE
或SearchType.STATUS
时,则paramName不生效SearchType.HEADER
时,若指定了paramName内容,则获取响应头对应键的内容(没有该键值则返回空串);若未指定paramName参数,则将响应头以AbstractMap.toString()
方法的形式返回SearchType.BODY
时,存在以下情况:
MessageType.JSON
类型时,其xpath参数不生效,判断paramName参数的方式与SearchType.HEADER
类似MessageType.XML
或MessageType.HTML
类型时,则优先判断xpath参数,若其为空时,则再判断paramName参数,其判断方式与SearchType.HEADER
类似searchType
- 提词搜索范围枚举paramName
- 提取内容参数名xpath
- 提取内容xpathleftBoundary
- 提取内容左边界rightBoundary
- 提取内容右边界index
- 边界提取到多条内容时指定的获取下标public String extractKey(SearchType searchType, String leftBoundary, String rightBoundary, int index)
提取规则如下:
searchType
- 提词搜索范围枚举leftBoundary
- 提取内容左边界rightBoundary
- 提取内容右边界index
- 边界提取到多条内容时指定的获取下标public String extractKey(SearchType searchType, String paramName, String xpath)
提取规则如下:
SearchType
枚举,否则抛出HttpResponseException
异常SearchType.MESSAGE
或SearchType.STATUS
时,则paramName不生效SearchType.HEADER
时,若指定了paramName内容,则获取响应头对应键的内容(没有该键值则返回空串);若未指定paramName参数,则将响应头以AbstractMap.toString()
方法的形式返回SearchType.BODY
时,存在以下情况:
MessageType.JSON
类型时,其xpath参数不生效,判断paramName参数的方式与SearchType.HEADER
类似MessageType.XML
或MessageType.HTML
类型时,则优先判断xpath参数,若其为空时,则再判断paramName参数,其判断方式与SearchType.HEADER
类似searchType
- 提词搜索范围枚举paramName
- 提取内容参数名xpath
- 提取内容xpathpublic String extractKey(SearchType searchType, String paramName)
提取规则如下:
SearchType
枚举,否则抛出HttpResponseException
异常SearchType.MESSAGE
或SearchType.STATUS
时,则paramName不生效SearchType.HEADER
时,若指定了paramName内容,则获取响应头对应键的内容(没有该键值则返回空串);若未指定paramName参数,则将响应头以AbstractMap.toString()
方法的形式返回SearchType.BODY
时,存在以下情况:
MessageType.JSON
或MessageType.XML
或MessageType.HTML
类型时,其判断paramName参数的方式与SearchType.HEADER
类似searchType
- 提词搜索范围枚举paramName
- 提取内容参数名public String extractKey(String paramName)
提取规则如下:
MessageType.JSON
或MessageType.XML
或MessageType.HTML
类型时,其判断paramName参数的方式与SearchType.HEADER
类似paramName
- 提取内容参数名Copyright © 2024. All rights reserved.