【问题标题】:How to define pact-specification matching rule for single string body?如何为单个字符串体定义契约规范匹配规则?
【发布时间】:2019-08-08 15:26:51
【问题描述】:

我正在为上传文件的 put 请求设置测试。我的 pact 文件中的请求正文由一个字符串组成,其中包含一个 mime 边界,每次测试运行都会发生变化。我正在尝试为请求正文字符串定义正则表达式匹配规则,但它不匹配。标题内容类型的类似匹配规则确实匹配。

如果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"
                    }
                ]
            }
        }
    }
}

上面的代码是测试中使用的协议文件的一部分。测试导致 BodyMismatch 错误。比较预期和接收的主体表明它们仅在 mime 边界上有所不同,因此正则表达式匹配不起作用。

【问题讨论】:

    标签: pact


    【解决方案1】:

    通过 Pact 的 Slack 频道,我们得到了当前 Pact 代码不支持此类匹配的答案。我们在 GitHub 上创建了一个功能请求问题: https://github.com/pact-foundation/pact-reference/issues/43

    【讨论】:

      【解决方案2】:

      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 中实现不会太难。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-15
        • 1970-01-01
        • 1970-01-01
        • 2014-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多