当前位置:网站首页>技术分享 | 接口自动化测试如何处理 Header cookie
技术分享 | 接口自动化测试如何处理 Header cookie
2022-08-09 12:20:00 【Hogwarts_测试】
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();
}
}
边栏推荐
- MySQL principle and optimization of Group By optimization techniques
- The new features of ABP 6.0.0 - rc. 1
- Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes
- 二叉树的序列化和反序列化
- glide工具类的简单封装
- 曲鸟全栈UI自动化教学(八):框架代码讲解和进一步优化
- K个结点的组内逆序调整
- Rust从入门到精通04-数据类型
- WeChat Mini Program Payment and Refund Overall Process
- #物联网征文#小熊派设备开发实战
猜你喜欢
Intra-group reverse order adjustment of K nodes
合并两个有序列表
Simple understanding of ThreadLocal
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!...
鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄...
Flutter入门进阶之旅(八)Button Widget
数字化转型之支撑保障单元
Manchester city launch emotional intelligence scarf can be detected, give the fans
Blocking, non-blocking, multiplexing, synchronous, asynchronous, BIO, NIO, AIO all in one pot
随机推荐
报告:想学AI的学生数量已涨200%,老师都不够用了
读写分离后,性能居然提升100%了呀
荣耀携手Blue Yonder,加快企业战略增长
听声辨物,这是AI视觉该干的???|ECCV 2022
世界第4疯狂的科学家,在103岁生日那天去世了
Win10 compiles the x264 library (there are also generated lib files)
在已打开图片上加水印(文字)
注:检测到当前使用的ADB不是HBuilder内置或自定义ADB:PID为:9544进程名称为:adb.exe 路径为:c:\users\administrator\appdata\local\and
Rust 入门指南(使用JSON)
造自己的芯,让谷歌买单!谷歌再度开源 180nm 工艺的芯片
位图与位运算
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
工作任务统计
About the handling of variable parameters in the Retrofit network request URL
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
系统提供的堆 VS 手动改写堆
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
超越CLIP的多模态模型,只需不到1%的训练数据!南加大最新研究来了
Flutter入门进阶之旅(五)Image Widget