【问题标题】:Robolectric: test Cookies in HttpResponseRobolectric:在 HttpResponse 中测试 Cookie
【发布时间】:2013-01-28 10:31:00
【问题描述】:

我正在使用 Robolectric 来测试我的 Android 应用程序。 我正在尝试编写一个测试来验证我的 Android 应用程序是否正确接收来自 HttpResponse 的 cookie。 我尝试了几个选项,但无法让它发挥作用。

这是我在测试中尝试的:

BasicHeader responseHeader = new BasicHeader("Set-Cookie", "cookiename=value");
String responseBody = "OK"
int responseCode = 200;
HttpResponse httpResponse = new TestHttpResponse(responseCode, responseBody, responseHeader);
Robolectric.addPendingHttpResponse(httpResponse);

这就是我的 Android 应用代码的样子:

DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(urlString);
// omitted: add username & password to request as urlencoded parameters
HttpResponse response = client.execute(httpPost);
// ...
List<Cookie> cookies = client.getCookieStore().getCookies();

其他测试运行良好,所以我让 Robolectric 工作。 但是 cookie 列表始终是空的,尽管当我将应用程序与(应用程序引擎)后端一起使用时,它工作正常。 但是,我可以在测试中从 HttpPost 获取 urlencoded 用户名和密码,因此请求确实得到了处理,只是没有任何 cookie。

有人试过类似的吗?

【问题讨论】:

    标签: android robolectric android-testing


    【解决方案1】:

    放置你的 Robolectric.addPendingHttpResponse(httpResponse);调用android的连接方法前的一行。

    它可能会起作用......

    【讨论】:

      猜你喜欢
      • 2014-03-11
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多