简体   繁体   中英

How to Do the Authentication in Rest Assured?

Unable to Authenticate the API using the Rest Assured? Could somebody share 1 Example for Authentication and Authorization of API Using the Rest Assured?

BASIC Authentication

 given().auth()
  .basic("username", "password")
  .when()
  .get("http://anyURL")
  .then()
  .assertThat()
  .statusCode(HttpStatus.SC_OK);

PREEMPTIVE Authentication

 given().auth()
      .preemptive()
      .basic("username", "password")
      .when()
      .get("http://anyURL")
      .then()
      .assertThat()
      .statusCode(HttpStatus.SC_OK);

DIGEST Authentication

 given().auth()
      .digest("username", "user1Pass")
      .when()
      .get("http://anyURL")
      .then()
      .assertThat()
      .statusCode(HttpStatus.SC_OK);

FORM Authentication

given().auth()
  .form("user1", "user1Pass")
  .when()
  .get("http://anyURL")
  .then()
  .assertThat()
  .statusCode(HttpStatus.SC_OK);

OAuth Authentication

given().auth()
  .oauth2(accessToken)
  .when()
  .get("http://anyURL")
  .then()
  .assertThat()
  .statusCode(HttpStatus.SC_OK);

AUTHORIZATION

given()
       .header("Authorization", "Basic XXXXXXXXXXXXXXXXXX")
       .when()
       .get("http://anyURL")
       .then()
       .assertThat()
       .statusCode(HttpStatus.SC_OK);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM