如何在play2.0中操作Session,Request和Response进行测试

nie*_*els 7 playframework-2.0

嗨我有一个身份验证服务,它可以处理Request(一些Header-Data和Cookie),Response(设置或删除cookie)和session(存储userId和rememberMe-information).

在播放1.x中很容易伪造请求,响应,Cookie和会话.它可以轻松设置Session.current().set(new Session()).在游戏2.0中,这不再起作用.

如何在请求中添加cookie?我怎么能操纵会话?我看到存在FakeApplication和FakeRequest,但我没有得到它,如何使用它们.

任何提示都表示赞赏.

Jul*_*Foy 13

它还没有为Play 2.0做好准备,但是在Play 2.1(以及当前的主人)中你可以写:

fakeRequest(GET, "/foo")
    .withSession("bar", "baz")
    .withCookies(cookie("bah", "toto"));
Run Code Online (Sandbox Code Playgroud)


nie*_*els 4

可以像 play1.x 那样进行操作。中心点是Context. 此外,您必须创建一个DummyRequest实现您需要的方法的方法。然后就可以创建以下内容

final Request request = new DummyRequest();
Context.current.set(new Context(request, new HashMap <String, String>(), 
        new HashMap <String, String>()));
Run Code Online (Sandbox Code Playgroud)

在你的测试中你可以得到Context.current().session(),Context.current().response()Context.current().request().

您可以在这里看到一个测试示例