How to manipulate Session, Request and Response for test in play2.0

删除回忆录丶 提交于 2020-01-09 22:10:21

问题


Hi I had an authentication service which works on Request (some Header-Data and Cookie), Response (to set or delete a cookie) and session (Store userId and rememberMe-information).

In play 1.x it was easy to fake Request, Response, Cookie and Session. It could be easily set with Session.current().set(new Session()). In play 2.0 this doesn't work anymore.

How can I add a cookie to a request? How could I manipulate the session? I saw there exists FakeApplication and FakeRequest but I didn't get it, how to work with them.

Any hint is appreciated.


回答1:


It was not ready for Play 2.0, but in Play 2.1 (and in current master) you’ll be able to write:

fakeRequest(GET, "/foo")
    .withSession("bar", "baz")
    .withCookies(cookie("bah", "toto"));



回答2:


It's possible to do it similar to play1.x. The central point is the Context. Furthermore you must create a DummyRequest which implements the methods you need. Then it's possible to create the following

final Request request = new DummyRequest();
Context.current.set(new Context(request, new HashMap <String, String>(), 
        new HashMap <String, String>()));

in your test you can get Context.current().session(), Context.current().response() or Context.current().request().

You can see here an test-example.



来源:https://stackoverflow.com/questions/10381354/how-to-manipulate-session-request-and-response-for-test-in-play2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!