简体   繁体   English

放心获取响应大小(以 KB 为单位)

[英]Restassured Get response size in KB

When I do a rest call on postman I see a response size next to status in KB.当我对 postman 进行 rest 调用时,我看到状态旁边的响应大小以 KB 为单位。 Example: Size: 605 B示例:尺寸:605 B

I need this information using rest assured for my unit testing.我需要使用 rest 确保我的单元测试的这些信息。

Currently I have tried converting the response in byte and got its length however this gives the character count and not the Size of response in Bytes or KBs目前我已经尝试将响应转换为字节并得到它的长度,但是这给出了字符数而不是响应的大小(以字节或 KB 为单位)

Can anyone help how to achieve this任何人都可以帮助如何实现这一目标

The Content-Length HTTP header gives you the size of the payload. Content-Length HTTP header 为您提供了有效负载的大小。 Copied from MDN :MDN复制:

The Content-Length entity header indicates the size of the entity-body, in bytes, sent to the recipient.

Thus, you can use Rest Assured to check it, eg因此,您可以使用 Rest Assured 进行检查,例如

    when().
            get("/some-resource").
    then().
            header("Content-Length", "200");

The HeaderITest class in the Rest Assured example project has many more examples, for example if you need to parse the string to an int, how to use Hamcrest to check for equality, if it is less than or equal a specific value, how to check multiple header values, etc. Rest Assured 示例项目中的 HeaderITest class 有更多示例,例如如果您需要将字符串解析为 int,如何使用 Hamcrest 来检查是否相等,如果小于或等于特定值,如何检查多个 header 值等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM