“Mock Test APIs In a Distributed Application Environment with AWS Serverless Stack and Zero Application Code Changes”
In an enterprise world, your application might be depended on many other sub systems. Most of the time you have zero ownership on those. Actual issue will come to surface when your application depended on data from those sub systems. Data might be refreshed most of the time, so you can’t have fixed set of data for your test automation work. It will always be a bottleneck for your QA team as well. How can you overcome this situation?
As developers, there are might be many options. One quick solution that might come in to our mind is to have set of pre-defined APIs. If client is requesting for that URL pattern, then from our backend system we will intercept the request and will send the mock data set to client. In the above approach, you need to do code changes in the backend system. And you need to have a mechanism to exclude such interceptors or conditions been push to production. It will be an overhead all the time.
Thought process was carried out to implement a solution which will have zero code changes to our application code and zero operation and maintenance cost. Since our application is deployed in AWS, we thought of using few serverless services to address the above requirement. Let’s drive through it step by step.
Using AWS Application Load Balancer (ALB)
Usual deployment pattern in an enterprise web application is that, you will have a Load balancer on top of the application servers. In AWS, you have a component called Application Load Balancer (ALB), where you can do many actions to your client requests before reaching the application servers. Actually, we were using the AWS classic load balancer, but with the feature set of ALB we moved to ALB and used some of the features to our implementation.
Mock Small Size Response
In ALB you have Listeners and for each listener you can configure rules. In the Listener rules, you can configure a fixed response.
Example: You need to mock the order history data of a certain account. So, if user submit a request URL as below, you need to submit a fixed data set all the time. To configure it, just use the Fixed response listener rule.
IF URL path is /PMT/api/fetchOrderGuide/067-123456, (You can add many other rule conditions to filter your request)
Then Return a Fixed Response (Add your fixed response body)
It’s simple as that. But there is a limitation on max length of the response body, which is 1024 characters. Hence if you have a large response body, then you have to move to the next option.
Medium Size Response
If you have a medium size response less than ~10,000 characters, then integration of ALB listener rules and API Gateway will help your course. Here how you achieve it,
There is a Rule action called Redirect which will Redirect requests from one URL to another. When you have the redirect capability, then you need a component that can produce the response without much effort. Since API Gateway is fully managed by AWS an it has a feature to generate Mock APIs, you can choose API Gateway as the response producer.
In API Gateway (APIG) create a Mock API. For testing purpose you can use an existing APIG and do the following steps or follow this link to go from starch.
- Create a resource in the APIG (ex: /fetchOrderguide)
- Attach a HTTP Get method to the resource while doing that select Integration type as Mock
- In the GET method execution pane, click on the Integration Response container
- Expand the HTTP 200 row and click on the Mapping Templates
- Click on application/json in the Content Type section
- Add the Mock Response in the Text Area and Save
- Deploy the new resource in the Stage you wish and note down the endpoint URL for the newly created GET method
Now the Mock API which is exposed via API Gateway is ready to use. You can test the API through the API Gateway console if needed. By default, API gateway and the APIs are public. But if you need to adhere to your organization security compliance, you might need to make the API Gateway and APIs private. You can follow this AWS resource to make your API Gateway private.
Final step for Medium size responses is to link the Mock API and ALB. As per the below diagram, you can add the filtering rules and then use the Redirect Action. In that section, give the API URL information and keep other options as per the diagram.
Now all set to test your API via postman. If your criteria match with the listener rules, then ALB will redirect the request to API Gateway and utilize the Mock API to generate the response and provided it back to the client.
Note: If you need to test this with your frontend (browser), then you need to start browser without CORS. Follow this link to Start chrome without CORS.
Large Size Response
There is a response limit for API Gateway Mock API responses as well. If you try to add a very large response, then you will get this warning.
“The resource being saved is too large. Consider reducing the number of modeled parameters, the number of response mappings, or perhaps the size of your VTL templates if used.”
Therefore, you need to think on an alternative. The quickest solution would be use a lambda function to generate the large response. Lambda functions are serverless and will have very less cost.
- Create the lambda function using the AWS console
- Give a function name
- Use Author from scratch and use NodeJs 12.x as the runtime. (Feel free to use any)
- Choose a role to execution
- In the Index.js file
exports.handler = function(event, context) {
context.done(null, <Add the large response>);
}
- Save and Test the function to check whether you receive the response as expected
Now your large response is ready. Next you need to integrate it with API Gateway.
- Create a resource in the APIG (ex: /fetchOrderguideGroupView)
- Attach a HTTP Get method to the resource while doing that select Integration type as Lambda Function
- Provide the ARN of the newly created lambda in the Lambda function text box
- Click Save
- Test the API to see you get the response from the Lambda function
All good now. Repeat the same steps that we followed in final step of linking Medium size response of the Mock API with ALB using the Redirect rule action. Test with postman to see your integration works as expected.
There might be many other alternatives, but this helped our requirement on Mocking APIs for test automation in a very fast and serverless manner with zero code changes to your application.