<p>MockServer supports verification of requests it has received, including both proxied requests and requests that have matched an expectation.</p>
<p>Verification can be specified as follows:</p>
<ul>
    <li>a <a href="/mock_server/creating_expectations.html#request_matchers"><strong>request matcher</strong></a> and a condition indicating the number of times the request should be matched</li>
	<li>a sequence of <a href="/mock_server/creating_expectations.html#request_matchers"><strong>request matchers</strong></a> that is matched in order</li>
</ul>

<a id="how_verification_works" class="anchor" href="#how_verification_works">&nbsp;</a>

<h3>How Verification Works</h3>

<p>MockServer records all received requests in an in-memory event log (separate from application logs). Verification checks this event log:</p>

<p><strong>For request matcher count verification:</strong> MockServer filters the event log to find all <code>RECEIVED_REQUEST</code> entries matching your request matcher, counts them, and compares the count against your constraint (<code>atLeast(n)</code>, <code>exactly(n)</code>, etc.). Returns HTTP 202 if the count matches, or HTTP 406 with an error message if it does not.</p>

<p><strong>For expectation ID verification:</strong> MockServer retrieves all events associated with the specified expectation ID (including <code>EXPECTATION_RESPONSE</code> and <code>FORWARDED_REQUEST</code> entries) and verifies the count.</p>

<p><strong>For sequence verification:</strong> MockServer scans recorded requests in order, checking that each expected request appears after the previous match. This is not a simple count comparison—order matters.</p>

<p><strong>Important notes for parallel testing:</strong></p>
<ul>
    <li>The event log size is bounded by the <code>maxLogEntries</code> configuration property (default: minimum of free heap KB / 8 and 100,000 entries—see <a href="/mock_server/configuration_properties.html">configuration</a>). When this limit is reached, the oldest entries are evicted. If you run many parallel tests generating thousands of requests, consider increasing this value.</li>
    <li>Event recording uses an asynchronous ring buffer. However, verification operations are serialized through the same ring buffer in FIFO order, so a request that has reached MockServer and been recorded will be visible to subsequent verification calls. Intermittent failures are more commonly caused by:
        <ul>
            <li>Your application under test sending requests asynchronously—ensure the application has completed sending before you verify</li>
            <li>Cross-test interference—one test's requests appearing in another test's verification</li>
            <li>Log eviction due to high request volume exceeding <code>maxLogEntries</code></li>
        </ul>
    </li>
    <li><strong>Best practices for parallel tests:</strong>
        <ul>
            <li>Use separate MockServer instances (different ports or containers) per test for complete isolation</li>
            <li>If sharing an instance, use unique paths per test (e.g., <code>/test/{testId}/...</code>) or unique headers/query parameters in matchers</li>
            <li>Avoid broad matchers like only <code>path("/api")</code> in parallel tests</li>
            <li>Be careful with <code>clear()</code> or <code>reset()</code>—they affect all tests sharing the instance</li>
            <li>If verification fails intermittently because your system under test is asynchronous, use retry-with-backoff to wait for requests to arrive (not to wait for MockServer to process already-received requests)</li>
        </ul>
    </li>
    <li>To retrieve recorded requests for debugging, use <code>PUT /mockserver/retrieve?type=REQUESTS</code> or <code>mockServerClient.retrieveRecordedRequests(null)</code>.</li>
</ul>

<p>For more details on the internal architecture, see the <a href="https://github.com/mock-server/mockserver-monorepo/blob/master/docs/code/event-system.md#verification" target="_blank" rel="noopener noreferrer">Event System documentation</a>.</p>

<a id="verification_timing" class="anchor" href="#verification_timing">&nbsp;</a>

<h3>Verification Timing</h3>

<p><strong>Note:</strong> MockServer processes log entries asynchronously using a ring buffer. When calling <code>verify()</code> immediately after sending requests, there may be a brief delay before log entries are available for verification. If verification fails unexpectedly right after requests are sent, add a small delay (e.g., 100ms) before retrying. This is most commonly needed in high-throughput scenarios or when the system under test sends requests asynchronously.</p>

<h3>Verifying Repeating Requests</h3>

<p>Verify that a request has been received by MockServer a specific number of times using a <strong>Verification</strong></p>

