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\" }"))));
}
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());
}
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")));
}
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")));
}
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());
}
Aggregations