简体   繁体   English

如何使用通用代码从 REST Assured 响应中提取 cookie

[英]How to extract cookies from a REST Assured response using common code

Using REST Assured (4.3.3, from Java) I can freely manipulate request and response specification objects in a base test class, so that when the actual test needs a request specification to execute the actual call, it's "pre-configured" with certain common statements.使用 REST Assured(4.3.3,来自 Java)我可以自由地操作基础测试类中的请求和响应规范对象,这样当实际测试需要请求规范来执行实际调用时,它是“预配置”的常见的说法。 For example:例如:

public abstract class TestBase
    protected RequestSpecification request() {
        return given()
                .cookies(globalCookies)
                .port(serverPort)
                .log().ifValidationFails()
                .then()
                .log().ifValidationFails()
                .given();
    }
}
public class ActualTest extends TestBase {
  @Test
  public void test1() {
    String content = request().get("/some").then()
           .statusCode(200)
           .extract()
           .body().asString();
  }
}

What I'd like to do now is to "pre-configure" the specifications to preserve cookies between the requests.我现在想做的是“预配置”规范以在请求之间保留 cookie。 I am feeding the list of cookies to send using RequestSpecification.cookies(Cookies) , but I can't find any way to instruct the specifications to extract the returned cookies before obtaining the instance of Response .我正在使用RequestSpecification.cookies(Cookies)要发送的 cookie 列表,但我找不到任何方法来指示规范在获取Response实例之前提取返回的 cookie。 However, such instance is only available after calling one of the RequestSender methods, which I can't do in the base code.但是,此类实例仅在调用RequestSender方法之一后才可用,而我无法在基本代码中执行此操作。

The methods I've considered to solve this are outlined below, but the first two are quite clunky, and the third is probably just outright wrong as it meddles with classes in "internal" sub-packages.下面概述了我考虑过的解决此问题的方法,但前两个非常笨拙,第三个可能完全错误,因为它与“内部”子包中的类发​​生冲突。 Is there a "right" way of doing this?有没有“正确”的方法来做到这一点?

  1. Add a base method saveCookies() that takes in an instance of ValidatableResponse , on which it can call extract() , and get the cookies.添加一个基本方法saveCookies() ,它接受ValidatableResponse一个实例,它可以在该实例上调用extract()并获取 cookie。 Then each test will have to use the method.然后每次测试都必须使用该方法。
public abstract class TestBase
  // ...
  protected ValidatableResponse saveCookies(ValidatableResponse r) {
    saveGlobalCookies(r.extract().detailedCookies());
    return r;
  }
}
public class ActualTest extends TestBase {
  @Test
  public void test1() {
    String content = saveCookies(request().get("/some").then())
           .statusCode(200)
           .extract()
           .body().asString();
  }
}
  1. Use a base method that takes in lambdas so that the potentially intermediate ValidatableResponse can be intercepted, and the final result (if needed) returned to the invoker.使用接受 lambdas 的基本方法,以便可以拦截潜在的中间ValidatableResponse ,并将最终结果(如果需要)返回给调用者。
public abstract class TestBase
  // ...
  <T> T cookieRequest(Function<RequestSender, ValidatableResponse> exec, Function<ValidatableResponse, T> post) {

        ValidatableResponse vr = exec.apply(request());
        saveGlobalCookies(vr.extract().detailedCookies());
        return post.apply(vr);

    }
}
public class ActualTest extends TestBase {
  @Test
  public void test1() {
    String content = cookieRequest(r->r.get("/some").then(),
       r->r.extract().body().asString());
  }
}
  1. Instantiate extended instances of ResponseSpecificationImpl (dragging in RequestSpecificationImpl and TestSpecificationImpl ).实例化ResponseSpecificationImpl扩展实例(拖动RequestSpecificationImplTestSpecificationImpl )。

You can use CookieFilter to satify your need.您可以使用CookieFilter来满足您的需求。

The cookie filter can be used to keep track of all the cookies sent by the server and use them in subsequent requests cookie 过滤器可用于跟踪服务器发送的所有 cookie 并在后续请求中使用它们

You can use this class in 2 ways:您可以通过两种方式使用此类:

  1. Static setting:静态设置:
RestAssured.filters(new CookieFilter());
  1. Specify for request you want:指定您想要的请求:
CookieFilter cookieFilter = new CookieFilter();

//Request 1  
given().filter(cookieFilter).get("/x");

//Request 2
given().filter(cookieFilter).get("/y");

For more information:想要查询更多的信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何验证响应并从 Rest Assured 中的响应正文中提取值? - How to validate a response and extract a value from Response body in Rest Assured? 如何使用 Rest Assured 在位置响应标头字符串中提取身份验证代码 - How to extract an auth code in Location response header String using Rest Assured 如何使用放心从具有多个命名空间的 SOAP XML 响应中提取价值? - How to extract value from SOAP XML response with multiple namespaces using Rest-assured? 放心:从响应列表中提取值 - Rest Assured: extract value from Response List 从 rest 中提取一些字段保证响应 - extract some field from rest assured response 如何将访问令牌值从一个 API 响应主体(在一个类中)提取到另一个 API 标头(在另一个类中)在 rest 保证代码中 - How to extract accesss token value from one API response body(in one class) into another API header(in another class) in rest assured code 放心。 是否可以从响应 json 中提取 JSONObject/JSONArray? - Rest-assured. Is it possible to extract JSONObject/JSONArray from response json? 使用Rest Assured从JSON响应获取所有ID - Getting all ids from a JSON response using Rest Assured 使用 GPath 和 Rest Assured 从响应元素获取值 - Getting the value from the response element using GPath and Rest Assured 如何从嵌套列表中提取项目 - Rest Assured - How to extract item from nested list - Rest Assured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM