Skip to main content

Day 8: Understanding authentication in Rest Assured

Day 8: Understanding authentication in Rest Assured

On Day 8 of Rest Assured training, you will learn about authentication in Rest Assured. Authentication is the process of verifying the identity of the user or client making a request to a server or API. In Rest Assured, there are several authentication techniques you can use, such as basic authentication, OAuth, and JWT authentication.

Basic authentication involves sending a username and password in the headers of the request. OAuth is a more complex authentication method that involves exchanging tokens between the client and server. JWT authentication involves using JSON Web Tokens to authenticate requests.

During this training, you will learn how to use these authentication techniques in Rest Assured, and how to incorporate them into your tests. This knowledge will be particularly useful if you need to test APIs that require authentication to access certain endpoints or resources.

here are some examples of how to authenticate requests using Rest Assured with different authentication techniques:

Basic Authentication:

To authenticate using basic authentication, you can use the .auth() method provided by Rest Assured. Here's an example:



given()
  .auth()
    .basic("username", "password")
  .when()
    .get("/api/endpoint")
  .then()
    .statusCode(200);

  

In the above example, we're sending a GET request to /api/endpoint with basic authentication using the username and password provided.

OAuth Authentication:

To authenticate using OAuth, you can use the .oauth2() method provided by Rest Assured. Here's an example:


       
       given()
  .auth()
    .oauth2("access_token")
  .when()
    .get("/api/endpoint")
  .then()
    .statusCode(200);



  

In the above example, we're sending a GET request to /api/endpoint with OAuth authentication using the access token provided.

JWT Authentication:

To authenticate using JWT, you can add the JWT token to the header of your request using the .header() method provided by Rest Assured. Here's an example:


given()
  .header("Authorization", "Bearer jwt_token")
  .when()
    .get("/api/endpoint")
  .then()
    .statusCode(200);

  

In the above example, we're sending a GET request to /api/endpoint with JWT authentication using the token provided.
These are just a few examples of how to authenticate requests using Rest Assured with different authentication techniques.

Comments

Popular posts from this blog

How to integrate Autoit with selenium

For handling Dialog boxes which are not web based, Then Autoit is the best Tool to handle this These are the following code should written in Selenium Selenium Code: try { String[] commands = new String[] {}; commands = new String[] { "Path" }; // location of Autoit EXE file Runtime.getRuntime().exec(commands); } catch (IOException e) { } Autoit code: if WinWaitActive("File Upload") Then ;MsgBox(2,"window found","Found the window") WinActivate("File upload") Send("!n") Sleep(5000) Send("File path") SEND("{ENTER}") ;location of the file you want to a to the form and submit ;Send("!O") Else MsgBox(1,"TimeOut","Timed out") EndIf Here we need to create a Exe file for the Autoit script and that path should be mentioned in selenium code.

Reflection API

The Reflection API allows Java code to examine classes and objects at run time.The new reflection classes allow you to call another class's methods dynamically at run time. With the reflection classes, you can also examine an instance's fields and change the fields' contents. The Reflection API consists of the java.lang.Class class and the java.lang.reflect classes: Field, Method, Constructor, Array, and Modifier. Example program: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; // calling method whose name we store in a variable public class ReflectionAPI { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { String x="sampleTest"; //String meth=read name from xls file Method method= ReflectionAPI.class.getMethod(x, String.class); method.invoke(method, "welcome"); System.out.

Collection API

CollectionAPI: The Java Collections API's provide Java developers with a set of classes and interfaces that makes it easier to handle collections of objects. In a sense Collection's works a bit like arrays, except their size can change dynamically, and they have more advanced behaviour than arrays. Example program: import java.util.ArrayList; import java.util.Hashtable; public class CollectionAPI { /** * @param args */ public static void main(String[] args) { int names[] = new int[5]; ArrayList list = new ArrayList (); list.add("Ramu");//0 list.add("Venu");//1 list.add("Raju");//2 System.out.println(list.size()); for(int i =0; i<=list.size(); i++) { System.out.println(list.get(i)); } // key - value // key - unique Hashtable table = new Hashtable (); table.put("name", "Hyderabad"); table.put("place", "Mumbai"); table.put("na