Search code examples
testingplayframeworkplayframework-2.5

Play framework redirecting to other method returns 303 in test


I have 2 endpoints: GET /users/me and GET /users/:id. I want to redirect the first to the second, passing the current user's ID. So I've made this method:

def getCurrentUser = SecuredAction { implicit request =>

  request.identity.id.map { currentUserId =>
    logger.info(s"Returning current user $currentUserId's profile")
    Redirect(routes.UserController.getUserById(currentUserId))
  }.getOrElse(InternalServerError)
}

This works fine and I don't get a 303 (I think) with postman. However, the following test fails because the status is 303:

val result = userController.getCurrentUser()(authenticatedRequest)
status(result) mustBe OK

Is this some quirk of the testing framework? I thought the syntax I was using was for a silent internal redirect. I don't actually want to return a 303 to the client.

How can I test this and does the method I've written perform a silent internal redirect?


Solution

  • Your result can never be OK (that is equivalent to a http status 200) since the two status you're returning are 303 (Redirect) and 500 (InternalServerError).

    Why don't you want to return a Redirect status?

    If you want to return a OK status, you could replace this line:

    Redirect(routes.UserController.getUserById(currentUserId))
    

    by the method getUserById.