Testing scala Play (2.2.1) controllers with CSRF protection

前端 未结 6 983
清歌不尽
清歌不尽 2021-01-13 17:21

I\'ve been having some problems testing controllers that use Play\'s CSRF protection. To demonstrate this, I\'ve created a very simple Play application that minimally exhibi

6条回答
  •  情书的邮戳
    2021-01-13 17:47

    Following on from @plade, I added a helper method to my base test class:

    protected static FakeRequest csrfRequest(String method, String url) {
        String token = CSRFFilter.apply$default$5().generateToken();
        return fakeRequest(method, url + "?csrfToken=" + token)
            .withSession(CSRF.TokenName(), token);
    }
    

提交回复
热议问题