如何在Play2.0中操纵会话,请求和响应进行测试 [英] How to manipulate Session, Request and Response for test in play2.0

查看:69
本文介绍了如何在Play2.0中操纵会话,请求和响应进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个身份验证服务,该服务可用于Request(一些Header-Data和Cookie),Response(用于设置或删除Cookie)和会话(存储userId和RememberMe信息).

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).

在1.x版中,很容易伪造Request,Response,Cookie和Session.可以通过Session.current().set(new Session())轻松设置.在play 2.0中,它不再起作用.

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.

如何将Cookie添加到请求中?我该如何操纵会议?我看到存在FakeApplication和FakeRequest,但是我不明白如何使用它们.

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.

任何提示都值得赞赏.

推荐答案

可以执行类似于play1.x的方法.中心点是Context.此外,您必须创建一个DummyRequest来实现所需的方法.然后可以创建以下内容

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>()));

在测试中,您可以获得Context.current().session()Context.current().response()Context.current().request().

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

您可以在此处看到 查看全文

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