Java 如何在RestAssured中作为请求头的一部分通过基本身份验证?

Java 如何在RestAssured中作为请求头的一部分通过基本身份验证?,java,api,rest-assured,Java,Api,Rest Assured,我不熟悉API测试和重新发行。我一直试图通过重启访问API(受基本身份验证保护),但不知道如何操作 以下是我迄今为止编写的代码: import io.restassured.RestAssured; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; RestAssured.baseURI = "http://restapi.demoqa.com/a

我不熟悉API测试和重新发行。我一直试图通过重启访问API(受基本身份验证保护),但不知道如何操作

以下是我迄今为止编写的代码:

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();

在Restasured中,您可以这样做:

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页面上有关于重新授权身份验证的更多信息:

在重新授权中,您可以这样做:

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()

这里的官方维基页面上有关于重新认证的更多信息:

包jira_APIProject

导入org.testng.annotations.Test

导入io.restassured.restassured; 导入io.restassured.authentication.PreemptiveBasicAuthScheme

导入静态io.restassured.restassured.*

导入java.util.Base64

公共类TestBasicAuth{

@Test
public void auth() {

    RestAssured.baseURI = "https://sdfsdfsdf-home.atlassian.net/";
    PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
    authScheme.setUserName("sdfsdf.sdfsdf@gmail.com");
    authScheme.setPassword("sdfsdfsfdASDAFLq8U1XLsXZ02927");
    RestAssured.authentication = authScheme;

    given().header("Content-Type","application/json")
    .body("{\r\n" + 
            "    \"body\": \"Updated on 22nd april.\",\r\n" + 
            "    \"visibility\": {\r\n" + 
            "        \"type\": \"role\",\r\n" + 
            "        \"value\": \"Administrator\"\r\n" + 
            "    }\r\n" + 
            "}")

    .when().post("rest/api/2/issue/10004/comment")
    .then().log().all().assertThat().statusCode(201);

}

}

一揽子jira_Api项目

导入org.testng.annotations.Test

导入io.restassured.restassured; 导入io.restassured.authentication.PreemptiveBasicAuthScheme

导入静态io.restassured.restassured.*

导入java.util.Base64

公共类TestBasicAuth{

@Test
public void auth() {

    RestAssured.baseURI = "https://sdfsdfsdf-home.atlassian.net/";
    PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
    authScheme.setUserName("sdfsdf.sdfsdf@gmail.com");
    authScheme.setPassword("sdfsdfsfdASDAFLq8U1XLsXZ02927");
    RestAssured.authentication = authScheme;

    given().header("Content-Type","application/json")
    .body("{\r\n" + 
            "    \"body\": \"Updated on 22nd april.\",\r\n" + 
            "    \"visibility\": {\r\n" + 
            "        \"type\": \"role\",\r\n" + 
            "        \"value\": \"Administrator\"\r\n" + 
            "    }\r\n" + 
            "}")

    .when().post("rest/api/2/issue/10004/comment")
    .then().log().all().assertThat().statusCode(201);

}
}