REST Assured 4 - 第一个GET Request

REST Assured 系列汇总 之 REST Assured 4 - 第一个GET Request

需求:

我们将自动化一个GET 请求叫作GetBookingIdsRestful Booker,验证返回的status code状态码和status line状态行

有关BDD风格,大家可以先忽略,后续会详细介绍。

Rest Assured基础

开始前,我们先了解一下有关Rest Assured的基础知识:

  1. RestAssured 是一个类包含许多static的 字段和方法。 支持POST, GET, PUT, DELETE, HEAD, PATCH, OPTIONS 请求并且验证这些请求的响应。

  2. RestAssured 有一个static重载方法叫get(), 它返回一个Response interface的引用。实际上所有http方法返回的类型在RestAssured class中都称为Response类型。这个response包含发起请求返回的所有细节,比如:response body, response headers, status code, status lines, cookies 等。

  3. 为了验证response的status或其它值,我们需要得到ValidatableResponse 类型的引用。ValidatableResponse 是一个接口。Response 接口有一个方法then(),就是返回ValidatableResponse。实际上有一个接口Validatable包含then()方法,Response 接口就是继承自Validatable 接口。实现Response接口的实现类就是RestAssuredResponseImpl,我们后面会介绍有关类和接口的层级关系。

  4. 一旦我们得到ValidatableResponse引用,我们就可以进行很多断言。本文中我们验证status code和状态行。ValidatableResponse有许多的验证方法。

代码实践:

请先参考《REST Assured 2 - 用IDEA创建一个基本的REST Assured Maven项目》创建一个Maven项目,特别注意pom.xml中加入REST Assured和Junit依赖。

有关Junit运行,需要注意:

  1. 测试类名必须以Test结尾
  2. 测试方法必须使用@Test进行修饰
  3. 测试方法必须使用public void修饰,不能带任何参数

None BDD 风格:

import io.restassured.RestAssured;
import io.restassured.specification.RequestSpecification;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import org.junit.Test;


public class NonBDDStyleGetRequestTest {

    // Without static import and builder pattern
    @Test
    public void GetBookingIds_VerifyStatusCode() {

        // Create a request specification
        RequestSpecification request = RestAssured.given();

        //Adding URI
        request.baseUri("https://restful-booker.herokuapp.com/booking");

        // Calling GET method on URI. After hitting we get Response
        Response response = request.get();

        // Let's print response body.
        String responseStr = response.asString();
        System.out.println("Response Details" + responseStr);

        /*
         * To perform validation on response like status code or value, we need to get
         * ValidatableResponse type of response using then() method of Response
         * interface. ValidatableResponse is also an interface.
         */
        ValidatableResponse validateableResponse = response.then();
        // It will check if status code is 200
        validateableResponse.statusCode(200);
        // It will check if status line is as expected
        validateableResponse.statusLine("HTTP/1.1 200 OK");
    }
}

BDD 风格:

import org.junit.Test;
import io.restassured.RestAssured;
import org.hamcrest.Matchers;

public class BDDStyleGetRequestTest {

    @Test
    public void GetBookingIds_VerifyStatusCode(){

        //Given
        RestAssured.given()
                .baseUri("https://restful-booker.herokuapp.com")
        // When
        .when()
            .get("/booking")

        // Then
        .then()
            .statusCode(200)
            .statusLine("HTTP/1.1 200 OK")
            // To verify booking id at index 3
            .body("bookingid[3]", Matchers.equalTo(10))
            // To verify booking count
            .body("bookingid.sum()", Matchers.hasSize(3));
    }
}

BDD 风格加上static import style:

import org.junit.Test;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class BDDStyleGetRequestWithStaticImportTest {

    @Test
    public void GetBookingIds_VerifyStatusCode(){
        // Given
        given()
            .baseUri("https://restful-booker.herokuapp.com")
        // When
        .when()
            .get("/booking")
        //Then
        .then()
            .statusCode(200)
            .statusLine("HTTP/1.1 200 OK")
            // To verify booking id at index 3
            .body("bookingid[3]", equalTo(3))
            // To verify booking count
            .body("bookingid.sum()", hasSize(10));
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值