Search in sources :

Example 16 with OpenAPIDefinition

use of org.mockserver.model.OpenAPIDefinition in project mockserver by mock-server.

the class HttpRequestsPropertiesMatcherTest method shouldMatchByPlainTextBodyWithRequiredFalse.

@Test
public void shouldMatchByPlainTextBodyWithRequiredFalse() {
    // given
    HttpRequestsPropertiesMatcher httpRequestsPropertiesMatcher = new HttpRequestsPropertiesMatcher(configuration, mockServerLogger);
    // when
    httpRequestsPropertiesMatcher.update(new Expectation(new OpenAPIDefinition().withSpecUrlOrPayload("---" + NEW_LINE + "openapi: 3.0.0" + NEW_LINE + "paths:" + NEW_LINE + "  \"/somePath\":" + NEW_LINE + "    post:" + NEW_LINE + "      operationId: someOperation" + NEW_LINE + "      requestBody:" + NEW_LINE + "        required: false" + NEW_LINE + "        content:" + NEW_LINE + "          application/json:" + NEW_LINE + "            schema:" + NEW_LINE + "              type: object" + NEW_LINE + "              required:" + NEW_LINE + "                - id" + NEW_LINE + "                - name" + NEW_LINE + "              properties:" + NEW_LINE + "                id:" + NEW_LINE + "                  type: integer" + NEW_LINE + "                  format: int64" + NEW_LINE + "                name:" + NEW_LINE + "                  type: string" + NEW_LINE + "          plain/text:" + NEW_LINE + "            schema:" + NEW_LINE + "              type: object" + NEW_LINE + "              required:" + NEW_LINE + "                - id" + NEW_LINE + "                - name" + NEW_LINE + "              properties:" + NEW_LINE + "                id:" + NEW_LINE + "                  type: integer" + NEW_LINE + "                  format: int64" + NEW_LINE + "                name:" + NEW_LINE + "                  type: string" + NEW_LINE)));
    // then
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath").withHeader("content-type", "plain/text").withBody(exact("{ \"id\": 1, \"name\": \"abc\" }"))));
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath").withHeader("content-type", "plain/text; charset=utf-8").withBody(exact("{ \"id\": 1, \"name\": \"abc\" }"))));
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath")));
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath").withHeader("content-type", "plain/text")));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath").withHeader("content-type", "plain/text").withBody(exact("{ \"id\": 1, \"name\": 1 }"))));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath").withHeader("content-type", "plain/text").withBody(exact("{ \"id\": \"abc\", \"name\": \"abc\" }"))));
}
Also used : OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 17 with OpenAPIDefinition

use of org.mockserver.model.OpenAPIDefinition in project mockserver by mock-server.

the class HttpRequestsPropertiesMatcherTest method shouldNotMatchRequestWithBodyMismatchWithContentTypeInOpenAPIWithoutOperationID.

