4

I've got a test case that is supposed to verify that, after a POST call, the user is redirected to the correct page.

"Redirect Page" in {
  running(FakeApplication()) {
    val Some(result) = route(FakeRequest(POST, "/product/add/something")
      .withFormUrlEncodedBody(
       "Id" -> "666",
      )
      .withSession("email" -> "User")
    )
    status(result) must equalTo(SEE_OTHER)
    // contentAsString(result) at this point is just blank

This verifies that a redirect URL is given. How do I then get the unit test to go to the redirected URL so that I can verify its content?

Ren
  • 3,395
  • 2
  • 27
  • 46
  • And why not create another test precisely to test the new page because the redirection is well made to this page: "test redirected page" in { ... } – Momog Aug 16 '13 at 12:40
  • I am testing the new page, but what I'm unsure on how to do is that the redirect is pointing to the correct new page. All I've verified so far is that it is redirecting, not where it is redirecting to. – Ren Aug 18 '13 at 22:59

1 Answers1

8

You can test the URL redirected to with:

    redirectLocation(result) must beSome.which(_ == "/product/666")

If you want to check the content, follow the redirect:

    val nextUrl = redirectLocation(result) match {
      case Some(s: String) => s
      case _ => ""
    }
    nextUrl must contain("/product/666")

    val newResult = route(FakeRequest(GET, nextUrl)).get

    status(newResult) must equalTo(OK)
    contentType(newResult) must beSome.which(_ == "text/html")
    contentAsString(newResult) must contain("something")
ashawley
  • 4,195
  • 1
  • 27
  • 40
  • Thanks for your approach to make redirects. However is there any way in Play Framework ( Scala ) that the test case automatically redirects. I would not want to repeat the redirect logic every time. – tuxdna Jun 16 '14 at 07:38
  • tuxdna: Post a new question – ashawley Jun 16 '14 at 15:18