在随后的Java调用Play 2.0的fakeRequest中保持会话 [英] Keep session in subsequent Java calls to Play 2.0's fakeRequest

查看:68
本文介绍了在随后的Java调用Play 2.0的fakeRequest中保持会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在Java测试中使用Play 2.0的fakeRequest时保留会话的方法,但是在基于Scala的JAR中调用方法时,尝试失败.

I'm looking for a way to preserve the session when using Play 2.0's fakeRequest in my Java tests, but my attempts fail while invoking methods in the Scala-based JARs.

基于Scala问题向其中添加值的拉取请求测试期间的会话(FakeRequest,FakeApplication),我发现以下可能在Java中起作用:

Based on a pull request mentioned in the Scala question Add values to Session during testing (FakeRequest, FakeApplication), I figured the following might work in Java:

public Session getSession(Result result) {
  play.api.mvc.Cookies scalaCookies =
      play.api.test.Helpers.cookies(result.getWrappedResult());
  play.api.mvc.Cookie scalaSessionCookie =
      scalaCookies.get(play.api.mvc.Session.COOKIE_NAME()).get();
  scala.Option<play.api.mvc.Cookie> optionalCookie =
      scala.Option.apply(scalaSessionCookie);

  // Compiles fine, but fails with NoSuchMethodError:
  play.api.mvc.Session scalaSession =
      play.api.mvc.Session.decodeFromCookie(optionalCookie);

  return new play.mvc.Http.Session(Scala.asJava(scalaSession.data()));
}

这可以很好地编译,但是在运行测试时却得到了我

This compiles just fine, but while running the tests it gets me:

java.lang.NoSuchMethodError: 
  play.api.mvc.Session.decodeFromCookie(Lscala/Option;)Lplay/api/mvc/Session;

作为Scala的新手,我真的不知道我是否接近. Scala会话

Being a total Scala newby, I really have no idea if I'm even close. The Scala Session does expose (trait) that method through CookieBaker, I think.

请注意,我并不一定要寻找一种使上述代码运行的方法.以上实际上只是获得会话的第一步(可能的步骤).接下来,我可能会尝试使用play.api.mvc.Session.encodeAsCookie(session)之类的东西将其传递给后续请求.对于 ZenTasks演示:

Note that I am not necessarily looking for a way to get the above code running; the above is really just the first (possible) step to get the session. Next I'd probably try to use something like play.api.mvc.Session.encodeAsCookie(session) to pass it to the subsequent requests. Like for the ZenTasks demo:

@Test
public void testLoginAndMore() {
  Helpers.running(Helpers.fakeApplication(Helpers.inMemoryDatabase()), 
  new Runnable() {
    public void run() {
      Map<String, String> data = new HashMap<String, String>();
      data.put("email", "guillaume@sample.com");
      data.put("password", "secret");

      Result result = 
        callAction(controllers.routes.ref.Application.authenticate(),
          fakeRequest().withFormUrlEncodedBody(data));
      assertThat(status(result)).isEqualTo(Status.SEE_OTHER);
      assertThat(redirectLocation(result)).isEqualTo("/");

      // All fine; we're logged in. Now somehow preserve the cookie. This
      // does NOT do the trick:
      Session session = getSession(result);
      // ...subsequent callAction(..)s, somehow passing the session cookie 
    }
  });
}

对于1.x,有帮助,但是在2.0中情况似乎有所改变,而我从未使用过1.x.

For 1.x, Playframework Secure module: how do you "log in" to test a secured controller in a FunctionalTest? helps, but things seem to have changed in 2.0, and I never used 1.x.

推荐答案

毕竟不需要太多魔术.下面仅保留设置cookie的HTTP标头,并在下一个请求中将其传递:

Not much magic needed after all. The following simply preserves the HTTP header that sets the cookies, and passes that in the next request:

Map<String, String> data = new HashMap<String, String>();
data.put("email", "guillaume@sample.com");
data.put("password", "secret");

Result result = callAction(controllers.routes.ref.Application.authenticate(),
  fakeRequest().withFormUrlEncodedBody(data));

assertThat(status(result)).isEqualTo(Status.SEE_OTHER);
assertThat(redirectLocation(result)).isEqualTo("/");
// All fine; we're logged in. Preserve the cookies:
String cookies = header(HeaderNames.SET_COOKIE, result);

// Fetch next page, passing the cookies
result = routeAndCall(fakeRequest(GET, redirectLocation(result))
  .withHeader(HeaderNames.COOKIE, cookies));

assertThat(status(result)).isEqualTo(Status.OK);
assertThat(contentAsString(result).contains("Guillaume Bort"));

(请参阅此答案的第一个版本,以获取有关仅获取PLAY_SESSION cookie和解析的一些信息.那.虽然几乎不需要.)

(See the first version of this very answer for some information about getting only the PLAY_SESSION cookie, and parsing that. That's hardly needed though.)

这篇关于在随后的Java调用Play 2.0的fakeRequest中保持会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