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?
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With