java - 如何在 RestAssured 中将基本身份验证作为请求 header 的一部分传递?

标签 java api rest-assured

我是 API 测试和 RestAssured 的新手。我一直在尝试访问API(受基本身份验证保护)http://restapi.demoqa.com/authentication/CheckForAuthentication通过 RestAssured 但不知道该怎么做。

这是我到目前为止编写的代码:

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

RestAssured.baseURI = "http://restapi.demoqa.com/authentication/CheckForAuthentication";
RequestSpecification request = RestAssured.given();

**I guess authentication code goes here but not sure**
Response response = request.get();

最佳答案

在 RestAssured 中,您可以这样做:

RequestSpecification request = RestAssured.given().auth().basic("username", "password");
Response response = request.get()

还可以进行抢先身份验证:

RequestSpecification request = RestAssured.given().auth().preemptive().basic("username", "password");
Response response = request.get()

官方 wiki 页面上有有关 RestAssured 身份验证的更多信息:https://github.com/rest-assured/rest-assured/wiki/Usage#authentication

关于java - 如何在 RestAssured 中将基本身份验证作为请求 header 的一部分传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53800878/

相关文章:

java - Spring mvc 向其他包隐藏一个包

java - Java中如何保证只能对 `Comparable`个元素进行排序?

c - lua c api : how to push a string with a null character in the middle?

java - RestAssured : annot determine how to serialize content-type application/x-www-form-urlencoded. 如何创建具有键/值结构的请求?

java - 如何让下载文件时保留页面而不重定向?

java - 从另一个 JFrame 打开 JFrame,无需每次都打开一个全新的 JFrame

mysql - StrongLoop 验证错误和必填字段

javascript - 如何使用带有 greasemonkey 的 google maps api 读取地址表并跟踪路线?

RestFuse vs Rest Assured vs MockMVC Rest 服务单元测试框架

java - NoClassDefFoundError:ResponseParserRegistrar