简体   繁体   中英

Junit testing play framework

I need to test controllers that are secured with:

@Security.Authenticated(Secured.class).

Even after i log in i get unauthorized,i read that i need to copy the cookie from the log in response and send it with every request to secured method. tried that without any luck.

any idea how to solve that?

Assuming that you are using Helper.route method for testing, logged in behavior can be accomplished by using FakeRequest.withSession method.

For instance, if you are using email as authentication token in your Secured class;

@Override
public String getUsername(Http.Context ctx) {
    return ctx.session().get("email");
}

Your test method would be like this;

@Test
public void testPage() {
    FakeRequest testRequest = new FakeRequest(Helpers.GET, "/page")
                               .withSession("email", "mail@example.com");
    Result result = Helpers.route(testRequest);
    assertThat(Helpers.status(result)).isEqualTo(Helpers.OK);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM