Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
kt-keystone
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Matrix
kt-keystone
Commits
b9e78ad7
提交
b9e78ad7
authored
1月 25, 2022
作者:
黄夏豪
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(base): 将Http执行器和testCase执行器和checkPoint执行器做成的单例模式
上级
5747fd51
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
116 行增加
和
127 行删除
+116
-127
CheckPointActuator.java
...a/org/matrix/actuators/checkpoint/CheckPointActuator.java
+23
-25
HttpClientActuator.java
...a/org/matrix/actuators/httpclient/HttpClientActuator.java
+10
-29
CaseActuator.java
.../main/java/org/matrix/actuators/usecase/CaseActuator.java
+12
-15
HttpRequestConfig.java
...se/src/main/java/org/matrix/config/HttpRequestConfig.java
+57
-44
application.yml
kt-base/src/main/resources/application.yml
+3
-2
CaseActuatorTest.java
.../test/java/org/matrix/actuators/sql/CaseActuatorTest.java
+5
-6
HttpClientActuatorTest.java
...java/org/matrix/actuators/sql/HttpClientActuatorTest.java
+6
-6
没有找到文件。
kt-base/src/main/java/org/matrix/actuators/checkpoint/CheckPointActuator.java
浏览文件 @
b9e78ad7
...
@@ -14,8 +14,10 @@ import org.matrix.actuators.Actuator;
...
@@ -14,8 +14,10 @@ import org.matrix.actuators.Actuator;
import
org.matrix.actuators.httpclient.HttpResponseDetail
;
import
org.matrix.actuators.httpclient.HttpResponseDetail
;
import
org.matrix.actuators.util.CompleteExpressionUtil
;
import
org.matrix.actuators.util.CompleteExpressionUtil
;
import
org.matrix.exception.CheckPointException
;
import
org.matrix.exception.CheckPointException
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Component
;
import
javax.script.ScriptEngine
;
import
javax.script.ScriptEngine
;
import
javax.script.ScriptException
;
import
javax.script.ScriptException
;
...
@@ -31,27 +33,24 @@ import java.util.List;
...
@@ -31,27 +33,24 @@ import java.util.List;
*
*
* @author huangxiahao
* @author huangxiahao
*/
*/
@Component
public
class
CheckPointActuator
implements
Actuator
{
public
class
CheckPointActuator
implements
Actuator
{
private
final
String
baseJs
;
@Value
(
"baseJs"
)
private
String
baseJs
;
private
Long
projectId
;
public
CheckPointActuator
()
{
ClassPathResource
cpr
=
new
ClassPathResource
(
baseJs
);
private
Long
envId
;
public
CheckPointActuator
(
String
baseJsPath
,
Long
env
,
Long
projectId
)
{
ClassPathResource
cpr
=
new
ClassPathResource
(
baseJsPath
);
try
{
try
{
this
.
baseJs
=
IOUtils
.
toString
(
cpr
.
getInputStream
(),
StandardCharsets
.
UTF_8
);
this
.
baseJs
=
IOUtils
.
toString
(
cpr
.
getInputStream
(),
StandardCharsets
.
UTF_8
);
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
CheckPointException
(
"初始JS加载失败"
);
throw
new
CheckPointException
(
"初始JS加载失败"
);
}
}
this
.
projectId
=
projectId
;
this
.
envId
=
env
;
}
}
public
CheckPointResult
httpCheck
(
HttpResponseDetail
httpResponseDetail
,
CheckPoint
checkPoint
)
{
public
CheckPointResult
httpCheck
(
HttpResponseDetail
httpResponseDetail
,
CheckPoint
checkPoint
,
Long
envId
,
Long
projectId
)
{
CheckPointResult
checkPointResult
=
new
CheckPointResult
();
CheckPointResult
checkPointResult
=
new
CheckPointResult
();
//根据checkPoint里的细节数据开始检测
//根据checkPoint里的细节数据开始检测
//异常检查点检测
//异常检查点检测
...
@@ -64,22 +63,21 @@ public class CheckPointActuator implements Actuator {
...
@@ -64,22 +63,21 @@ public class CheckPointActuator implements Actuator {
}
}
//包含检查点检测
//包含检查点检测
if
(!
StringUtils
.
isEmpty
(
checkPoint
.
getContainCheckPoint
())){
if
(!
StringUtils
.
isEmpty
(
checkPoint
.
getContainCheckPoint
())){
checkPointResult
.
addCheckPointResultDetail
(
containCheck
(
checkPoint
.
getContainCheckPoint
(),
httpResponseDetail
.
getResponseBody
()));
checkPointResult
.
addCheckPointResultDetail
(
containCheck
(
checkPoint
.
getContainCheckPoint
(),
httpResponseDetail
.
getResponseBody
()
,
envId
,
projectId
));
}
}
//不包含检查点检测
//不包含检查点检测
if
(!
StringUtils
.
isEmpty
(
checkPoint
.
getNoContainCheckPoint
())){
if
(!
StringUtils
.
isEmpty
(
checkPoint
.
getNoContainCheckPoint
())){
checkPointResult
.
addCheckPointResultDetail
(
noContainCheck
(
checkPoint
.
getNoContainCheckPoint
(),
httpResponseDetail
.
getResponseBody
()));
checkPointResult
.
addCheckPointResultDetail
(
noContainCheck
(
checkPoint
.
getNoContainCheckPoint
(),
httpResponseDetail
.
getResponseBody
()
,
envId
,
projectId
));
}
}
//JsonPath检查点检测
//JsonPath检查点检测
if
(!
StringUtils
.
isEmpty
(
checkPoint
.
getJsonPathCheckPoint
()))
{
if
(!
StringUtils
.
isEmpty
(
checkPoint
.
getJsonPathCheckPoint
()))
{
Object
jsonObject
=
Configuration
.
defaultConfiguration
().
jsonProvider
().
parse
(
httpResponseDetail
.
getResponseBody
());
Object
jsonObject
=
Configuration
.
defaultConfiguration
().
jsonProvider
().
parse
(
httpResponseDetail
.
getResponseBody
());
checkPointResult
.
addCheckPointResultDetail
(
jsonPathCheck
(
checkPoint
.
getJsonPathCheckPoint
(),
jsonObject
));
checkPointResult
.
addCheckPointResultDetail
(
jsonPathCheck
(
checkPoint
.
getJsonPathCheckPoint
(),
jsonObject
,
envId
,
projectId
));
}
}
return
checkPointResult
;
return
checkPointResult
;
}
}
public
CheckPointResultDetail
jsonPathCheck
(
String
jsonPathCheckPoint
,
Object
jsonObject
)
{
public
CheckPointResultDetail
jsonPathCheck
(
String
jsonPathCheckPoint
,
Object
jsonObject
,
Long
envId
,
Long
projectId
)
{
String
beforeExpression
=
jsonPathCheckPoint
;
String
expression
=
jsonPathCheckPoint
;
String
expression
=
jsonPathCheckPoint
;
try
{
try
{
expression
=
CompleteExpressionUtil
.
completeDynamicVariable
(
expression
=
CompleteExpressionUtil
.
completeDynamicVariable
(
...
@@ -117,26 +115,26 @@ public class CheckPointActuator implements Actuator {
...
@@ -117,26 +115,26 @@ public class CheckPointActuator implements Actuator {
return
new
CheckPointResultDetail
(
return
new
CheckPointResultDetail
(
true
,
true
,
CheckPointType
.
JSONPATH_CHECK
,
CheckPointType
.
JSONPATH_CHECK
,
beforeExpression
,
jsonPathCheckPoint
,
expression
,
expression
,
String
.
format
(
"JsonPath检查点,检查通过"
)
"JsonPath检查点,检查通过"
);
);
}
else
{
}
else
{
return
new
CheckPointResultDetail
(
return
new
CheckPointResultDetail
(
false
,
false
,
CheckPointType
.
JSONPATH_CHECK
,
CheckPointType
.
JSONPATH_CHECK
,
beforeExpression
,
jsonPathCheckPoint
,
expression
,
expression
,
String
.
format
(
"JsonPath检查点,检查未通过,计算结果为false"
)
"JsonPath检查点,检查未通过,计算结果为false"
);
);
}
}
}
else
{
}
else
{
return
new
CheckPointResultDetail
(
return
new
CheckPointResultDetail
(
false
,
false
,
CheckPointType
.
JSONPATH_CHECK
,
CheckPointType
.
JSONPATH_CHECK
,
beforeExpression
,
jsonPathCheckPoint
,
expression
,
expression
,
String
.
format
(
"JsonPath检查点,检查未通过,JsonPath的返回值不是一个布尔类型"
)
"JsonPath检查点,检查未通过,JsonPath的返回值不是一个布尔类型"
);
);
}
}
}
catch
(
ScriptException
e
)
{
}
catch
(
ScriptException
e
)
{
...
@@ -151,7 +149,7 @@ public class CheckPointActuator implements Actuator {
...
@@ -151,7 +149,7 @@ public class CheckPointActuator implements Actuator {
}
}
public
List
<
CheckPointResultDetail
>
containCheck
(
String
containCheckPoint
,
String
responseBody
)
{
public
List
<
CheckPointResultDetail
>
containCheck
(
String
containCheckPoint
,
String
responseBody
,
Long
envId
,
Long
projectId
)
{
List
<
CheckPointResultDetail
>
resultDetails
=
new
ArrayList
<>();
List
<
CheckPointResultDetail
>
resultDetails
=
new
ArrayList
<>();
String
[]
split
=
containCheckPoint
.
split
(
","
);
String
[]
split
=
containCheckPoint
.
split
(
","
);
for
(
String
containExpression
:
split
)
{
for
(
String
containExpression
:
split
)
{
...
@@ -174,14 +172,14 @@ public class CheckPointActuator implements Actuator {
...
@@ -174,14 +172,14 @@ public class CheckPointActuator implements Actuator {
CheckPointType
.
CONTAIN_CHECK
,
CheckPointType
.
CONTAIN_CHECK
,
containExpression
,
containExpression
,
parseContainExpression
,
parseContainExpression
,
String
.
format
(
"包含检查点,检查未通过,不包含目标值"
)
"包含检查点,检查未通过,不包含目标值"
));
));
}
}
}
}
return
resultDetails
;
return
resultDetails
;
}
}
public
List
<
CheckPointResultDetail
>
noContainCheck
(
String
noContainCheckPoint
,
String
responseBody
)
{
public
List
<
CheckPointResultDetail
>
noContainCheck
(
String
noContainCheckPoint
,
String
responseBody
,
Long
envId
,
Long
projectId
)
{
List
<
CheckPointResultDetail
>
resultDetails
=
new
ArrayList
<>();
List
<
CheckPointResultDetail
>
resultDetails
=
new
ArrayList
<>();
String
[]
split
=
noContainCheckPoint
.
split
(
","
);
String
[]
split
=
noContainCheckPoint
.
split
(
","
);
for
(
String
noContainExpression
:
split
)
{
for
(
String
noContainExpression
:
split
)
{
...
@@ -196,7 +194,7 @@ public class CheckPointActuator implements Actuator {
...
@@ -196,7 +194,7 @@ public class CheckPointActuator implements Actuator {
CheckPointType
.
NO_CONTAIN_CHECK
,
CheckPointType
.
NO_CONTAIN_CHECK
,
noContainExpression
,
noContainExpression
,
parseNoContainExpression
,
parseNoContainExpression
,
String
.
format
(
"不包含检查点,检查未通过"
)
"不包含检查点,检查未通过"
));
));
}
else
{
}
else
{
resultDetails
.
add
(
new
CheckPointResultDetail
(
resultDetails
.
add
(
new
CheckPointResultDetail
(
...
...
kt-base/src/main/java/org/matrix/actuators/httpclient/HttpClientActuator.java
浏览文件 @
b9e78ad7
...
@@ -22,8 +22,10 @@ import org.apache.http.impl.client.CloseableHttpClient;
...
@@ -22,8 +22,10 @@ import org.apache.http.impl.client.CloseableHttpClient;
import
org.apache.http.message.BasicNameValuePair
;
import
org.apache.http.message.BasicNameValuePair
;
import
org.apache.http.util.EntityUtils
;
import
org.apache.http.util.EntityUtils
;
import
org.matrix.exception.HttpRequestException
;
import
org.matrix.exception.HttpRequestException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Component
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileInputStream
;
...
@@ -42,41 +44,20 @@ import java.util.List;
...
@@ -42,41 +44,20 @@ import java.util.List;
* todo 打印LOG
* todo 打印LOG
* @author HuangXiahao
* @author HuangXiahao
**/
**/
@Component
public
class
HttpClientActuator
implements
Actuator
{
public
class
HttpClientActuator
implements
Actuator
{
HttpRequestConfig
config
;
final
CookieStore
cookieStore
;
CloseableHttpClient
client
;
CloseableHttpClient
client
;
private
final
Long
projectId
;
public
HttpClientActuator
(
CloseableHttpClient
client
)
{
this
.
client
=
client
;
private
final
Long
envId
;
public
HttpClientActuator
(
HttpRequestConfig
config
,
Long
env
,
Long
projectId
)
{
this
.
config
=
config
;
this
.
cookieStore
=
config
.
cookieStore
();
try
{
this
.
client
=
config
.
client
();
}
catch
(
KeyStoreException
|
NoSuchAlgorithmException
|
KeyManagementException
e
)
{
e
.
printStackTrace
();
}
this
.
projectId
=
projectId
;
this
.
envId
=
env
;
}
public
CloseableHttpClient
getClient
()
{
return
client
;
}
}
/**
/**
* 初始化请求内数据并补全动态变量
* 初始化请求内数据并补全动态变量
*/
*/
private
void
completeHttpRequestDetail
(
HttpRequestDetail
httpRequestDetail
){
private
void
completeHttpRequestDetail
(
HttpRequestDetail
httpRequestDetail
,
Long
envId
,
Long
projectId
){
for
(
RequestHeader
header
:
httpRequestDetail
.
getHeaders
())
{
for
(
RequestHeader
header
:
httpRequestDetail
.
getHeaders
())
{
//todo 李迪凡 将header中的key和value里的 动态变量补全
//todo 李迪凡 将header中的key和value里的 动态变量补全
//获取动态变量,并将动态变量替换进去
//获取动态变量,并将动态变量替换进去
...
@@ -128,8 +109,8 @@ public class HttpClientActuator implements Actuator {
...
@@ -128,8 +109,8 @@ public class HttpClientActuator implements Actuator {
* 发起Http请求
* 发起Http请求
*
*
*/
*/
public
HttpResponseDetail
sendHttpRequest
(
HttpRequestDetail
httpRequestDetail
)
{
public
HttpResponseDetail
sendHttpRequest
(
HttpRequestDetail
httpRequestDetail
,
Long
envId
,
Long
projectId
)
{
completeHttpRequestDetail
(
httpRequestDetail
);
completeHttpRequestDetail
(
httpRequestDetail
,
envId
,
projectId
);
CloseableHttpResponse
response
=
null
;
CloseableHttpResponse
response
=
null
;
Date
startTime
=
new
Date
();
Date
startTime
=
new
Date
();
String
url
=
initUrlString
(
httpRequestDetail
);
String
url
=
initUrlString
(
httpRequestDetail
);
...
@@ -140,7 +121,7 @@ public class HttpClientActuator implements Actuator {
...
@@ -140,7 +121,7 @@ public class HttpClientActuator implements Actuator {
}
}
requestBase
.
setHeaders
(
httpRequestDetail
.
getHeadersArray
());
requestBase
.
setHeaders
(
httpRequestDetail
.
getHeadersArray
());
try
{
try
{
response
=
getClient
()
.
execute
(
requestBase
);
response
=
client
.
execute
(
requestBase
);
Date
endTime
=
new
Date
();
Date
endTime
=
new
Date
();
return
new
HttpResponseDetail
(
return
new
HttpResponseDetail
(
response
,
response
,
...
...
kt-base/src/main/java/org/matrix/actuators/usecase/CaseActuator.java
浏览文件 @
b9e78ad7
...
@@ -11,6 +11,8 @@ import org.matrix.actuators.checkpoint.CheckPoint;
...
@@ -11,6 +11,8 @@ import org.matrix.actuators.checkpoint.CheckPoint;
import
org.matrix.actuators.checkpoint.CheckPointResult
;
import
org.matrix.actuators.checkpoint.CheckPointResult
;
import
org.matrix.actuators.httpclient.HttpRequestDetail
;
import
org.matrix.actuators.httpclient.HttpRequestDetail
;
import
org.matrix.actuators.httpclient.HttpResponseDetail
;
import
org.matrix.actuators.httpclient.HttpResponseDetail
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
/**
/**
* 测试用例执行器
* 测试用例执行器
...
@@ -18,41 +20,36 @@ import org.matrix.actuators.httpclient.HttpResponseDetail;
...
@@ -18,41 +20,36 @@ import org.matrix.actuators.httpclient.HttpResponseDetail;
*
*
* @author huangxiahao
* @author huangxiahao
*/
*/
@Component
public
class
CaseActuator
implements
Actuator
{
public
class
CaseActuator
implements
Actuator
{
private
Long
projectId
;
private
final
CheckPointActuator
checkPointActuator
;
private
Long
envId
;
private
final
HttpClientActuator
httpClientActuator
;
private
CheckPointActuator
checkPointActuator
;
private
HttpClientActuator
httpClientActuator
;
private
final
String
baseJsPath
=
"syntaxCheck.js"
;
private
final
String
baseJsPath
=
"syntaxCheck.js"
;
public
CaseActuator
(
Long
envId
,
Long
projectId
)
{
public
CaseActuator
(
CheckPointActuator
checkPointActuator
,
HttpClientActuator
httpClientActuator
)
{
this
.
projectId
=
projectId
;
this
.
checkPointActuator
=
checkPointActuator
;
this
.
envId
=
envId
;
this
.
httpClientActuator
=
httpClientActuator
;
checkPointActuator
=
new
CheckPointActuator
(
baseJsPath
,
envId
,
projectId
);
httpClientActuator
=
new
HttpClientActuator
(
new
HttpRequestConfig
(),
envId
,
projectId
);
}
}
/**
/**
* 执行测试用例
* 执行测试用例
*/
*/
public
TestCaseExecuteResult
executeTestCase
(
TestCase
testCase
)
{
public
TestCaseExecuteResult
executeTestCase
(
TestCase
testCase
,
Long
envId
,
Long
projectId
)
{
//todo 李迪凡 执行前置动作
//todo 李迪凡 执行前置动作
//执行测试用例的本体内容
//执行测试用例的本体内容
BaseTestCase
ResponseDetail
baseTestCaseResponseDetail
=
null
;
Http
ResponseDetail
baseTestCaseResponseDetail
=
null
;
if
(
testCase
.
getType
().
equals
(
TestCaseTypeEnum
.
HTTP
.
getValue
()))
{
if
(
testCase
.
getType
().
equals
(
TestCaseTypeEnum
.
HTTP
.
getValue
()))
{
HttpRequestDetail
httpRequestDetail
=
JSON
.
parseObject
(
testCase
.
getDetail
(),
HttpRequestDetail
.
class
);
HttpRequestDetail
httpRequestDetail
=
JSON
.
parseObject
(
testCase
.
getDetail
(),
HttpRequestDetail
.
class
);
baseTestCaseResponseDetail
=
httpClientActuator
.
sendHttpRequest
(
httpRequestDetail
);
baseTestCaseResponseDetail
=
httpClientActuator
.
sendHttpRequest
(
httpRequestDetail
,
envId
,
projectId
);
}
}
//todo 李迪凡 执行测试后动作
//todo 李迪凡 执行测试后动作
//进行检验
//进行检验
CheckPointResult
checkPointResult
=
null
;
CheckPointResult
checkPointResult
=
null
;
if
(
testCase
.
getType
().
equals
(
TestCaseTypeEnum
.
HTTP
.
getValue
()))
{
if
(
testCase
.
getType
().
equals
(
TestCaseTypeEnum
.
HTTP
.
getValue
()))
{
checkPointResult
=
checkPointActuator
.
httpCheck
(
(
HttpResponseDetail
)
baseTestCaseResponseDetail
,
getCheckPointEntity
(
testCase
)
);
checkPointResult
=
checkPointActuator
.
httpCheck
(
baseTestCaseResponseDetail
,
getCheckPointEntity
(
testCase
),
envId
,
projectId
);
}
}
//todo 李迪凡 执行后置动作
//todo 李迪凡 执行后置动作
return
new
TestCaseExecuteResult
(
baseTestCaseResponseDetail
,
checkPointResult
);
return
new
TestCaseExecuteResult
(
baseTestCaseResponseDetail
,
checkPointResult
);
...
...
kt-base/src/main/java/org/matrix/config/HttpRequestConfig.java
浏览文件 @
b9e78ad7
package
org
.
matrix
.
config
;
package
org
.
matrix
.
config
;
import
org.apache.http.client.CookieStore
;
import
org.apache.http.client.CookieStore
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.config.Registry
;
import
org.apache.http.config.Registry
;
import
org.apache.http.config.RegistryBuilder
;
import
org.apache.http.config.RegistryBuilder
;
...
@@ -16,6 +17,9 @@ import org.apache.http.impl.client.CloseableHttpClient;
...
@@ -16,6 +17,9 @@ import org.apache.http.impl.client.CloseableHttpClient;
import
org.apache.http.impl.client.DefaultHttpRequestRetryHandler
;
import
org.apache.http.impl.client.DefaultHttpRequestRetryHandler
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.impl.conn.PoolingHttpClientConnectionManager
;
import
org.apache.http.impl.conn.PoolingHttpClientConnectionManager
;
import
org.matrix.exception.GlobalException
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.stereotype.Component
;
import
javax.net.ssl.SSLContext
;
import
javax.net.ssl.SSLContext
;
import
java.security.KeyManagementException
;
import
java.security.KeyManagementException
;
...
@@ -24,62 +28,71 @@ import java.security.NoSuchAlgorithmException;
...
@@ -24,62 +28,71 @@ import java.security.NoSuchAlgorithmException;
import
java.security.cert.X509Certificate
;
import
java.security.cert.X509Certificate
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
/**
* httpClient配置
* @author huangxiahao
*/
@Component
public
class
HttpRequestConfig
{
public
class
HttpRequestConfig
{
/**
/**
* HttpClient的Cookie管理器,可在其他类中调用该Bean清空Cookie缓存。
* HttpClient的Cookie管理器,可在其他类中调用该Bean清空Cookie缓存。
* 主要针对某些网站Cookie多次使用会造成Cookie失效的问题
* 主要针对某些网站Cookie多次使用会造成Cookie失效的问题
* @return
*/
*/
public
CookieStore
cookieStore
(){
public
CookieStore
cookieStore
(){
CookieStore
cookieStore
=
new
BasicCookieStore
();
return
new
BasicCookieStore
();
return
cookieStore
;
}
}
/**
/**
* HttpClient的配置,所返回的CloseableHttpClient可以用来发送网络请求。
* HttpClient的配置,所返回的CloseableHttpClient可以用来发送网络请求。
* @return
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
*/
public
CloseableHttpClient
client
()
throws
KeyStoreException
,
NoSuchAlgorithmException
,
KeyManagementException
{
public
CloseableHttpClient
client
()
{
TrustStrategy
acceptingTrustStrategy
=
(
X509Certificate
[]
x509Certificates
,
String
s
)
->
true
;
try
{
SSLContext
sslContext
=
org
.
apache
.
http
.
ssl
.
SSLContexts
.
custom
()
TrustStrategy
acceptingTrustStrategy
=
(
X509Certificate
[]
x509Certificates
,
String
s
)
->
true
;
.
loadTrustMaterial
(
null
,
acceptingTrustStrategy
)
SSLContext
sslContext
=
org
.
apache
.
http
.
ssl
.
SSLContexts
.
custom
()
.
build
();
.
loadTrustMaterial
(
null
,
acceptingTrustStrategy
)
SSLConnectionSocketFactory
csf
=
new
SSLConnectionSocketFactory
(
sslContext
.
build
();
,
null
,
null
,
NoopHostnameVerifier
.
INSTANCE
);
SSLConnectionSocketFactory
csf
=
new
SSLConnectionSocketFactory
(
sslContext
SocketConfig
socketConfig
=
SocketConfig
.
custom
()
,
null
,
null
,
NoopHostnameVerifier
.
INSTANCE
);
.
setSoKeepAlive
(
false
)
SocketConfig
socketConfig
=
SocketConfig
.
custom
()
.
setSoLinger
(
1
)
.
setSoKeepAlive
(
false
)
.
setSoReuseAddress
(
true
)
.
setSoLinger
(
1
)
.
setSoTimeout
(
5000
)
.
setSoReuseAddress
(
true
)
.
setTcpNoDelay
(
true
).
build
();
.
setSoTimeout
(
5000
)
Registry
<
ConnectionSocketFactory
>
registry
=
RegistryBuilder
.<
ConnectionSocketFactory
>
create
()
.
setTcpNoDelay
(
true
).
build
();
.
register
(
"http"
,
PlainConnectionSocketFactory
.
getSocketFactory
())
Registry
<
ConnectionSocketFactory
>
registry
=
RegistryBuilder
.<
ConnectionSocketFactory
>
create
()
.
register
(
"https"
,
csf
)
.
register
(
"http"
,
PlainConnectionSocketFactory
.
getSocketFactory
())
.
build
();
.
register
(
"https"
,
csf
)
PoolingHttpClientConnectionManager
connectionManager
=
new
PoolingHttpClientConnectionManager
(
registry
);
.
build
();
//最大连接数3000
PoolingHttpClientConnectionManager
connectionManager
=
new
PoolingHttpClientConnectionManager
(
registry
);
connectionManager
.
setMaxTotal
(
3000
);
//最大连接数3000
//路由链接数400
connectionManager
.
setMaxTotal
(
3000
);
connectionManager
.
setDefaultMaxPerRoute
(
400
);
//路由链接数400
RequestConfig
requestConfig
=
RequestConfig
.
custom
()
connectionManager
.
setDefaultMaxPerRoute
(
400
);
.
setMaxRedirects
(
1
)
RequestConfig
requestConfig
=
RequestConfig
.
custom
()
.
setRedirectsEnabled
(
true
)
.
setMaxRedirects
(
1
)
.
build
();
.
setRedirectsEnabled
(
true
)
return
HttpClients
.
custom
().
setDefaultRequestConfig
(
requestConfig
)
.
build
();
.
setConnectionManager
(
connectionManager
)
return
HttpClients
.
custom
().
setDefaultRequestConfig
(
requestConfig
)
.
setDefaultSocketConfig
(
socketConfig
)
.
setConnectionManager
(
connectionManager
)
.
setConnectionReuseStrategy
(
new
NoConnectionReuseStrategy
())
.
setDefaultSocketConfig
(
socketConfig
)
.
evictExpiredConnections
()
.
setConnectionReuseStrategy
(
new
NoConnectionReuseStrategy
())
.
evictIdleConnections
(
30
,
TimeUnit
.
SECONDS
)
.
evictExpiredConnections
()
.
setRetryHandler
(
new
DefaultHttpRequestRetryHandler
())
.
evictIdleConnections
(
30
,
TimeUnit
.
SECONDS
)
.
disableAutomaticRetries
()
.
setRetryHandler
(
new
DefaultHttpRequestRetryHandler
())
.
disableRedirectHandling
()
.
disableAutomaticRetries
()
.
setDefaultCookieStore
(
cookieStore
())
.
disableRedirectHandling
()
.
build
();
.
setDefaultCookieStore
(
cookieStore
())
.
build
();
}
catch
(
Exception
e
){
throw
new
GlobalException
(
"初始化HttpClient引擎失败"
);
}
}
}
@Bean
CloseableHttpClient
getHttpClient
()
{
return
client
();
}
}
}
kt-base/src/main/resources/application.yml
浏览文件 @
b9e78ad7
...
@@ -13,4 +13,5 @@ spring:
...
@@ -13,4 +13,5 @@ spring:
password
:
root
password
:
root
mybatis-plus
:
mybatis-plus
:
type-enums-package
:
org.matrix.enums
type-enums-package
:
org.matrix.enums
\ No newline at end of file
baseJsPath
:
syntaxCheck.js
\ No newline at end of file
kt-base/src/test/java/org/matrix/actuators/sql/CaseActuatorTest.java
浏览文件 @
b9e78ad7
...
@@ -3,9 +3,11 @@ package org.matrix.actuators.sql;
...
@@ -3,9 +3,11 @@ package org.matrix.actuators.sql;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.matrix.actuators.httpclient.HttpClientActuator
;
import
org.matrix.actuators.usecase.CaseActuator
;
import
org.matrix.actuators.usecase.CaseActuator
;
import
org.matrix.actuators.usecase.TestCaseExecuteResult
;
import
org.matrix.actuators.usecase.TestCaseExecuteResult
;
import
org.matrix.database.entity.TestCase
;
import
org.matrix.database.entity.TestCase
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.test.context.junit4.SpringRunner
;
...
@@ -13,13 +15,11 @@ import org.springframework.test.context.junit4.SpringRunner;
...
@@ -13,13 +15,11 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@SpringBootTest
class
CaseActuatorTest
{
class
CaseActuatorTest
{
@Autowired
CaseActuator
caseActuator
;
@Test
@Test
void
test
(){
void
test
(){
CaseActuator
httpClientActuator
=
new
CaseActuator
(
1
l
,
1
l
);
String
json
=
"{\n"
+
String
json
=
"{\n"
+
" \"url\":\"http://127.0.0.1:8080/test/sendMessage\",\n"
+
" \"url\":\"http://127.0.0.1:8080/test/sendMessage\",\n"
+
" \"method\":\"GET\",\n"
+
" \"method\":\"GET\",\n"
+
...
@@ -47,8 +47,7 @@ class CaseActuatorTest {
...
@@ -47,8 +47,7 @@ class CaseActuatorTest {
testCase
.
setContainCheckpoint
(
"张三,李四"
);
testCase
.
setContainCheckpoint
(
"张三,李四"
);
testCase
.
setNoContainCheckpoint
(
"张三,李四"
);
testCase
.
setNoContainCheckpoint
(
"张三,李四"
);
testCase
.
setJsonpathCheckpoint
(
"contains({$..category},'${componentName}[0]')"
);
testCase
.
setJsonpathCheckpoint
(
"contains({$..category},'${componentName}[0]')"
);
TestCaseExecuteResult
testCaseExecuteResult
=
caseActuator
.
executeTestCase
(
testCase
,
1L
,
1L
);
TestCaseExecuteResult
testCaseExecuteResult
=
httpClientActuator
.
executeTestCase
(
testCase
);
System
.
out
.
println
(
testCaseExecuteResult
);
System
.
out
.
println
(
testCaseExecuteResult
);
...
...
kt-base/src/test/java/org/matrix/actuators/sql/HttpClientActuatorTest.java
浏览文件 @
b9e78ad7
...
@@ -7,6 +7,7 @@ import org.matrix.actuators.httpclient.HttpClientActuator;
...
@@ -7,6 +7,7 @@ import org.matrix.actuators.httpclient.HttpClientActuator;
import
org.matrix.actuators.httpclient.HttpRequestDetail
;
import
org.matrix.actuators.httpclient.HttpRequestDetail
;
import
org.matrix.actuators.httpclient.HttpResponseDetail
;
import
org.matrix.actuators.httpclient.HttpResponseDetail
;
import
org.matrix.config.HttpRequestConfig
;
import
org.matrix.config.HttpRequestConfig
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.test.context.junit4.SpringRunner
;
...
@@ -14,13 +15,12 @@ import org.springframework.test.context.junit4.SpringRunner;
...
@@ -14,13 +15,12 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@SpringBootTest
class
HttpClientActuatorTest
{
class
HttpClientActuatorTest
{
@Autowired
HttpClientActuator
httpClientActuator
;
@Test
@Test
void
test
(){
void
test
(){
HttpClientActuator
httpClientActuator
=
new
HttpClientActuator
(
new
HttpRequestConfig
(),
1
l
,
1
l
);
String
json
=
"{\n"
+
String
json
=
"{\n"
+
" \"url\": \"http://127.0.0.1:8080/test/tableName\",\n"
+
" \"url\": \"http://127.0.0.1:8080/test/tableName\",\n"
+
" \"method\": \"GET\",\n"
+
" \"method\": \"GET\",\n"
+
...
@@ -35,7 +35,7 @@ class HttpClientActuatorTest {
...
@@ -35,7 +35,7 @@ class HttpClientActuatorTest {
"}"
;
"}"
;
HttpRequestDetail
httpRequestDetail1
=
JSON
.
parseObject
(
json
,
HttpRequestDetail
.
class
);
HttpRequestDetail
httpRequestDetail1
=
JSON
.
parseObject
(
json
,
HttpRequestDetail
.
class
);
HttpRequestDetail
httpRequestDetail
=
JSON
.
parseObject
(
json
,
HttpRequestDetail
.
class
);
HttpRequestDetail
httpRequestDetail
=
JSON
.
parseObject
(
json
,
HttpRequestDetail
.
class
);
HttpResponseDetail
httpResponseDetail
=
httpClientActuator
.
sendHttpRequest
(
httpRequestDetail
);
HttpResponseDetail
httpResponseDetail
=
httpClientActuator
.
sendHttpRequest
(
httpRequestDetail
,
1
l
,
1
l
);
System
.
out
.
println
(
httpResponseDetail
);
System
.
out
.
println
(
httpResponseDetail
);
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论