3

我们有一个 WebService,它将formData键值对作为请求而不是 json。使用openTest我们如何通过这些formData?基本上我们需要一个代码片段来发布formData使用OpenTestyaml 脚本。

下面是我们需要使用 OpenTest 发布的示例 curl 命令,其中 Content-Type 为 multipart/form-data

`
curl --location --request POST 'https://serviceurl.com/getacb' \
--form 'userKey=a1b23' \
--form 'apiKey=1_ffER_hk6Rb89--2EElfsdeF3' \
--form 'secret=Ude+6NIjojo89/gyAB7huGS5' \
--form 'targetUID=ulknnk4kjlkj5'
`

我们正在寻找一个示例片段来发布上述多部分/表单数据。

4

1 回答 1

0

在将 FormData() 对象传递给下一个操作之前,您需要自己构建它。

var data = new FormData();
data.append("userKey", "a1b23");
data.append("apiKey", "1_ffER_hk6Rb89--2EElfsdeF3");
data.append("secret", "Ude+6NIjojo89/gyAB7huGS5");
data.append("targetUID", "ulknnk4kjlkj5");

这是对OpenTest API testing YAML进行了一些修改的示例。

description: Example Post based off SO Question
actors:
  - actor: ACTOR1
    segments:
      - segment: 1
        actions:
          - description: Create a random post ID
            script: | 
              var data = new FormData();
              data.append("userKey", "a1b23");
              data.append("apiKey", "1_ffER_hk6Rb89--2EElfsdeF3");
              data.append("secret", "Ude+6NIjojo89/gyAB7huGS5");
              data.append("targetUID", "ulknnk4kjlkj5");

          - description: Send a request to getacb
            action: org.getopentest.actions.HttpRequest
            args:
              url: https://serviceurl.com/getacb
              headers:
                Content-Type: multipart/form-data
              verb: POST
              body: data

          - description: Extract the response's status code and body
            script: |
              var statusCode = $output.statusCode;
              var postInfo = $output.body;

          - description: Validate the response status code
            script: |
              if (statusCode != 201) {
                $fail($format(
                  "We expected the status code to be {0} but it was {1}",
                  201,
                  statusCode));
              }
于 2021-08-05T00:37:53.633 回答