当前位置:网站首页>接口测试框架中的鉴权处理
接口测试框架中的鉴权处理
2022-07-01 16:00:00 【码同学软件测试】
图片
接口自动化测试中通常都有鉴权机制,就是判断是否在登录状态下,已登录方可调用接口,未登录则不可调用。本文将带领大家学习使用rest-assured框架实现基于cookies和token的鉴权关联,实现接口自动化测试。
1
基于cookies的鉴权关联
业务接口熟悉
码同学全栈接口项目中有基于cookies的查询余额接口,必须先调用登录接口获取cookie并传递给查询余额接口,两个接口的信息如下

通过抓包查看cookies
在页面上登录银行系统,通过Chrome开发者工具抓包,可以看到登录成功以后接口返回中产生了cookie信息

- 使用rest-assured框架实现cookie关联
首先,使用脚本调用登录接口,并从返回信息中获取cookie,代码如下,该方法执行完成后会将登录后的cookies存储在代码中的cookies对象中

其次,调用查询余额接口,并在请求信息中添加cookies对象,这样做就可以让查询余额接口在请求时带上有效cookies,代码如下

2
基于token的鉴权关联
业务接口熟悉
码同学全栈接口项目中有基于token的查询余额接口,必须先调用登录接口获取token并传递给查询余额接口,两个接口的信息如下

- 抓包分析token的产生和传递
在页面上登录银行系统,通过fiddler工具抓包,可以看到登录成功以后接口返回中产生的data信息就是token的值
那我们再分析下查询余额的接口在发起时token信息是如何传递的,依然是fidder工具抓包,我们可以发现在请求的header中有一个字段叫做testfan-token,对应的值就是登录接口返回的token值

- 使用rest-assured框架实现token关联
首先,使用脚本调用登录接口,并从返回信息中获取token,代码如下,该方法执行完成后会将登录后的token值存储在代码中的token变量中

其次,调用查询余额接口,并在请求信息中增加header信息,这样做就可以让查询余额接口在请求时带上有效token,代码如下

好了,不管是cookies还是token,我们都已经有了在rest-assured框架的关联使用,赶紧去试试吧
边栏推荐
- 【LeetCode】43. 字符串相乘
- 你TM到底几点下班?!!!
- Seata中1.5.1 是否支持mysql8?
- Vscode find and replace the data of all files in a folder
- ATSs: automatically select samples to eliminate the difference between anchor based and anchor free object detection methods
- What is the forkjoin framework in the concurrent programming series?
- For the sustainable development of software testing, we must learn to knock code?
- Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
- ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别
- 圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
猜你喜欢

Programming examples of stm32f1 and stm32subeide - production melody of PWM driven buzzer

电脑照片尺寸如何调整成自己想要的

Zero copy technology of MySQL

How does win11 set user permissions? Win11 method of setting user permissions

Where should older test / development programmers go? Will it be abandoned by the times?

Five years after graduation, I became a test development engineer with an annual salary of 30w+

She is the "HR of others" | ones character

【IDM】IDM下载器安装

MySQL的零拷贝技术

大龄测试/开发程序员该何去何从?是否会被时代抛弃?
随机推荐
Zhou Shaojian, rare
When ABAP screen switching, refresh the previous screen
IM即时通讯开发实现心跳保活遇到的问题
Some abilities can't be learned from work. Look at this article, more than 90% of peers
Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)
She is the "HR of others" | ones character
Problems encountered in IM instant messaging development to maintain heartbeat
Detailed explanation of stm32adc analog / digital conversion
Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
使用腾讯云搭建图床服务
【每日一题】1175. 质数排列
TensorFlow團隊:我們沒被拋弃
The picgo shortcut is amazing. This person thinks exactly the same as me
自动、智能、可视!深信服SSLO方案背后的八大设计
[200 opencv routines] 216 Draw polylines and polygons
What time do you get off work?!!!
Zero copy technology of MySQL
[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes
MySQL的零拷贝技术
HR interview: the most common interview questions and technical answers