解释 Rest Assured 中的 DELETE 请求。
rest assureddynamic programmingprogramming更新于 2024/11/6 18:14:00
我们可以在 Rest Assured 中执行 DELETE 请求。这是在 http DELETE 方法的帮助下完成的。它负责删除服务器资源。
删除请求可以有请求或响应主体。DELETE 请求可用的状态代码列在下面 −
- 200(OK)
- 204(如果我们要删除的记录没有内容)
- 202(已接受,删除不是单个操作)。
我们首先应通过 Postman 在端点上发送 DELETE 请求 − http://dummy.restapiexample.com/api/v1/delete/100.
使用 Rest Assured,我们将检查响应正文是否包含字符串 Successfully! Record has been deleted.
示例
代码实现
import org.testng.Assert; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; public class NewTest { @Test public void deleteRequest() { int record = 100; //带有 Rest Assured 类的基本 URI RestAssured.baseURI ="https://dummy.restapiexample.com/api/v1/"; //输入详细信息 RequestSpecification r = RestAssured.given(); //请求标头 r.header("Content-Type", "application/json"); //捕获来自删除请求的响应 Response res = r.delete("/delete/"+ record); //验证响应的状态代码 int s = res.getStatusCode(); Assert.assertEquals(s, 200); //将响应转换为字符串然后验证 String jsonString =res.asString(); Assert.assertEquals (jsonString.contains("成功!记录已被删除"), true); } }