Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
kt-keystone
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Matrix
kt-keystone
Commits
22e81adc
提交
22e81adc
authored
1月 18, 2022
作者:
黄夏豪
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(base): 补充了异常情况的处理
上级
047c7faf
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
56 行增加
和
72 行删除
+56
-72
HttpResponseDetail.java
...ava/org/matrix/entity/httpRequest/HttpResponseDetail.java
+2
-1
CheckPointException.java
...c/main/java/org/matrix/exception/CheckPointException.java
+12
-0
HttpRequestException.java
.../main/java/org/matrix/exception/HttpRequestException.java
+12
-0
CheckPointUtil.java
kt-base/src/main/java/org/matrix/util/CheckPointUtil.java
+6
-45
HttpClientUtil.java
kt-base/src/main/java/org/matrix/util/HttpClientUtil.java
+21
-24
AppTest.java
kt-base/src/test/java/org/matrix/AppTest.java
+3
-2
没有找到文件。
kt-base/src/main/java/org/matrix/entity/httpRequest/HttpResponseDetail.java
浏览文件 @
22e81adc
...
...
@@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.springframework.http.HttpStatus
;
@Data
@AllArgsConstructor
...
...
@@ -18,7 +19,7 @@ public class HttpResponseDetail {
private
String
responseBody
=
"error request"
;
@ApiModelProperty
(
"响应状态,例如:200、401、500"
)
private
Integer
statusCode
=
500
;
private
HttpStatus
statusCode
=
HttpStatus
.
INTERNAL_SERVER_ERROR
;
@ApiModelProperty
(
"响应时间,单位为 ms "
)
private
Long
responseTime
=
0
l
;
...
...
kt-base/src/main/java/org/matrix/exception/CheckPointException.java
0 → 100644
浏览文件 @
22e81adc
package
org
.
matrix
.
exception
;
/**
* @author huangxiahao
*/
public
class
CheckPointException
extends
RuntimeException
{
public
CheckPointException
(
String
message
){
super
(
message
);
}
}
kt-base/src/main/java/org/matrix/exception/HttpRequestException.java
0 → 100644
浏览文件 @
22e81adc
package
org
.
matrix
.
exception
;
/**
* @author huangxiahao
*/
public
class
HttpRequestException
extends
RuntimeException
{
public
HttpRequestException
(
String
message
){
super
(
message
);
}
}
kt-base/src/main/java/org/matrix/util/CheckPointUtil.java
浏览文件 @
22e81adc
...
...
@@ -13,6 +13,7 @@ import org.apache.commons.io.IOUtils;
import
org.apache.commons.lang3.StringUtils
;
import
org.matrix.entity.checkPoint.*
;
import
org.matrix.entity.httpRequest.HttpResponseDetail
;
import
org.matrix.exception.CheckPointException
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -28,7 +29,7 @@ import java.util.regex.Pattern;
* 数据检测点工具
* todo 打印LOG
*
* @author
Administrator
* @author
huangxiahao
*/
public
class
CheckPointUtil
{
...
...
@@ -39,8 +40,7 @@ public class CheckPointUtil {
try
{
this
.
baseJs
=
IOUtils
.
toString
(
cpr
.
getInputStream
(),
StandardCharsets
.
UTF_8
);
}
catch
(
IOException
e
)
{
//todo 抛出异常 初始JS加载失败
e
.
printStackTrace
();
throw
new
CheckPointException
(
"初始JS加载失败"
);
}
}
...
...
@@ -59,7 +59,6 @@ public class CheckPointUtil {
for
(
ContainCheckPoint
containCheckPoint
:
checkPoint
.
getContainCheckPoints
())
{
checkPointResult
.
addCheckPointResultDetail
(
containCheck
(
containCheckPoint
,
httpResponseDetail
.
getResponseBody
()));
}
//todo 数据库参数检测
//JsonPath检查点检测
if
(
checkPoint
.
getJsonPathCheckPoints
().
size
()
>
0
)
{
Object
jsonObject
=
Configuration
.
defaultConfiguration
().
jsonProvider
().
parse
(
httpResponseDetail
.
getResponseBody
());
...
...
@@ -106,13 +105,8 @@ public class CheckPointUtil {
);
}
}
catch
(
ScriptException
e
)
{
//todo 抛出异常初始化js引擎失败
e
.
printStackTrace
();
throw
new
CheckPointException
(
"初始化js引擎失败"
);
}
return
new
CheckPointResultDetail
(
false
,
"JsonPath检查点,检查未通过,空的表达式"
);
}
public
ScriptEngine
getScriptEngine
()
throws
ScriptException
{
...
...
@@ -136,39 +130,6 @@ public class CheckPointUtil {
return
result
;
}
public
static
void
main
(
String
[]
args
)
{
String
json
=
"{\n"
+
" \"school\": [\n"
+
" {\n"
+
" \"className\": \"一班\",\n"
+
" \"student\":[\n"
+
" \"一班的张三\",\"一班的李四\",\"一班的王五\",\"一班的赵六\",\"一班的田七\"\n"
+
" ]\n"
+
" },\n"
+
" {\n"
+
" \"className\": \"二班\",\n"
+
" \"student\":[\n"
+
" \"二班的张三\",\"二班的李四\",\"二班的王五\",\"二班的赵六\",\"二班的田七\"\n"
+
" ]\n"
+
" },\n"
+
" {\n"
+
" \"className\": \"三班\",\n"
+
" \"student\":[\n"
+
" \"三班的张三\",\"三班的李四\",\"三班的王五\",\"三班的赵六\",\"三班的田七\"\n"
+
" ]\n"
+
" }\n"
+
" ]\n"
+
"}"
;
Object
jsonObject
=
Configuration
.
defaultConfiguration
().
jsonProvider
().
parse
(
json
);
CheckPointUtil
checkPointUtil
=
new
CheckPointUtil
(
"baseJS.js"
);
CheckPointResultDetail
checkPointResultDetail
=
checkPointUtil
.
jsonPathCheck
(
new
JsonPathCheckPoint
(
"contains({$..className},\"一班\")"
),
jsonObject
);
System
.
out
.
println
(
checkPointResultDetail
);
}
public
CheckPointResultDetail
containCheck
(
ContainCheckPoint
containCheckPoint
,
String
responseBody
)
{
if
(
containCheckPoint
.
getIsContain
())
{
if
(
responseBody
.
contains
(
containCheckPoint
.
getValue
()))
{
...
...
@@ -222,14 +183,14 @@ public class CheckPointUtil {
}
else
{
return
new
CheckPointResultDetail
(
true
,
"
异常
检查点,检查通过"
"
不为空
检查点,检查通过"
);
}
}
public
CheckPointResultDetail
exceptionCheck
(
HttpResponseDetail
httpResponseDetail
)
{
if
(
httpResponseDetail
.
getStatusCode
()
==
HttpStatus
.
OK
.
value
())
{
if
(
httpResponseDetail
.
getStatusCode
()
.
value
()
==
HttpStatus
.
OK
.
value
())
{
return
new
CheckPointResultDetail
(
true
,
"异常检查点,检查通过"
...
...
kt-base/src/main/java/org/matrix/util/HttpClientUtil.java
浏览文件 @
22e81adc
...
...
@@ -19,7 +19,9 @@ import org.apache.http.impl.client.CloseableHttpClient;
import
org.apache.http.message.BasicNameValuePair
;
import
org.apache.http.util.EntityUtils
;
import
org.matrix.entity.httpRequest.*
;
import
org.matrix.exception.HttpRequestException
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpStatus
;
import
java.io.File
;
import
java.io.FileInputStream
;
...
...
@@ -32,12 +34,10 @@ import java.security.NoSuchAlgorithmException;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
* HttpClient调用封装
*
*
todo 打印LOG
* @author HuangXiahao
**/
public
class
HttpClientUtil
{
...
...
@@ -98,12 +98,11 @@ public class HttpClientUtil {
return
new
HttpResponseDetail
(
response
,
EntityUtils
.
toString
(
response
.
getEntity
(),
"UTF-8"
),
response
.
getStatusLine
().
getStatusCode
(
),
HttpStatus
.
valueOf
(
response
.
getStatusLine
().
getStatusCode
()
),
endTime
.
getTime
()
-
startTime
.
getTime
()
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
null
;
throw
new
HttpRequestException
(
String
.
format
(
"解析返回值失败,本次请求详细参数如下: %s "
,
httpRequestDetail
));
}
finally
{
//关闭请求request
closeRequest
(
requestBase
);
...
...
@@ -202,28 +201,25 @@ public class HttpClientUtil {
public
String
initUrlString
(
HttpRequestDetail
httpRequestDetail
)
{
String
url
=
httpRequestDetail
.
getUrl
();
try
{
switch
(
httpRequestDetail
.
getRequestType
())
{
case
QUERY:
if
(
httpRequestDetail
.
getMethod
().
equals
(
HttpMethod
.
GET
))
{
URIBuilder
uriBuilder
;
uriBuilder
=
new
URIBuilder
(
url
);
for
(
RequestBody
requestBody
:
httpRequestDetail
.
getRequestBodies
())
{
switch
(
requestBody
.
getType
())
{
case
TEXT:
uriBuilder
.
setParameter
(
requestBody
.
getKey
(),
requestBody
.
getValue
());
break
;
case
FILE:
throw
new
NullPointerException
(
"QUERY请求不能传入文件流"
);
default
:
}
if
(
httpRequestDetail
.
getRequestType
()
==
HttpRequestType
.
QUERY
)
{
if
(
httpRequestDetail
.
getMethod
().
equals
(
HttpMethod
.
GET
))
{
URIBuilder
uriBuilder
;
uriBuilder
=
new
URIBuilder
(
url
);
for
(
RequestBody
requestBody
:
httpRequestDetail
.
getRequestBodies
())
{
switch
(
requestBody
.
getType
())
{
case
TEXT:
uriBuilder
.
setParameter
(
requestBody
.
getKey
(),
requestBody
.
getValue
());
break
;
case
FILE:
throw
new
NullPointerException
(
"QUERY请求不能传入文件流"
);
default
:
}
url
=
uriBuilder
.
build
().
toString
();
}
break
;
default
:
url
=
uriBuilder
.
build
().
toString
()
;
}
}
}
catch
(
URISyntaxException
e
)
{
e
.
printStackTrace
(
);
throw
new
HttpRequestException
(
String
.
format
(
"URL格式不正确,不正确的URL为: %s"
,
url
)
);
}
return
url
;
}
...
...
@@ -269,6 +265,7 @@ public class HttpClientUtil {
response
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
throw
new
HttpRequestException
(
String
.
format
(
"关闭返回流失败"
));
}
}
}
...
...
kt-base/src/test/java/org/matrix/AppTest.java
浏览文件 @
22e81adc
package
org
.
matrix
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
org.junit.Test
;
import
org.junit.jupiter.api.Test
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
/**
* Unit test for simple App.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论