Search in sources :

Example 26 with OpenAPIDefinition

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

the class HttpRequestsPropertiesMatcherTest method shouldMatchByPlainTextBody.

// - PLAIN TEXT BODY (via JsonSchema)
@Test
public void shouldMatchByPlainTextBody() {
    // 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: true" + 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().withHeader("content-type", "plain/text").withBody(exact("{ \"id\": 1, \"name\": \"abc\" }"))));
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withHeader("content-type", "plain/text; charset=utf-8").withBody(exact("{ \"id\": 1, \"name\": \"abc\" }"))));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withHeader("content-type", "plain/text").withBody(exact("{ \"id\": 1, \"name\": 1 }"))));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().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 27 with OpenAPIDefinition

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

the class HttpRequestsPropertiesMatcherTest method shouldMatchBySecuritySchemeInCookieWithBasic.

// COOKIE (SECURITY SCHEMES)
@Test
public void shouldMatchBySecuritySchemeInCookieWithBasic() {
    // 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 + "      security:" + NEW_LINE + "        - BasicAuth: []" + NEW_LINE + "components:" + NEW_LINE + "  securitySchemes:" + NEW_LINE + "    BasicAuth:" + NEW_LINE + "      type: http" + NEW_LINE + "      in: cookie" + NEW_LINE + "      scheme: basic" + NEW_LINE)));
    // then
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withCookie("Authorization", "basic " + UUID.randomUUID())));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withCookie("Authorization", "basic")));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withCookie("Authorization", "wrong_scheme " + UUID.randomUUID())));
    assertFalse(httpRequestsPropertiesMatcher.matches(request()));
}
Also used : OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 28 with OpenAPIDefinition

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

the class HttpRequestsPropertiesMatcherTest method shouldMatchBySecuritySchemeInHeaderWithMultiAuthorizationHeaderSchemes.

@Test
public void shouldMatchBySecuritySchemeInHeaderWithMultiAuthorizationHeaderSchemes() {
    // 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 + "      security:" + NEW_LINE + "        - BasicAuth: []" + NEW_LINE + "        - BearerAuth: []" + NEW_LINE + "components:" + NEW_LINE + "  securitySchemes:" + NEW_LINE + "    BasicAuth:" + NEW_LINE + "      type: http" + NEW_LINE + "      scheme: basic" + NEW_LINE + "    BearerAuth:" + NEW_LINE + "      type: http" + NEW_LINE + "      scheme: bearer" + NEW_LINE)));
    // then
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withHeader("Authorization", "basic " + UUIDService.getUUID())));
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withHeader("Authorization", "bearer " + UUIDService.getUUID())));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withHeader("Authorization", "bearer")));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withHeader("Authorization", "wrong_scheme " + UUIDService.getUUID())));
    assertFalse(httpRequestsPropertiesMatcher.matches(request()));
}
Also used : OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 29 with OpenAPIDefinition

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

the class HttpRequestsPropertiesMatcherTest method shouldMatchByPathWithLabelExplodedPathParameter.

@Test
public void shouldMatchByPathWithLabelExplodedPathParameter() {
    // 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/{.someParam}\":" + NEW_LINE + "    get:" + NEW_LINE + "      operationId: someOperation" + NEW_LINE + "      parameters:" + NEW_LINE + "        - in: path" + NEW_LINE + "          name: someParam" + NEW_LINE + "          required: true" + NEW_LINE + "          style: label" + NEW_LINE + "          explode: true" + NEW_LINE + "          schema:" + NEW_LINE + "            type: integer" + NEW_LINE + "            minimum: 1")));
    // then
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath/.1.2.3")));
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath/.1")));
    assertTrue(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath/.1.2.3.4")));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath/.0.1.2")));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath/.a.1.2")));
    assertFalse(httpRequestsPropertiesMatcher.matches(request().withPath("/somePath")));
}
Also used : OpenAPIDefinition(org.mockserver.model.OpenAPIDefinition) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 30 with OpenAPIDefinition

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

the class HttpRequestsPropertiesMatcherTest method shouldMatchAnyOperationInOpenAPI.

@Test
public void shouldMatchAnyOperationInOpenAPI() {
    // given
    HttpRequestsPropertiesMatcher httpRequestsPropertiesMatcher = new HttpRequestsPropertiesMatcher(configuration, mockServerLogger);
    httpRequestsPropertiesMatcher.update(new Expectation(new OpenAPIDefinition().withSpecUrlOrPayload("org/mockserver/openapi/openapi_petstore_example.json").withOperationId("")));
    HttpRequest httpRequest = request().withMethod("GET").withPath("/v1/pets").withQueryStringParameter("limit", "10");
    MatchDifference context = new MatchDifference(configuration.detailedMatchFailures(), httpRequest);
    // when
    boolean matches = httpRequestsPropertiesMatcher.matches(context, httpRequest);
    // then
    thenMatchesEmptyFieldDifferences(context, matches, true);
}
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