Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a page that is loaded after a redirect

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?

like image 715
Ren Avatar asked Feb 15 '23 13:02

Ren


1 Answers

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")
like image 141
ashawley Avatar answered Feb 23 '23 03:02

ashawley