Mock nth request

At a glance
Goal: Return different responses for sequential requests to the same endpoint
Time: 10 minutes
Plugins: MockResponsePlugin
Prerequisites: Set up Dev Proxy

Dev Proxy supports mocking n-th through the nth property on the request object.

Tip

Download this preset by running in the command prompt devproxy config get microsoft-graph-connector.

Using the following mock file as an example, we can see that it contains two mocks to the same request URL. Proxy uses the first response that uses the nth property, when it intercepts a request with the specified URL for the second time. For all other requests, the proxy returns the second response.

Dev Proxy counts every intercepted request that matches the mock's method, url, and bodyFragment, whether or not it returns a mock response for it. The second mock is optional. Without it, Dev Proxy passes all other requests to the API and returns the mock response only for the second request.

Tip

Mocks with the nth property should be first. Proxy uses mocks based on the first match.

File: mocks.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/mockresponseplugin.mocksfile.schema.json",
  "mocks": [
    {
      "request": {
        "url": "https://graph.microsoft.com/v1.0/external/connections/*/operations/*",
        "method": "GET",
        "nth": 2
      },
      "response": {
        "statusCode": 200,
        "body": {
          "id": "1.neu.0278337E599FC8DBF5607ED12CF463E4.6410CCF8F6DB8758539FB58EB56BF8DC",
          "status": "completed",
          "error": null
        }
      }
    },
    {
      "request": {
        "url": "https://graph.microsoft.com/v1.0/external/connections/*/operations/*",
        "method": "GET"
      },
      "response": {
        "statusCode": 200,
        "body": {
          "id": "1.neu.0278337E599FC8DBF5607ED12CF463E4.6410CCF8F6DB8758539FB58EB56BF8DC",
          "status": "inprogress",
          "error": null
        }
      }
    }
  ]
}

Next step

Learn more about the MockResponsePlugin.

Samples

See also the related Dev Proxy samples:

See also