Sometimes, you may need to perform some tasks, such as altering the traffic that is captured by HyperTest. This can be done programmatically via "Middlewares" which are in simple terms, functions that a user can write in order to implement a custom functionality.
Using middlewares
A middleware section contains the following parts:
Middleware Name
Description
Sample data contains test data on which the middleware provided would be run, in order to verify its correctness.
Input globals is the list of variables globally available in the middleware body
Code contains the actual middleware code, it is here that the middleware is written
After writing your middleware always use the check output function to verify the middleware
Middleware Types
Following is a detailed explanation of all the middlewares that HyperTest provides:
REQUEST_LOGGER: Changes request parameter in newly logged requests based on the parameters provided to it.
SESSION_DIFFERENTIATOR: Used when the session differentiator value needs to figured out programmatically. For example, sorting incoming requests into groups; those that have a specific key in header are grouped together, those that have same ip are grouped together and so on.
PRE_REQUEST: Allows altering request object before making request to you application.
POST_RESPONSE: Updates the session variables for a particular request.
PRE_SESSION: Updates the session variables at the start of a test(User Session).
Common Use Cases
Modifying a captured request before logging it to the database (REQUEST LOGGER)
Suppose you want to strip the base API path coming from the kubernetes ingress. (because it's specified in PRIMARY_BASE_URL and CANDIDATE_BASE_URL). We could easily do this with the use of REQUEST_LOGGER middleware.
You would be provided with standard request attributes such as headers, verb, path, query, body, timestamp, ip, and an additional attribute MIDDLEWARE_ENV which you have set in the Service Config.
REQUEST_LOGGER middleware expects you to return an object with these properties: headers, verb, path, query, body, timestamp, ip.
Custom differentiation logic between user sessions (SESSION DIFFERENTIATOR)
For differentiating between sessions by default, HYPERTEST uses GROUP_SESSION_BY from Service Config or if x-ht-session-id is found in headers.
However, in some scenarios, you might want to differentiate based on a property such as userId/customerId that could appear in the body at different levels.
For extracting such value we have SESSION_DIFFERENTIATOR middleware where you could write your logic for extraction of such value.
Randomizing request parameter value at runtime(PRE REQUEST)
More often than not while writing a test for a user registration flow we would want to pass a new and unique email id in the request so that we could identify different user accounts or sometimes it’s just a constraint from the system that every user should have a unique email, or in some cases of e-commerce platforms it’s the client’s responsibility to generate the orderId and obviously, these id’s have to be unique.
To facilitate this we have PRE_REQUEST middleware which will be executed before making a request and the updated request object will be used for making the request.
For input params you would be provided with headers, verb, path, query, body, sessionVariables, instanceName, baseUrl, and MIDDLEWARE_ENV.
PRE_REQUEST middleware expects you to return an object with these properties headers, verb, path, query, body, sessionVariables and meta object which gets stored in response object, you can use it for logging errors or storing variables generated inside middleware for debugging purposes.
Verifying a newly created user via an internal API call(POST RESPONSE)
In some scenarios, you might want to do an email or OTP based verification after creating a new user. To automate this entire process of verification you might have to make an internal API call.
For adding such custom logic which would be executed after getting a response we have POST_RESPONSE middleware, where you can make API calls, modify a response object, or update sessionVariables.
For input params, you would be provided with req_headers, req_verb, req_path, req_query, req_body, req_meta, res_statusCode, res_headers, res_body, res_responseTime, sessionVariables, baseUrl, MIDDLEWARE_ENV, and axios(HTTP client).
POST_RESPONSE middleware expects you to return an object with sessionVariables property.
Generating a short-lived access token for a test case(PRE SESSION, PRE REQUEST)
One of the most critical features for any application would be administrative functionalities.
Usually while writing automation scripts for admin flows you would write a function with @before annotation which would generate all the tokens required for that particular test case.
Similarly, for generating these access tokens before running a test(user session) we have middleware which will be executed before starting a test.
Let’s have a look at the code for such a use case.
PRE_SESSION implementation: For input params, you would be provided with ip, sessionDifferentiatorVal, lastRequestTimestamp, shouldPlayBeforeAllTests, shouldStopOnFail, shouldPlayOnLocal, shouldPlayInSmokeTest, description, isSavedSession, isActiveSession, sessionTags, baseUrl, MIDDLEWARE_ENV, and axios(HTTP client).
PRE_SESSION middleware expects you to return an object with sessionVariables property.
Suppose we want to test a loan processing flow for a money lending application, where we have to create an initial state for the application before the actual test starts.
All the state operations for the flow is automatically captured by HyperTest, but by default sessions are segregated based on the GROUP_SESSION_BY configuration.
Here we would want to group the requests based on loanId for creating a end-to-end test for loan process, for this we can use SESSION_DIFFERENTIATOR middleware.
Now, for creating initial state we will use PRE_SESSION middleware, where we will implement following steps:
Generate access token for admin
Create a new Agent.
Create a new Customer.
Create a new Loan for the Customer.
Assign this loan as a task to agent.
Generate access token for agent.
To update these sessionVariables in the captured request we will use PRE_REQUEST middleware