Playframework Secure module: how do you “log in” to test a secured controller in a FunctionalTest?

前端 未结 4 850
鱼传尺愫
鱼传尺愫 2020-12-08 08:46

EDIT: I\'m using Play! version 1.2 (production release)

I want to test controller actions that are secured by Secure module class, so I need to log

4条回答
  •  渐次进展
    2020-12-08 09:16

    I think there must be a misunderstanding of what the @Before interceptor does. It executes before your test is executed. If your test then logs you in, then this event happens AFTER the @Before code has been executed and the results of the secure module should be saved to the Cookie.

    Therefore, I can only assume that the Cookie is not being sent with the following request, therefore, I would suggest trying the following...

    get the cookie used by the secure cookie from the Response object immediately following your login. Create a Request object and set the Cookie to the request object, then call your POST method, passing in your request object.

    I have not tested this code, so not sure how it is going to react to mixing a pre-built request object, and passing in a URL, but not sure what else to suggest.

提交回复
热议问题