Showing posts with label how do I.... Show all posts
Showing posts with label how do I.... Show all posts

Tuesday, 13 December 2022

How do I ignore JSON array order during request matching?

When matching JSON requests, sometimes the array order is unpredictable e.g. if the array is used to represent a set of unordered objects.

{
  "exampleId": 1
  "exampleRoot": {
    "exampleArray": [
      {
        "id": 1
      },
      {
        "id": 2
      }
    ]
  }
}

To match this type of request in Traffic Parrot, there are several options, here are some of them:

  • matchesJson matcher with {{anyElements}} wildcard token
    {
      "exampleId": 1,
      "exampleRoot": {
        "exampleArray": "{{anyElements}}"
      }
    }
  • matchesJsonPath matcher which uses standard JSONPath notation
    $[?(@.exampleId == 1 && '1' in @.exampleRoot.exampleArray[*].id && '2' in @.exampleRoot.exampleArray[*].id)]

You can find more examples of advanced matchers in the Traffic Parrot documentation

Monday, 12 September 2022

How do I forward the HTTP requests I don't have mappings for to another endpoint?

Sometimes we need to work with a mixture of real and mock APIs, for example using mocks for APIs that are not yet developed and real APIs for those that already exist.

To support this use case, you can use Traffic Parrot to forward HTTP requests to another endpoint instead of returning the default "not found" response.

You can use the proxy responses feature to provide a default response for unmatched requests:
  1. Set a low priority like 10 for the mapping with the proxy response
  2. Leave default priority for other non-proxy response mappings
  3. Now any unmatched requests will return a response from the proxy mapping