Headers, Cookies and Parameters with REST-assured – 用REST保证的头信息、Cookies和参数

最后修改: 2018年 3月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll explore some REST-assured advanced scenarios. We explored REST-assured before in the tutorial a Guide to REST-assured.

在这个快速教程中,我们将探索一些REST-assured的高级应用场景。我们之前在教程REST-assured指南中探讨了REST-assured。

To continue, we’ll cover examples that show how to set headers, cookie and parameters for our requests.

为了继续,我们将介绍一些例子,说明如何为我们的请求设置头文件、cookie和参数。

The setup is the same as the previous article, so let’s dive into our examples.

设置与前一篇文章相同,让我们深入了解一下我们的例子。

2. Setting Parameters

2.设置参数

Now, let’s discuss how to specify different parameters to our request – starting with path parameters.

现在,让我们讨论如何为我们的请求指定不同的参数–首先是路径参数。

2.1. Path Parameters

2.1.路径参数

We can use pathParam(parameter-name, value) to specify a path parameter:

我们可以使用pathParam(parameter-name, value)来指定一个路径参数。

@Test
public void whenUsePathParam_thenOK() {
    given().pathParam("user", "eugenp")
      .when().get("/users/{user}/repos")
      .then().statusCode(200);
}

To add multiple path parameters we’ll use the pathParams() method:

为了添加多个路径参数,我们将使用pathParams()方法。

@Test
public void whenUseMultiplePathParam_thenOK() {
    given().pathParams("owner", "eugenp", "repo", "tutorials")
      .when().get("/repos/{owner}/{repo}")
      .then().statusCode(200);

    given().pathParams("owner", "eugenp")
      .when().get("/repos/{owner}/{repo}","tutorials")
      .then().statusCode(200);
}

In this example, we’ve used named path parameters, but we can also add unnamed parameters, and even combine the two:

在这个例子中,我们使用了命名的路径参数,但我们也可以添加未命名的参数,甚至可以将两者结合起来。

given().pathParams("owner", "eugenp")
  .when().get("/repos/{owner}/{repo}", "tutorials")
  .then().statusCode(200);

The resulting URL, in this case, is https://api.github.com/repos/eugenp/tutorials.

在这种情况下,产生的URL是https://api.github.com/repos/eugenp/tutorials.

Note that the unnamed parameters are index-based.

注意,未命名的参数是基于索引的。

2.2. Query Parameters

2.2.查询参数

Next, let’s see how we can specify query parameters using queryParam():

接下来,让我们看看如何使用queryParam()指定查询参数:

@Test
public void whenUseQueryParam_thenOK() {
    given().queryParam("q", "john").when().get("/search/users")
      .then().statusCode(200);

    given().param("q", "john").when().get("/search/users")
      .then().statusCode(200);
}

The param() method will act like queryParam() with GET requests.

param()方法将像queryParam()一样用于GET请求。

For adding multiple query parameters, we can either chain several queryParam() methods, or add the parameters to a queryParams() method:

对于添加多个查询参数,我们可以将几个queryParam()方法连锁起来,或者将参数添加到一个queryParams()方法中。

@Test
public void whenUseMultipleQueryParam_thenOK() {
 
    int perPage = 20;
    given().queryParam("q", "john").queryParam("per_page",perPage)
      .when().get("/search/users")
      .then().body("items.size()", is(perPage));   
     
    given().queryParams("q", "john","per_page",perPage)
      .when().get("/search/users")
      .then().body("items.size()", is(perPage));
}

2.3. Form Parameters

2.3.表格参数

Finally, we can specify form parameters using formParam():

最后,我们可以使用formParam()指定表单参数:

@Test
public void whenUseFormParam_thenSuccess() {
 
    given().formParams("username", "john","password","1234").post("/");

    given().params("username", "john","password","1234").post("/");
}

The param() method will act life formParam() for POST requests.

对于POST请求,param()方法将作为生命formParam()

Also note that formParam() adds a Content-Type header with the value “application/x-www-form-urlencoded“.

还请注意,formParam()添加了一个Content-Type头,值为”application/x-www-form-urlencoded“。

3. Setting Headers

3.设置标题

Next, we can customize our request headers using header():

接下来,我们可以使用header()自定义我们的请求头信息:

@Test
public void whenUseCustomHeader_thenOK() {
 
    given().header("User-Agent", "MyAppName").when().get("/users/eugenp")
      .then().statusCode(200);
}

In this example, we’ve used header() to set the User-Agent header.

在这个例子中,我们使用header()来设置User-Agent标头。

We can also add a header with multiple values using the same method:

我们也可以用同样的方法添加一个有多个值的页眉。

@Test
public void whenUseMultipleHeaderValues_thenOK() {
 
    given().header("My-Header", "val1", "val2")
      .when().get("/users/eugenp")
      .then().statusCode(200);
}

In this example, we’ll have a request with two headers: My-Header:val1 and My-Header:val2.

在这个例子中,我们将有一个带有两个头的请求。My-Header:val1My-Header:val2.

For adding multiple headers, we’ll use the headers() method:

对于添加多个头文件,我们将使用headers()方法:

@Test
public void whenUseMultipleHeaders_thenOK() {
 
    given().headers("User-Agent", "MyAppName", "Accept-Charset", "utf-8")
      .when().get("/users/eugenp")
      .then().statusCode(200);
}

4. Adding Cookies

4.添加Cookies

We can also specify custom cookie to our request using cookie():

我们还可以使用cookie()为我们的请求指定自定义cookie。

@Test
public void whenUseCookie_thenOK() {
 
    given().cookie("session_id", "1234").when().get("/users/eugenp")
      .then().statusCode(200);
}

We can also customize our cookie using cookie Builder:

我们还可以使用cookieBuilder定制我们的cookie。

@Test
public void whenUseCookieBuilder_thenOK() {
    Cookie myCookie = new Cookie.Builder("session_id", "1234")
      .setSecured(true)
      .setComment("session id cookie")
      .build();

    given().cookie(myCookie)
      .when().get("/users/eugenp")
      .then().statusCode(200);
}

5. Conclusion

5.结论

In this article, we’ve shown how we can specify request parameters, headers, and cookies when using REST-assured.

在这篇文章中,我们已经展示了在使用REST-assured时如何指定请求参数、头信息和cookies。

And, as always, the full source code for the examples is available over on GitHub.

而且,像往常一样,这些例子的完整源代码可在GitHub上获得。