性能测试全栈笔记:7.http接口脚本编写(通用/Restful)
性能测试全栈笔记
七、http接口脚本编写
学习目的:
- 1.区分传统接口、Restful风格接口
- 2.深入接口文档解析,和jmeter结合
传统风格接口:
接口名称 | 接口类型 | URL | 参数 |
---|---|---|---|
普通的GET类型接口 | GET | /pinter/com/getSku | id=1 |
参数为k=v的POST接口 | POST | /pinter/com/login | userName=admin&password=1234 |
参数为json的POST接口 | POST | /pinter/com/register | {“userName”:”test”,”password”:”1234″,”gender”:1,”phoneNum”:”110″,”email”:”beihe@163.com”,”address”:”Beijing”} |
参数为k=json的POST接口 | POST | /pinter/com/buy | param={“skuId”:123,”num”:10} |
需要签名验证的POST接口 | POST | /pinter/com/userInfo | {“phoneNum”:”123434″,”optCode”:”testfan”,”timestamp”:”12112121212″,”sign”:”your sign data”} |
返回json数组的GET接口 | GET | /pinter/com/userList | genderType=0 |
1.普通的GET类型接口

查看结果数:

2.参数为k=v的POST接口

查看结果数:

3.参数为json的POST接口

看到JSON格式,就要想到信息头:Content-Type=application/json

查看结果数:

4.参数为k=json的POST接口
这其实也是一个表单格式

查看结果数:

5.需要签名验证的POST接口
签字在后期是难点,需要额外学习函数(time,MD5,digest等)知识

因为请求是json格式,同样需要信息头:Content-Type=application/json

结果数里的timestamp其实就是时间戳,可使用time时间函数

6.返回json数组的GET接口
涉及到数组,难点在后期的断言上,主要是注意格式和正则表达式

看一下响应,和上面几个有所不同:

发现内容很多,很繁杂,我们通过json工具进行解析:
json在线解析:JSON在线解析及格式化验证 – JSON.cn

Restful风格接口:
接口名称 | 接口类型 | URL | 参数 |
---|---|---|---|
Restful-GET类型接口 | GET | /pinter/com/phone/{id} | 手机id |
Restful-POST类型接口 | POST | /pinter/com/phone | {“brand”:”Huawei”,”color”:”yellow”,”memorySize”:”64G”,”cpuCore”:”8核”,”price”:”8848″,”desc”:”全新上市”} |
Restful-PUT类型接口 | PUT | /pinter/com/phone | 同上 |
Restful-DELETE类型接口 | DELETE | /pinter/com/phone | 同上 |
1.Restful-GET类型接口

查看结果数:

2.Restful-POST类型接口

添加信息头:Content-Type=application/json

查看结果数:

3.Restful-PUT类型接口

添加信息头:Content-Type=application/json

查看结果数:

4.Restful-DELETE类型接口

添加信息头:Content-Type=application/json

查看结果数:

相信通过这样的对比,我们对Restfull风格的接口有了更深入的了解,曾说过,Restfull只是一种设计手法,并非标准。
下集预告:响应断言