有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在请求中设置cookie

我正在使用rest-assured库进行API测试,似乎无法设置新的cookie。但是,我可以修改服务器设置的cookie

given()
  .cookie("cookie1", "true")
  .get(url)
  .then()
  .assertThat().cookie("cookie1", "true");
// Fails with "Cookie "cookie1" was not
// defined in the response. Cookies are: cookie2=true, cookie3=true


given()
  .cookie("cookie2", "false")
  .get(url)
  .then()
  .assertThat().cookie("cookie2", "false");
// PASS

共 (1) 个答案

  1. # 1 楼答案

    根据REST-assured Documentation,您需要调用when()body()方法:

    given()
    .cookie("cookie1", "true")
    .when() // <  
    .get(url)
    .then()
    .assertThat()
    .body(equalTo("true")) // <  
    

    请注意,我从未使用过这个API,我只是根据提供的规范进行推测