前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >REST-assured 实现 GET, POST, PUT, PATCH, DELETE 请求

REST-assured 实现 GET, POST, PUT, PATCH, DELETE 请求

作者头像
RiemannHypothesis
发布2022-10-31 14:44:28
1.1K0
发布2022-10-31 14:44:28
举报
文章被收录于专栏:Elixir

本文中将展示如何使用 REST Assured 框架发送 API 请求。例子中包含了 GETPOSTPUTPATCHDELETE 格式的请求。

一、REST Assured API 请求

创建一个 maven 项目 rest-assured-examples,并在 pom.xml 中添加 REST Assured 和 Junit 的依赖,如下代码所示:

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
</dependencies>

GET 请求

GET 请求是用来向服务器获取资源的。

接下来的例子将会使用 REST Assuredget() 方法实现 GET 请求。

test 包下创建一个 RestAssuredGetRequest 类,用来发送不带参数 GET 请求,代码如下:

代码语言:javascript
复制
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;

public class RestAssuredRequest {

    @BeforeAll
    public static void setup(){
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
    }

    @Test
    public void getRequest(){
        Response response = given()
                .contentType(ContentType.JSON)
                .when()
                .get("/posts")
                .then()
                .extract().response();

        Assertions.assertEquals(200, response.statusCode());
        Assertions.assertEquals("qui est esse", response.jsonPath().getString("title[1]"));
    }
}

在 GET 请求中发送数据,可以使用 query() 方法,在 RestAssuredGetRequest 类中添加 getRequestWithQueryParam 方法,代码如下:

代码语言:javascript
复制
@Test
public void getRequestWithQueryParam(){
    Response response = given()
            .contentType(ContentType.JSON)
            .param("postId","2")
            .when()
            .get("/comments")
            .then()
            .extract()
            .response();

    Assertions.assertEquals(200, response.statusCode());
    Assertions.assertEquals("Meghan_Littel@rene.us",response.jsonPath().getString("email[3]"));
}

在浏览器中访问 https://jsonplaceholder.typicode.com/comments?postId=2 页面显示结果如下:

image.png
image.png

代码 response.jsonPath().getString("email[3]" 其实就是获取第 4 个 post 中的 email 字段的内容,通过与 Assertions 中提供的期望值进行比较。

执行测试,控制台显示结果如下:

image.png
image.png

POST 请求

POST 格式请求常用于往服务端发送数据或者创建一个资源。

在 REST Assured 中发送一个 POST 请求,这里使用 post() 方法。

新创建一个 RestAssuredPostRequest 类,添加一个 requestBody 作为 POST 请求的请求体,再添加一个 postRequest 方法用于发送 POST 请求。

代码语言:javascript
复制
public class RestAssuredPostRequest {

    @BeforeAll
    public static void setup(){
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
    }

    private static String requestBody = "{\n" +
            "  \"title\": \"foo\",\n" +
            "  \"body\": \"bar\",\n" +
            "  \"userId\": \"1\" \n}";
            
    @Test
    public void postRequest(){
        Response response = given()
                .header("Content-type", "application/json")
                .and()
                .body(requestBody)
                .when()
                .post("/posts")
                .then()
                .extract().response();

        Assertions.assertEquals(201, response.statusCode());
        Assertions.assertEquals("foo", response.jsonPath().getString("title"));
        Assertions.assertEquals("bar", response.jsonPath().getString("body"));
        Assertions.assertEquals("1", response.jsonPath().getString("userId"));
        Assertions.assertEquals("101", response.jsonPath().getString("id"));
    }

}

执行测试,测试结果如下:

image.png
image.png

PUT 请求

PUT 请求用更新资源,PUT 请求要求传递一个 JSON 请求体。

REST Assured 中发送 PUT 格式请求需要使用 put() 方法。

新创建一个 RestAssuredPutRequest 类,添加一个 requestBody 作为 PUT 请求的请求体,这个 requestBody 中只包含更新的内容,再添加一个 putRequest 方法用于发送 PUT 请求。

代码语言:javascript
复制
public class RestAssuredPutRequest {

    @BeforeAll
    public static void setup(){
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
    }

    private static String requestBody = "{\n" +
            "  \"title\": \"foo\",\n" +
            "  \"body\": \"baz\",\n" +
            "  \"userId\": \"1\",\n" +
            "  \"id\": \"1\" \n}";

    @Test
    public void putRequest(){
        Response response = given()
                .header("Content-type", "application/json")
                .and()
                .body(requestBody)
                .when()
                .put("/posts/1")
                .then()
                .extract().response();

        Assertions.assertEquals(200, response.statusCode());
        Assertions.assertEquals("foo", response.jsonPath().getString("title"));
        Assertions.assertEquals("baz", response.jsonPath().getString("body"));
        Assertions.assertEquals("1", response.jsonPath().getString("userId"));
        Assertions.assertEquals("1", response.jsonPath().getString("id"));
    }

}

执行测试,测试结果如下图所示:

image.png
image.png

PATCH 请求

PATCH 请求也用于更新资源,但只需要有请求 body 中正在更新的字段即可。

新创建一个 RestAssuredPatchRequest 类,添加一个 requestBody 作为 Patch 请求的请求体,这个 requestBody 中只包含更新的内容即可,再添加一个 patchRequest 方法用于发送 Patch 请求。

代码语言:javascript
复制
public class RestAssuredPatchRequest {

    @BeforeAll
    public static void setup(){
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
    }

    private static String requestBody = "{\n" +
            "  \"title\": \"bax\",\n";

    @Test
    public void putRequest(){
        Response response = given()
                .header("Content-type", "application/json")
                .and()
                .body(requestBody)
                .when()
                .patch("/posts/1")
                .then()
                .extract().response();

        Assertions.assertEquals(200, response.statusCode());
        Assertions.assertEquals("bax", response.jsonPath().getString("body"));
        Assertions.assertEquals("1", response.jsonPath().getString("userId"));
        Assertions.assertEquals("1", response.jsonPath().getString("id"));
    }
}

执行测试用例,显示结果如下:

image.png
image.png

DELETE 请求

DELETE 请求可以删除服务端的资源。

REST Assured 中发送 DELETE 请求可以使用到 delete() 方法。

新创建一个 RestAssuredDeleteRequest 类,再添加一个 deleteRequest 方法用于发送 DELETE 请求。

代码语言:javascript
复制
public class RestAssuredDeleteRequest {

    @BeforeAll
    public static void setup(){
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
    }


    @Test
    public void putRequest(){
        Response response = given()
                .header("Content-type", "application/json")
                .and()
                .when()
                .put("/posts/1")
                .then()
                .extract().response();

        Assertions.assertEquals(200, response.statusCode());
    }

}

执行测试用例,显示结果如下:

image.png
image.png

本文翻译自 https://devqa.io/rest-assured-api-requests-examples/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、REST Assured API 请求
    • GET 请求
      • POST 请求
        • PUT 请求
          • PATCH 请求
            • DELETE 请求
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档