Using Oauth2 in API for the load testing (HP Load Runner)

Using Oauth2 in API for the load testing.

So the problem that we got is

  1. get the Authorization key by sending the Client id, secret,grant Type,username and password.
  2. Using the authorization key perform GET operation.

We are using Web_Rest in LR for sending the request to server

web_rest("POST: https://fasteningcode.com/connect...",
        "URL=https://fasteningcode2.com/connect",
        "Method=POST",
        "EncType=x-www-form-urlencoded",
        "Snapshot=t87516.inf",
        ITEMDATA,
        "Name=client_id", "Value=SeCretCient", ENDITEM,
        "Name=client_secret", "Value=simplePass", ENDITEM,
        "Name=grant_type", "Value=Complex", ENDITEM,
        "Name=username", "Value=FunnyName", ENDITEM,
        "Name=password", "Value=FunnyPass", ENDITEM,
        "Name=scope", "Value=api", ENDITEM,
        LAST);

To save the authorization key from the server we are using web_reg_save_param_ex

web_reg_save_param_ex(
  "ParamName=auth",
  "LB=\"token\":\"",
  "RB=\",\"exp",
  SEARCH_FILTERS,
  "Scope=BODY",
  "IgnoreRedirections=Yes",
  LAST);

In my case I need to add a string in front of the authorization key, for that I used

lr_param_sprintf("auth","AuthCode %s",lr_eval_string("{auth}"));

Next step is to use this auth key for the next query to the server

web_rest("GET: https://fasteningcode.com/connect/abc...",
  "URL=https://fasteningcode.com/connect/abc/......",
  "Method=GET",
  "Snapshot=t692861.inf",
  HEADERS,
  "Name=Accept", "Value=application/json", ENDHEADER,
  "Name=Authorization", "Value={auth}", ENDHEADER,
  LAST);

 

On top of this I had saved the response like we did in the auth in the second response. So the full code look like this

 

Action()
{
  // for getting the response from the server
  web_reg_save_param_ex(
    "ParamName=auth",
    "LB=\"token\":\"",
    "RB=\",\"exp",
    SEARCH_FILTERS,
    "Scope=BODY",
    "IgnoreRedirections=Yes",
    LAST);

  
    web_rest("POST: https://fasteningcode.com/connect...",
    "URL=https://fasteningcode2.com/connect",
    "Method=POST",
    "EncType=x-www-form-urlencoded",
    "Snapshot=t87516.inf",
    ITEMDATA,
    "Name=client_id", "Value=SeCretCient", ENDITEM,
    "Name=client_secret", "Value=simplePass", ENDITEM,
    "Name=grant_type", "Value=Complex", ENDITEM,
    "Name=username", "Value=FunnyName", ENDITEM,
    "Name=password", "Value=FunnyPass", ENDITEM,
    "Name=scope", "Value=api", ENDITEM,
    LAST);

  lr_param_sprintf("auth","AuthCode %s",lr_eval_string("{auth}"));

  lr_message(lr_eval_string("{auth}"));	

  
  // for getting the response from the server
  web_reg_save_param_ex(
    "ParamName=ResponseBody",
    "LB=",
    "RB=",
    SEARCH_FILTERS,
    "Scope=BODY",
    LAST);

  
  web_rest("GET: https://fasteningcode.com/connect/abc...",
    "URL=https://fasteningcode.com/connect/abc/......",
    "Method=GET",
    "Snapshot=t692861.inf",
    HEADERS,
    "Name=Accept", "Value=application/json", ENDHEADER,
    "Name=Authorization", "Value={auth}", ENDHEADER,
    LAST);

  lr_message(lr_eval_string("{ResponseBody}"));	
  
  return 0;
  
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *