2012-03-26 48 views
5

我正在寻找一种方法来保留在我的Java测试中使用Play 2.0的fakeRequest会话,但我的尝试失败,同时调用基于Scala的方法JAR文件。保留会话在随后的Java调用播放2.0的fakeRequest

根据在斯卡拉问题Add values to Session during testing (FakeRequest, FakeApplication)提到拉的要求,我想下面的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())); 
} 

这将编译得很好,但在运行测试中,它打动了我:

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

作为一个总Scala newby,我真的不知道我是否接近。斯卡拉会议does expose (trait) that method through CookieBaker,我认为

请注意,我不一定在寻找一种方法来获得上述代码的运行;上述内容实际上只是第一个(可能的)获取会话的步骤。接下来,我可能会尝试使用类似play.api.mvc.Session.encodeAsCookie(session)的内容将它传递给后续请求。像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", "[email protected]"); 
     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中,Playframework Secure module: how do you “log in” to test a secured controller in a FunctionalTest?帮助,但事情似乎在2.0已经改变了,我从来没有使用1.x的

+0

(也添加到[播放框架谷歌组相关的职位(https://groups.google.com/forum/#!searchin/play-framework/session/play-framework/FuXaP7z9wz8) ;等待审核,如果可以的话,将保持与这篇文章同步。) – Arjan 2012-03-26 20:23:35

+0

在Google Groups中,Peter Hausel刚刚写道:*嗨,我今天将推出一个修复程序(与cookie和flash支持一起)。感谢彼得* – Arjan 2012-03-28 12:07:18

回答

6

毕竟没什么魔法需要。下面简单地保留了HTTP头,设置饼干,并将在未来的要求:

Map<String, String> data = new HashMap<String, String>(); 
data.put("email", "[email protected]"); 
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,并对其进行解析时的一些信息的the first version这是很难)

+0

非常感谢这个答案。 – Mikesname 2012-10-22 14:06:18

3

使用当前版本的Play在您的测试中使用会话真的很容易。您可以使用cookies(Result result)静态辅助方法。

// Route that sets some session data to be used in subsequent requests 
Result result = callAction(...); 
Http.Cookie[] cookies = FluentIterable.from(cookies(result)).toArray(Http.Cookie.class); 

FakeRequest request = new FakeRequest(GET, "/someRoute").withCookies(cookies); 
callAction(controllers.routes.ref.Application.requestNeedingSession(), request);