软件测试学习笔记丨接口请求头
作者:测试人
- 2024-07-15 北京
本文字数:3051 字
阅读完需:约 10 分钟
本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/28389
什么是请求头信息
HTTP 请求头是在 HTTP 请求消息中包含的元数据信息,用于描述请求或响应的一些属性和特征。
实际工作过程中具体要关注的头信息字段需要和研发沟通。
常见的头信息
应用场景
身份认证。
指定请求数据类型。
接口请求头构造(Java)
直接使用参数。
使用 HashMap。
import org.junit.jupiter.api.Test;import java.util.HashMap;import static io.restassured.RestAssured.given;
public class TestHeaders { @Test void header(){ given() .headers("Authorization", "verifyMessage") .headers("name", "ad") .log().all() .when() .get("https://httpbin.ceshiren.com/get") .then() .log().all(); } @Test void headerMap(){ HashMap<String, String> headerMap = new HashMap<>(); headerMap.put("Authorization", "verifyMessage"); headerMap.put("name", "ad"); headerMap.put("school", "hogwarts"); given() .headers(headerMap) .log().all() .when() .get("https://httpbin.ceshiren.com/get") .then() .log().all(); }}复制代码
附录:完整依赖配置(Java)
<!-- 其他使用到的依赖配置 --><!-- 版本配置--><properties> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <java.version>11</java.version> <junit.jupiter.version>5.9.2</junit.jupiter.version> <maven.compiler.version>3.11.0</maven.compiler.version> <maven-surefire-plugin.version>3.0.0</maven-surefire-plugin.version> <rest-assured.version>5.3.0</rest-assured.version> <json-path.version>2.8.0</json-path.version> <!-- allure报告 --> <allure.version>2.21.0</allure.version> <aspectj.version>1.9.19</aspectj.version> <allure.maven.version>2.12.0</allure.maven.version> <allure.cmd.download.url> https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline </allure.cmd.download.url></properties><dependencyManagement> <!-- junit5 版本管理, 找到对应依赖关系的 pom 文件,为了解决依赖冲突问题--> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>${junit.jupiter.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement><dependencies>
<!-- junit 相关依赖下载--> <!-- junit5 --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> <!-- junit5-suite --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite</artifactId> <scope>test</scope> </dependency> <!-- 用做兼容老版本 --> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <scope>test</scope> </dependency> <!-- rest-assured --> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>${rest-assured.version}</version> <scope>compile</scope> </dependency> <!-- json path 解析json文件 --> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>${json-path.version}</version> </dependency> <!-- allure报告--> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-junit5</artifactId> <version>${allure.version}</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency>
</dependencies><build> <plugins> <!-- maven 命令行执行插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin.version}</version> <configuration> <argLine> -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar" </argLine> </configuration> <!-- 防止maven与junit5使用依赖冲突的问题--> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>${junit.jupiter.version}</version> </dependency> </dependencies> </plugin> <!-- maven 编译使用插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> <configuration> <parameters>true</parameters> <source>${java.version}</source> <target>${java.version}</target> <encoding>${maven.compiler.encoding}</encoding> </configuration> </plugin>
<plugin> <groupId>io.qameta.allure</groupId> <artifactId>allure-maven</artifactId> <version>${allure.maven.version}</version> <configuration> <reportVersion>${allure.version}</reportVersion> <allureDownloadUrl>${allure.cmd.download.url}/${allure.version}/allure-commandline-${allure.version}.zip</allureDownloadUrl> </configuration> </plugin> </plugins></build>复制代码
软件测试开发免费视频教程分享
划线
评论
复制
发布于: 刚刚阅读数: 3
版权声明: 本文为 InfoQ 作者【测试人】的原创文章。
原文链接:【http://xie.infoq.cn/article/f0c63cafc3a802e79cc74cdf1】。文章转载请联系作者。
测试人
关注
专注于软件测试开发 2022-08-29 加入
霍格沃兹测试开发学社,测试人社区:https://ceshiren.com/t/topic/22284







评论