简体   繁体   中英

Spring cloud contract with XML. Trying to specify mock depend on request

In the most cases I can specify mock via element content-type and the value inside of it. I'm connecting it with xpath and everything is going well. But now i got a case where inside wsdl every xsd got the same content-type. I have no idea how I can make a condition to specify concrete mock. I guess the only thing which is different in every xsd inside this url is root element. Have u got any solution? Do u think it is possible to specify mock depend on request root element and how ? This is example of my contract

Contract.make {
        request {
            priority(1)
            method 'POST'
            url '/ws/xxxx/xxx'
            body(new MatchingStrategy(anyNonEmptyString(), MatchingStrategy.Type.MATCHING))
            bodyMatchers {
                xPath("//*[local-name()='Chnl']/text()", new MatchingTypeValue(MatchingType.EQUALITY, "xxx"))
            }
            headers {
                header(contentType(), regex('application/.*xml.*xxxxxxx.*'))
            }
        }
        response {
            status 200
            body(fileAsBytes("xxxx.xml"))
            headers {
                contentType(applicationXml())
            }
        }
    }

我通过在 bodyMatcher 中为主 xml 元素添加新的 xpath 条件解决了我的问题。

xPath("//*[local-name()='Body']", new MatchingTypeValue(MatchingType.REGEX, ".*nameOfRootXmlElement.*"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM