Mock Response based on specification

We have requirement of building mock response framework based on spec.

Whenever a request is made for the API , it should dynamically fetch the mock response data based on the path and method ; and return it to the end user.

Please suggest a best way to do so.

Any help or suggestions are appreciated.Thanks.

0 5 869
5 REPLIES 5

sidd-harth
Participant V

Well, I do not understand your question completely. I am not sure if you are looking for an automation tool or a proxy.

In Apigee,

  • If you have a Swagger/OAS spec, then you can create an API Proxy.
  • Based on spec, the proxy will contain all the resources with path & methods.
  • In each resource, you can add an Assign Message policy on the response side and set some mock data.
  • Whenever a request is made, it will give the mock data based on the method & path conditions.

I am aware of creating proxies from swagger/oas spec.Also adding assign message policy in each resource won't be good practice as the number of resources may increase over time.

So i am looking for more dynamic way that generates mock response on the basis of http routes and methods in the spec. Also if i am having some standalone server which takes in oas spec as input and generates responses for the same , that would work for me.

Why we need it? --> As the APIs are not yet developed , we need to test it and we need to return some meaningful mock response based on the request made by end user

Have you tried something like https://stoplight.io/open-source/prism/ ?

jaupadhyay
Participant IV

Hi

One simple approach for this is to use KeyValueMapOperations policy.

You can define Mock responses in KVM.

For example you can create KeyValueMap called MyMockData. for Key you can have mocked.GetAccounts.GET and value is you can have mockreponse {"AccountId":"Acc1","AccountName":"Mock Account ","AccountBranch":"MockBranch"}


You can do two step process. In first Step use Javascript to construct Key based on your flow path and operation. You can have these common steps for each flow execution.

Second step Read KVM based on dynamically generated key in first step.

First Step looks something like this.

// Function to construct Key to Read KVM value
ConstructMockKey();

function ConstructMockKey()
{
    // construct the key using Flow name and request verb
    // in this example your flow name is defined as GetAccounts and Request Verb is GET
    // Key will be mocked.GetAccounts.GET
    var myKey =   "mocked." + context.getVariable('current.flow.name') + "."+ context.getVariable('request.verb')  ;
    context.setVariable("Response.BodyKey",myKey );
}

Second step Read KVM looks like this.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="ReadMockData" mapIdentifier="MyMockData">
    <DisplayName>ReadMockData</DisplayName>
    <Properties/>
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>10</ExpiryTimeInSecs>
    <Get assignTo="MockResponse.Body">
        <Key>
            <Parameter ref="Response.BodyKey"/>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

Context variable MockResponse.Body will holds your mock response data you can return this as your response.

With this simple approach you are in full control of defining your mock key and data. It dynamically generates the response for your each unique request.

See if this approach suits your needs.