如何在 RestAssured 规范构建器类中使用基本身份验证
我正在创建一个新的框架来保证 Api 测试。为了优化我的测试类,我使用了 specbuilder 类并在另一个包中创建了 Utils 类。
这就是我在 Utils 类中尝试做的事情:
public class Utils {
RequestSpecification req;
public RequestSpecification requestSpecification() {
RestAssured.baseURI = "https://api-stgmars.com";
req = new RequestSpecBuilder().setBaseUri("https://api-stgmars.com").setContentType(ContentType.JSON)
.build();
return req;
}
}
这里的问题是我不确定如何添加 basic auth 。在我的测试类中,基本身份验证是强制性的。这是这样的:
public static void createUser() {
RestAssured.baseURI = "https://api-stgmars.com";
String response = given().auth() // Storing response body for user creation
.basic(apikey, app_Secret).header("Content-Type", "application/json")
}
请建议我如何在我的specbuilder类中添加基本身份验证,因为我发现没有内置方法可以这样做。
I am creating a new framework for restassured for Api testing.Just to optimize my test class I am using specbuilder class and created Utils class in another package.
This is what I am trying to do in Utils class:
public class Utils {
RequestSpecification req;
public RequestSpecification requestSpecification() {
RestAssured.baseURI = "https://api-stgmars.com";
req = new RequestSpecBuilder().setBaseUri("https://api-stgmars.com").setContentType(ContentType.JSON)
.build();
return req;
}
}
The problem here is I am not sure how to add basic auth . In my test class Basic auth is mandatory. which is something like this:
public static void createUser() {
RestAssured.baseURI = "https://api-stgmars.com";
String response = given().auth() // Storing response body for user creation
.basic(apikey, app_Secret).header("Content-Type", "application/json")
}
Please suggest how can I add Basic auth in my specbuilder class as there is no inbuilt method i found to do so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
RequestSpecBuilder
中有一个内置方法setAuth(AuthenticationScheme auth)
这可以工作:
There is a built-in method
setAuth(AuthenticationScheme auth)
inRequestSpecBuilder
This would work: