Citrus Framework - 可以从响应中分配API调用变量吗?

时间:2018-06-06 22:37:38

标签: cucumber citrus-framework

我想POST一个新对象并使用响应中的一个字段将我的URI映射到正确的resourceId。我在路径中使用@variable(' resourceId')@,但它使用文字URI字符串来发出请求,而不是使用POST响应中的resourceId填充。

有什么想法让这个有效吗?谢谢。

Given message itemPostRequestSingle
        And <itemPostRequestSingle> header Content-Type is "application/json;charset=utf-8"
        And <itemPostRequestSingle> header citrus_http_method is "POST"
 -----> And <itemPostRequestSingle> header citrus_request_path is "/path/@variable('resourceId')@/path"
        And <itemPostRequestSingle> payload is
        """

        [
          {
            "relativeId": "${relativeId2}"
          }
        ]

        """

        Given message itemPostResponseSingle
        And <itemPostResponseSingle> header Content-Type is "application/json;charset=utf-8"
        And <itemPostResponseSingle> header citrus_http_status_code is "201"
        And <itemPostResponseSingle> header citrus_http_reason_phrase is "CREATED"
        And <itemPostResponseSingle> payload is

        """
        [
          {
            "relativeId": "${relativeId}"
          },
          {
            "relativeId": "${relativeId2}"
          }
        ]
    """


Scenario: Example
    Given variables
      | relativeId           | citrus:randomString(10,false) |
      | relativeId2          | citrus:randomString(10,false) |

    When <ApiClient> sends message <PostRequest>
    And <ApiClient> should receive Json message <PostResponse>
    And <ApiClient> sends message <itemPostRequestSingle>
    Then <ApiClient> should receive Json message <itemPostResponseSingle>

1 个答案:

答案 0 :(得分:0)

您正在混合验证匹配器表达式(例如@variable('resourceId')@)和变量引用(例如${resourceId})。

您的itemPostRequestSingle应该使用变量引用。您的PostResponse应使用验证匹配器表达式,以便将id提取到变量resourceId

因此,我们的想法是使用验证匹配器表达式resourceId提取@variable('resourceId')@值,以便稍后在${resourceId}的测试用例中引用该变量。