Testing scala Play (2.2.1) controllers with CSRF protection

前端 未结 6 930
清歌不尽
清歌不尽 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:50

    One solution is to test using a browser, eg Fluentlenium, as this will manage cookies etc, so the CSRF protection should all just work.

    The other solution is to add a session to the FakeRequest so that it contains a token, eg:

    FakeRequest().withSession("csrfToken" -> CSRF.SignedTokenProvider.generateToken)
    

    Obviously if you're doing that a lot, you can create a help method to do that for you.

提交回复
热议问题