ng-apimock icon indicating copy to clipboard operation
ng-apimock copied to clipboard

Is it possible to get a custom GET response after a mock POST?

Open marcelhageman opened this issue 6 years ago • 1 comments

So as a developer I would like to have the mock API respond with different sets of data automatically, based on what happens in the application I'm developing.

I was thinking it would be great if for each mock file I could configure certain "triggers" with the following setup:

The field triggers is a new field in the mock configuration file. Each separate trigger has its own name and contains two fields: when and then.

The when part is an array of objects (or simply 1 object if not an array).

It it's an array then it's always an AND type of selection:

  • The key "url" or "cookie" or "data" with its name as a value;
  • And a key "value" with its value being whatever it needs to match.

Examples:

  • "when": { "cookie": "userId", value: "ab123" }
  • "when": { "url": "userId", value: "ab123" }
  • "when": { "data": "data.userId", value: "ab123" }

And the then part is basically the result of when this match is found:

The then field is an array of consequences with two fields:

  1. mock (optional, defaults to self) - what mock file are we changing the scenario in?
  2. scenario - to what scenario are we changing when the when criteria are met?

So that would allow any type of API call to automatically change selected scenario's in this same mock but also all other mocks.

Configuration idea

{
  "expression": "/api/select-product/:productId",
  "method": "POST",
  "name": "selectProduct",
  "isArray": false,
  "triggers": {
    "changeBlockedUser": {
      "when": [
        {
          "url": "productId",
          "value": "123"
        }
      ],
      "then": [
        {
          "scenario": "wrongAccount"
        },
        {
          "mock": "customer-details",
          "scenario": "bannedAccount"
        },
        {
          "mock": "customer-products",
          "scenario": "noProducts"
        }
      ]
    },
    "changeEnabledUser": {
      "when": [
        {
          "url": "productId",
          "value": "888"
        }
      ],
      "then": [
        {
          "scenario": "default"
        },
        {
          "mock": "customer-details",
          "scenario": "goodAccount"
        },
        {
          "mock": "customer-products",
          "scenario": "tooManyProducts"
        }
      ]
    }
  },
  "responses": {
    "default": {
      "default": true,
      "status": 200,
      "data": {
        "customerId": "123",
        "firstName": "John",
        "lastName": "Doe"
      }
    },
    "wrongAccount": {
      "default": false,
      "status": 404,
      "data": {}
    }
  }
}

In the example above the API would see that if there's a POST to /api/select-product/123, there are a few changes in the ng-apimock scenario selections:

  • The current mock will from now on respond with the wrongAccount scenario
  • The customer-details mock will from now on respond with the bannedAccount scenario
  • The customer-products mock will from now on respond with the noProducts scenario

And if it receives the product with ID 888 if would switch to different scenarios again.


Would love to hear your thoughts, @mdasberg – we're currently thinking of taking this sort of feature into sprint if at all possible.

Cheers,

Your FrontMen colleague

marcelhageman avatar Nov 06 '18 15:11 marcelhageman

Looks like a great addition. Lets see if we can get a working poc for this.

mdasberg avatar Dec 14 '18 13:12 mdasberg