如何在 Rest Assured 中验证 JSON 响应头?

rest assureddynamic programmingprogramming

我们可以在 Rest Assured 中验证 JSON 响应头。这可以通过 header 方法实现。我们将通过 Postman 模拟 API 发送 GET 请求,并观察响应头。

头 −

示例

使用 Rest Assured,我们将验证头中 Content-Length 的值。

代码实现

import org.hamcrest.Matchers;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import io.restassured.RestAssured;
public class NewTest {
   @Test
   public void ressponseAssertion() {

      //基础 URL
      RestAssured.baseURI = "https://run.mocky.io";

      //GET 操作
      given() .when().get("/v3/6c6ed634-5e78-4b80-94c7-cf17c04c7055");
      then().log().all()

      //验证状态码为 200
      .assertThat().statusCode(200)

      //验证主体
      .body(&"Location&", Matchers.equalTo(&"Makinac Island&"))

      //验证标题
      .header("Content-Length" , "57");
   }
}

输出


相关文章