常见的顶层元素
描述
你可以添加一个描述对你的合同。描述是任意的文字。这
以下代码展示了一个示例:
org.springframework.cloud.contract.spec.Contract.make {
description('''
given:
An input
when:
Sth happens
then:
Output
''')
}
description: Some description
name: some name
priority: 8
ignored: true
request:
url: /foo
queryParameters:
a: b
b: c
method: PUT
headers:
foo: bar
fooReq: baz
body:
foo: bar
matchers:
body:
- path: $.foo
type: by_regex
value: bar
headers:
- key: foo
regex: bar
response:
status: 200
headers:
foo2: bar
foo3: foo33
fooRes: baz
body:
foo2: bar
foo3: baz
nullValue: null
matchers:
body:
- path: $.foo2
type: by_regex
value: bar
- path: $.foo3
type: by_command
value: executeMe($it)
- path: $.nullValue
type: by_null
value: null
headers:
- key: foo2
regex: bar
- key: foo3
command: andMeToo($it)
Contract.make(c -> {
c.description("Some description");
}));
contract {
description = """
given:
An input
when:
Sth happens
then:
Output
"""
}
名称
你可以为合同提供一个名字。假设你提供了以下名称:应该注册一个用户.如果你这样做,自动生成测试的名称是validate_should_register_a_user.另外,WireMock 存根中的存根名称是should_register_a_user.json.
| 你必须确保名称中没有任何使 生成测试,不是编译。另外,请记住,如果你提供相同的名称 多个合同,自动生成的测试编译失败,生成的存根也存在 互相覆盖。 |
以下示例展示了如何在合同中添加名称:
org.springframework.cloud.contract.spec.Contract.make {
name("some_special_name")
}
name: some name
Contract.make(c -> {
c.name("some name");
}));
contract {
name = "some_special_name"
}
忽视合同
如果你想忽略某个契约,你可以在
插件配置或设置忽视合同本身的财产。如下
示例展示了如何实现:
org.springframework.cloud.contract.spec.Contract.make {
ignored()
}
ignored: true
Contract.make(c -> {
c.ignored();
}));
contract {
ignored = true
}
合同进行中
进行中的合同不会在生产方生成测试,但允许生成存根。
| 使用此功能时要谨慎,因为这可能导致误报,因为你生成了供消费者使用的存根,但实际上并没有实际实现。 |
如果你想设置合同进行中,可以做以下作 示例展示了如何实现:
org.springframework.cloud.contract.spec.Contract.make {
inProgress()
}
inProgress: true
Contract.make(c -> {
c.inProgress();
}));
contract {
inProgress = true
}
你可以设置失败进行Spring Cloud Contract插件属性,确保当至少一个进行中的合同在源码中时,你的构建会崩溃。
从文件传递数值
从版本开始1.2.0,你可以从文件中传递数值。假设你拥有
以下是您项目中的资源:
└── src
└── test
└── resources
└── contracts
├── readFromFile.groovy
├── request.json
└── response.json
进一步假设你的合同如下:
/*
* Copyright 2013-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method('PUT')
headers {
contentType(applicationJson())
}
body(file("request.json"))
url("/1")
}
response {
status OK()
body(file("response.json"))
headers {
contentType(applicationJson())
}
}
}
request:
method: GET
url: /foo
bodyFromFile: request.json
response:
status: 200
bodyFromFile: response.json
import java.util.Collection;
import java.util.Collections;
import java.util.function.Supplier;
import org.springframework.cloud.contract.spec.Contract;
class contract_rest_from_file implements Supplier<Collection<Contract>> {
@Override
public Collection<Contract> get() {
return Collections.singletonList(Contract.make(c -> {
c.request(r -> {
r.url("/foo");
r.method(r.GET());
r.body(r.file("request.json"));
});
c.response(r -> {
r.status(r.OK());
r.body(r.file("response.json"));
});
}));
}
}
import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract
contract {
request {
url = url("/1")
method = PUT
headers {
contentType = APPLICATION_JSON
}
body = bodyFromFile("request.json")
}
response {
status = OK
body = bodyFromFile("response.json")
headers {
contentType = APPLICATION_JSON
}
}
}
进一步假设 JSON 文件如下:
{
"status": "REQUEST"
}
{
"status": "RESPONSE"
}
当测试或存根生成时,内容request.json和response.json文件传递给主体
无论是请求还是回应。文件名必须是某个位置上的文件
相对于合同所在文件夹而言。
如果你需要以二进制形式传递文件内容,
你可以使用文件AsBytes在编码的DSL或bodyFromFileAsBytesYAML中的字段。
以下示例展示了如何传递二进制文件的内容:
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
url("/1")
method(PUT())
headers {
contentType(applicationOctetStream())
}
body(fileAsBytes("request.pdf"))
}
response {
status 200
body(fileAsBytes("response.pdf"))
headers {
contentType(applicationOctetStream())
}
}
}
request:
url: /1
method: PUT
headers:
Content-Type: application/octet-stream
bodyFromFileAsBytes: request.pdf
response:
status: 200
bodyFromFileAsBytes: response.pdf
headers:
Content-Type: application/octet-stream
import java.util.Collection;
import java.util.Collections;
import java.util.function.Supplier;
import org.springframework.cloud.contract.spec.Contract;
class contract_rest_from_pdf implements Supplier<Collection<Contract>> {
@Override
public Collection<Contract> get() {
return Collections.singletonList(Contract.make(c -> {
c.request(r -> {
r.url("/1");
r.method(r.PUT());
r.body(r.fileAsBytes("request.pdf"));
r.headers(h -> {
h.contentType(h.applicationOctetStream());
});
});
c.response(r -> {
r.status(r.OK());
r.body(r.fileAsBytes("response.pdf"));
r.headers(h -> {
h.contentType(h.applicationOctetStream());
});
});
}));
}
}
import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract
contract {
request {
url = url("/1")
method = PUT
headers {
contentType = APPLICATION_OCTET_STREAM
}
body = bodyFromFileAsBytes("contracts/request.pdf")
}
response {
status = OK
body = bodyFromFileAsBytes("contracts/response.pdf")
headers {
contentType = APPLICATION_OCTET_STREAM
}
}
}
| 每当你想处理二进制有效载荷时,都应该采用这种方法, 无论是用于HTTP还是消息传输。 |
元数据
你可以添加元数据对你的合同。通过元数据,你可以把配置传递给扩展。下面你可以找到
一个使用铁丝模仿钥匙。它的值是一个键为存根地图价值则是WireMock的存根地图对象。春云合约能够
用自定义代码修补生成的存根映射部分内容。你可能想这样做来添加webhooks,自定义
延迟或与第三方WireMock扩展集成。
Contract.make(c -> {
c.metadata(MetadataUtil.map()
.entry("wiremock", ContractVerifierUtil.map()
.entry("stubMapping", "{ \"response\" : { \"fixedDelayMilliseconds\" : 2000 } }")));
}));
contract {
metadata("wiremock" to ("stubmapping" to """
{
"response" : {
"fixedDelayMilliseconds": 2000
}
}"""))
}
在以下章节中,您可以找到支持的元数据条目的示例。
HTTP的契约
Spring Cloud Contract 允许你验证使用 REST 或 HTTP 作为
沟通方式。Spring Cloud Contract会验证这一点,对于匹配
来自请求作为合同的一部分,服务器会提供一个响应,该响应在
遵循响应合同的一部分。随后,这些合同被用于
生成WireMock存根,对于任何符合指定条件的请求,提供
合适的回应。