scalaplayframework-2.4

Play2 scala specs2 test FakeRequest with remote address


I'm working with Play! 2.4 scala, spec2 and Mockito.

I'm trying to set the remote address of a fake request in one of my integration tests but I can't found the right way to do that.

I would like to do something similar to the following snippet of code (that does not compile):

route(FakeRequest(GET, "/users/geographicPoint", remoteAddress =  "81.xxx.xxx.xxx"))

How can I easily do that?


Solution

  • FakeRequest.apply does not have an overload that accepts only those three parameters. The case class itself has some parameters without defaults that you would have to fill in. (Before editing) I would say you could just use the copy method, since FakeRequest is a case class, then fill in the value you want. But annoyingly, the copy method is inherited from RequestHeader, so when you copy a FakeRequest, you do not get a FakeRequest in return. Instead, you have to invoke the constructor manually:

    import play.api.mvc.AnyContentAsEmpty
    
    new FakeRequest("GET", "/", FakeHeaders(), AnyContentAsEmpty, remoteAddress = "1.2.3.4")