java - 无法在请求中设置 cookie

标签 java testing junit rest-assured

我正在为 API 测试使用 rest-assured 库,似乎无法设置新的 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

最佳答案

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

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

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

关于java - 无法在请求中设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32913594/

相关文章:

javascript - Qunit 使用 ember-simple-auth 身份验证测试 Ember.js 应用程序?

python - Django 测试 : AttributeError: 'WSGIRequest' object has no attribute 'user'

python - 如何终止蝗虫负载测试中的特定线程

unit-testing - Db 单元测试。禁用约束

java - 如何为现有 Java 项目创建 ant 构建器文件 (build.xml)?

java.io.InvalidClassException 即使使用serialVersionUID

java - 无法在eclipse中设置共享库路径

java - 将第三个 Android Activity 的结果返回给第一个 Activity

java - 空回复正文发帖放心

java - JUnit parameterized Tests 获取测试后的参数