REST Assured 51 - How To Retrieve And Assert Content-Type Of Response In Rest Assured

REST Assured 系列汇总 之 REST Assured 51 - How To Retrieve And Assert Content-Type Of Response In Rest Assured

介绍

Content-Type header 是指媒体类型 或 MIME( Multipurpose Internet Mail Extensions ) 或 文件类型。当发起任何 POST 或 PUT 请求时,我们可能需要传一个 payload。payload 可能是 API 支持的任意格式,如 XML, JSON 等。我们需要用到 Content-Type header 让服务器知道一个请求中 payload 的格式。

同样,response 中的 Content-Type header 是指 response 返回的格式。request 中的 Content-Type header 和 response 中的 Content-Type header 不是强制必须一样的。有时我们需要断言 response 中 Content-Type header 的值。

前面我们已经了解了怎样设置 request 中的 Content-Type header

前提条件

添加 rest assured 依赖包

 <!-- REST Assured -->
 <dependency>
   <groupId>io.rest-assured</groupId>
   <artifactId>rest-assured</artifactId>
   <version>4.4.0</version>
</dependency>

Retrieving Content-Type of a response

当我们用 Rest Assured 调用一个 CRUD 操作时,将返回一个 Response 接口的一个引用。这个引用指向一个 API 响应的相关数据。我们可以用 Response 接口来获取一个响应的 Content-Type。

Response 接口继承自 ResponseOptions 接口,ResponseOptions 接口包含下面两个方法来获取一个响应的 Content-Type 值。

String contentType(); – Get the content type of the response
String getContentType(); – Get the content type of the response

上面两个方法仅仅只是名字不同,其实是一样的。第二个方法可读性更好。Rest Assured 提供很多语法糖 syntactic sugar 方法。返回值类型均是 String,返回 Content-Type header 的值,如果 header 不存在,则返回 NULL

// Direct method to fetch Content-Type
String contentType = response.getContentType();
System.out.println("Content-Type of response is : "+contentType);

因为 Content-Type 是一个 header, 我们也可以用 headers 的方法提取,可以参考 How To Retrieve Single And MultiValue Headers From Response Using Rest Assure

代码

import org.junit.Test;
 
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
 
public class RetrieveAndAssertContentType {
    
    
	
	@Test
	public void retrieveAndAssertContentType()
	{
    
    
		Response response = RestAssured
		.given()
		.contentType(ContentType.JSON)
		.body("{\r\n" + 
				"    \"firstname\" : \"Jim\",\r\n" + 
				"    \"lastname\" : \"Brown\",\r\n" + 
				"    \"totalprice\" : 111,\r\n" + 
				"    \"depositpaid\" : true,\r\n" + 
				"    \"bookingdates\" : {\r\n" + 
				"        \"checkin\" : \"2018-01-01\",\r\n" + 
				"        \"checkout\" : \"2019-01-01\"\r\n" + 
				"    },\r\n" + 
				"    \"additionalneeds\" : \"Breakfast\"\r\n" + 
				"}")
		.post("https://restful-booker.herokuapp.com/booking");
		
		// Direct method to fetch Content-Type
		String contentType = response.getContentType();
		System.out.println("Content-Type of response is : "+contentType);
		
		// Since Content-Type is an header
		String contentType1 = response.getHeader("Content-Type");
		System.out.println("Content-Type of response is : "+contentType1);
	}
 
}

输出:

Content-Type of response is : application/json; charset=utf-8
Content-Type of response is : application/json; charset=utf-8

Asserting Content-Type of a response without extracting

上面我们提取了 Content-Type,可以很方便地断言。但是也有一种方法可以直接断言,不用提取其值。我们可以用 ValidatableResponseOptions 接口的 content-Type() 方法。我们需要用 then() 方法作用在 Response 或获取 ValidatableResponseOptions 的一个实例,就可以用其方法 content-Type() 方法。

@Test
	public void retrieveAndAssertContentType2()
	{
    
    
		RestAssured
		.given()
		.contentType(ContentType.JSON)
		.body("{\r\n" + 
				"    \"firstname\" : \"Jim\",\r\n" + 
				"    \"lastname\" : \"Brown\",\r\n" + 
				"    \"totalprice\" : 111,\r\n" + 
				"    \"depositpaid\" : true,\r\n" + 
				"    \"bookingdates\" : {\r\n" + 
				"        \"checkin\" : \"2018-01-01\",\r\n" + 
				"        \"checkout\" : \"2019-01-01\"\r\n" + 
				"    },\r\n" + 
				"    \"additionalneeds\" : \"Breakfast\"\r\n" + 
				"}")
		.post("https://restful-booker.herokuapp.com/booking")
		.then()
		.contentType(ContentType.JSON);
	}

Guess you like

Origin blog.csdn.net/wumingxiaoyao/article/details/120576133