use of org.mockserver.model.OpenAPIDefinition in project mockserver by mock-server.
the class HttpRequestsPropertiesMatcherTest method shouldHandleInvalidOpenAPIYamlSpec.
@Test
public void shouldHandleInvalidOpenAPIYamlSpec() {
// given
HttpRequestsPropertiesMatcher httpRequestsPropertiesMatcher = new HttpRequestsPropertiesMatcher(configuration, mockServerLogger);
try {
// when
httpRequestsPropertiesMatcher.update(new OpenAPIDefinition().withSpecUrlOrPayload(FileReader.readFileFromClassPathOrPath("org/mockserver/mock/openapi_petstore_example.yaml").substring(0, 100)).withOperationId("listPets"));
// then
fail("expected exception");
} catch (IllegalArgumentException iae) {
assertThat(iae.getMessage(), is("Unable to load API spec, while scanning a simple key" + NEW_LINE + " in 'reader', line 8, column 1:" + NEW_LINE + " servers" + NEW_LINE + " ^" + NEW_LINE + "could not find expected ':'" + NEW_LINE + " in 'reader', line 8, column 8:" + NEW_LINE + " servers" + NEW_LINE + " ^"));
}
}
use of org.mockserver.model.OpenAPIDefinition in project mockserver by mock-server.
the class ExpectationToJavaSerializer method serialize.
@Override
public String serialize(int numberOfSpacesToIndent, Expectation expectation) {
StringBuffer output = new StringBuffer();
if (expectation != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append("new MockServerClient(\"localhost\", 1080)");
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(".when(");
RequestDefinition requestDefinition = expectation.getHttpRequest();
if (requestDefinition instanceof HttpRequest) {
output.append(new HttpRequestToJavaSerializer().serialize(numberOfSpacesToIndent + 1, (HttpRequest) requestDefinition));
} else if (requestDefinition instanceof OpenAPIDefinition) {
output.append(new OpenAPIMatcherToJavaSerializer().serialize(numberOfSpacesToIndent + 1, (OpenAPIDefinition) requestDefinition));
}
output.append(",");
if (expectation.getTimes() != null) {
output.append(new TimesToJavaSerializer().serialize(numberOfSpacesToIndent + 1, expectation.getTimes()));
} else {
appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output).append("null");
}
output.append(",");
if (expectation.getTimeToLive() != null) {
output.append(new TimeToLiveToJavaSerializer().serialize(numberOfSpacesToIndent + 1, expectation.getTimeToLive()));
} else {
appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output).append("null");
}
output.append(",");
appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output).append(expectation.getPriority());
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(")");
if (expectation.getHttpResponse() != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(".respond(");
output.append(new HttpResponseToJavaSerializer().serialize(numberOfSpacesToIndent + 1, expectation.getHttpResponse()));
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(")");
}
if (expectation.getHttpResponseTemplate() != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(".respond(");
output.append(new HttpTemplateToJavaSerializer().serialize(numberOfSpacesToIndent + 1, expectation.getHttpResponseTemplate()));
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(")");
}
if (expectation.getHttpResponseClassCallback() != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(".respond(");
output.append(new HttpClassCallbackToJavaSerializer().serialize(numberOfSpacesToIndent + 1, expectation.getHttpResponseClassCallback()));
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(")");
}
if (expectation.getHttpResponseObjectCallback() != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append("/*NOT POSSIBLE TO GENERATE CODE FOR OBJECT CALLBACK*/");
}
if (expectation.getHttpForward() != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(".forward(");
output.append(new HttpForwardToJavaSerializer().serialize(numberOfSpacesToIndent + 1, expectation.getHttpForward()));
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(")");
}
if (expectation.getHttpOverrideForwardedRequest() != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(".forward(");
output.append(new HttpOverrideForwardedRequestToJavaSerializer().serialize(numberOfSpacesToIndent + 1, expectation.getHttpOverrideForwardedRequest()));
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(")");
}
if (expectation.getHttpForwardTemplate() != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(".forward(");
output.append(new HttpTemplateToJavaSerializer().serialize(numberOfSpacesToIndent + 1, expectation.getHttpForwardTemplate()));
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(")");
}
if (expectation.getHttpForwardClassCallback() != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(".forward(");
output.append(new HttpClassCallbackToJavaSerializer().serialize(numberOfSpacesToIndent + 1, expectation.getHttpForwardClassCallback()));
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(")");
}
if (expectation.getHttpForwardObjectCallback() != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append("/*NOT POSSIBLE TO GENERATE CODE FOR OBJECT CALLBACK*/");
}
if (expectation.getHttpError() != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(".error(");
output.append(new HttpErrorToJavaSerializer().serialize(numberOfSpacesToIndent + 1, expectation.getHttpError()));
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append(")");
}
output.append(";");
}
return output.toString();
}
use of org.mockserver.model.OpenAPIDefinition in project mockserver by mock-server.
the class HttpRequestsPropertiesMatcherTest method shouldMatchByPathWithLabelPathParameter.
@Test
public void shouldMatchByPathWithLabelPathParameter() {
// 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: false" + 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")));
}
use of org.mockserver.model.OpenAPIDefinition in project mockserver by mock-server.
the class HttpRequestsPropertiesMatcherTest method shouldMatchByFormBody.
// - FORM BODY (via JsonSchema)
@Test
public void shouldMatchByFormBody() {
// 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/x-www-form-urlencoded:" + 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 + " text/plain:" + NEW_LINE + " schema:" + NEW_LINE + " type: string" + NEW_LINE)));
// then
assertTrue(httpRequestsPropertiesMatcher.matches(request().withHeader("content-type", "application/x-www-form-urlencoded").withBody("id=1&name=abc")));
assertTrue(httpRequestsPropertiesMatcher.matches(request().withHeader("content-type", "application/x-www-form-urlencoded; charset=utf-8").withBody("id=1&name=abc")));
assertFalse(httpRequestsPropertiesMatcher.matches(request().withHeader("content-type", "application/x-www-form-urlencoded").withBody("id=1&name=1")));
assertFalse(httpRequestsPropertiesMatcher.matches(request().withHeader("content-type", "application/x-www-form-urlencoded").withBody("id=abc&name=1")));
}
use of org.mockserver.model.OpenAPIDefinition in project mockserver by mock-server.
the class HttpRequestsPropertiesMatcherTest method shouldMatchByHeader.
// HEADERS
@Test
public void shouldMatchByHeader() {
// 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 + " required: true" + NEW_LINE + " schema:" + NEW_LINE + " type: integer" + NEW_LINE + " minimum: 1")));
// then
assertTrue(httpRequestsPropertiesMatcher.matches(request().withHeader("someParam", "1")));
assertFalse(httpRequestsPropertiesMatcher.matches(request().withHeader("someParam", "0")));
assertFalse(httpRequestsPropertiesMatcher.matches(request().withHeader("someOtherParam", "1")));
}
Aggregations