use of org.springframework.cloud.contract.spec.internal.Request in project spring-cloud-contract by spring-cloud.
the class Contract method request.
/**
* The HTTP request part of the contract.
* @param consumer function to manipulate the request
*/
public void request(Consumer<Request> consumer) {
this.request = new Request();
consumer.accept(this.request);
}
use of org.springframework.cloud.contract.spec.internal.Request in project spring-cloud-contract by spring-cloud.
the class JaxRsRequestHeadersWhen method appendHeaders.
private void appendHeaders(Request request) {
Iterator<Header> iterator = request.getHeaders().getEntries().stream().filter(header -> !headerToIgnore(header)).iterator();
while (iterator.hasNext()) {
Header header = iterator.next();
String text = ".header(\"" + header.getName() + "\", " + headerValue(header) + ")";
if (iterator.hasNext()) {
this.blockBuilder.addLine(text);
} else {
this.blockBuilder.addIndented(text);
}
}
}
use of org.springframework.cloud.contract.spec.internal.Request 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.spec.internal.Request 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.spec.internal.Request 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);
}
});
}
}
Aggregations