当前位置:网站首页>技术分享 | 接口自动化测试如何处理 Header cookie
技术分享 | 接口自动化测试如何处理 Header cookie
2022-08-09 21:35:00 【hog_ceshiren】
Cookie(复数形态:Cookies)是某些网站为了辨别用户身份而储存在用户本地终端上的数据。在接口测试过程中,如果网站采取了 Cookie 认证的方式,那么发送的请求需要附带 Cookie,才会得到正常的响应的结果。接口自动化测试也是同理,需要在构造接口测试用例时加入 Cookie。
实战练习
对于要对 Cookie 进行验证的网站发起请求,如果在请求过程中没有传递 Cookie 数据,则会返回错误的响应结果。下面的实战以雪球为例。
没有 Cookie 数据的场景
下方示例对雪球的 url 发起一个 get 请求,在请求过程中没有添加 Cookie 数据。
>>> import requests
>>>
>>> url="https://xueqiu.com/stock/search.json"
>>> params={"code": "sogo", "size": "3", "page": "1"}
>>> header={ "Accept": "application/json",
... "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
... }
>>> r = requests.get(url, params=params, headers=header)
>>> r.text
'{"error_description":
"遇到错误,请刷新页面或者重新登录帐号后再试",\
"error_uri":"/stock/search.json",
"error_code":"400016"}'
>>>
由上面的例子可以看到,如果在请求过程中没有添加 Cookie 数据,结果报错,要求登录账号。
添加 Cookie 的场景
接下来,在请求过程中添加正确的 Cookie 信息,即可得到正常的响应信息。
Python 版本
同样访问雪球,通过关键字参数 cookies 传递正确的 Cookie 数据。
>>> import requests
>>>
>>> url="https://xueqiu.com/stock/search.json"
>>> params={"code": "sogo", "size": "3", "page": "1"}
>>> header={ "Accept": "application/json",
... "User-Agent": "Mozilla/5.0 \
(Macintosh; Intel Mac OS X 10_14_6)\
AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/77.0.3865.90 Safari/537.36",
... }
>>> cookies={
"xq_a_token":"...省略..."
}
>>> requests.get(url,
params=params, headers=header, cookies=cookies).text
'{"q":"sogo","page":1,"size":3,"stocks":
[{"code":"SOGO","name":"搜狗",
"enName":"","hasexist":"false","flag":null,
"type":0,"stock_id":1029472,"ind_id":0,
"nd_name":"通讯业务","ind_color":null,
"_source":"sc_1:1:sogo"}]}'
Java 版本
在 Java 中,可以使用 cookie() 方法传入雪球所需要的 Cookie 数据信息。
import static io.restassured.RestAssured.*;
public class Requests {
public static void main(String[] args) {
given().
params("code", "sogo", "size", 3, "page", 1).
cookie("xq_a_token", "...省略...").
when().
get("https://xueqiu.com/stock/search.json").
then().statusCode(200).log().all();
}
}
️ 复制“下方链接”,提升测试核心竞争力!
你好呀,喜欢这篇文章的话记得点个“赞”哦!大家的支持很重要~(▽) PS:有问题可以联系我们哦
边栏推荐
- 安科瑞支持以太网通讯、profibus通讯嵌入式电能表APM指导性技术要求-Susie 周
- Acrel5000web能耗系统在某学院的应用-Susie 周
- 抽象类 or 接口
- How are data integration APIs key to enterprise digital transformation?
- Reverse Analysis of Unknown Cryptographic Protocol Based on Network Data Flow
- Daily practice of PMP | Do not get lost in the exam -8.8 (including agility + multiple choice)
- 小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
- Definition and Basic Operations of Sequence Tables
- Endpoint mode for NetCore routing
- buuctf (Adventure 2)
猜你喜欢

痛击面试官 CURD系统也能做出技术含量
[Deep learning] pix2pix GAN theory and code implementation

Two methods of implementing inverted strings in C language

What to do if Windows 11 can't find Internet Explorer

Problems with compiling SIP with QGIS

10个 Istio 流量管理 最常用的例子,你知道几个?

Byte side: Can TCP and UDP use the same port?

没有 accept,我可以建立 TCP 连接吗?

Acrel5000web能耗系统在某学院的应用-Susie 周

微软word怎么转换成pdf文件?微软word转换为pdf格式的方法
随机推荐
访问控制知识
Word怎么制作双面席卡?使用Word制作双面席卡方法
题解:Edu Codeforces 109(div2)
MySQL跨表、多表更新SQL语句总结
Referenced file contains errors 完美解决方法
编程时请选择正确的输入法,严格区分中英文
哪款C语言编译器(IDE)适合初学者?
微软Excel表格点击单元格行和列都显示颜色怎么弄?聚光灯效果设置
Don't use array.length-1 to get the last item of the array
Skywalking系列学习之Trace Profiling源码分析
Problems with compiling SIP with QGIS
【随笔】致19期的小伙伴们
Puyuan Jingdian turned losses into profits in the first half of the year, and high-end products continued to develop!Are you optimistic about "Huawei" in the instrument industry?
Optimization of SQL Statements and Indexes
智能家居设备安全分析技术综述
Jmeter 使用正则表达式提取器将返回值全部保存到一个文件中
Word怎么制作一张标准的答题卡?
Unity_平滑移动
[corctf 2022] 部分
Characteristics and Development Prospects of Korea's Cyber Security System