当前位置:网站首页>技术分享 | 接口自动化测试如何处理 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:有问题可以联系我们哦
边栏推荐
- MySQL Notes-06 Basic SQL Operations
- 筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
- NetCore路由的Endpoint模式
- An overall security understanding and method of cyberspace based on connection and security entropy
- 自监督学习 —— MoCo v2
- 基于光通信的6G水下信道建模综述
- Win11搜索不到文件的解决方法
- 2021(ICPC)亚洲区域赛昆明站(CGHIJLM)
- 威纶通触摸屏制作自定义弹出窗口的具体方法(3种)
- Access Characteristics of Constructor under Inheritance Relationship
猜你喜欢
POWER SOURCE ETA埃塔电源维修FHG24SX-U概述
别叫我玩,我要考PMP:考PMP选择机构需要了解的那些事儿
Cholesterol-PEG-Thiol, CLS-PEG-SH, Cholesterol-PEG-Sulfhydryl for improved solubility
fixed investment fund
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?
CMake installation upgrade higher version
威纶通触摸屏制作自定义弹出窗口的具体方法(3种)
Don't use array.length-1 to get the last item of the array
QGIS编译SIP的问题
How are data integration APIs key to enterprise digital transformation?
随机推荐
基于Docker构建MySQL主从复制数据库
Word怎么制作双面席卡?使用Word制作双面席卡方法
题解:Edu Codeforces 109(div2)
MySQL, which is asked on both sides of the byte, almost didn't answer well
一千以内的水仙花数
Win11找不到Internet Explore怎么办
定投的基金
Referenced file contains errors 完美解决方法
【Efficient Tools】Remote Control Software ToDesk (Favorites)
[corctf 2022] section
How to deal with keys when Redis is large?
数据集成API如何成为企业数字化转型的关键?
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰二氧化硅颗粒用
人人都可以DIY的大玩具,宏光MINIEV GAMEBOY产品力强,出行新装备
URL Protocol 网页打开应用程序
Characteristics and Development Prospects of Korea's Cyber Security System
Don't use array.length-1 to get the last item of the array
cad图纸怎么复制到word文档里面?Word里插CAD图怎么弄?
【云原生】4.2 DevOps 精讲篇
Can I make a TCP connection without accept?