This article is the third part of a multi-part series. In this section, I will explain how to render custom mock responses from JSON files stored in a storage container. Below are the different parts of this article series.
- Mocking Custom Responses with Azure API Management – Simple Mock Response
- Mocking Custom Responses with Azure API Management – Custom Mock Response
- Mocking Custom Responses with Azure API Management – Custom Mock Response from External Files
As discussed in the second part of this series, we can embed JSON responses directly within the policy. However, this approach can become messy and difficult to modify or maintain over time if you have many mock scenarios.
Custom mock responses from external files
As an improvement, we can store mock responses externally and retrieve them based on the request using APIM policies. An added advantage is that we can better organize our mock responses in external storage, where we can leverage features like versioning, enhanced availability, and more efficient management.
Here’s the approach I used:
I provisioned an Azure Storage Account and created a Blob Container. Then, I organized my mock responses within a dedicated folder inside the container.
Following is the content of a JSON file
{
"id": 1,
"name": "Introduction to Azure",
"category": "Cloud Computing",
"description": "A beginner-friendly course covering the fundamentals of Microsoft Azure.",
"duration": "4 weeks",
"instructor": "John Doe"
}
Next, I generated a Shared Access Signature (SAS) token with Read permissions and obtained the Blob SAS URL to securely access the mock responses.
Then, I navigated to the APIM instance, selected the Inbound Processing section, and opened the Policy Editor.
Following is the extract of the policy
<inbound>
<base />
<choose>
<when condition="@(context.Request.Url.Query.GetValueOrDefault("id") == "1")">
<send-request mode="new" response-variable-name="response" timeout="10" ignore-error="false">
<set-url>@($"https://rgstoragefedora001.blob.core.windows.net/data/Subject/1.json?sp=r&st=2024-03-08T12:01:42Z&se=2024-03-08T20:01:42Z&spr=https&sv=2022-11-02&sr=b&sig=DrqWZJxGP38PNJYFKCoMgIqn6AjNC71nw0x%2FjITu4aM%3D")</set-url>
<set-method>GET</set-method>
</send-request>
<return-response>
<set-status code="200" reason="OK" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@(new JObject(((IResponse)context.Variables["response"]).Body.As<JObject>()).ToString())</set-body>
</return-response>
</when>
<when condition="@(context.Request.Url.Query.GetValueOrDefault("id") == "2")">
<send-request mode="new" response-variable-name="response" timeout="10" ignore-error="false">
<set-url>@($"https://rgstoragefedora001.blob.core.windows.net/data/Subject/2.json?sp=r&st=2024-03-08T12:34:15Z&se=2024-03-08T20:34:15Z&spr=https&sv=2022-11-02&sr=b&sig=OVhf3uGIASMHWHtg8F%2BypQzLatI9DeEzqoFns2iGOUk%3D")</set-url>
<set-method>GET</set-method>
</send-request>
<return-response>
<set-status code="200" reason="OK" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@(new JObject(((IResponse)context.Variables["response"]).Body.As<JObject>()).ToString())</set-body>
</return-response>
</when>
<otherwise>
<return-response>
<set-status code="404" reason="Not Found" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{
"error": "Student ID not found"
}</set-body>
</return-response>
</otherwise>
</choose>
</inbound>
No comments:
Post a Comment