Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
marcelhageman opened this issue Nov 6, 2018 · 1 comment
Open

Comments

@marcelhageman
Copy link

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

@mdasberg
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants