如何在Rest Assured框架中验证响应头部

huangapple go评论62阅读模式
英文:

How to verify the response headers in Rest Assured framework

问题

Sure, here's the translation of the provided content:

如何在Rest Assured框架中验证响应头?我尝试以下方法来检查响应头,但我的代码没有给出任何结果。

Util文件

    public void verifyResponseHeaders(Map<String, String> data) {
        softAssert = new SoftAssert();
        data.forEach((headers, expected) -> {
            LogUtil.log(String.format("验证 => %s", headers));
            Object actual = response.headers();
            LogUtil.log(String.format("断言头部 期望值 = %s; 实际值 = %s", headers, expected, actual));
            assertValue(actual, expected);
        });
        softAssert.assertAll();
    }

Cucumber文件通用步骤:

      @And("我看到字段的头部匹配")
      public void verifyResponseHeaders(DataTable data) {
      apiUtil.verifyResponseHeaders(new HashMap<>(data.asMap()));
      }

Login.feature

 且我看到字段的头部匹配
 | Content-Type | application/json |

请注意,我只翻译了您提供的代码和文本,没有添加额外的内容。

英文:

How to verify the response headers in Rest Assured framework? I tried the below way to check for response headers, but my code is not giving any result.

Util file

    public void verifyResponseHeaders(Map&lt;String, String&gt; data {
        softAssert = new SoftAssert();
        data.forEach((headers, expected) -&gt; {
            LogUtil.log(String.format(&quot;Validating =&gt; %s&quot;, headers));
            Object actual = response.headers();
            LogUtil.log(String.format(&quot;Assertion for headers  Expected = %s; Actual = %s&quot;, headers,expected, actual));
            assertValue(actual, expected);
        });
        softAssert.assertAll();
       }

Cucumber file common Step:

      @And(&quot;I see headers matches for fields&quot;)
      public void verifyResponseHeaders(DataTable data) {
      apiUtil.verifyResponseHeaders(new HashMap&lt;&gt;(data.asMap()));
      }

Login.feature

 And I see headers matches for fields
 | Content-Type | application/json  |

答案1

得分: 3

让我们使用这个示例 curl -v https://httpbin.org/get 进行检查。如下所示,返回的标头为:

date: Fri, 26 May 2023 09:09:57 GMT
content-type: application/json
content-length: 256
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true

我们还假设我们的步骤接收到以下地图,作为预期标头集:

Map&lt;String, String&gt; expected = Map.of(
        &quot;content-type&quot;, &quot;application/json&quot;,
        &quot;access-control-allow-origin&quot;, &quot;*&quot;
);

首先,我们需要将其转换为 RA 内部表示标头的列表:

List&lt;Header&gt; expectedHeaders = expected
        .entrySet()
        .stream()
        .map(e -&gt;
                new Header(e.getKey(), e.getValue()))
        .collect(Collectors.toList());

现在,我们从我们正在测试的端点中获取标头:

List&lt;Header&gt; observedHeaders = RestAssured
        .get(&quot;https://httpbin.org/get&quot;)
        .getHeaders().asList();

我们需要做的最后一件事是决定我们的验证有多严格。如果我们需要检查我们的预期标头是否实际集合中,那么我们执行:

assertThat(expectedHeaders, everyItem(is(in(observedHeaders))));

否则,如果我们需要确保集合相等(通常我们不关心项的顺序),那么我们执行:

assertThat(observedHeaders, containsInAnyOrder(expectedHeaders));

这两个检查都使用了由 RestAssured 传递提供的 Hamcrest。因此,您需要包含以下导入,以使断言正常工作:

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
英文:

Let's check with this example curl -v https://httpbin.org/get. As it can bee seen the headers returned are:

date: Fri, 26 May 2023 09:09:57 GMT
content-type: application/json
content-length: 256
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true

Let's assume also that our step receives the map like this as expected set of headers:

Map&lt;String, String&gt; expected = Map.of(
        &quot;content-type&quot;, &quot;application/json&quot;,
        &quot;access-control-allow-origin&quot;, &quot;*&quot;
);

First of all we need to convert it to a list of RA internal representation of headers:

List&lt;Header&gt; expectedHeaders = expected
        .entrySet()
        .stream()
        .map(e -&gt;
                new Header(e.getKey(), e.getValue()))
        .collect(Collectors.toList());

Now we fetch the headers from our endpoint under test:

List&lt;Header&gt; observedHeaders = RestAssured
        .get(&quot;https://httpbin.org/get&quot;)
        .getHeaders().asList();

The last thing we need to do is to decide how strict our verification has to be. If we need to check if our expected headers are in the actual set, then we do:

assertThat(expectedHeaders, everyItem(is(in(observedHeaders))));

otherwise if we need to make sure the sets are equal (normally we do not care of item order), then we do:

assertThat(observedHeaders, containsInAnyOrder(expectedHeaders));

Both checks utilize Hamcrest that is supplied with RestAssured transitively. So you will need to include the following imports to make assertions work:

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;

huangapple
  • 本文由 发表于 2023年5月26日 13:19:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定