当前位置:网站首页>技术分享 | 接口自动化测试如何处理 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();
}
}边栏推荐
- 两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
- AI篮球裁判火了,走步算得特别准,就问哈登慌不慌
- Resolved IndentationError: unindent does not match any oute r indentation Level
- The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
- 在已打开图片上加水印(文字)
- Say goodbye to the AI era of hand looms
- Ten minutes to teach you how to use VitePress to build and deploy a personal blog site
- Simple understanding of ThreadLocal
- Flutter Getting Started and Advanced Tour (3) Text Widgets
- Extract EventBus encapsulation to base class using annotations
猜你喜欢

Simple understanding of ThreadLocal

罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...

又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...

保存Simulink仿真模型为图片或者PDF的方法

Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?

#物联网征文#小熊派设备开发实战

脱光衣服待着就能减肥,当真有这好事?

h264协议

900页数学论文证明旋转的黑洞不会爆炸,丘成桐:30多年来广义相对论首次重大突破...

The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
随机推荐
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
API调用,API传参,面向对接开发,你真的会写接口文档吗?
使用注解将EventBus封装抽取到基类
Intranet penetration tool ngrok usage tutorial
使用RecyclerView实现三级折叠列表
中断系统结构及中断控制详解
Manchester city launch emotional intelligence scarf can be detected, give the fans
Report: The number of students who want to learn AI has increased by 200%, and there are not enough teachers
AI篮球裁判火了,走步算得特别准,就问哈登慌不慌
FFmpeg compiles and installs on win10 (configure libx264)
Flutter入门进阶之旅(五)Image Widget
手写大根堆
The new features of ABP 6.0.0 - rc. 1
Too much volume... Tencent was asked on the side that the memory was full, what would happen?
Ten minutes to teach you how to use VitePress to build and deploy a personal blog site
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
Compensation transaction and idempotency guarantee based on CAP components
Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
Flutter入门进阶之旅(一)-初识Flutter
字符串转换整数 (atoi)