Search in sources :

Example 1 with Header

use of org.springframework.cloud.contract.spec.internal.Header 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);
        }
    }
}
Also used : ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty) Iterator(java.util.Iterator) MatchingStrategy(org.springframework.cloud.contract.spec.internal.MatchingStrategy) Set(java.util.Set) SingleContractMetadata(org.springframework.cloud.contract.verifier.file.SingleContractMetadata) Request(org.springframework.cloud.contract.spec.internal.Request) Header(org.springframework.cloud.contract.spec.internal.Header) MapConverter(org.springframework.cloud.contract.verifier.util.MapConverter) Header(org.springframework.cloud.contract.spec.internal.Header)

Example 2 with Header

use of org.springframework.cloud.contract.spec.internal.Header in project spring-cloud-contract by spring-cloud.

the class CustomModeHeadersThen method apply.

@Override
public MethodVisitor<Then> apply(SingleContractMetadata metadata) {
    Response response = metadata.getContract().getResponse();
    Headers headers = response.getHeaders();
    Iterator<Header> iterator = headers.getEntries().iterator();
    while (iterator.hasNext()) {
        Header header = iterator.next();
        String text = processHeaderElement(header.getName(), header.getServerValue() instanceof NotToEscapePattern ? header.getServerValue() : MapConverter.getTestSideValues(header.getServerValue()));
        if (iterator.hasNext()) {
            this.blockBuilder.addLineWithEnding(text);
        } else {
            this.blockBuilder.addIndented(text);
        }
    }
    this.blockBuilder.addEndingIfNotPresent();
    return this;
}
Also used : Response(org.springframework.cloud.contract.spec.internal.Response) Header(org.springframework.cloud.contract.spec.internal.Header) Headers(org.springframework.cloud.contract.spec.internal.Headers) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern)

Example 3 with Header

use of org.springframework.cloud.contract.spec.internal.Header in project spring-cloud-contract by spring-cloud.

the class JaxRsResponseHeadersThen method validateResponseHeadersBlock.

private void validateResponseHeadersBlock(SingleContractMetadata metadata) {
    Response response = metadata.getContract().getResponse();
    Headers headers = response.getHeaders();
    Iterator<Header> iterator = headers.getEntries().iterator();
    while (iterator.hasNext()) {
        Header header = iterator.next();
        String text = processHeaderElement(header.getName(), header.getServerValue() instanceof NotToEscapePattern ? header.getServerValue() : MapConverter.getTestSideValues(header.getServerValue()));
        if (iterator.hasNext()) {
            this.blockBuilder.addLineWithEnding(text);
        } else {
            this.blockBuilder.addIndented(text);
        }
    }
}
Also used : Response(org.springframework.cloud.contract.spec.internal.Response) Header(org.springframework.cloud.contract.spec.internal.Header) Headers(org.springframework.cloud.contract.spec.internal.Headers) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern)

Example 4 with Header

use of org.springframework.cloud.contract.spec.internal.Header in project spring-cloud-contract by spring-cloud.

the class RestAssuredHeadersThen method apply.

@Override
public MethodVisitor<Then> apply(SingleContractMetadata metadata) {
    Response response = metadata.getContract().getResponse();
    Headers headers = response.getHeaders();
    Iterator<Header> iterator = headers.getEntries().iterator();
    while (iterator.hasNext()) {
        Header header = iterator.next();
        String text = processHeaderElement(header.getName(), header.getServerValue() instanceof NotToEscapePattern ? header.getServerValue() : MapConverter.getTestSideValues(header.getServerValue()));
        if (iterator.hasNext()) {
            this.blockBuilder.addLineWithEnding(text);
        } else {
            this.blockBuilder.addIndented(text);
        }
    }
    this.blockBuilder.addEndingIfNotPresent();
    return this;
}
Also used : Response(org.springframework.cloud.contract.spec.internal.Response) Header(org.springframework.cloud.contract.spec.internal.Header) Headers(org.springframework.cloud.contract.spec.internal.Headers) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern)

Example 5 with Header

use of org.springframework.cloud.contract.spec.internal.Header in project spring-cloud-contract by spring-cloud.

the class StubRunnerJmsMessageSelector method headersMatch.

private List<String> headersMatch(Message message, Contract groovyDsl) {
    List<String> unmatchedHeaders = new ArrayList<>();
    Map<String, Object> headers = StubRunnerJmsAccessor.getHeaders(message);
    for (Header it : groovyDsl.getInput().getMessageHeaders().getEntries()) {
        String name = it.getName();
        Object value = it.getClientValue();
        Object valueInHeader = headers.get(name);
        boolean matches;
        if (value instanceof RegexProperty) {
            Pattern pattern = ((RegexProperty) value).getPattern();
            matches = pattern.matcher(valueInHeader.toString()).matches();
        } else {
            matches = valueInHeader != null && valueInHeader.toString().equals(value.toString());
        }
        if (!matches) {
            unmatchedHeaders.add("Header with name [" + name + "] was supposed to " + unmatchedText(value) + " but the value is [" + (valueInHeader != null ? valueInHeader.toString() : "null") + "]");
        }
    }
    return unmatchedHeaders;
}
Also used : Pattern(java.util.regex.Pattern) Header(org.springframework.cloud.contract.spec.internal.Header) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) ArrayList(java.util.ArrayList)

Aggregations

Header (org.springframework.cloud.contract.spec.internal.Header)13 Pattern (java.util.regex.Pattern)7 ArrayList (java.util.ArrayList)6 RegexProperty (org.springframework.cloud.contract.spec.internal.RegexProperty)5 Headers (org.springframework.cloud.contract.spec.internal.Headers)4 Response (org.springframework.cloud.contract.spec.internal.Response)4 NotToEscapePattern (org.springframework.cloud.contract.spec.internal.NotToEscapePattern)3 Set (java.util.Set)2 Contract (org.springframework.cloud.contract.spec.Contract)2 ExecutionProperty (org.springframework.cloud.contract.spec.internal.ExecutionProperty)2 Request (org.springframework.cloud.contract.spec.internal.Request)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)1 MessageBuilder (io.eventuate.tram.messaging.producer.MessageBuilder)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1