我们如何在放心的情况下传递多个标题? [英] How do we pass multiple headers in rest assured?
问题描述
我是新来的放心和Java,我正在尝试做一个非常基本的测试,检查响应是200 ok的API。
你可以告诉我在下面的脚本中需要更改什么才能传递多个标题ID,Key和ConId?
I am new to rest-assured and Java, I am trying to do a very basic test of checking the response is 200 ok for API. can you any one please tell me what do I need to change in the below script in order to pass multiple headers Id, Key and ConId?
import org.junit.Test;
import com.jayway.restassured.*;
import com.jayway.restassured.http.ContentType;
import static org.hamcrest.Matchers.*;
import static com.jayway.restassured.RestAssured.*;
public class APIresponse
{
public static void main(String[] args)
{
APIresponse apiresponse = new APIresponse();
apiresponse.response();
}
@Test
public void response ()
{
baseURI="http://testme/api/";
given().
header("Id", "abc").
param("Key", "NuDVhdsfYmNkDLOZQ").
param("ConId", "xyz").
when().
get("/uk?Id=DT44FR100731").
then().
contentType(ContentType.JSON).
body("response.code", equalTo("200"));
}
}
推荐答案
添加多个标头的最简单方法是在 .given()$ c之后多次重复
.header(headername,headervalue)
$ c>
Simplest way to add multiple headers is to just repeat .header(headername,headervalue)
multiple times after .given()
given().
header("Id", "abc").
header("name","name").
header("","")
...
你可以在此测试套件中找到使用REST-Assured框架传递标头的不同方法 github link 。
You can find different ways of passing headers using REST-Assured framework in its test suite at this github link.
编辑:
验证Rest-Assured中的响应状态:
To verify response status in Rest-Assured:
expect().statusCode(200),log().ifError().given()......
或选择一个如何测试响应头的示例 github link
or pick an example of how you want to test response header from this github link
这篇关于我们如何在放心的情况下传递多个标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!