use of org.springframework.cloud.contract.verifier.util.ContentType in project spring-cloud-contract by spring-cloud.
the class JaxRsRequestMethodWhen method appendMethodAndBody.
void appendMethodAndBody(SingleContractMetadata metadata) {
Request request = metadata.getContract().getRequest();
ContentType type = metadata.getInputTestContentType();
String method = request.getMethod().getServerValue().toString().toLowerCase();
if (request.getBody() != null) {
String contentType = StringUtils.hasText(metadata.getDefinedInputTestContentType()) ? metadata.getDefinedInputTestContentType() : type.getMimeType();
Object body = request.getBody().getServerValue();
String value;
if (body instanceof ExecutionProperty) {
value = body.toString();
} else if (body instanceof FromFileProperty) {
FromFileProperty fileProperty = (FromFileProperty) body;
value = fileProperty.isByte() ? this.bodyReader.readBytesFromFileString(metadata, fileProperty, CommunicationType.REQUEST) : this.bodyReader.readStringFromFileString(metadata, fileProperty, CommunicationType.REQUEST);
} else {
value = "\"" + requestBodyAsString(metadata) + "\"";
}
this.blockBuilder.addIndented(".build(\"" + method.toUpperCase() + "\", entity(" + value + ", \"" + contentType + "\"))");
} else {
this.blockBuilder.addIndented(".build(\"" + method.toUpperCase() + "\")");
}
}
use of org.springframework.cloud.contract.verifier.util.ContentType in project spring-cloud-contract by spring-cloud.
the class ContractsToYaml method request.
protected void request(Contract contract, YamlContract yamlContract) {
Request request = contract.getRequest();
if (request != null) {
ContentType requestContentType = evaluateClientSideContentType(request.getHeaders(), request.getBody());
yamlContract.request = new YamlContract.Request();
mapRequestMethod(yamlContract.request, request);
mapRequestUrl(yamlContract.request, request);
mapRequestUrlPath(yamlContract.request, request);
mapRequestMatchers(yamlContract.request);
Url requestUrl = Optional.ofNullable(request.getUrl()).orElse(request.getUrlPath());
if (requestUrl.getQueryParameters() != null) {
mapRequestQueryParameters(yamlContract.request, requestUrl);
mapRequestMatchersQueryParameters(yamlContract.request, requestUrl);
}
mapRequestHeaders(yamlContract.request, request);
mapRequestCookies(yamlContract.request, request);
mapRequestBody(yamlContract.request, request);
mapRequestMultipart(yamlContract.request, request);
mapRequestMatchersBody(yamlContract.request, request);
mapRequestMatchersUrl(yamlContract.request, request);
mapRequestMatchersMultipart(yamlContract.request, request);
// TODO: Cookie matchers - including absent
if (XML != requestContentType) {
setInputBodyMatchers(request.getBody(), yamlContract.request.matchers.body);
}
setInputHeadersMatchers(request.getHeaders(), yamlContract.request.matchers.headers);
}
}
use of org.springframework.cloud.contract.verifier.util.ContentType in project spring-cloud-contract by spring-cloud.
the class ContractsToYaml method mapRequestMultipart.
private void mapRequestMultipart(YamlContract.Request yamlContractRequest, Request request) {
Multipart multipart = request.getMultipart();
if (multipart != null) {
yamlContractRequest.multipart = new YamlContract.Multipart();
Map<String, Object> map = (Map<String, Object>) MapConverter.getTestSideValues(multipart);
map.forEach((key, value) -> {
if (value instanceof NamedProperty) {
Object fileName = Optional.ofNullable(((NamedProperty) value).getName()).map(DslProperty::getServerValue).orElse(null);
Object contentType = Optional.ofNullable(((NamedProperty) value).getContentType()).map(DslProperty::getServerValue).orElse(null);
Object fileContent = Optional.ofNullable(((NamedProperty) value).getValue()).map(DslProperty::getServerValue).orElse(null);
YamlContract.Named named = new YamlContract.Named();
named.paramName = key;
named.fileName = fileName instanceof String ? Optional.ofNullable(((NamedProperty) value).getName()).map(DslProperty::getServerValue).map(Object::toString).orElse(null) : null;
named.fileContent = (String) Optional.ofNullable(fileContent).filter(f -> f instanceof String).orElse(null);
named.fileContentAsBytes = fileContent instanceof FromFileProperty ? new String(((FromFileProperty) fileContent).asBytes()) : null;
named.fileContentFromFileAsBytes = resolveFileNameAsBytes(fileContent);
named.contentType = (String) Optional.ofNullable(contentType).filter(f -> f instanceof String).orElse(null);
named.fileNameCommand = fileName instanceof ExecutionProperty ? fileName.toString() : null;
named.fileContentCommand = fileContent instanceof ExecutionProperty ? fileContent.toString() : null;
named.contentTypeCommand = contentType instanceof ExecutionProperty ? contentType.toString() : null;
yamlContractRequest.multipart.named.add(named);
} else {
yamlContractRequest.multipart.params.put(key, value != null ? value.toString() : null);
}
});
}
}
use of org.springframework.cloud.contract.verifier.util.ContentType in project spring-cloud-contract by spring-cloud.
the class ContractsToYaml method response.
protected void response(YamlContract yamlContract, Contract contract) {
if (contract.getResponse() != null) {
Response contractResponse = contract.getResponse();
ContentType contentType = evaluateClientSideContentType(contractResponse.getHeaders(), contractResponse.getBody());
YamlContract.Response response = new YamlContract.Response();
yamlContract.response = response;
mapResponseAsync(contractResponse, response);
mapResponseFixedDelayMilliseconds(contractResponse, response);
mapResponseStatus(contractResponse, response);
mapResponseHeaders(contractResponse, response);
mapResponseCookies(contractResponse, response);
mapResponseBody(contractResponse, response);
mapResponseBodyMatchers(contractResponse, response);
if (XML != contentType) {
setOutputBodyMatchers(contractResponse.getBody(), yamlContract.response.matchers.body);
}
setOutputHeadersMatchers(contractResponse.getHeaders(), yamlContract.response.matchers.headers);
}
}
use of org.springframework.cloud.contract.verifier.util.ContentType in project spring-cloud-contract by spring-cloud.
the class YamlToContracts method mapResponseBodyMatchers.
private void mapResponseBodyMatchers(YamlContract.Response yamlContractResponse, Response dslContractResponse) {
dslContractResponse.bodyMatchers(bodyMatchers -> Optional.ofNullable(yamlContractResponse.matchers).map(yamlContractResponseTestMatchers -> yamlContractResponseTestMatchers.body).ifPresent(yamlContractBodyTestMatchers -> yamlContractBodyTestMatchers.forEach(yamlContractBodyTestMatcher -> {
ContentType contentType = evaluateClientSideContentType(yamlHeadersToContractHeaders(yamlContractResponse.headers), yamlContractResponse.body);
MatchingTypeValue value;
switch(yamlContractBodyTestMatcher.type) {
case by_date:
value = bodyMatchers.byDate();
break;
case by_time:
value = bodyMatchers.byTime();
break;
case by_timestamp:
value = bodyMatchers.byTimestamp();
break;
case by_regex:
String regex = yamlContractBodyTestMatcher.value;
if (yamlContractBodyTestMatcher.predefined != null) {
regex = predefinedToPattern(yamlContractBodyTestMatcher.predefined).pattern();
}
value = bodyMatchers.byRegex(regex);
break;
case by_equality:
value = bodyMatchers.byEquality();
break;
case by_type:
value = bodyMatchers.byType(v -> {
if (yamlContractBodyTestMatcher.minOccurrence != null) {
v.minOccurrence(yamlContractBodyTestMatcher.minOccurrence);
}
if (yamlContractBodyTestMatcher.maxOccurrence != null) {
v.maxOccurrence(yamlContractBodyTestMatcher.maxOccurrence);
}
});
break;
case by_command:
value = bodyMatchers.byCommand(yamlContractBodyTestMatcher.value);
break;
case by_null:
value = bodyMatchers.byNull();
break;
default:
throw new UnsupportedOperationException("The type [" + yamlContractBodyTestMatcher.type + "] is unsupported. " + "Hint: If you're using <predefined> remember to pass < type:by_regex > ");
}
if (yamlContractBodyTestMatcher.path != null) {
if (XML == contentType) {
bodyMatchers.xPath(yamlContractBodyTestMatcher.path, value);
} else {
bodyMatchers.jsonPath(yamlContractBodyTestMatcher.path, value);
}
}
})));
}
Aggregations