Custom Data Support

How To Add Custom Header

Custom Header Support

Once the framework is generated then there will be a file HeaderManager.py present inside the config folder.

  • Open that file and you can see one code block i.e.

CUSTOM_HEADER = {}
  • You can add your header name: header value inside this dictionary object. e.g. I need to add a custom header “perf-test”: “true”.

You just need to follow the below procedure.

CUSTOM_HEADER = {
"perf-test": "true"
}

Auth Header Support

  • Once the framework is generated then there will be a file HeaderManager.py present inside the config folder.

  • For auth header, you need to implement the logic to fetch auth token if your API use Oauth2.

import os
import json
"""
Please add custom header in CUSTOM_HEADER dict
"""
CUSTOM_HEADER = {
#"perf-test": "true"
}
AUTH_HEADER = {'apiKey': 'api_key', 'oauth2': 'Bearer'}
class HeaderManager:
     """
     This is a Header manager class which handle global and custom header support.
     """
     def __init__(self):
         """
         Constructor for HeaderManager to initialize empty header
         """
         self.header = {}

     def initialize_header(self):
         """
         This method is responsible to call all header initializer.
         :return: updated header dict
         """
         self.custom_header_initializer()
         self.auth_header_initializer()
         return self.header

     def custom_header_initializer(self):
         """
         This method is responsible to handle custom header given by user.
         :return: updated header dict
         """
         return self.header.update(CUSTOM_HEADER)

     def auth_header_initializer(self):
         """
         This method is responsible to handlde auth header
         :return: updated header dict
         """
         auth_header = {}
         #fixme: Please select one auth and comment remaining
         auth_header = {AUTH_HEADER['apiKey']: self._get_auth_header()}
         auth_header = {'Authorization': "Bearer {}".format(self._get_auth_header())}
         return self.header.update(auth_header)

     @staticmethod
     def _get_auth_header():
         """
         This method handle token generation for auth header handler.
         Note: Please write auth generation logic here.
         :return: token as string
         """
         #fixme: Please add your logic to get token
         token = "API Token"
         return token
  • You need to implement the auth token fetch logic inside _get_auth_header() function

How to Configure Payload In The Framework

If your API uses various data payloads or params then you need to provide that data in the framework. For that, you need to open “parsers/request_helpers/” folder. You can see each API path corresponding payload helper script present inside the folder.

def delete_store_store_order_orderid_(self):
     path_headers = {'Content-Type': 'application/json'}
     headers = path_headers.update(self.header)
     path_payload = {'orderId': None}
     request_path = self.helper_object.url_parser(path_payload, "/store/order/{orderId}")
     self.url = self.host+request_path
     response = self.locust_object.client.delete(self.url, headers=headers, name="delete_store_store_order_orderid_")
  • You need to update “orderId” with your actual value instead of None. You can parameterize this too. So that value will be fetched from sample data present inside the data folder.

  • Similarly each payloads/params you need to update.