play-scala-starter-example icon indicating copy to clipboard operation
play-scala-starter-example copied to clipboard

Allow localhost:9000 explicitly in example.

Open wsargent opened this issue 7 years ago • 3 comments

Same as https://github.com/playframework/play-java-starter-example/pull/61

wsargent avatar Mar 16 '18 17:03 wsargent

Beware the tests are failing because they are using another port (not 9000).

marcospereira avatar Mar 17 '18 04:03 marcospereira

I think this is failing because FakeRequest will render with HOST set to localhost with no port at all if not explicitly defined:

class FakeRequestFactory(requestFactory: RequestFactory) {
  def apply(): FakeRequest[AnyContentAsEmpty.type] = {
    apply(method = "GET", uri = "/", headers = FakeHeaders(Seq(HeaderNames.HOST -> "localhost")), body = AnyContentAsEmpty)
  }
}

https://github.com/playframework/playframework/blob/master/framework/src/play-test/src/main/scala/play/api/test/Fakes.scala#L174

whereas it should be using the default test port (or whatever it is overridden to in the application).

class FunctionalSpec extends PlaySpec with GuiceOneServerPerSuite {

  "Routes" should {
    "send 404 on a bad request" in  {
      route(app, FakeRequest(GET, "/boum").withHeaders(FakeHeaders(Seq(HeaderNames.HOST -> s"localhost:$port")))).map(status(_)) mustBe Some(NOT_FOUND)
    }

    "send 200 on a good request" in  {
      route(app, FakeRequest(GET, "/").withHeaders(FakeHeaders(Seq(HeaderNames.HOST -> s"localhost:$port")))).map(status(_)) mustBe Some(OK)
    }

  }
}

The problem is that GuiceOneAppPerSuite doesn't have a port. The best thing to do is use play.api.test.Helpers.testServerPort in that case.

wsargent avatar Mar 18 '18 02:03 wsargent

Fix needs to change documentation as well. https://www.playframework.com/documentation/2.6.x/AllowedHostsFilter#Testing

Also note the original example was actually about 127.0.0.1 rather than localhost as it was running on Windows 10, so when the original user uncommented, the default did not work: https://github.com/playframework/play-java-starter-example/issues/60

wsargent avatar Mar 18 '18 23:03 wsargent