@Test
public void shouldNotMatchRequestWithBodyMismatchWithContentTypeInOpenAPIWithoutOperationID() {
    // given
    HttpRequestsPropertiesMatcher httpRequestsPropertiesMatcher = new HttpRequestsPropertiesMatcher(configuration, mockServerLogger);
    httpRequestsPropertiesMatcher.update(new Expectation(new OpenAPIDefinition().withSpecUrlOrPayload("org/mockserver/openapi/openapi_petstore_example.json")));
    HttpRequest httpRequest = request().withMethod("POST").withPath("/v1/pets").withHeader("content-type", "application/json").withBody(json("{" + NEW_LINE + "    \"id\": \"invalid_id_format\", " + NEW_LINE + "    \"name\": \"scruffles\", " + NEW_LINE + "    \"tag\": \"dog\"" + NEW_LINE + "}"));
    MatchDifference context = new MatchDifference(configuration.detailedMatchFailures(), httpRequest);
    // when
    boolean matches = httpRequestsPropertiesMatcher.matches(context, httpRequest);
    // then
    assertThat(matches, is(false));
    String methodError = "  string or regex match failed expected:" + NEW_LINE + NEW_LINE + "    GET" + NEW_LINE + NEW_LINE + "   found:" + NEW_LINE + NEW_LINE + "    POST" + NEW_LINE;
    assertThat(context.getDifferences(METHOD), containsInAnyOrder(methodError, methodError, methodError));
    assertThat(context.getDifferences(PATH), nullValue());
    assertThat(context.getDifferences(QUERY_PARAMETERS), nullValue());
    assertThat(context.getDifferences(COOKIES), nullValue());
    assertThat(context.getDifferences(HEADERS), nullValue());
    String bodyError = "  json schema match failed expected:" + NEW_LINE + NEW_LINE + "    {" + NEW_LINE + "      \"properties\" : {" + NEW_LINE + "        \"id\" : {" + NEW_LINE + "          \"format\" : \"int64\"," + NEW_LINE + "          \"type\" : \"integer\"" + NEW_LINE + "        }," + NEW_LINE + "        \"name\" : {" + NEW_LINE + "          \"type\" : \"string\"" + NEW_LINE + "        }," + NEW_LINE + "        \"tag\" : {" + NEW_LINE + "          \"type\" : \"string\"" + NEW_LINE + "        }" + NEW_LINE + "      }," + NEW_LINE + "      \"required\" : [ \"id\", \"name\" ]," + NEW_LINE + "      \"type\" : \"object\"" + NEW_LINE + "    }" + NEW_LINE + NEW_LINE + "   found:" + NEW_LINE + NEW_LINE + "    {" + NEW_LINE + "        \"id\": \"invalid_id_format\", " + NEW_LINE + "        \"name\": \"scruffles\", " + NEW_LINE + "        \"tag\": \"dog\"" + NEW_LINE + "    }" + NEW_LINE + NEW_LINE + "   failed because:" + NEW_LINE + NEW_LINE + "    1 error:" + NEW_LINE + "     - $.id: string found, integer expected" + NEW_LINE;
    assertThat(context.getDifferences(BODY).get(0), equalTo(bodyError));
    assertThat(context.getDifferences(BODY), containsInAnyOrder(bodyError, bodyError));
    assertThat(context.getDifferences(SSL_MATCHES), nullValue());
    assertThat(context.getDifferences(KEEP_ALIVE), nullValue());
    assertThat(context.getDifferences(OPERATION), nullValue());
    assertThat(context.getDifferences(OPENAPI), nullValue());
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 18 with OpenAPIDefinition

use of org.mockserver.model.OpenAPIDefinition in project mockserver by mock-server.

the class HttpRequestsPropertiesMatcherTest method shouldMatchByOptionalNotSpecifiedHeader.

@Test
public void shouldMatchByOptionalNotSpecifiedHeader() {
    // given
    HttpRequestsPropertiesMatcher httpRequestsPropertiesMatcher = new HttpRequestsPropertiesMatcher(configuration, mockServerLogger);
    // when
    httpRequestsPropertiesMatcher.update(new Expectation(new OpenAPIDefinition().withSpecUrlOrPayload("---" + NEW_LINE + "openapi: 3.0.0" + NEW_LINE + "paths:" + NEW_LINE + "  \"/somePath\":" + NEW_LINE + "    get:" + NEW_LINE + "      operationId: someOperation" + NEW_LINE + "      parameters:" + NEW_LINE + "        - in: header" + NEW_LINE + "          name: someParam" + NEW_LINE + "          schema:" + NEW_LINE + "            type: integer" + NEW_LINE + "            minimum: 1")));
    // then
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withHeader("someParam", "1")));
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withHeader("someOtherParam", "1")));
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withHeader("someOtherParam", "0")));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withHeader("someParam", "0")));
}
Also used : OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 19 with OpenAPIDefinition

use of org.mockserver.model.OpenAPIDefinition in project mockserver by mock-server.

the class HttpRequestsPropertiesMatcherTest method shouldMatchByCookieWithOperationId.

