【问题标题】:Which snippet to use in Postman test to validate a part of a header value在 Postman 测试中使用哪个片段来验证标题值的一部分
【发布时间】:2020-04-10 19:26:28
【问题描述】:

在 API POST 请求的响应中,位置作为键和值在标头中返回,由 2 部分组成:URL 路径+唯一 ID

我想在 Postman 测试中仅验证值的 URL 路径,因为在每个新请求之后唯一 ID 都会不同。

响应中的 Location 标头值示例:

api/v1/users/cbca75db-7d7d-4687-9aa0-c68f4rg46465/customers/EXXXXXXXXX/emailaddresses/5637454148

我只想验证:

api/v1/users/cbca75db-7d7d-4687-9aa0-c68f4rg46465/customers/EXXXXXXXXX/emailaddresses/

到目前为止我有:

    pm.response.to.have.header("Location");
    pm.response.to.be.header("Location", "api/v1/users/cbca75db-7d7d-4687-9aa0-c68f4rg46465/customers/E000088/emailaddresses");

但这不是100%正确

【问题讨论】:

    标签: validation testing header postman code-snippets


    【解决方案1】:

    如果您只是想检查标题是否包含字符串的一部分,您可以使用如下内容:

    pm.test("Location Header", () => {
            pm.response.to.have.header("Location");
            pm.expect(pm.response.headers.get("Location")).to.include("api/v1/users/cbca75db-7d7d-4687-9aa0-c68f4rg46465/customers/EXXXXXXXXX/emailaddresses")
        })
    

    我不知道 URL 路径中的 guidid 是否会改变,所以这是非常硬编码的,而不是动态的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-23
      • 2019-12-18
      • 1970-01-01
      • 2021-06-19
      • 2019-05-12
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多