当前位置: 移动技术网 > IT编程>开发语言>Java > 关于快速测试API接口的一个新技能

关于快速测试API接口的一个新技能

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

我们大家在日常开发过程中,或多或少都涉及到 api 接口的测试。例如,有的小伙伴使用 chrome 的 postman 插件,或者使用火狐的 restclient 等工具。事实上,这些工具是测试 api 接口非常有效的方式之一,笔者之前也一直使用 postman 完成 api 接口的测试工作。今天,笔者推荐另外一个非常好用的小工具,能够帮助读者快速测试 api 接口。这个工具就是 idea 的 editor rest client。

idea 的 editor rest client 在 intellij idea 2017.3 版本就开始支持,在 2018.1 版本添加了很多的特性。事实上,它是 intellij idea 的 http client 插件。

开始工作

首先,我们可以在任意目录下创建一个 xxx.http 文件,如图所示。

这里,我们需要使用 ### 进行 http 请求分割,并在后面添加注释,案例如下所示。

### 用户登录
post http://www.lhsxpumps.com/_localhost:8088/oauth/token?grant_type=password&username=lgz&password=123456
accept : application/json
content-type : application/json;charset=utf-8
authorization: basic client secret
cache-control : no-cache

因此,我们获得的响应内容。

多环境配置

在开发过程中,我们通常会存在多套环境,例如开发环境、测试环境、预发环境、生产环境 等。因此,如果 editor rest client 能够像 postman 一样做到多环境配置就太棒了。事实上,editor rest client 已经支持了这个特性,我们只需要创建 rest-client.env.json 文件,并且配置多环境信息即可。

{
 "development" : {
 "url" : "http://localhost:8088",
 "token" : "bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c"
 },
 "test" : {
 "url" : "http://localhost:8089",
 "token" : "bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c"
 },
 "preproduction" : {
 "url" : "http://activity.720ui.com",
 "token" : "bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c"
 },
 "product" : {
 "url" : "http://activity.720ui.com",
 "token" : "bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c"
 }
}

此时,改造之前的 url,将 http://www.lhsxpumps.com/_localhost:8088 改造成 url 代替。

post /oauth/token?grant_type=password&username=lgz&password=123456

这里,我们获得的整体效果。

案例详解

现在,我们来写一个完整的案例。

### 创建用户信息
post /v1/m/users
accept : application/json
content-type : application/json;charset=utf-8
authorization: 
{
 "username": "xiaoyue",
 "realname": "小岳",
 "password": "111111",
 "email": "lianggzone@163.com",
 "tel": "18305930000",
 "weixin": "lianggzone",
 "sex": 1
}
 
### 修改用户信息
put /v1/m/users/723181
accept : application/json
content-type : application/json;charset=utf-8
authorization: 
cachepatch-control : no-cache
{
 "username": "xiaoyue"
}
 
### 查询用户信息
get /v1/c/users/lgz/username
accept : application/json
content-type : application/json;charset=utf-8
authorization: 
 
### 查询用户信息列表
get /v1/c/users?keyword=梁
accept : application/json
content-type : application/json;charset=utf-8
authorization: 

参考资料

https://marketplace.visualstudio.com/items?itemname=humao.rest-clientt

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网