【发布时间】:2017-03-27 17:41:49
【问题描述】:
我正在为 http 请求编写 spring cloud contract,其中一个 json 正文(我们称之为 myMap)应该是 non空 映射(字符串到字符串)。所以请求者(又名 consumer)必须有这样的东西: "myMap": {"key": "val"}
可以在合同中强制执行吗?
这里是我为确定上下文而编写的现有合同的示例:
package contracts
org.springframework.cloud.contract.spec.Contract.make {
description("""
Represents a successful scenario of registering new host
given:
hostProperties are valid
then:
we'll register the host
""")
request {
method 'POST'
urlPath value(consumer(~/\/api\/hosts\/[a-zA-Z0-90-9]+/), producer('/api/hosts/icsl7875'))
body([
timeStamp : $(consumer(anyNumber()), producer(334)),
hyperThreaded : $(consumer(regex('^(true|false)$')), producer(false)),
virtualMachine: $(consumer(regex('^(true|false)$')), producer(false)),
poolName : $(consumer(regex('(.+)')), producer("dev_regression")),
osImage : $(consumer(regex('(.+)')), producer("osImage1")),
cores : $(consumer(anyNumber()), producer(2)),
memory : $(consumer(anyNumber()), producer(256)),
osRelease : $(consumer(regex('(.+)')), producer("osRelease1")),
wsmVeriosn : $(consumer(regex('(.+)')), producer("8.2.16")),
cpuCount : $(consumer(anyNumber()), producer(2445L)),
cpuMhz : $(consumer(anyNumber()), producer(22354L)),
cpuMips : $(consumer(anyNumber()), producer(256F))
])
headers {
contentType(applicationJsonUtf8())
}
}
response {
status 201
body([
groupId: $('067e6162-3b6f-4ae2-a171-2470b63dff00')
])
headers {
contentType(applicationJson())
}
}
}
【问题讨论】:
标签: groovy spring-cloud-contract