spring-cloud-contract - Spring Cloud Contract YAML 头匹配
问题描述
使用方法创建新资源POST
时,新资源的位置作为Location
标头添加到响应中。
如何创建一个 spring-cloud-contract YML 来验证响应是否包含Location
具有有效值URI
的标头?
我尝试使用以下 YAML,但它不起作用。
request:
method: POST
url: /customers/v1
body:
firstName: First Name
lastName: Last Name
dateOfBirth: "1990-12-12"
active: false
headers:
Content-Type: application/json
response:
status: 201
matchers:
headers:
- key: Location
regex: "http://localhost/customers/v1/*"
生成的测试代码
@Test
public void validate_create_customer_successfully() throws Exception {
// given:
MockMvcRequestSpecification request = given()
.header("Content-Type", "application/json")
.body("{\"firstName\":\"First Name\",\"lastName\":\"Last Name\",\"dateOfBirth\":\"1990-12-12\",\"active\":false}");
// when:
ResponseOptions response = given().spec(request)
.post("/customers/v1");
// then:
assertThat(response.statusCode()).isEqualTo(201);
}
生成的代码不包含任何标头验证。
解决方案
以下 yaml 定义有效
response:
status: 201
headers:
Location: "http://localhost/customers/v1/"
matchers:
headers:
- key: Location
regex: "http://localhost/customers/v1/.*"
生成的测试代码
// then:
assertThat(response.statusCode()).isEqualTo(201);
assertThat(response.header("Location")).matches("http://localhost/customers/v1/.*");
这按预期工作。我不知道为什么。有人可以解释一下吗?
推荐阅读
- javascript - 限制用户在输入中写入超过 2 个小数
- asp.net-core - 支持 ASP.NET Core 中同一用户使用电子邮件或用户名登录
- xcode9 - XCode 先决条件版本检查失败?无法使用 Intune Packager 打包应用
- rinsim - 速度和距离单位不一致
- java - 如何在 android oreo 中运行恒定的后台服务?
- swift - 关于设置可选变量的困惑
- c# - 我想完全理解 ().velocity 之后的含义
- python - git 存储库数据结构是否使用规范编码?
- javadoc - 如何链接到 Javadoc 中的外部 HTML 文件并使其显示格式正确?
- c# - 如何将 3D 模型另存为 .PNG