<button id="button_verify_req" class="accordion title"><strong>Verify Repeating Requests Code Examples</strong></button>
<div class="panel title">
<button id="button_verify_req_at_least_twice" class="accordion">verify requests received at least twice</button>
<div class="panel">
    <button class="accordion inner">Java</button>
    <div class="panel">
        <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .verify(
        request()
            .withPath("/some/path"),
        VerificationTimes.atLeast(2)
    );</code></pre>
    </div>
    <button class="accordion inner">JavaScript</button>
    <div class="panel">
            <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .verify(
    {
      'path': '/some/path'
    }, 2)
  .then(
    function () {
      console.log("request found at least 2 times");
    },
    function (error) {
      console.log(error);
    }
  );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
            <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
    "httpRequest": {
        "path": "/simple"
    },
    "times": {
        "atLeast": 2
    }
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_verify_req_at_most_twice" class="accordion">verify requests received at most twice</button>
<div class="panel">
    <button class="accordion inner">Java</button>
    <div class="panel">
    <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
.verify(
    request()
        .withPath("/some/path"),
    VerificationTimes.atMost(2)
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.verify(
{
  'path': '/some/path'
}, 0, 2)
.then(
function () {
  console.log("request found at most 2 times");
},
function (error) {
  console.log(error);
}
);</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
    "httpRequest": {
        "path": "/simple"
    },
    "times": {
        "atMost": 2
    }
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_verify_req_exactly_twice" class="accordion">verify requests received exactly twice</button>
<div class="panel">
    <button class="accordion inner">Java</button>
    <div class="panel">
        <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .verify(
        request()
            .withPath("/some/path"),
        VerificationTimes.exactly(2)
    );</code></pre>
    </div>
    <button class="accordion inner">JavaScript</button>
    <div class="panel">
            <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .verify(
    {
      'path': '/some/path'
    }, 2, 2)
    .then(
        function () {
            console.log("request found exactly 1 time");
        },
        function (error) {
            console.log(error);
        }
    );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
            <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
    "httpRequest": {
        "path": "/simple"
    },
    "times": {
        "atLeast": 2,
        "atMost": 2
    }
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_verify_req_at_least_twice_by_openapi" class="accordion">verify requests received at least twice by openapi</button>
<div class="panel">
    <button class="accordion inner">Java</button>
    <div class="panel">
    <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .verify(
        openAPI(
            "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json"
        ),
        VerificationTimes.atLeast(2)
    );</code></pre>
    </div>
    <button class="accordion inner">JavaScript</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .verify(
        {
            'specUrlOrPayload': 'https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json'
        }, 2)
    .then(
        function () {
            console.log("request found exactly 2 times");
        },
        function (error) {
            console.log(error);
        }
    );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
    "httpRequest": {
        "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json"
    },
    "times": {
        "atLeast": 2
    }
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_verify_req_exactly_once_by_openapi" class="accordion">verify requests received at exactly once by openapi and operation</button>
<div class="panel">
    <button class="accordion inner">Java</button>
    <div class="panel">
    <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .verify(
        openAPI(
            "org/mockserver/openapi/openapi_petstore_example.json",
            "showPetById"
        ),
        VerificationTimes.once()
    );</code></pre>
    </div>
    <button class="accordion inner">JavaScript</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .verify(
        {
            'specUrlOrPayload': 'org/mockserver/openapi/openapi_petstore_example.json',
            'operationId': 'showPetById'
        }, 1, 1)
    .then(
        function () {
            console.log("request found exactly 2 times");
        },
        function (error) {
            console.log(error);
        }
    );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
    "httpRequest": {
        "specUrlOrPayload": "org/mockserver/openapi/openapi_petstore_example.json",
        "operationId": "showPetById"
    },
    "times": {
        "atLeast": 1,
        "atMost": 1
    }
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_verify_req_exactly_once_by_expectation_id" class="accordion">verify requests received at exactly once by expectation id</button>
<div class="panel">
    <button class="accordion inner">Java</button>
    <div class="panel">
    <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .verify(
        "31e4ca35-66c6-4645-afeb-6e66c4ca0559",
        VerificationTimes.once()
    );</code></pre>
    </div>
    <button class="accordion inner">JavaScript</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .verifyById(
        {
            'id': '31e4ca35-66c6-4645-afeb-6e66c4ca0559'
        }, 1, 1)
    .then(
        function () {
            console.log("request found exactly 1 time");
        },
        function (error) {
            console.log(error);
        }
    );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
            "id": "31e4ca35-66c6-4645-afeb-6e66c4ca0559"
        }'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_verify_req_never_received" class="accordion">verify requests never received</button>
<div class="panel">
    <button class="accordion inner">Java</button>
    <div class="panel">
        <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .verify(
        request()
            .withPath("/some/path"),
        VerificationTimes.exactly(0)
    );</code></pre>
    </div>
    <button class="accordion inner">JavaScript</button>
    <div class="panel">
            <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .verify(
    {
      'path': '/some/path'
    }, 0, true)
  .then(
    function () {
      console.log("request found zero times");
    },
    function (error) {
      console.log(error);
    }
  );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
            <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
    "httpRequest": {
        "path": "/simple"
    },
    "times": {
        "atMost": 0
    }
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verify" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
</div>

<h3>Verifying Request Sequences</h3>

<p>Verify that a sequence of requests has been received by MockServer in the specified order using a <strong>VerificationSequence</strong></p>

<p>The each request in the sequence will be verified to have been received at least once, in the exact order specified.</p>

<button id="button_verify_sequence" class="accordion title"><strong>Verify Request Sequences Code Examples</strong></button>
<div class="panel title">
<button id="button_verify_sequence_of_reqs" class="accordion">verify request sequence received</button>
<div class="panel">
    <button class="accordion inner">Java</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .verify(
        request()
            .withPath("/some/path/one"),
        request()
            .withPath("/some/path/two"),
        request()
            .withPath("/some/path/three")
    );</code></pre>
    </div>
    <button class="accordion inner">JavaScript</button>
    <div class="panel">
            <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .verifySequence(
    {
      'path': '/some/path/one'
    },
    {
      'path': '/some/path/two'
    },
    {
      'path': '/some/path/three'
    }
  )
  .then(
    function () {
      console.log("request sequence found in the order specified");
    },
    function (error) {
      console.log(error);
    }
  );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verifySequence" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
            <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/verifySequence" -d '{
   "httpRequests":[
      {
         "path":"/some/path/one"
      },
      {
         "path":"/some/path/two"
      },
      {
         "path":"/some/path/three"
      }
   ]
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verifySequence" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_verify_sequence_of_reqs_using_openapi" class="accordion">verify request sequence received using openapi</button>
<div class="panel">
    <button class="accordion inner">Java</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .verify(
        request()
            .withPath("/status"),
        openAPI(
            "org/mockserver/openapi/openapi_petstore_example.json",
            "listPets"
        ),
        openAPI(
            "org/mockserver/openapi/openapi_petstore_example.json",
            "showPetById"
        )
    );</code></pre>
    </div>
    <button class="accordion inner">JavaScript</button>
    <div class="panel">
            <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .verifySequence(
        {
            'path': '/status'
        },
        {
            'specUrlOrPayload': 'org/mockserver/openapi/openapi_petstore_example.json',
            'operationId': 'listPets'
        },
        {
            'specUrlOrPayload': 'org/mockserver/openapi/openapi_petstore_example.json',
            'operationId': 'showPetById'
        }
    )
    .then(
        function () {
            console.log("request sequence found in the order specified");
        },
        function (error) {
            console.log(error);
        }
    );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verifySequence" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
            <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/verifySequence" -d '{
   "httpRequests":[
      {
         "path": "/status"
      },
      {
         "specUrlOrPayload": "org/mockserver/openapi/openapi_petstore_example.json",
         "operationId": "listPets"
      },
      {
         "specUrlOrPayload": "org/mockserver/openapi/openapi_petstore_example.json",
         "operationId": "showPetById"
      }
   ]
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verifySequence" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_verify_sequence_of_reqs_using_expectation_ids" class="accordion">verify request sequence received using expectation ids</button>
<div class="panel">
    <button class="accordion inner">Java</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .verify(
        "31e4ca35-66c6-4645-afeb-6e66c4ca0559",
        "66c6ca35-ca35-66f5-8feb-5e6ac7ca0559",
        "ca3531e4-23c8-ff45-88f5-4ca0c7ca0559"
    );</code></pre>
    </div>
    <button class="accordion inner">JavaScript</button>
    <div class="panel">
            <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .verifySequenceById(
        {
            'id': '31e4ca35-66c6-4645-afeb-6e66c4ca0559'
        },
        {
            'id': '66c6ca35-ca35-66f5-8feb-5e6ac7ca0559'
        },
        {
            'id': 'ca3531e4-23c8-ff45-88f5-4ca0c7ca0559'
        }
    )
    .then(
        function () {
            console.log("request sequence found in the order specified");
        },
        function (error) {
            console.log(error);
        }
    );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verifySequence" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
            <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/verifySequence" -d '{
    "httpRequests": [
        {
            "id": "31e4ca35-66c6-4645-afeb-6e66c4ca0559"
        },
        {
            "id": "66c6ca35-ca35-66f5-8feb-5e6ac7ca0559"
        },
        {
            "id": "ca3531e4-23c8-ff45-88f5-4ca0c7ca0559"
        }
    ]
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/verify/put_verifySequence" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
</div>
