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.
There is a built-in method setAuth(AuthenticationScheme auth)
in RequestSpecBuilder
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();