@Test
public void shouldMatchByCookieWithOperationId() {
    // given
    HttpRequestsPropertiesMatcher httpRequestsPropertiesMatcher = new HttpRequestsPropertiesMatcher(configuration, mockServerLogger);
    // when
    httpRequestsPropertiesMatcher.update(new Expectation(new OpenAPIDefinition().withSpecUrlOrPayload("---" + NEW_LINE + "openapi: 3.0.0" + NEW_LINE + "paths:" + NEW_LINE + "  \"/somePath\":" + NEW_LINE + "    get:" + NEW_LINE + "      operationId: someOperation" + NEW_LINE + "      parameters:" + NEW_LINE + "        - in: cookie" + NEW_LINE + "          name: someParam" + NEW_LINE + "          required: true" + NEW_LINE + "          schema:" + NEW_LINE + "            type: integer" + NEW_LINE + "            minimum: 1").withOperationId("someOperation")));
    // then
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withCookie("someParam", "1")));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withCookie("someParam", "0")));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withCookie("someOtherParam", "1")));
}
Also used : OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 20 with OpenAPIDefinition

use of org.mockserver.model.OpenAPIDefinition in project mockserver by mock-server.

the class HttpRequestsPropertiesMatcherTest method shouldNotMatchRequestWithHeaderAndQueryParameterMismatchInOpenAPI.

@Test
public void shouldNotMatchRequestWithHeaderAndQueryParameterMismatchInOpenAPI() {
    // given
    HttpRequestsPropertiesMatcher httpRequestsPropertiesMatcher = new HttpRequestsPropertiesMatcher(configuration, mockServerLogger);
    httpRequestsPropertiesMatcher.update(new Expectation(new OpenAPIDefinition().withSpecUrlOrPayload("org/mockserver/openapi/openapi_petstore_example.json").withOperationId("somePath")));
    HttpRequest httpRequest = request().withMethod("GET").withPath("/v1/some/path").withQueryStringParameter("limit", "not_a_number");
    MatchDifference context = new MatchDifference(configuration.detailedMatchFailures(), httpRequest);
    // when
    boolean matches = httpRequestsPropertiesMatcher.matches(context, httpRequest);
    // then
    assertThat(matches, is(false));
    assertThat(context.getDifferences(METHOD), nullValue());
    assertThat(context.getDifferences(PATH), nullValue());
    assertThat(context.getDifferences(QUERY_PARAMETERS), nullValue());
    assertThat(context.getDifferences(COOKIES), nullValue());
    assertThat(context.getDifferences(HEADERS), containsInAnyOrder("  multimap match failed expected:" + NEW_LINE + "" + NEW_LINE + "    {" + NEW_LINE + "      \"keyMatchStyle\" : \"MATCHING_KEY\"," + NEW_LINE + "      \"X-Request-ID\" : {" + NEW_LINE + "        \"parameterStyle\" : \"SIMPLE\"," + NEW_LINE + "        \"values\" : [ {" + NEW_LINE + "          \"schema\" : {" + NEW_LINE + "            \"format\" : \"uuid\"," + NEW_LINE + "            \"type\" : \"string\"" + NEW_LINE + "          }" + NEW_LINE + "        } ]" + NEW_LINE + "      }" + NEW_LINE + "    }" + NEW_LINE + "" + NEW_LINE + "   found:" + NEW_LINE + "" + NEW_LINE + "    none" + NEW_LINE + "" + NEW_LINE + "   failed because:" + NEW_LINE + "" + NEW_LINE + "    none is not a subset" + NEW_LINE));
    assertThat(context.getDifferences(BODY), nullValue());
    assertThat(context.getDifferences(SSL_MATCHES), nullValue());
    assertThat(context.getDifferences(KEEP_ALIVE), nullValue());
    assertThat(context.getDifferences(OPERATION), nullValue());
    assertThat(context.getDifferences(OPENAPI), nullValue());
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Aggregations

OpenAPIDefinition (org.mockserver.model.OpenAPIDefinition)137 Test (org.junit.Test)134 Expectation (org.mockserver.mock.Expectation)127 HttpRequest (org.mockserver.model.HttpRequest)25 StringContains.containsString (org.hamcrest.core.StringContains.containsString)3 LogEntry (org.mockserver.log.model.LogEntry)3 ArrayList (java.util.ArrayList)2 DashboardLogEntryDTO (org.mockserver.dashboard.model.DashboardLogEntryDTO)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HOST (com.google.common.net.HttpHeaders.HOST)1 io.netty.channel (io.netty.channel)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 io.netty.handler.codec.http.websocketx (io.netty.handler.codec.http.websocketx)1