[英]How to pass basic authentication as part of request header in RestAssured?
我是 API 测试和 RestAssured 的新手。 我一直在尝试通过 RestAssured 访问 API(受基本身份验证保护)http://restapi.demoqa.com/authentication/CheckForAuthentication但不知道该怎么做。
这是我到目前为止编写的代码:
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 身份验证的更多信息: https ://github.com/rest-assured/rest-assured/wiki/Usage#authentication
包 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.