Search in sources :

Example 1 with Headers

use of org.springframework.cloud.contract.spec.internal.Headers 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 2 with Headers

use of org.springframework.cloud.contract.spec.internal.Headers 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 3 with Headers

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

the class ContractsToYaml method setOutputHeadersMatchers.

protected void setOutputHeadersMatchers(Headers headers, List<YamlContract.TestHeaderMatcher> headerMatchers) {
    Optional.ofNullable(headers).map(Headers::asTestSideMap).ifPresent(testSideMap -> testSideMap.forEach((key, value) -> {
        if (value instanceof RegexProperty || value instanceof Pattern) {
            RegexProperty property = new RegexProperty(value);
            YamlContract.TestHeaderMatcher testHeaderMatcher = new YamlContract.TestHeaderMatcher();
            testHeaderMatcher.key = key;
            testHeaderMatcher.regex = property.pattern();
            testHeaderMatcher.regexType = regexType(property.clazz());
            headerMatchers.add(testHeaderMatcher);
        } else if (value instanceof ExecutionProperty) {
            YamlContract.TestHeaderMatcher testHeaderMatcher = new YamlContract.TestHeaderMatcher();
            testHeaderMatcher.key = key;
            testHeaderMatcher.command = ((ExecutionProperty) value).getExecutionCommand();
            headerMatchers.add(testHeaderMatcher);
        } else if (value instanceof NotToEscapePattern) {
            YamlContract.TestHeaderMatcher testHeaderMatcher = new YamlContract.TestHeaderMatcher();
            testHeaderMatcher.key = key;
            testHeaderMatcher.regex = (((NotToEscapePattern) value).getServerValue()).pattern();
            headerMatchers.add(testHeaderMatcher);
        }
    }));
}
Also used : JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) HashMap(java.util.HashMap) ContentUtils.evaluateClientSideContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.evaluateClientSideContentType) Request(org.springframework.cloud.contract.spec.internal.Request) XML(org.springframework.cloud.contract.verifier.util.ContentType.XML) OutputMessage(org.springframework.cloud.contract.spec.internal.OutputMessage) MatchingType(org.springframework.cloud.contract.spec.internal.MatchingType) FromFileProperty(org.springframework.cloud.contract.spec.internal.FromFileProperty) NamedProperty(org.springframework.cloud.contract.spec.internal.NamedProperty) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) Cookies(org.springframework.cloud.contract.spec.internal.Cookies) BodyMatchers(org.springframework.cloud.contract.spec.internal.BodyMatchers) Url(org.springframework.cloud.contract.spec.internal.Url) Input(org.springframework.cloud.contract.spec.internal.Input) MapConverter(org.springframework.cloud.contract.verifier.util.MapConverter) Multipart(org.springframework.cloud.contract.spec.internal.Multipart) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) List(java.util.List) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern) MatchingStrategy(org.springframework.cloud.contract.spec.internal.MatchingStrategy) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) QueryParameter(org.springframework.cloud.contract.spec.internal.QueryParameter) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Response(org.springframework.cloud.contract.spec.internal.Response) Headers(org.springframework.cloud.contract.spec.internal.Headers) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern) Pattern(java.util.regex.Pattern) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty)

Example 4 with Headers

use of org.springframework.cloud.contract.spec.internal.Headers 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 Headers

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

the class MessagingSCContractCreator method convertFrom.

Collection<Contract> convertFrom(MessagePact pact) {
    return pact.getMessages().stream().map(message -> Contract.make(contract -> {
        contract.label(message.getDescription());
        if (!message.getProviderStates().isEmpty()) {
            contract.input(i -> i.triggeredBy(this.getTriggeredBy(message)));
        }
        contract.outputMessage((outputMessage) -> {
            if (message.getContents().isPresent()) {
                outputMessage.body(BodyConverter.toSCCBody(message));
                Category bodyRules = message.getMatchingRules().rulesForCategory("body");
                if (bodyRules != null && !bodyRules.getMatchingRules().isEmpty()) {
                    outputMessage.bodyMatchers((responseBodyMatchers) -> outputMessageBodyMatchers(message, bodyRules, responseBodyMatchers));
                }
            }
            if (!message.getMetaData().isEmpty()) {
                outputMessage.headers((headers) -> outputMessageHeaders(message, headers));
            }
            String dest = findDestination(message);
            if (StringUtils.isNotBlank(dest)) {
                outputMessage.sentTo(dest);
            }
        });
    })).collect(Collectors.toList());
}
Also used : JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) TimestampMatcher(au.com.dius.pact.core.model.matchingrules.TimestampMatcher) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) Category(au.com.dius.pact.core.model.matchingrules.Category) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) MessagePact(au.com.dius.pact.core.model.messaging.MessagePact) StringUtils(org.apache.commons.lang3.StringUtils) Contract(org.springframework.cloud.contract.spec.Contract) Message(au.com.dius.pact.core.model.messaging.Message) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) Collection(java.util.Collection) TypeMatcher(au.com.dius.pact.core.model.matchingrules.TypeMatcher) TimeMatcher(au.com.dius.pact.core.model.matchingrules.TimeMatcher) ResponseBodyMatchers(org.springframework.cloud.contract.spec.internal.ResponseBodyMatchers) Collectors(java.util.stream.Collectors) NullMatcher(au.com.dius.pact.core.model.matchingrules.NullMatcher) DateMatcher(au.com.dius.pact.core.model.matchingrules.DateMatcher) RuleLogic(au.com.dius.pact.core.model.matchingrules.RuleLogic) List(java.util.List) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule) RegexPatterns(org.springframework.cloud.contract.spec.internal.RegexPatterns) Collections(java.util.Collections) Headers(org.springframework.cloud.contract.spec.internal.Headers) Category(au.com.dius.pact.core.model.matchingrules.Category)

Aggregations

Headers (org.springframework.cloud.contract.spec.internal.Headers)8 Response (org.springframework.cloud.contract.spec.internal.Response)6 Collection (java.util.Collection)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Contract (org.springframework.cloud.contract.spec.Contract)4 NotToEscapePattern (org.springframework.cloud.contract.spec.internal.NotToEscapePattern)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Pattern (java.util.regex.Pattern)3 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)3 ExecutionProperty (org.springframework.cloud.contract.spec.internal.ExecutionProperty)3 Header (org.springframework.cloud.contract.spec.internal.Header)3 Input (org.springframework.cloud.contract.spec.internal.Input)3 OutputMessage (org.springframework.cloud.contract.spec.internal.OutputMessage)3 Collections (java.util.Collections)2 Objects (java.util.Objects)2 BodyMatchers (org.springframework.cloud.contract.spec.internal.BodyMatchers)2 Cookies (org.springframework.cloud.contract.spec.internal.Cookies)2