Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
holmes
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
holmes
Commits
0897055e
Commit
0897055e
authored
Apr 02, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新一大波
parent
bfba25e9
Changes
27
Show whitespace changes
Inline
Side-by-side
Showing
27 changed files
with
754 additions
and
279 deletions
+754
-279
pom.xml
pom.xml
+6
-0
SingleTestcaseExecution.java
...ain/java/cn/qg/holmes/common/SingleTestcaseExecution.java
+0
-29
TestExecutionController.java
...cn/qg/holmes/controller/auto/TestExecutionController.java
+28
-0
Interface.java
src/main/java/cn/qg/holmes/entity/auto/Interface.java
+30
-0
Project.java
src/main/java/cn/qg/holmes/entity/auto/Project.java
+24
-0
Scene.java
src/main/java/cn/qg/holmes/entity/auto/Scene.java
+25
-0
SceneTestcase.java
src/main/java/cn/qg/holmes/entity/auto/SceneTestcase.java
+30
-0
Testcase.java
src/main/java/cn/qg/holmes/entity/auto/Testcase.java
+4
-1
InterfaceMapper.java
src/main/java/cn/qg/holmes/mapper/auto/InterfaceMapper.java
+7
-0
ProjectMapper.java
src/main/java/cn/qg/holmes/mapper/auto/ProjectMapper.java
+7
-0
SceneMapper.java
src/main/java/cn/qg/holmes/mapper/auto/SceneMapper.java
+7
-0
SceneTestcaseMapper.java
...in/java/cn/qg/holmes/mapper/auto/SceneTestcaseMapper.java
+7
-0
TestcaseMapper.java
src/main/java/cn/qg/holmes/mapper/auto/TestcaseMapper.java
+7
-0
AutoUtilsService.java
...main/java/cn/qg/holmes/service/auto/AutoUtilsService.java
+15
-0
InterfaceService.java
...main/java/cn/qg/holmes/service/auto/InterfaceService.java
+7
-0
ProjectService.java
src/main/java/cn/qg/holmes/service/auto/ProjectService.java
+7
-0
SceneService.java
src/main/java/cn/qg/holmes/service/auto/SceneService.java
+7
-0
SceneTestcaseService.java
.../java/cn/qg/holmes/service/auto/SceneTestcaseService.java
+9
-0
TestcaseService.java
src/main/java/cn/qg/holmes/service/auto/TestcaseService.java
+9
-0
AutoUtilsServiceImpl.java
.../cn/qg/holmes/service/auto/impl/AutoUtilsServiceImpl.java
+127
-0
InterfaceServiceImpl.java
.../cn/qg/holmes/service/auto/impl/InterfaceServiceImpl.java
+11
-0
ProjectServiceImpl.java
...va/cn/qg/holmes/service/auto/impl/ProjectServiceImpl.java
+11
-0
SceneServiceImpl.java
...java/cn/qg/holmes/service/auto/impl/SceneServiceImpl.java
+12
-0
SceneTestcaseServiceImpl.java
...qg/holmes/service/auto/impl/SceneTestcaseServiceImpl.java
+107
-0
TestcaseServiceImpl.java
...a/cn/qg/holmes/service/auto/impl/TestcaseServiceImpl.java
+92
-0
BankCardUtils.java
src/main/java/cn/qg/holmes/utils/BankCardUtils.java
+3
-2
HttpClientUtils.java
src/main/java/cn/qg/holmes/utils/HttpClientUtils.java
+155
-247
No files found.
pom.xml
View file @
0897055e
...
...
@@ -123,6 +123,12 @@
<version>
7.3.0
</version>
</dependency>
<dependency>
<groupId>
com.jayway.jsonpath
</groupId>
<artifactId>
json-path
</artifactId>
<version>
2.5.0
</version>
</dependency>
</dependencies>
<build>
...
...
src/main/java/cn/qg/holmes/common/SingleTestcaseExecution.java
deleted
100644 → 0
View file @
bfba25e9
package
cn
.
qg
.
holmes
.
common
;
import
cn.qg.holmes.entity.auto.Testcase
;
import
org.testng.annotations.AfterClass
;
import
org.testng.annotations.BeforeClass
;
import
org.testng.annotations.Test
;
/**
* 单接口用例执行器
* @author libo
*/
public
class
SingleTestcaseExecution
{
Testcase
testcase
;
SingleTestcaseExecution
(
Testcase
testcase
)
{
this
.
testcase
=
testcase
;
}
@BeforeClass
()
public
void
beforeClass
()
{}
@Test
public
void
executeTestcase
()
{}
@AfterClass
()
public
void
afterClass
()
{}
}
src/main/java/cn/qg/holmes/controller/auto/TestExecutionController.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
controller
.
auto
;
import
cn.qg.holmes.service.auto.SceneTestcaseService
;
import
cn.qg.holmes.service.auto.TestcaseService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
@CrossOrigin
@RestController
@RequestMapping
(
"/execute"
)
public
class
TestExecutionController
{
@Autowired
TestcaseService
testcaseService
;
@Autowired
SceneTestcaseService
sceneTestcaseService
;
@GetMapping
(
"/testcase"
)
public
void
testSingleTestcase
(
@RequestParam
Integer
testcaseId
)
{
testcaseService
.
singleTestcaseExecutor
(
testcaseId
);
}
@GetMapping
(
"/scene"
)
public
void
testScene
(
@RequestParam
Integer
sceneId
)
{
sceneTestcaseService
.
executeSceneTestcase
(
sceneId
);
}
}
src/main/java/cn/qg/holmes/entity/auto/Interface.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
entity
.
auto
;
import
com.baomidou.mybatisplus.annotation.IdType
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
lombok.Data
;
import
java.util.Date
;
/**
* 接口实体类
*/
@Data
public
class
Interface
{
@TableId
(
type
=
IdType
.
AUTO
)
private
Integer
id
;
private
String
name
;
private
String
url
;
private
String
method
;
private
String
headers
;
private
String
paramType
;
private
String
requestTemplate
;
private
String
responseTemplate
;
private
String
author
;
private
Integer
projectId
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
createTime
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
updateTime
;
}
src/main/java/cn/qg/holmes/entity/auto/Project.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
entity
.
auto
;
import
com.baomidou.mybatisplus.annotation.IdType
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
lombok.Data
;
import
java.util.Date
;
/**
* 项目实体类
*/
@Data
public
class
Project
{
@TableId
(
type
=
IdType
.
AUTO
)
private
Integer
id
;
private
String
name
;
private
String
key
;
private
String
domain
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
createTime
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
updateTime
;
}
src/main/java/cn/qg/holmes/entity/auto/Scene.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
entity
.
auto
;
import
com.baomidou.mybatisplus.annotation.IdType
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
com.baomidou.mybatisplus.annotation.TableName
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
lombok.Data
;
import
java.util.Date
;
/**
* 场景实体类
*/
@Data
@TableName
(
"scene"
)
public
class
Scene
{
@TableId
(
type
=
IdType
.
AUTO
)
private
Integer
id
;
private
String
name
;
private
Integer
projectId
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
createTime
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
updateTime
;
}
src/main/java/cn/qg/holmes/entity/auto/SceneTestcase.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
entity
.
auto
;
import
com.baomidou.mybatisplus.annotation.IdType
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
com.baomidou.mybatisplus.annotation.TableName
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
lombok.Data
;
import
java.util.Date
;
@Data
@TableName
(
"scene_testcase"
)
public
class
SceneTestcase
{
@TableId
(
type
=
IdType
.
AUTO
)
private
Integer
id
;
private
Integer
sceneId
;
private
Integer
interfaceId
;
private
String
preAction
;
private
String
postAction
;
private
String
headers
;
private
String
parameters
;
private
String
variables
;
private
String
extract
;
private
String
validate
;
private
Integer
sequence
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
createTime
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
updateTime
;
}
src/main/java/cn/qg/holmes/entity/auto/Testcase.java
View file @
0897055e
...
...
@@ -16,11 +16,14 @@ import java.util.Date;
public
class
Testcase
{
@TableId
(
type
=
IdType
.
AUTO
)
private
Integer
id
;
private
String
name
;
private
Integer
interfaceId
;
private
String
preAction
;
private
String
postAction
;
private
String
headers
;
private
String
parameter
;
private
String
parameters
;
private
String
variables
;
private
String
extract
;
private
String
validate
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
createTime
;
...
...
src/main/java/cn/qg/holmes/mapper/auto/InterfaceMapper.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
mapper
.
auto
;
import
cn.qg.holmes.entity.auto.Interface
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
public
interface
InterfaceMapper
extends
BaseMapper
<
Interface
>
{
}
src/main/java/cn/qg/holmes/mapper/auto/ProjectMapper.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
mapper
.
auto
;
import
cn.qg.holmes.entity.auto.Project
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
public
interface
ProjectMapper
extends
BaseMapper
<
Project
>
{
}
src/main/java/cn/qg/holmes/mapper/auto/SceneMapper.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
mapper
.
auto
;
import
cn.qg.holmes.entity.auto.Scene
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
public
interface
SceneMapper
extends
BaseMapper
<
Scene
>
{
}
src/main/java/cn/qg/holmes/mapper/auto/SceneTestcaseMapper.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
mapper
.
auto
;
import
cn.qg.holmes.entity.auto.SceneTestcase
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
public
interface
SceneTestcaseMapper
extends
BaseMapper
<
SceneTestcase
>
{
}
src/main/java/cn/qg/holmes/mapper/auto/TestcaseMapper.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
mapper
.
auto
;
import
cn.qg.holmes.entity.auto.Testcase
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
public
interface
TestcaseMapper
extends
BaseMapper
<
Testcase
>
{
}
src/main/java/cn/qg/holmes/service/auto/AutoUtilsService.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
;
import
java.util.List
;
import
java.util.Map
;
public
interface
AutoUtilsService
{
Map
<
String
,
String
>
replaceHeaders
(
String
headers
,
String
variables
);
Map
<
String
,
String
>
replaceParameters
(
String
parameters
,
String
variables
);
boolean
extractResponse
(
String
response
,
String
extract
);
boolean
assertResponse
(
String
response
,
List
<
Map
>
validateList
);
}
src/main/java/cn/qg/holmes/service/auto/InterfaceService.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
;
import
cn.qg.holmes.entity.auto.Interface
;
import
com.baomidou.mybatisplus.extension.service.IService
;
public
interface
InterfaceService
extends
IService
<
Interface
>
{
}
src/main/java/cn/qg/holmes/service/auto/ProjectService.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
;
import
cn.qg.holmes.entity.auto.Project
;
import
com.baomidou.mybatisplus.extension.service.IService
;
public
interface
ProjectService
extends
IService
<
Project
>
{
}
src/main/java/cn/qg/holmes/service/auto/SceneService.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
;
import
cn.qg.holmes.entity.auto.Scene
;
import
com.baomidou.mybatisplus.extension.service.IService
;
public
interface
SceneService
extends
IService
<
Scene
>
{
}
src/main/java/cn/qg/holmes/service/auto/SceneTestcaseService.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
;
import
cn.qg.holmes.entity.auto.SceneTestcase
;
import
com.baomidou.mybatisplus.extension.service.IService
;
public
interface
SceneTestcaseService
extends
IService
<
SceneTestcase
>
{
void
executeSceneTestcase
(
Integer
sceneId
);
}
src/main/java/cn/qg/holmes/service/auto/TestcaseService.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
;
import
cn.qg.holmes.entity.auto.Testcase
;
import
com.baomidou.mybatisplus.extension.service.IService
;
public
interface
TestcaseService
extends
IService
<
Testcase
>
{
String
singleTestcaseExecutor
(
Integer
testcaseId
);
}
src/main/java/cn/qg/holmes/service/auto/impl/AutoUtilsServiceImpl.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
.
impl
;
import
cn.qg.holmes.service.auto.AutoUtilsService
;
import
cn.qg.holmes.utils.RedisUtils
;
import
com.alibaba.fastjson.JSON
;
import
com.jayway.jsonpath.JsonPath
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author libo
* created at 2021-03-30
*/
@Service
@Slf4j
public
class
AutoUtilsServiceImpl
implements
AutoUtilsService
{
@Autowired
RedisUtils
redisUtils
;
/**
* 将请求headers中的变量替换成对应的值
* @param headers 请求头json
* @param variables 参数json
* @return
*/
public
Map
<
String
,
String
>
replaceHeaders
(
String
headers
,
String
variables
)
{
log
.
info
(
"开始执行请求头替换!"
);
Map
<
String
,
String
>
headersMap
=
JSON
.
parseObject
(
headers
,
Map
.
class
);
Map
<
String
,
String
>
varMap
=
JSON
.
parseObject
(
variables
,
Map
.
class
);
log
.
info
(
"替换之前的headers:{}"
,
headersMap
);
log
.
info
(
"参数列表:{}"
,
varMap
);
for
(
String
key:
varMap
.
keySet
())
{
String
value
=
varMap
.
get
(
key
);
headersMap
.
put
(
key
,
redisUtils
.
get
(
value
.
substring
(
1
)).
toString
());
}
log
.
info
(
"替换之后的headers:{}"
,
headersMap
);
return
headersMap
;
}
/**
* 将请求体中的变量替换成对应的值
* @param parameters 请求参数json
* @param variables 变量json
* @return 替换后的参数Map
*/
public
Map
<
String
,
String
>
replaceParameters
(
String
parameters
,
String
variables
)
{
log
.
info
(
"开始执行请求参数替换!"
);
Map
<
String
,
String
>
parameterMap
=
JSON
.
parseObject
(
parameters
,
Map
.
class
);
Map
<
String
,
String
>
varMap
=
JSON
.
parseObject
(
variables
,
Map
.
class
);
log
.
info
(
"替换之前的参数:{}"
,
parameterMap
);
log
.
info
(
"参数列表:{}"
,
varMap
);
for
(
String
key:
varMap
.
keySet
())
{
String
value
=
varMap
.
get
(
key
);
parameterMap
.
put
(
key
,
redisUtils
.
get
(
value
.
substring
(
1
)).
toString
());
}
log
.
info
(
"替换之后的参数:{}"
,
parameterMap
);
return
parameterMap
;
}
/**
* 将响应中的值解析出来并存储到redis值
* @param response 响应json
* @param extract 解析的字段及保存到redis的key
* @return true-解析成功,false-解析失败
*/
public
boolean
extractResponse
(
String
response
,
String
extract
)
{
Map
<
String
,
String
>
extractMap
=
JSON
.
parseObject
(
extract
,
Map
.
class
);
try
{
for
(
String
key:
extractMap
.
keySet
())
{
String
value
=
extractMap
.
get
(
key
);
redisUtils
.
set
(
key
,
JsonPath
.
read
(
response
,
value
));
redisUtils
.
expire
(
key
,
120
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
return
true
;
}
/**
* 响应断言
* @return
*/
public
boolean
assertResponse
(
String
response
,
List
<
Map
>
validateList
)
{
for
(
Map
<
String
,
String
>
map:
validateList
)
{
String
comparator
=
map
.
get
(
"comparator"
);
String
checkpoint
=
JsonPath
.
read
(
response
,
map
.
get
(
"check"
));
String
expectValue
=
map
.
get
(
"expect"
);
boolean
result
=
singleAssertResponse
(
response
,
comparator
,
checkpoint
,
expectValue
);
if
(!
result
)
{
return
false
;
}
}
return
false
;
}
/**
*
* @param response
* @param comparator
* @param checkpoint
* @param expectValue
* @return
*/
public
boolean
singleAssertResponse
(
String
response
,
String
comparator
,
String
checkpoint
,
String
expectValue
)
{
switch
(
comparator
)
{
case
"eq"
:
return
JsonPath
.
read
(
response
,
checkpoint
).
equals
(
expectValue
);
case
"gt"
:
return
Integer
.
parseInt
(
JsonPath
.
read
(
response
,
checkpoint
))
>
Integer
.
parseInt
(
expectValue
);
case
"lt"
:
return
Integer
.
parseInt
(
JsonPath
.
read
(
response
,
checkpoint
))
<
Integer
.
parseInt
(
expectValue
);
case
"neq"
:
return
!
JsonPath
.
read
(
response
,
checkpoint
).
equals
(
expectValue
);
default
:
log
.
info
(
"暂不支持该比较符号:{}"
,
comparator
);
break
;
}
return
false
;
}
}
src/main/java/cn/qg/holmes/service/auto/impl/InterfaceServiceImpl.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
.
impl
;
import
cn.qg.holmes.entity.auto.Interface
;
import
cn.qg.holmes.mapper.auto.InterfaceMapper
;
import
cn.qg.holmes.service.auto.InterfaceService
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
org.springframework.stereotype.Service
;
@Service
public
class
InterfaceServiceImpl
extends
ServiceImpl
<
InterfaceMapper
,
Interface
>
implements
InterfaceService
{
}
src/main/java/cn/qg/holmes/service/auto/impl/ProjectServiceImpl.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
.
impl
;
import
cn.qg.holmes.entity.auto.Project
;
import
cn.qg.holmes.mapper.auto.ProjectMapper
;
import
cn.qg.holmes.service.auto.ProjectService
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
org.springframework.stereotype.Service
;
@Service
public
class
ProjectServiceImpl
extends
ServiceImpl
<
ProjectMapper
,
Project
>
implements
ProjectService
{
}
src/main/java/cn/qg/holmes/service/auto/impl/SceneServiceImpl.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
.
impl
;
import
cn.qg.holmes.entity.auto.Scene
;
import
cn.qg.holmes.mapper.auto.SceneMapper
;
import
cn.qg.holmes.service.auto.SceneService
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
org.springframework.stereotype.Service
;
@Service
public
class
SceneServiceImpl
extends
ServiceImpl
<
SceneMapper
,
Scene
>
implements
SceneService
{
}
src/main/java/cn/qg/holmes/service/auto/impl/SceneTestcaseServiceImpl.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
.
impl
;
import
cn.qg.holmes.entity.auto.Interface
;
import
cn.qg.holmes.entity.auto.SceneTestcase
;
import
cn.qg.holmes.mapper.auto.SceneTestcaseMapper
;
import
cn.qg.holmes.service.auto.AutoUtilsService
;
import
cn.qg.holmes.service.auto.InterfaceService
;
import
cn.qg.holmes.service.auto.SceneTestcaseService
;
import
cn.qg.holmes.service.auto.TestcaseService
;
import
cn.qg.holmes.utils.HttpClientUtils
;
import
com.alibaba.fastjson.JSON
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author libo
* created at 2021-03-30
*/
@Service
@Slf4j
public
class
SceneTestcaseServiceImpl
extends
ServiceImpl
<
SceneTestcaseMapper
,
SceneTestcase
>
implements
SceneTestcaseService
{
@Autowired
SceneTestcaseMapper
sceneTestcaseMapper
;
@Autowired
TestcaseService
testcaseService
;
@Autowired
InterfaceService
interfaceService
;
@Autowired
AutoUtilsService
autoUtilsService
;
@Override
public
void
executeSceneTestcase
(
Integer
sceneId
)
{
QueryWrapper
queryWrapper
=
new
QueryWrapper
();
queryWrapper
.
eq
(
"scene_id"
,
sceneId
);
queryWrapper
.
orderByAsc
(
"sequence"
);
List
<
SceneTestcase
>
sceneTestcaseList
=
sceneTestcaseMapper
.
selectList
(
queryWrapper
);
for
(
SceneTestcase
sceneTestcase:
sceneTestcaseList
)
{
sceneTestcaseExecution
(
sceneId
,
sceneTestcase
.
getInterfaceId
());
}
}
public
String
sceneTestcaseExecution
(
Integer
sceneId
,
Integer
interfaceId
)
{
QueryWrapper
queryWrapper
=
new
QueryWrapper
();
queryWrapper
.
eq
(
"scene_id"
,
sceneId
);
queryWrapper
.
eq
(
"interface_id"
,
interfaceId
);
SceneTestcase
sceneTestcase
=
sceneTestcaseMapper
.
selectOne
(
queryWrapper
);
Interface
anInterface
=
interfaceService
.
getById
(
interfaceId
);
String
url
=
anInterface
.
getUrl
();
log
.
info
(
"开始执行接口:{}"
,
url
);
String
method
=
anInterface
.
getMethod
().
toUpperCase
();
String
paramType
=
anInterface
.
getParamType
();
String
headers
=
sceneTestcase
.
getHeaders
();
String
parameters
=
sceneTestcase
.
getParameters
();
String
variables
=
sceneTestcase
.
getVariables
();
String
extract
=
sceneTestcase
.
getExtract
();
String
validate
=
sceneTestcase
.
getValidate
();
Map
<
String
,
String
>
parameterMap
=
JSON
.
parseObject
(
parameters
,
Map
.
class
);
Map
<
String
,
String
>
headersMap
=
JSON
.
parseObject
(
headers
,
Map
.
class
);
if
(
variables
!=
null
&&
!
variables
.
isEmpty
()
&&
parameters
!=
null
&&
!
parameters
.
isEmpty
())
{
parameterMap
=
autoUtilsService
.
replaceParameters
(
parameters
,
variables
);
}
if
(
variables
!=
null
&&
!
variables
.
isEmpty
()
&&
headers
!=
null
&&
!
headers
.
isEmpty
())
{
headersMap
=
autoUtilsService
.
replaceParameters
(
headers
,
variables
);
}
List
<
Map
>
validateList
=
new
ArrayList
<>();
if
(
validate
!=
null
&&
!
validate
.
isEmpty
())
{
validateList
=
JSON
.
parseArray
(
validate
,
Map
.
class
);
}
String
response
=
""
;
switch
(
method
)
{
case
"GET"
:
response
=
HttpClientUtils
.
doGet
(
url
,
headersMap
,
parameterMap
);
break
;
case
"POST"
:
if
(
paramType
.
equals
(
"json"
))
{
response
=
HttpClientUtils
.
doPostJson
(
url
,
headersMap
,
JSON
.
toJSONString
(
parameterMap
));
}
else
if
(
paramType
.
equals
(
"form"
))
{
response
=
HttpClientUtils
.
doPost
(
url
,
headersMap
,
parameterMap
);
}
break
;
default
:
log
.
info
(
"暂不支持该请求方法:{}"
,
method
);
break
;
}
// 进行断言
if
(!
validateList
.
isEmpty
())
{
autoUtilsService
.
assertResponse
(
response
,
validateList
);
}
// 解析响应
if
(
extract
!=
null
&&
!
extract
.
isEmpty
())
{
autoUtilsService
.
extractResponse
(
response
,
extract
);
}
return
response
;
}
}
src/main/java/cn/qg/holmes/service/auto/impl/TestcaseServiceImpl.java
0 → 100644
View file @
0897055e
package
cn
.
qg
.
holmes
.
service
.
auto
.
impl
;
import
cn.qg.holmes.entity.auto.Interface
;
import
cn.qg.holmes.entity.auto.Testcase
;
import
cn.qg.holmes.mapper.auto.TestcaseMapper
;
import
cn.qg.holmes.service.auto.AutoUtilsService
;
import
cn.qg.holmes.service.auto.InterfaceService
;
import
cn.qg.holmes.service.auto.TestcaseService
;
import
cn.qg.holmes.utils.HttpClientUtils
;
import
com.alibaba.fastjson.JSON
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author libo
* created at 2021-03-30
*/
@Service
@Slf4j
public
class
TestcaseServiceImpl
extends
ServiceImpl
<
TestcaseMapper
,
Testcase
>
implements
TestcaseService
{
@Autowired
TestcaseMapper
testcaseMapper
;
@Autowired
InterfaceService
interfaceService
;
@Autowired
AutoUtilsService
autoUtilsService
;
@Override
public
String
singleTestcaseExecutor
(
Integer
testcaseId
)
{
Testcase
testcase
=
testcaseMapper
.
selectById
(
testcaseId
);
Interface
anInterface
=
interfaceService
.
getById
(
testcase
.
getInterfaceId
());
String
url
=
anInterface
.
getUrl
();
String
method
=
anInterface
.
getMethod
().
toUpperCase
();
String
paramType
=
anInterface
.
getParamType
();
String
headers
=
testcase
.
getHeaders
();
String
parameters
=
testcase
.
getParameters
();
String
testcaseName
=
testcase
.
getName
();
String
variables
=
testcase
.
getVariables
();
String
extract
=
testcase
.
getExtract
();
String
validate
=
testcase
.
getValidate
();
// 参数准备
Map
<
String
,
String
>
parameterMap
=
JSON
.
parseObject
(
parameters
,
Map
.
class
);
Map
<
String
,
String
>
headersMap
=
JSON
.
parseObject
(
headers
,
Map
.
class
);
if
(
variables
!=
null
&&
!
variables
.
isEmpty
()
&&
parameters
!=
null
&&
!
parameters
.
isEmpty
())
{
parameterMap
=
autoUtilsService
.
replaceParameters
(
parameters
,
variables
);
}
if
(
variables
!=
null
&&
!
variables
.
isEmpty
()
&&
headers
!=
null
&&
!
headers
.
isEmpty
())
{
headersMap
=
autoUtilsService
.
replaceParameters
(
headers
,
variables
);
}
List
<
Map
>
validateList
=
new
ArrayList
<>();
if
(
validate
!=
null
&&
!
validate
.
isEmpty
())
{
validateList
=
JSON
.
parseArray
(
validate
,
Map
.
class
);
}
String
response
=
""
;
// 发起http请求
switch
(
method
)
{
case
"GET"
:
response
=
HttpClientUtils
.
doGet
(
url
,
headersMap
,
parameterMap
);
break
;
case
"POST"
:
if
(
paramType
.
equals
(
"json"
))
{
response
=
HttpClientUtils
.
doPostJson
(
url
,
headersMap
,
JSON
.
toJSONString
(
parameterMap
));
}
else
if
(
paramType
.
equals
(
"form"
))
{
response
=
HttpClientUtils
.
doPost
(
url
,
headersMap
,
parameterMap
);
}
break
;
default
:
log
.
info
(
"暂不支持该请求方法:{}"
,
method
);
break
;
}
log
.
info
(
"用例:{}, 返回为:{}"
,
testcaseName
,
response
);
// 进行断言
if
(!
validateList
.
isEmpty
())
{
autoUtilsService
.
assertResponse
(
response
,
validateList
);
}
// 解析响应
if
(
extract
!=
null
&&
!
extract
.
isEmpty
())
{
autoUtilsService
.
extractResponse
(
response
,
extract
);
}
return
response
;
}
}
src/main/java/cn/qg/holmes/utils/BankCardUtils.java
View file @
0897055e
package
cn
.
qg
.
holmes
.
utils
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
/**
...
...
@@ -9,8 +10,8 @@ public class BankCardUtils {
public
static
String
getCardCode
(
String
cardNo
)
{
String
url
=
"https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo="
+
cardNo
+
"&cardBinCheck=true"
;
JSONObject
resposne
=
HttpClientUtils
.
doGetReturnJson
(
url
);
return
resposne
.
get
(
"bank"
).
toString
();
String
resposne
=
HttpClientUtils
.
doGet
(
url
);
return
JSON
.
parseObject
(
resposne
)
.
get
(
"bank"
).
toString
();
}
public
static
void
main
(
String
[]
args
)
{
...
...
src/main/java/cn/qg/holmes/utils/HttpClientUtils.java
View file @
0897055e
package
cn
.
qg
.
holmes
.
utils
;
import
com.alibaba.fastjson.JSONException
;
import
com.alibaba.fastjson.JSONObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.NameValuePair
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.entity.UrlEncodedFormEntity
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.client.methods.HttpRequestBase
;
import
org.apache.http.client.utils.URIBuilder
;
import
org.apache.http.entity.ContentType
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
...
...
@@ -18,290 +16,200 @@ import org.apache.http.message.BasicNameValuePair;
import
org.apache.http.util.EntityUtils
;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URISyntaxException
;
import
java.util.*
;
import
java.net.URI
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
/**
* HttpClient 工具类
* @author libo
*/
@Slf4j
public
class
HttpClientUtils
{
private
static
final
CloseableHttpClient
client
=
HttpClients
.
createDefault
();
/**
* 不带参数的GET请求
* @param url 请求的url
* @return json
*/
public
static
JSONObject
doGetReturnJson
(
String
url
)
{
JSONObject
jsonResult
=
new
JSONObject
();
HttpGet
get
=
new
HttpGet
(
url
);
try
{
CloseableHttpResponse
response
=
client
.
execute
(
get
);
HttpEntity
httpEntity
=
response
.
getEntity
();
String
respStr
=
EntityUtils
.
toString
(
httpEntity
,
"utf-8"
);
jsonResult
=
JSONObject
.
parseObject
(
respStr
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
jsonResult
;
}
// 编码格式。发送编码格式统一用UTF-8
private
static
final
String
ENCODING
=
"UTF-8"
;
/**
* 不带参数的GET请求
* @param url 请求的url
* @return html代码
*/
public
static
String
doGetReturnHtml
(
String
url
)
{
String
strResult
=
null
;
HttpGet
get
=
new
HttpGet
(
url
);
try
{
CloseableHttpResponse
response
=
client
.
execute
(
get
);
HttpEntity
httpEntity
=
response
.
getEntity
();
strResult
=
EntityUtils
.
toString
(
httpEntity
,
"utf-8"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
strResult
;
}
// 设置连接超时时间,单位毫秒。
private
static
final
int
CONNECT_TIMEOUT
=
6000
;
// 请求获取数据的超时时间(即响应时间),单位毫秒。
private
static
final
int
SOCKET_TIMEOUT
=
6000
;
/**
* 带参数的GET请求
* @param url 请求url
* @param params 请求参数
* @param headers 请求头
* @throws URISyntaxException
* http get 请求
* @param url
* @param headers
* @param params
* @return
*/
public
static
String
doGetReturnHtml
(
String
url
,
Map
<
String
,
Object
>
params
,
Map
<
String
,
Object
>
headers
)
throws
URISyntaxException
{
public
static
String
doGet
(
String
url
,
Map
<
String
,
String
>
headers
,
Map
<
String
,
String
>
params
)
{
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
CloseableHttpResponse
httpResponse
=
null
;
String
resultString
=
""
;
try
{
URIBuilder
uriBuilder
=
new
URIBuilder
(
url
);
String
strResult
=
null
;
// 设置请求参数
if
(
params
!=
null
&&
!
params
.
isEmpty
())
{
Set
<
String
>
keySet
=
params
.
keySet
();
for
(
String
key
:
keySet
)
{
uriBuilder
.
setParameter
(
key
,
params
.
get
(
key
).
toString
());
if
(
params
!=
null
)
{
for
(
String
key:
params
.
keySet
())
{
uriBuilder
.
addParameter
(
key
,
params
.
get
(
key
));
}
}
HttpGet
get
=
new
HttpGet
(
uriBuilder
.
build
());
URI
uri
=
uriBuilder
.
build
();
// 创建Http Get 请求
HttpGet
httpGet
=
new
HttpGet
(
uri
);
// 设置请求超时时间
RequestConfig
requestConfig
=
RequestConfig
.
custom
().
setConnectTimeout
(
CONNECT_TIMEOUT
).
setSocketTimeout
(
SOCKET_TIMEOUT
).
build
();
httpGet
.
setConfig
(
requestConfig
);
// 设置请求头
if
(
headers
!=
null
&&
!
headers
.
isEmpty
())
{
Set
<
String
>
keySet
=
headers
.
keySet
();
for
(
String
s
:
keySet
)
{
get
.
addHeader
(
s
,
headers
.
get
(
s
).
toString
());
}
}
setHeader
(
headers
,
httpGet
);
// 执行http请求
httpResponse
=
httpClient
.
execute
(
httpGet
);
resultString
=
EntityUtils
.
toString
(
httpResponse
.
getEntity
(),
ENCODING
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
CloseableHttpResponse
response
=
client
.
execute
(
get
);
HttpEntity
httpEntity
=
response
.
getEntity
();
strResult
=
EntityUtils
.
toString
(
httpEntity
,
"utf-8"
);
// 释放资源
if
(
httpResponse
!=
null
)
{
httpResponse
.
close
();
}
httpClient
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
strResult
;
}
/**
* 带参数的GET请求
* @param url url
* @param params 参数
* @param headers 请求头
* @return json
* @throws URISyntaxException
*/
public
static
JSONObject
doGetReturnJson
(
String
url
,
Map
<
String
,
Object
>
params
,
Map
<
String
,
Object
>
headers
)
throws
URISyntaxException
{
JSONObject
jsonResult
=
new
JSONObject
();
URIBuilder
uriBuilder
=
new
URIBuilder
(
url
);
// 设置参数
if
(
params
!=
null
&&
!
params
.
isEmpty
())
{
Set
<
String
>
keySet
=
params
.
keySet
();
for
(
String
key
:
keySet
)
{
uriBuilder
.
setParameter
(
key
,
params
.
get
(
key
).
toString
());
return
resultString
;
}
// 不带参数的get请求
public
static
String
doGet
(
String
url
)
{
return
doGet
(
url
,
null
,
null
);
}
HttpGet
get
=
new
HttpGet
(
uriBuilder
.
build
());
if
(
headers
!=
null
&&
!
headers
.
isEmpty
())
{
Set
<
String
>
keySet
=
headers
.
keySet
();
for
(
String
s
:
keySet
)
{
get
.
addHeader
(
s
,
headers
.
get
(
s
).
toString
());
}
}
try
{
CloseableHttpResponse
response
=
client
.
execute
(
get
);
HttpEntity
httpEntity
=
response
.
getEntity
();
String
respStr
=
EntityUtils
.
toString
(
httpEntity
,
"utf-8"
);
jsonResult
=
JSONObject
.
parseObject
(
respStr
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
jsonResult
;
// 不带headers的get请求
public
static
String
doGet
(
String
url
,
Map
<
String
,
String
>
params
)
{
return
doGet
(
url
,
null
,
params
);
}
/**
* 带参数的post请求
* @param url
* @param params
* 发送POST请求,携带map格式的参数
* @param url 请求地址
* @param headers 请求头
* @param params 请求参数,Map格式
* @return
*/
public
static
JSONObject
doPost
(
String
url
,
String
params
)
{
JSONObject
jsonResult
=
new
JSONObject
();
public
static
String
doPost
(
String
url
,
Map
<
String
,
String
>
headers
,
Map
<
String
,
String
>
params
)
{
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
CloseableHttpResponse
httpResponse
=
null
;
String
resultString
=
""
;
try
{
HttpPost
post
=
new
HttpPost
(
url
);
List
<
NameValuePair
>
list
=
new
ArrayList
<>();
if
(
StringUtils
.
isNotEmpty
(
params
))
{
String
[]
splitStrings
=
params
.
split
(
"&"
);
for
(
String
splitString
:
splitStrings
)
{
String
[]
split
=
splitString
.
split
(
"="
);
list
.
add
(
new
BasicNameValuePair
(
split
[
0
],
split
[
1
]));
// 创建 HttpPost
HttpPost
httpPost
=
new
HttpPost
(
url
);
// 设置请求超时时间
RequestConfig
requestConfig
=
RequestConfig
.
custom
().
setConnectTimeout
(
CONNECT_TIMEOUT
).
setSocketTimeout
(
SOCKET_TIMEOUT
).
build
();
httpPost
.
setConfig
(
requestConfig
);
// 设置请求头
setHeader
(
headers
,
httpPost
);
// 设置请求参数
if
(
params
!=
null
&&
!
params
.
isEmpty
())
{
List
<
NameValuePair
>
nameValuePairs
=
new
ArrayList
<>();
for
(
String
key:
params
.
keySet
())
{
nameValuePairs
.
add
(
new
BasicNameValuePair
(
key
,
params
.
get
(
key
)));
}
UrlEncodedFormEntity
urlEncodedFormEntity
=
new
UrlEncodedFormEntity
(
list
,
"utf-8"
);
post
.
setEntity
(
urlEncodedFormEntity
);
httpPost
.
setEntity
(
new
UrlEncodedFormEntity
(
nameValuePairs
,
ENCODING
));
}
CloseableHttpResponse
response
=
client
.
execute
(
post
);
HttpEntity
responseEntity
=
response
.
getEntity
();
String
responseString
=
EntityUtils
.
toString
(
responseEntity
,
"utf-8"
);
// 执行http请求
httpResponse
=
httpClient
.
execute
(
httpPost
);
resultString
=
EntityUtils
.
toString
(
httpResponse
.
getEntity
(),
ENCODING
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
jsonResult
=
JSONObject
.
parseObject
(
responseString
);
}
catch
(
JSONException
e
)
{
jsonResult
.
put
(
"content"
,
responseString
);
// 释放资源
if
(
httpResponse
!=
null
)
{
httpResponse
.
close
(
);
}
httpClient
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
jsonResult
;
}
public
static
JSONObject
doPost
(
String
url
,
Map
<
String
,
Object
>
params
)
{
JSONObject
jsonResult
=
new
JSONObject
();
try
{
HttpPost
post
=
new
HttpPost
(
url
);
List
list
=
new
ArrayList
();
if
(
params
!=
null
&&
!
params
.
isEmpty
())
{
Set
<
String
>
keySet
=
params
.
keySet
();
for
(
String
key
:
keySet
)
{
list
.
add
(
new
BasicNameValuePair
(
key
,
params
.
get
(
key
).
toString
()));
}
UrlEncodedFormEntity
urlEncodedFormEntity
=
new
UrlEncodedFormEntity
(
list
,
"utf-8"
);
post
.
setEntity
(
urlEncodedFormEntity
);
}
CloseableHttpResponse
response
=
client
.
execute
(
post
);
HttpEntity
entity
=
response
.
getEntity
();
String
respEntity
=
EntityUtils
.
toString
(
entity
,
"utf-8"
);
try
{
jsonResult
=
JSONObject
.
parseObject
(
respEntity
);
}
catch
(
JSONException
e
)
{
// e.printStackTrace();
jsonResult
.
put
(
"content"
,
respEntity
.
toString
());
return
resultString
;
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
// 不带参数的post请求
public
static
String
doPost
(
String
url
)
{
return
doPost
(
url
,
null
,
null
);
}
return
jsonResult
;
// 不带headers的post请求
public
static
String
doPost
(
String
url
,
Map
<
String
,
String
>
params
)
{
return
doPost
(
url
,
null
,
params
);
}
public
static
JSONObject
doPost
(
String
url
,
Map
<
String
,
Object
>
params
,
Map
<
String
,
Object
>
headers
)
{
JSONObject
jsonResult
=
new
JSONObject
();
String
responseString
=
null
;
/**
* 发送post请求,携带json类型数据
* @param url 请求地址
* @param headers 请求头
* @param json json格式参数
* @return
*/
public
static
String
doPostJson
(
String
url
,
Map
<
String
,
String
>
headers
,
String
json
)
{
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
CloseableHttpResponse
httpResponse
=
null
;
String
resultString
=
""
;
try
{
HttpPost
post
=
new
HttpPost
(
url
);
List
list
=
new
ArrayList
();
if
(
params
!=
null
&&
!
params
.
isEmpty
())
{
Set
<
String
>
keySet
=
params
.
keySet
();
for
(
String
key
:
keySet
)
{
list
.
add
(
new
BasicNameValuePair
(
key
,
params
.
get
(
key
).
toString
()));
}
UrlEncodedFormEntity
urlEncodedFormEntity
=
new
UrlEncodedFormEntity
(
list
,
"utf-8"
);
post
.
setEntity
(
urlEncodedFormEntity
);
}
if
(
headers
!=
null
&&
!
headers
.
isEmpty
())
{
Set
<
String
>
keySet
=
headers
.
keySet
();
for
(
String
s
:
keySet
)
{
post
.
addHeader
(
s
,
headers
.
get
(
s
).
toString
());
}
}
// 创建Http Post请求
HttpPost
httpPost
=
new
HttpPost
(
url
);
// 创建请求内容
StringEntity
entity
=
new
StringEntity
(
json
,
ContentType
.
APPLICATION_JSON
);
httpPost
.
setEntity
(
entity
);
httpResponse
=
httpClient
.
execute
(
httpPost
);
resultString
=
EntityUtils
.
toString
(
httpResponse
.
getEntity
(),
ENCODING
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
CloseableHttpResponse
response
=
client
.
execute
(
post
);
HttpEntity
responseEntity
=
response
.
getEntity
();
responseString
=
EntityUtils
.
toString
(
responseEntity
,
"utf-8"
);
jsonResult
=
JSONObject
.
parseObject
(
responseString
);
}
catch
(
JSONException
e
)
{
// e.printStackTrace();
jsonResult
.
put
(
"content"
,
responseString
);
// 释放资源
if
(
httpResponse
!=
null
)
{
httpResponse
.
close
();
}
httpClient
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
jsonResult
;
}
public
static
JSONObject
doPostJson
(
String
url
,
String
params
)
{
JSONObject
result
=
new
JSONObject
();
HttpPost
httpPost
=
new
HttpPost
(
url
);
if
(
StringUtils
.
isNotEmpty
(
params
))
{
StringEntity
stringEntity
=
new
StringEntity
(
params
,
"utf-8"
);
httpPost
.
setEntity
(
stringEntity
);
}
// if (StringUtils.isNotEmpty(headers)) {
// httpPost.setHeader("Content-Type", "application/json");
// }
httpPost
.
setHeader
(
"Content-Type"
,
"application/json"
);
try
{
CloseableHttpResponse
response
=
client
.
execute
(
httpPost
);
HttpEntity
responseEntity
=
response
.
getEntity
();
String
responseString
=
EntityUtils
.
toString
(
responseEntity
,
"utf-8"
);
result
=
JSONObject
.
parseObject
(
responseString
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
result
;
return
result
String
;
}
public
static
JSONObject
doPostJson
(
String
url
,
String
params
,
Map
<
String
,
Object
>
headers
)
{
JSONObject
result
=
new
JSONObject
();
HttpPost
httpPost
=
new
HttpPost
(
url
);
if
(
StringUtils
.
isNotEmpty
(
params
))
{
StringEntity
stringEntity
=
new
StringEntity
(
params
,
"utf-8"
);
httpPost
.
setEntity
(
stringEntity
);
}
/**
* Description: 封装请求头
* @param headers 请求头
* @param httpRequestBase HttpRequestBase
*/
public
static
void
setHeader
(
Map
<
String
,
String
>
headers
,
HttpRequestBase
httpRequestBase
)
{
// 封装请求头
if
(
headers
!=
null
&&
!
headers
.
isEmpty
())
{
Set
<
String
>
keySet
=
headers
.
keySet
();
for
(
String
s
:
keySet
)
{
httpPost
.
addHeader
(
s
,
headers
.
get
(
s
).
toString
());
}
}
try
{
CloseableHttpResponse
response
=
client
.
execute
(
httpPost
);
HttpEntity
responseEntity
=
response
.
getEntity
();
String
responseString
=
EntityUtils
.
toString
(
responseEntity
,
"utf-8"
);
result
=
JSONObject
.
parseObject
(
responseString
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
for
(
String
key
:
keySet
)
{
// 设置到请求头到HttpRequestBase对象中
httpRequestBase
.
setHeader
(
key
,
headers
.
get
(
key
));
}
return
result
;
}
public
static
void
main
(
String
[]
args
)
throws
UnsupportedEncodingException
{
String
url
=
"http://passportapi-qa.liangkebang.net/user/login/fastV1"
;
String
phoneNo
=
"13712345678:0000"
;
Base64
.
Encoder
encoder
=
Base64
.
getEncoder
();
byte
[]
textByte
=
phoneNo
.
getBytes
(
"UTF-8"
);
String
phoneNoBase64
=
encoder
.
encodeToString
(
textByte
);
Map
<
String
,
Object
>
headers
=
new
HashMap
<>();
Map
<
String
,
Object
>
formData
=
new
HashMap
<>();
headers
.
put
(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
headers
.
put
(
"Authorization"
,
"Verification "
+
phoneNoBase64
);
formData
.
put
(
"channelId"
,
1
);
formData
.
put
(
"createdFrom"
,
1
);
formData
.
put
(
"key"
,
"xyqb"
);
formData
.
put
(
"btRegisterChannelId"
,
""
);
formData
.
put
(
"dimension"
,
""
);
formData
.
put
(
"click_id"
,
""
);
JSONObject
result
=
doPost
(
url
,
formData
,
headers
);
System
.
out
.
println
(
result
);
System
.
out
.
println
(
result
.
get
(
"data"
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment