codecamp

SpringCloud 对REST的XML支持

对于REST合同,我们还支持XML请求和响应主体。XML主体必须作为StringGStringbody元素内传递。还可以为请求和响应提供身体匹配器。代替jsonPath(…​)方法,应使用org.springframework.cloud.contract.spec.internal.BodyMatchers.xPath方法,并以所需的xPath作为第一个参数,并以适当的MatchingType作为第二个参数。支持byType()以外的所有主体匹配器。

这是带有XML响应主体的Groovy DSL合同的示例:

Contract.make {
	request {
		method GET()
		urlPath '/get'
		headers {
			contentType(applicationXml())
			}
		}
		response {
			status(OK())
			headers {
				contentType(applicationXml())
				}
				body """
<test>
<duck type='xtype'>123</duck>
<alpha>abc</alpha>
<list>
<elem>abc</elem>
<elem>def</elem>
<elem>ghi</elem>
</list>
<number>123</number>
<aBoolean>true</aBoolean>
<date>2017-01-01</date>
<dateTime>2017-01-01T01:23:45</dateTime>
<time>01:02:34</time>
<valueWithoutAMatcher>foo</valueWithoutAMatcher>
<key><complex>foo</complex></key>
</test>"""
        bodyMatchers {
	        xPath('/test/duck/text()', byRegex("[0-9]{3}"))
	        xPath('/test/duck/text()', byCommand('test($it)'))
	        xPath('/test/duck/xxx', byNull())
	        xPath('/test/duck/text()', byEquality())
	        xPath('/test/alpha/text()', byRegex(onlyAlphaUnicode()))
	        xPath('/test/alpha/text()', byEquality())
	        xPath('/test/number/text()', byRegex(number()))
	        xPath('/test/date/text()', byDate())
	        xPath('/test/dateTime/text()', byTimestamp())
	        xPath('/test/time/text()', byTime())
	        xPath('/test/*/complex/text()', byEquality())
	        xPath('/test/duck/@type', byEquality())
        }
    }
}

以下是带有XML请求和响应主体的YAML合同的示例:

include::{verifier_core_path}/src/test/resources/yml/contract_rest_xml.yml

这是自动生成的XML响应正文测试的示例:

@Test
public void validate_xmlMatches() throws Exception {
	// given:
	MockMvcRequestSpecification request = given()
				.header("Content-Type", "application/xml");

	// when:
	ResponseOptions response = given().spec(request).get("/get");

	// then:
	assertThat(response.statusCode()).isEqualTo(200);
	// and:
	DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
	Document parsedXml = documentBuilder.parse(new InputSource(
				new StringReader(response.getBody().asString())));
	// and:
	assertThat(valueFromXPath(parsedXml, "/test/list/elem/text()")).isEqualTo("abc");
	assertThat(valueFromXPath(parsedXml,"/test/list/elem[2]/text()")).isEqualTo("def");
	assertThat(valueFromXPath(parsedXml, "/test/duck/text()")).matches("[0-9]{3}");
	assertThat(nodeFromXPath(parsedXml, "/test/duck/xxx")).isNull();
	assertThat(valueFromXPath(parsedXml, "/test/alpha/text()")).matches("[\\p{L}]*");
	assertThat(valueFromXPath(parsedXml, "/test/*/complex/text()")).isEqualTo("foo");
	assertThat(valueFromXPath(parsedXml, "/test/duck/@type")).isEqualTo("xtype");
	}


SpringCloud 具有显式模式的WebFlux
SpringCloud 方法触发的输出
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

三、Spring Cloud Netflix

SpringCloud Hystrix超时和Ribbon客户

SpringCloud 重试失败的请求

五、Spring Cloud Stream

六、SpringCloud Binder实现

SpringCloud 重试RabbitMQ Binder

SpringCloud Dead-Letter队列处理

八、Spring Cloud Sleuth

SpringCloud 当前Span

十二、Spring Cloud for Cloud Foundry

十三、Spring Cloud Contract

Spring Cloud Contract验证程序设置

SrpingCloud Gradle项目

十五、Spring Cloud网关

Spring Cloud 配置路由谓词工厂和网关过滤工厂

Spring Cloud TLS / SSL

Spring Cloud网关配置

SpringCloud 故障排除

十八、Spring Cloud GCP

Spring Cloud GCP Spring资源

Spring Cloud Spring JDBC

Spring Cloud Redis的Cloud Memorystore

Spring Cloud 云身份识别代理(IAP)身份验证

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }