如何在RestAssured中将基本身份验证作为请求头的一部分进行传递



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

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

包jira_APIProject;

导入org.testng.annotations.Test;

import io.restassured.restassured;import io.restasured.authentication.PreemptiveBasicAuthScheme;

导入静态io.ressured.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("{rn" + 
"    "body": "Updated on 22nd april.",rn" + 
"    "visibility": {rn" + 
"        "type": "role",rn" + 
"        "value": "Administrator"rn" + 
"    }rn" + 
"}")
.when().post("rest/api/2/issue/10004/comment")
.then().log().all().assertThat().statusCode(201);
}

}

相关内容

  • 没有找到相关文章

最新更新