How to write a mock service in Apigee

How to write a mock service in  Apigee

 

Need :

Many times, we have the details of the microservice that we need to use but the actual microservice is not ready. We only have swagger specs.

Sometimes, we are not supposed to hit actual endpoints due to data issues or some data cost issues, mainly for local or dev environment.

 

Solution:

We can create a mock service that can simulate responses based on the data that has been passed. For example, we pass the data- user id and we are expecting a 200 ok response, and for other user id or data, we are expecting 404, 503, or some other errors. So let’s simulate responses based on the user inputs and we can create a mock service in APIGEE.

 

Create a new API proxy with “No Target” option

Go to Apigee and select “create new proxy” option. Since it is mock , there is no need of target. Just create and give some name and build and deploy.

 

 

 

Let’s create error cases

After that, now let’s get into development mode, and let’s create some error cases. So here if the request has some specific text, then return this.

First, create an error policy. Click on “+”  next to policies

Select Raise Fault and let’s simulate 404 error. One can add any dummy payload in the response and we need to use HTTP status code as used below.

Add below content in the policy


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<RaiseFault async="false" continueOnError="false" enabled="true" name="Raise-Fault-404">

<DisplayName>Raise Fault-404</DisplayName>

<Properties/>

<FaultResponse>

<Set>

<Headers/>

<Payload contentType="application/json; charset=utf-8" variablePrefix="@" variableSuffix="#">

{

"status": 404,

 

}

</Payload>

<StatusCode>404</StatusCode>

<ReasonPhrase>404 error</ReasonPhrase>

</Set>

</FaultResponse>

<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>

</RaiseFault>

 

Now create a flow condition.

 

Select the “+” icon and select flow condition. Here we can specify the condition used to simulate the responses. For example, if there is any text in the request, like ‘404Data’ in the request, then use this condition. Here we need to add “*…*” as a regular expression since the request string will be having other values also.

So we have both condition and fault response, and now let’s connect both of them.

Select the Flow 404 and then click on “+Step” button and add the fault condition.

 

 

 

 

Similarly, we can simulate many error cases. Just add new condition flows and their raise faults.

 

 

Now let’s add a positive case.

Add an assign message policy and put the response that is required in the success case scenario.



 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">

<DisplayName>Assign Message-1</DisplayName>

<Properties/>

<Set>

 

<Payload contentType="application/json">

{

"status": 200,

"data": "data"

}

}

</Payload>

</Set>

<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>

<AssignTo createNew="false" transport="http" type="response"/>

</AssignMessage>

 

 

 

Now just create a condition just like an error message and assign this policy to the flow.

 

Now just save the API proxy and test it with your data.

 

 

Leave a Comment

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