Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a form on Play Framework 2.5?

Tags:

I' m new to PlayFramework, so I' m reading about it here. However, the link is for an older version, 2.1.1:

@Test
public void authenticateSuccess() {
    Result result = callAction(
        controllers.routes.ref.Application.authenticate(),
        fakeRequest().withFormUrlEncodedBody(ImmutableMap.of(
            "email", "[email protected]",
            "password", "secret"))
    );
    assertEquals(302, status(result));
    assertEquals("[email protected]", session(result).get("email"));
}

How the code above would look like in the current version 2.5 ?

like image 845
Valter Silva Avatar asked Nov 02 '16 18:11

Valter Silva


People also ask

What is activator in play framework?

The activator command can be used to create a new Play application. Activator allows you to select a template that your new application should be based off. For vanilla Play projects, the names of these templates are play-scala for Scala based Play applications, and play-java for Java based Play applications.

What is action in play framework?

What is an Action? Most of the requests received by a Play application are handled by an action. An action is basically a Java method that processes the request parameters, and produces a result to be sent to the client. public Result index(Http. Request request) { return ok("Got request " + request + "!"

Is Play framework asynchronous?

Internally, Play Framework is asynchronous from the bottom up. Play handles every request in an asynchronous, non-blocking way. The default configuration is tuned for asynchronous controllers.


1 Answers

For those who might be in the same situation, here's how I solved my problem:

@Test
public void authenticateAndRedirect() {
    Result result = route(new Http.RequestBuilder()
            .method(POST)
            .bodyForm(ImmutableMap.of("email", "[email protected]", "password", "secret"))
            .uri(controllers.routes.ApplicationController.authenticate().url()));
    assertEquals(SEE_OTHER, result.status());
}

@Test
public void authenticateAndRedirectJson() throws Exception {

    JsonNode jsonNode = (new ObjectMapper()).readTree("{ \"email\": \"[email protected]\", \"password\": \"secret\"  }");
    Http.RequestBuilder request = new Http.RequestBuilder()
            .method("POST")
            .bodyJson(jsonNode)
            .uri(controllers.routes.ApplicationController.authenticate().url());
    Result result = route(request);

    assertEquals(SEE_OTHER, result.status());
}

Keep in mind that I' m redirecting the user to another page once he' s authenticated, that' s why I' m expecting the code SEE_OTHER=303

public Result authenticate() {
    Form<Login> form = formFactory.form(Login.class).bindFromRequest();

    if (form.hasErrors()) {
        return badRequest(login.render(form));
    }

    Optional<User> maybeUser = User.findByEmail(form.get().email);

    if (maybeUser.isPresent()) {
        session("email", maybeUser.get().email);
        return redirect(routes.ApplicationController.index());
    }

    return badRequest(login.render(form));
}
like image 92
Valter Silva Avatar answered Sep 22 '22 16:09

Valter Silva