mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.52k stars 1.06k forks source link

RetrieveRecordedRequests does not return all requests when request Body matcher is used #1734

Open narras-oss opened 1 year ago

narras-oss commented 1 year ago

Reproducer program below. After upgrading from 5.9 version to 5.12 , started noticing this issue with retrieving recorded requests (active_expectation works fine) being less than the actual requests made. This only happens when request matcher has Body match with substring.


package com.vmware.vidm;

import static org.mockserver.model.StringBody.subString;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.UUID;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.mockserver.client.MockServerClient;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;

public class MockServerTest {
  protected static MockServerClient mockServerClient;

  public static void main(String[] args) throws Exception {
    /// Start mockserver with command line "java -jar ~/Downloads/mockserver-netty-5.12.0-shaded.jar -serverPort 50055"
    mockServerClient = new MockServerClient("localhost", 50055);

    HttpClient client = HttpClient.newHttpClient();
    String[] names = new String[100];
    for (int i = 0; i < 100; i++) {
      final String name = "MyAttribute-" + RandomStringUtils.randomAlphabetic(21);
      names[i] = name;
      mockServerClient.when(HttpRequest.request().withPath("/test/path")
              .withMethod("POST")
              .withBody(subString(name)))
          .respond(HttpResponse.response().withBody("\"schemas\" : [ \"urn:AttributeAlias\" ],\n"
              + "      \"externalId\" : \"bf5089b9-ba16-439a-bcfd-200f71df8e35\",\n"
              + "      \"id\" : \"" + UUID.randomUUID() + "\",\n"
              + "      \"name\" : \"" + name + "\",\n"
              + "      \"standard\" : true,\n"
              + "      \"scimExpression\" : \"urn:ietf:params:scim:schemas:core:2.0:User:" + name + "\""));
    }
    for (int i = 0; i < 100; i++) {
      java.net.http.HttpRequest request = java.net.http.HttpRequest.newBuilder()
          .uri(URI.create("http://localhost:50055/test/path"))
          .header("Authorization", "HZN dummytoken")
          .POST(java.net.http.HttpRequest.BodyPublishers.ofString("\"schemas\" : [ \"urn:AttributeAlias\" ],\n"
              + "      \"externalId\" : \"bf5089b9-ba16-439a-bcfd-200f71df8e35\",\n"
              + "      \"name\" : \"" + names[i] + "\",\n"
              + "      \"standard\" : true,\n"
              + "      \"scimExpression\" : \"urn:ietf:params:scim:schemas:core:2.0:User:" + names[i] + "\""))
          .build();
      java.net.http.HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
      if (response.statusCode() != 200) {
        throw new RuntimeException("Failed request status code: " + response.statusCode());
      }
    }
    HttpRequest[] requests = mockServerClient.retrieveRecordedRequests(HttpRequest.request().withPath("/test/path")
        .withMethod("POST"));
    if (requests.length != 100) {
      throw new RuntimeException("Captured request count: " + requests.length);
    };
  }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>mockservertest</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.mock-server</groupId>
      <artifactId>mockserver-core</artifactId>
      <version>5.12.0</version>
    </dependency>
    <dependency>
      <groupId>org.mock-server</groupId>
      <artifactId>mockserver-client-java</artifactId>
      <version>5.12.0</version>
    </dependency>
    <dependency>
      <groupId>org.mock-server</groupId>
      <artifactId>mockserver-netty</artifactId>
      <version>5.12.0</version>
    </dependency>
  </dependencies>

</project>

Output: Exception in thread "main" java.lang.RuntimeException: Captured request count: 69 at com.vmware.vidm.MockServerTest.main(MockServerTest.java:54)

narras-oss commented 1 year ago

When i try with version 5.14 i cannot reproduce this issue (i.e missing requests in RetrieveRecordedRequests) but after 200 or so requests, we start seeing timeouts from mockserver. In version 5.9 , i tried this up to 7000 requests without any problems.