简体   繁体   English

如何在我的 RestAssured 规范生成器中使用 Basic Auth class

[英]How can I use Basic Auth in my RestAssured spec builder class

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.我正在为 Api 测试创建一个新的框架。只是为了优化我的测试 class 我正在使用 specbuilder class 并在另一个 package 中创建了 Utils class。

This is what I am trying to do in Utils class:这就是我在 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.在我的测试中 class 基本身份验证是强制性的。 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.请建议我如何在我的 specbuilder class 中添加基本身份验证,因为我没有找到这样做的内置方法。

There is a built-in method setAuth(AuthenticationScheme auth) in RequestSpecBuilder RequestSpecBuilder中有一个内置方法setAuth(AuthenticationScheme auth)

This would work:这会起作用:

BasicAuthScheme basicAuthScheme = new BasicAuthScheme();
basicAuthScheme.setUserName(apikey);
basicAuthScheme.setPassword(app_Secret);

req = new RequestSpecBuilder()
             .setBaseUri("https://api-stgmars.com")
             .setContentType(ContentType.JSON)
             .setAuth(basicAuthScheme)
             .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将使用 testng 的放心测试更改为 cucumber? - How can I change my restassured test using testng into cucumber? RestAssured:我可以在配置 class 中指定 oauth 设置吗? - RestAssured: Can I specify the oauth setting in the Configuration class? 如何使用我在主类中通过 Netbeans GUI 构建器创建的按钮? - How to use the Button I created via Netbeans GUI builder in my main class? 如何选择我将在Builder类中使用的函数类型? - How can I choose the type of function I'm going to use in Builder class? 我可以在一个 class Lombok 中同时使用 @data 和 @builder 注释吗? - Can I use both @data and @builder annotations in one class Lombok? 我可以使用 Lombok @Builder 传递父 class 作为参数吗? - Can I use the Lombok @Builder passing the parent class as parameter? 如何在 Restassured 请求期间转换 Instant? - How can I convert an Instant during a Restassured request? 如何使用我在 main in java 中创建的 class? - How can I use the class I created in my main in java? 如何将这个JScrollPane类添加到我的swing生成器中? - How could I add this JScrollPane class to my swing builder? 如何使用电报库进行身份验证并将消息发送到Java中的电话号码? - How can i use telegram lib to auth and send message to my phone number in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM