如何为单个字符串body定义pact-specification匹配规则?
How to define pact-specification matching rule for single string body?
我正在为上传文件的放置请求设置测试。我的 pact-file 中的请求 body 由一个字符串组成,其中包含一个 mime 边界,该边界随每个测试 运行 而变化。我正在尝试为请求 body 字符串定义一个正则表达式匹配规则,但它不会匹配。 header content-type 的类似匹配规则确实匹配。
如果body只是一个字符串,我该如何定义body的匹配规则?
我正在使用 Rust 中的 Pact 参考实现。 Pact-Specification 版本是 3.
"request": {
"headers": {
"Content-Length": "206",
"Host": "127.0.0.1:1234",
"Connection": "Close",
"Content-Type": "multipart/form-data; boundary=\"MIME_boundary_4FBA8D0826C707B6\""
},
"body": "--MIME_boundary_4FBA8D0826C707B6\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test_file.txt\"\r\nContent-Type: application/octet-stream\r\n\r\nContent of test file.\r\n--MIME_boundary_4FBA8D0826C707B6--\r\n",
"matchingRules": {
"header": {
"$.Content-Type": {
"matchers": [
{
"match": "regex",
"regex": "multipart/form-data; boundary=\"MIME_boundary_[A-Z0-9]{16}\""
}
]
}
},
"body": {
"$": {
"matchers": [
{
"match": "regex",
"regex": "--MIME_boundary_[A-Z0-9]{16}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test_file.txt\"\r\nContent-Type: application/octet-stream\r\n\r\nContent of test file.\r\n--MIME_boundary_[A-Z0-9]{16}--\r\n"
}
]
}
}
}
}
以上代码是测试中使用的pact文件的一部分。测试导致 BodyMismatch 错误。比较预期的和接收到的正文表明它们仅在 mime 边界上不同,因此正则表达式匹配不起作用。
mime 边界值将始终更改。编写一个正则表达式来匹配这将是非常具有挑战性的。最好有一个理解多部分主体的匹配实现。 Pact-JVM 支持这一点(参见 https://github.com/DiUS/pact-jvm/blob/master/consumer/pact-jvm-consumer-junit/src/test/groovy/au/com/dius/pact/consumer/junit/ExampleFileUploadSpec.groovy),因此在 Pact-Rust 中实现起来并不难。
通过Pact的Slack渠道得到的答复是目前的Pact代码不支持这种匹配。我们在 GitHub 上创建了一个功能请求问题:
https://github.com/pact-foundation/pact-reference/issues/43
我正在为上传文件的放置请求设置测试。我的 pact-file 中的请求 body 由一个字符串组成,其中包含一个 mime 边界,该边界随每个测试 运行 而变化。我正在尝试为请求 body 字符串定义一个正则表达式匹配规则,但它不会匹配。 header content-type 的类似匹配规则确实匹配。
如果body只是一个字符串,我该如何定义body的匹配规则?
我正在使用 Rust 中的 Pact 参考实现。 Pact-Specification 版本是 3.
"request": {
"headers": {
"Content-Length": "206",
"Host": "127.0.0.1:1234",
"Connection": "Close",
"Content-Type": "multipart/form-data; boundary=\"MIME_boundary_4FBA8D0826C707B6\""
},
"body": "--MIME_boundary_4FBA8D0826C707B6\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test_file.txt\"\r\nContent-Type: application/octet-stream\r\n\r\nContent of test file.\r\n--MIME_boundary_4FBA8D0826C707B6--\r\n",
"matchingRules": {
"header": {
"$.Content-Type": {
"matchers": [
{
"match": "regex",
"regex": "multipart/form-data; boundary=\"MIME_boundary_[A-Z0-9]{16}\""
}
]
}
},
"body": {
"$": {
"matchers": [
{
"match": "regex",
"regex": "--MIME_boundary_[A-Z0-9]{16}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test_file.txt\"\r\nContent-Type: application/octet-stream\r\n\r\nContent of test file.\r\n--MIME_boundary_[A-Z0-9]{16}--\r\n"
}
]
}
}
}
}
以上代码是测试中使用的pact文件的一部分。测试导致 BodyMismatch 错误。比较预期的和接收到的正文表明它们仅在 mime 边界上不同,因此正则表达式匹配不起作用。
mime 边界值将始终更改。编写一个正则表达式来匹配这将是非常具有挑战性的。最好有一个理解多部分主体的匹配实现。 Pact-JVM 支持这一点(参见 https://github.com/DiUS/pact-jvm/blob/master/consumer/pact-jvm-consumer-junit/src/test/groovy/au/com/dius/pact/consumer/junit/ExampleFileUploadSpec.groovy),因此在 Pact-Rust 中实现起来并不难。
通过Pact的Slack渠道得到的答复是目前的Pact代码不支持这种匹配。我们在 GitHub 上创建了一个功能请求问题: https://github.com/pact-foundation/pact-reference/issues/43