diff --git a/play-scalatest/src/main/scala/akka/grpc/scalatestplus/play/ServerGrpcClient.scala b/play-scalatest/src/main/scala/akka/grpc/scalatestplus/play/ServerGrpcClient.scala index 10c08ea2c..cef2aa02d 100644 --- a/play-scalatest/src/main/scala/akka/grpc/scalatestplus/play/ServerGrpcClient.scala +++ b/play-scalatest/src/main/scala/akka/grpc/scalatestplus/play/ServerGrpcClient.scala @@ -7,20 +7,25 @@ package akka.grpc.scalatestplus.play import akka.grpc.internal.AkkaGrpcClientFactory import akka.grpc.play.AkkaGrpcClientHelpers import akka.grpc.scaladsl.AkkaGrpcClient -import org.scalatestplus.play.NewServerProvider -import play.api.test.RunningServer +import play.api.Application +import play.api.test.{ DefaultTestServerFactory, RunningServer } import scala.reflect.ClassTag +import org.scalatest.TestData +import org.scalatestplus.play.BaseOneServerPerTest /** * Helpers to test gRPC clients with Play using ScalaTest. * * Mixes a method into [[AkkaGrpcClientHelpers]] that knows how to configure */ -trait ServerGrpcClient extends AkkaGrpcClientHelpers { this: NewServerProvider => +trait ServerGrpcClient extends AkkaGrpcClientHelpers { this: BaseOneServerPerTest => /** Configure the factory by combining the current app and server information */ implicit def configuredAkkaGrpcClientFactory[T <: AkkaGrpcClient: ClassTag](implicit running: RunningServer): AkkaGrpcClientFactory.Configured[T] = { AkkaGrpcClientHelpers.factoryForAppEndpoints(running.app, running.endpoints) } + override protected def newServerForTest(app: Application, testData: TestData): RunningServer = + DefaultTestServerFactory.start(app) + } diff --git a/play-scalatest/src/main/scala/org/scalatestplus/play/NewBaseOneServerPerTest.scala b/play-scalatest/src/main/scala/org/scalatestplus/play/NewBaseOneServerPerTest.scala deleted file mode 100644 index 8e8050ff9..000000000 --- a/play-scalatest/src/main/scala/org/scalatestplus/play/NewBaseOneServerPerTest.scala +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2001-2016 Artima, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.scalatestplus.play - -import org.scalatest.{ TestData, TestSuite, TestSuiteMixin } -import play.api.Application -import play.api.test.{ DefaultTestServerFactory, NewTestServer, RunningServer } - -// RICH: Replacement for scalatestplusplay's BaseOneServerPerTest class -// RICH: Remember to make the same changes for BaseOneServerPerSuite -// RICH: Adds support for multiple endpoints; also fixes a bug with concurrent tests -trait NewBaseOneServerPerTest extends TestSuiteMixin with NewServerProvider { this: TestSuite with FakeApplicationFactory => - - @volatile private var privateApp: Application = _ - @volatile private var testServer: NewTestServer = _ - - /** - * Implicit method that returns the `Application` instance for the current test. - */ - implicit final def app: Application = { - val a = privateApp - if (a == null) { throw new IllegalStateException("Test isn't running yet so application is not available") } - a - } - - implicit final def runningServer: RunningServer = { - val ts = testServer - if (ts == null) { throw new IllegalStateException("Test isn't running yet so the server endpoints are not available") } - RunningServer(app, ts.endpoints) - } - - /** - * Creates new instance of `Application` with parameters set to their defaults. Override this method if you - * need an `Application` created with non-default parameter values. - */ - def newAppForTest(testData: TestData): Application = fakeApplication() - - protected def newServerForTest(app: Application, testData: TestData): NewTestServer = - DefaultTestServerFactory.start(app) - - /** - * Creates new `Application` and running `TestServer` instances before executing each test, and - * ensures they are cleaned up after the test completes. You can access the `Application` from - * your tests as `app` and the `TestServer`'s port number as `port`. - * - * @param test the no-arg test function to run with a fixture - * @return the `Outcome` of the test execution - */ - abstract override def withFixture(test: NoArgTest) = { - // Need to synchronize within a suite because we store current app/server in fields in the class - // Could possibly pass app/server info in a ScalaTest object? - synchronized { - privateApp = newAppForTest(test) - testServer = newServerForTest(app, test) - try super.withFixture(test) finally { - val ts = testServer // Store before nulling fields - privateApp = null - testServer = null - // Stop server and release locks - ts.stopServer.close() - - } - } - } -} diff --git a/play-scalatest/src/main/scala/org/scalatestplus/play/NewGuiceOneServerPerTest.scala b/play-scalatest/src/main/scala/org/scalatestplus/play/NewGuiceOneServerPerTest.scala deleted file mode 100644 index f5af1014f..000000000 --- a/play-scalatest/src/main/scala/org/scalatestplus/play/NewGuiceOneServerPerTest.scala +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2001-2016 Artima, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.scalatestplus.play - -import org.scalatest.TestSuite -import org.scalatestplus.play.guice.GuiceFakeApplicationFactory - -// RICH: Replacement for scalatestplus-play's GuiceOneServerPerTest class -// RICH: When moving into scalatestplus-play library, remember to also update NewGuiceOneServerPerSuite -trait NewGuiceOneServerPerTest extends NewBaseOneServerPerTest with GuiceFakeApplicationFactory { this: TestSuite => - -} diff --git a/play-scalatest/src/main/scala/org/scalatestplus/play/NewServerProvider.scala b/play-scalatest/src/main/scala/org/scalatestplus/play/NewServerProvider.scala deleted file mode 100644 index 412b425b6..000000000 --- a/play-scalatest/src/main/scala/org/scalatestplus/play/NewServerProvider.scala +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2001-2016 Artima, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.scalatestplus.play - -import play.api.Application -import play.api.test.RunningServer - -// RICH: A new version of scalatestplusplay's ServerProvider. -// The changes should be merged into scalatestplusplay. -trait NewServerProvider { - - /** - * Implicit method that returns a `Application` instance. - */ - implicit def app: Application - - /** - * The endpoints of the running test server. - * @return - */ - // RICH: new property - implicit protected def runningServer: RunningServer - - /** - * The port used by the `TestServer`. - */ - // RICH: changed to final because most people read this rather than override it - // TODO: Document that this has been converted to a final method - final def port: Int = portNumber.value - - /** - * Implicit `PortNumber` instance that wraps `port`. The value returned from `portNumber.value` - * will be same as the value of `port`. - * - * @return the configured port number, wrapped in a `PortNumber` - */ - implicit def portNumber: PortNumber = { - PortNumber(runningServer.endpoints.httpEndpoint.fold(throw new IllegalStateException("No HTTP port available for test server"))(_.port)) - } -} diff --git a/play-scalatest/src/test/scala/akka/grpc/scalatestplus/play/PlayScalaTestSpec.scala b/play-scalatest/src/test/scala/akka/grpc/scalatestplus/play/PlayScalaTestSpec.scala index 7da8118f5..5f8a69e49 100644 --- a/play-scalatest/src/test/scala/akka/grpc/scalatestplus/play/PlayScalaTestSpec.scala +++ b/play-scalatest/src/test/scala/akka/grpc/scalatestplus/play/PlayScalaTestSpec.scala @@ -5,20 +5,20 @@ package akka.grpc.scalatestplus.play import org.scalatest.concurrent.{ IntegrationPatience, ScalaFutures } -import org.scalatestplus.play.{ NewGuiceOneServerPerTest, PlaySpec } +import org.scalatestplus.play.PlaySpec +import org.scalatestplus.play.guice.GuiceOneServerPerTest import play.api.Application import play.api.inject.bind import play.api.inject.guice.GuiceApplicationBuilder import play.api.libs.ws.WSClient import play.api.routing.Router - -import example.myapp.helloworld.grpc.helloworld.{ GreeterService, GreeterServiceImpl, GreeterServiceClient, HelloRequest } +import example.myapp.helloworld.grpc.helloworld.{ GreeterService, GreeterServiceClient, GreeterServiceImpl, HelloRequest } /** * Test for the Play gRPC ScalaTest APIs */ -class PlayScalaTestSpec extends PlaySpec with ServerGrpcClient - with NewGuiceOneServerPerTest with ScalaFutures with IntegrationPatience { +class PlayScalaTestSpec extends PlaySpec with GuiceOneServerPerTest with ServerGrpcClient + with ScalaFutures with IntegrationPatience { override def fakeApplication(): Application = { GuiceApplicationBuilder() diff --git a/play-specs2/src/main/scala/akka/grpc/play/api/specs2/ServerGrpcClient.scala b/play-specs2/src/main/scala/akka/grpc/play/api/specs2/ServerGrpcClient.scala index 15c4c7455..11c1ed9e8 100644 --- a/play-specs2/src/main/scala/akka/grpc/play/api/specs2/ServerGrpcClient.scala +++ b/play-specs2/src/main/scala/akka/grpc/play/api/specs2/ServerGrpcClient.scala @@ -8,7 +8,7 @@ import scala.reflect.ClassTag import akka.grpc.internal.AkkaGrpcClientFactory import akka.grpc.play.AkkaGrpcClientHelpers import akka.grpc.scaladsl.AkkaGrpcClient -import play.api.test.{ NewForServer, RunningServer } +import play.api.test.RunningServer /** * Helpers to test gRPC clients with Play using Specs2. @@ -16,7 +16,7 @@ import play.api.test.{ NewForServer, RunningServer } * Mixes a method into [[AkkaGrpcClientHelpers]] that knows how to configure * gRPC clients for the running server. */ -trait ServerGrpcClient extends AkkaGrpcClientHelpers { this: NewForServer => +trait ServerGrpcClient extends AkkaGrpcClientHelpers { /** Configure the factory by combining the app and the current implicit server information */ implicit def configuredAkkaGrpcClientFactory[T <: AkkaGrpcClient: ClassTag](implicit running: RunningServer): AkkaGrpcClientFactory.Configured[T] = { diff --git a/play-specs2/src/main/scala/play/api/test/NewForServer.scala b/play-specs2/src/main/scala/play/api/test/NewForServer.scala deleted file mode 100644 index e5e610c41..000000000 --- a/play-specs2/src/main/scala/play/api/test/NewForServer.scala +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2018 Lightbend Inc. - */ - -package play.api.test - -import org.specs2.execute.{ AsResult, Result } -import org.specs2.specification.{ ForEach, Scope } -import play.api.Application - -// RICH: replacement for play-specs2's WithServer class; adding server endpoint info -trait NewForServer extends ForEach[RunningServer] with Scope { - - protected def applicationFactory: ApplicationFactory - protected def testServerFactory: TestServerFactory = new DefaultTestServerFactory() - - override protected def foreach[R: AsResult](f: RunningServer => R): Result = { - val app: Application = applicationFactory.create() - val ts: NewTestServer = testServerFactory.start(app) - try AsResult.effectively(f(RunningServer(app, ts.endpoints))) finally ts.stopServer.close() - } -} diff --git a/play-specs2/src/test/scala/akka/grpc/play/api/specs2/PlaySpecs2Spec.scala b/play-specs2/src/test/scala/akka/grpc/play/api/specs2/PlaySpecs2Spec.scala index b2cfab878..a237c84fc 100644 --- a/play-specs2/src/test/scala/akka/grpc/play/api/specs2/PlaySpecs2Spec.scala +++ b/play-specs2/src/test/scala/akka/grpc/play/api/specs2/PlaySpecs2Spec.scala @@ -18,10 +18,10 @@ import example.myapp.helloworld.grpc.helloworld.{ GreeterService, GreeterService * Test for the Play gRPC Specs2 APIs */ @RunWith(classOf[JUnitRunner]) -class PlaySpecs2Spec extends NewForServer with ServerGrpcClient with PlaySpecification with ApplicationFactories { +class PlaySpecs2Spec extends ForServer with ServerGrpcClient with PlaySpecification with ApplicationFactories { protected def applicationFactory: ApplicationFactory = - appFromGuice(GuiceApplicationBuilder().overrides(bind[Router].to[GreeterServiceImpl])) + withGuiceApp(GuiceApplicationBuilder().overrides(bind[Router].to[GreeterServiceImpl])) // RICH: Still need to work out how to make WSClient work properly with endpoints def wsUrl(path: String)(implicit running: RunningServer): WSRequest = { diff --git a/play-testkit/src/main/java/akka/grpc/play/JavaAkkaGrpcClientHelpers.java b/play-testkit/src/main/java/akka/grpc/play/JavaAkkaGrpcClientHelpers.java index 705ff39e4..fb7ec17fa 100644 --- a/play-testkit/src/main/java/akka/grpc/play/JavaAkkaGrpcClientHelpers.java +++ b/play-testkit/src/main/java/akka/grpc/play/JavaAkkaGrpcClientHelpers.java @@ -9,9 +9,9 @@ import akka.actor.ActorSystem; import akka.grpc.GrpcClientSettings; -import play.api.test.NewTestServer; -import play.api.test.ServerEndpoint; -import play.api.test.ServerEndpoints; +import play.api.test.RunningServer; +import play.core.server.ServerEndpoint; +import play.core.server.ServerEndpoints; import javax.net.ssl.SSLContext; @@ -20,9 +20,9 @@ public final class JavaAkkaGrpcClientHelpers { private JavaAkkaGrpcClientHelpers() {} /** Creates a GrpcClientSettings from the given NewTestServer. */ - public static GrpcClientSettings grpcClientSettings(final NewTestServer testServer) { - final ServerEndpoint http2Endpoint = getHttp2Endpoint(testServer.endpoints()); - return grpcClientSettings(http2Endpoint, testServer.testServer().application().actorSystem()); + public static GrpcClientSettings grpcClientSettings(final RunningServer runningServer) { + final ServerEndpoint http2Endpoint = getHttp2Endpoint(runningServer.endpoints()); + return grpcClientSettings(http2Endpoint, runningServer.app().actorSystem()); } /** @@ -32,7 +32,7 @@ public static GrpcClientSettings grpcClientSettings(final NewTestServer testServ */ public static ServerEndpoint getHttp2Endpoint(final ServerEndpoints serverEndpoints) { final scala.collection.Traversable possibleEndpoints = - serverEndpoints.endpoints().filter(func(e->e.httpVersions().contains("2"))); + serverEndpoints.endpoints().filter(func(e->e.expectedHttpVersions().contains("2"))); if (possibleEndpoints.size() != 1) { throw new IllegalArgumentException(String.format( "gRPC client can't automatically find HTTP/2 connection: " + diff --git a/play-testkit/src/main/scala/akka/grpc/play/AkkaGrpcClientHelpers.scala b/play-testkit/src/main/scala/akka/grpc/play/AkkaGrpcClientHelpers.scala index f456e3eb8..2340154c9 100644 --- a/play-testkit/src/main/scala/akka/grpc/play/AkkaGrpcClientHelpers.scala +++ b/play-testkit/src/main/scala/akka/grpc/play/AkkaGrpcClientHelpers.scala @@ -7,14 +7,11 @@ package akka.grpc.play import java.util.concurrent.TimeUnit import akka.actor.ActorSystem -import akka.grpc.GrpcClientSettings import akka.grpc.internal.AkkaGrpcClientFactory import akka.grpc.scaladsl.AkkaGrpcClient import akka.stream.Materializer -import com.typesafe.config.{ Config, ConfigFactory, ConfigValueFactory } -import javax.net.ssl.SSLContext import play.api.Application -import play.api.test.{ ServerEndpoint, ServerEndpoints } +import play.core.server.{ ServerEndpoint, ServerEndpoints } import scala.concurrent.duration.Duration import scala.concurrent.{ Await, ExecutionContext } diff --git a/play-testkit/src/main/scala/play/api/test/ApplicationFactory.scala b/play-testkit/src/main/scala/play/api/test/ApplicationFactory.scala deleted file mode 100644 index 74c4584d7..000000000 --- a/play-testkit/src/main/scala/play/api/test/ApplicationFactory.scala +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2018 Lightbend Inc. - */ - -package play.api.test - -import play.api._ -import play.api.inject.guice.GuiceApplicationBuilder -import play.api.mvc._ -import play.api.routing.Router - -// RICH: This is taken from the Play integration tests; it should replace that old -// code when merged back into Play. - -/** - * Creates an [[Application]]. Usually created by a helper in [[ApplicationFactories]]. - */ -trait ApplicationFactory { - /** Creates an [[Application]]. */ - def create(): Application -} - -/** - * Mixin with helpers for creating [[ApplicationFactory]] objects. - */ -// RICH: I recommend that this is merged into PlaySpec so that users can easily create applications -trait ApplicationFactories { - def appFromGuice(builder: GuiceApplicationBuilder): ApplicationFactory = new ApplicationFactory { - override def create(): Application = builder.build() - } - def appFromComponents(components: => BuiltInComponents): ApplicationFactory = new ApplicationFactory { - override def create(): Application = components.application - } - def appFromRouter(createRouter: BuiltInComponents => Router): ApplicationFactory = - appFromConfigAndRouter(Map.empty)(createRouter) - def appFromConfigAndRouter(extraConfig: Map[String, Any])(createRouter: BuiltInComponents => Router): ApplicationFactory = appFromComponents { - val context = ApplicationLoader.Context.create( - environment = Environment.simple(), - initialSettings = Map[String, AnyRef](Play.GlobalAppConfigKey -> java.lang.Boolean.FALSE) ++ extraConfig.asInstanceOf[Map[String, AnyRef]]) - new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { - override lazy val router: Router = createRouter(this) - } - } - def appFromAction(createAction: DefaultActionBuilder => Action[_]): ApplicationFactory = appFromRouter { components: BuiltInComponents => - val action = createAction(components.defaultActionBuilder) - Router.from { case _ => action } - } - def appFromResult(result: Result): ApplicationFactory = appFromAction { Action: DefaultActionBuilder => - Action { result } - } -} - -final object ApplicationFactory extends ApplicationFactories diff --git a/play-testkit/src/main/scala/play/api/test/NewTestServer.scala b/play-testkit/src/main/scala/play/api/test/NewTestServer.scala deleted file mode 100644 index 0f5dc475d..000000000 --- a/play-testkit/src/main/scala/play/api/test/NewTestServer.scala +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (C) 2018 Lightbend Inc. - */ - -package play.api.test - -/** - * This is a simple wrapper around Play's [[TestServer]] class. - * The fields in the NewTestServer class will eventually be merged into - * Play's TestServer class. - * - * @param testServer The object we're adding fields to. - * @param endpoints The list of endpoints for the running server. - * @param stopServer A handle that can be used to close the server. - */ -// TODO: Merge this functionality into TestServer itself -final case class NewTestServer( - testServer: TestServer, - endpoints: ServerEndpoints, - stopServer: AutoCloseable) diff --git a/play-testkit/src/main/scala/play/api/test/RunningServer.scala b/play-testkit/src/main/scala/play/api/test/RunningServer.scala deleted file mode 100644 index 074f79b38..000000000 --- a/play-testkit/src/main/scala/play/api/test/RunningServer.scala +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (C) 2018 Lightbend Inc. - */ - -package play.api.test - -import play.api.Application - -/** - * Contains information about a running TestServer. This object can be - * used by tests to find out about the running server, e.g. port information. - * - * We use a separate class to avoid including mutable state, such as methods - * for closing the server. - */ -final case class RunningServer(app: Application, endpoints: ServerEndpoints) diff --git a/play-testkit/src/main/scala/play/api/test/SelfSigned.scala b/play-testkit/src/main/scala/play/api/test/SelfSigned.scala deleted file mode 100644 index 0af29ba42..000000000 --- a/play-testkit/src/main/scala/play/api/test/SelfSigned.scala +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2018 Lightbend Inc. - */ - -package play.api.test - -import java.security.KeyStore - -import javax.net.ssl._ - -import com.typesafe.sslconfig.ssl.FakeSSLTools - -import play.core.ApplicationProvider -import play.core.server.ServerConfig -import play.core.server.ssl.FakeKeyStore -import play.server.api.SSLEngineProvider - -// RICH: When merging into Play refactor the existing class in the Play integration tests -/** - * Contains a statically initialized self-signed certificate. - */ -object SelfSigned { - - /** - * The SSLContext and TrustManager associated with the self-signed certificate. - */ - lazy val (sslContext, trustManager): (SSLContext, X509TrustManager) = { - val keyStore: KeyStore = FakeKeyStore.generateKeyStore - FakeSSLTools.buildContextAndTrust(keyStore) - } - -} - -/** - * An SSLEngineProvider which simply references the values in the - * SelfSigned object. - */ -class SelfSignedSSLEngineProvider(serverConfig: ServerConfig, appProvider: ApplicationProvider) extends SSLEngineProvider { - override def createSSLEngine: SSLEngine = SelfSigned.sslContext.createSSLEngine() -} diff --git a/play-testkit/src/main/scala/play/api/test/ServerEndpoint.scala b/play-testkit/src/main/scala/play/api/test/ServerEndpoint.scala deleted file mode 100644 index 8d6ff9507..000000000 --- a/play-testkit/src/main/scala/play/api/test/ServerEndpoint.scala +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2018 Lightbend Inc. - */ - -package play.api.test - -import javax.net.ssl.{ SSLContext, X509TrustManager } - -// RICH: When merging into Play refactor the existing class in the Play integration tests - -/** - * Contains information about which port and protocol can be used to connect to the server. - * This class is used to abstract out the details of connecting to different backends - * and protocols. Most tests will operate the same no matter which endpoint they - * are connected to. - */ -final case class ServerEndpoint( - scheme: String, - host: String, - port: Int, - httpVersions: Set[String], - ssl: Option[ServerEndpoint.ClientSsl]) { - - /** - * Create a full URL out of a path. E.g. a path of `/foo` becomes `http://localhost:12345/foo` - */ - final def pathUrl(path: String): String = s"$scheme://$host:$port$path" - -} - -object ServerEndpoint { - /** Contains SSL information for a client that wants to connect to a [[ServerEndpoint]]. */ - final case class ClientSsl(sslContext: SSLContext, trustManager: X509TrustManager) -} - -/** - * Wrapper for a sequence of [[ServerEndpoints]]. Has a few convenience methods. Also - * can be used as an implicit parameter to pass around server endpoint information. - */ -final case class ServerEndpoints(endpoints: Seq[ServerEndpoint]) { - private def endpointForScheme(scheme: String): Option[ServerEndpoint] = endpoints.filter(_.scheme == scheme).headOption - /** Convenient way to get an HTTP endpoint */ - val httpEndpoint: Option[ServerEndpoint] = endpointForScheme("http") - /** Convenient way to get an HTTPS endpoint */ - val httpsEndpoint: Option[ServerEndpoint] = endpointForScheme("https") -} diff --git a/play-testkit/src/main/scala/play/api/test/TestServerFactory.scala b/play-testkit/src/main/scala/play/api/test/TestServerFactory.scala deleted file mode 100644 index 60f42fbe9..000000000 --- a/play-testkit/src/main/scala/play/api/test/TestServerFactory.scala +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2018 Lightbend Inc. - */ - -package play.api.test - -import java.util.concurrent.locks.Lock - -import play.api.{ Application, Configuration, Mode } -import play.core.server.{ AkkaHttpServer, ServerConfig } - -import scala.util.control.NonFatal - -// RICH: Not sure if this abstraction is useful or if we should just have -// another helper method. See Play's Runner's class. - -/** - * Creates a server for an application. - */ -trait TestServerFactory { - def start(app: Application): NewTestServer -} - -object DefaultTestServerFactory extends DefaultTestServerFactory - -/** - * Creates a server for an application with both HTTP and HTTPS ports - * using a self-signed certificate. - * - * Most logic in this class is in a protected method so that users can - * extend the class and override its logic. - */ -class DefaultTestServerFactory extends TestServerFactory { - - override def start(app: Application): NewTestServer = { - val testServer: play.api.test.TestServer = new play.api.test.TestServer( - serverConfig(app), app, Some(serverProvider(app))) - - // TODO: Maybe move this logic into Helpers - val appLock: Option[Lock] = optionalGlobalLock(app) - appLock.foreach(_.lock()) - val stopServer = new AutoCloseable { - override def close(): Unit = { - testServer.stop() - appLock.foreach(_.unlock()) - } - } - - try { - testServer.start() - NewTestServer(testServer, serverEndpoints(testServer), stopServer) - } catch { - case NonFatal(e) => - stopServer.close() - throw e - } - } - - /** - * Get the lock (if any) that should be used to prevent concurrent - * applications from running. - */ - protected def optionalGlobalLock(app: Application): Option[Lock] = { - if (app.globalApplicationEnabled) Some(PlayRunners.mutex) else None - } - - protected def serverConfig(app: Application) = { - val sc = ServerConfig( - port = Some(0), - sslPort = Some(0), - mode = Mode.Test, - rootDir = app.path) - sc.copy(configuration = sc.configuration ++ overrideServerConfiguration(app)) - } - - protected def overrideServerConfiguration(app: Application): Configuration = Configuration( - "play.server.https.engineProvider" -> classOf[SelfSignedSSLEngineProvider].getName, - "play.server.akka.http2.enabled" -> true) - - protected def serverProvider(app: Application): play.core.server.ServerProvider = AkkaHttpServer.provider - - protected def serverEndpoints(testServer: play.api.test.TestServer): ServerEndpoints = { - val httpEndpoint: Option[ServerEndpoint] = testServer.runningHttpPort.map(port => ServerEndpoint( - scheme = "http", - host = "localhost", - port = port, - httpVersions = Set("1.0", "1.1"), - ssl = None)) - val httpsEndpoint: Option[ServerEndpoint] = testServer.runningHttpsPort.map(port => ServerEndpoint( - scheme = "https", - host = "localhost", - port = port, - httpVersions = Set("1.0", "1.1", "2"), - ssl = Some(ServerEndpoint.ClientSsl(SelfSigned.sslContext, SelfSigned.trustManager)))) - ServerEndpoints(httpEndpoint.toSeq ++ httpsEndpoint.toSeq) - } - -} diff --git a/play-testkit/src/test/java/akka/grpc/play/test/PlayJavaFunctionalTest.java b/play-testkit/src/test/java/akka/grpc/play/test/PlayJavaFunctionalTest.java index 2e6fecfad..8b8a98265 100644 --- a/play-testkit/src/test/java/akka/grpc/play/test/PlayJavaFunctionalTest.java +++ b/play-testkit/src/test/java/akka/grpc/play/test/PlayJavaFunctionalTest.java @@ -26,7 +26,7 @@ public final class PlayJavaFunctionalTest { private final TestServerFactory testServerFactory = new DefaultTestServerFactory(); private Application app; - private NewTestServer testServer; + private RunningServer runningServer; private Application provideApplication() { return new GuiceApplicationBuilder() @@ -36,25 +36,25 @@ private Application provideApplication() { @Before public void startServer() throws Exception { - if (testServer != null) - testServer.stopServer().close(); + if (runningServer != null) + runningServer.stopServer().close(); app = provideApplication(); final play.api.Application app = this.app.asScala(); - testServer = testServerFactory.start(app); + runningServer = testServerFactory.start(app); } @After public void stopServer() throws Exception { - if (testServer != null) { - testServer.stopServer().close(); - testServer = null; + if (runningServer != null) { + runningServer.stopServer().close(); + runningServer = null; app = null; } } private WSResponse wsGet(final String path) throws Exception { final WSClient wsClient = app.injector().instanceOf(WSClient.class); - final String url = testServer.endpoints().httpEndpoint().get().pathUrl(path); + final String url = runningServer.endpoints().httpEndpoint().get().pathUrl(path); return wsClient.url(url).get().toCompletableFuture().get(); } @@ -75,7 +75,7 @@ private WSResponse wsGet(final String path) throws Exception { @Test public void worksWithAGrpcClient() throws Exception { final HelloRequest req = HelloRequest.newBuilder().setName("Alice").build(); final GrpcClientSettings grpcClientSettings = - JavaAkkaGrpcClientHelpers.grpcClientSettings(testServer); + JavaAkkaGrpcClientHelpers.grpcClientSettings(runningServer); final GreeterServiceClient greeterServiceClient = GreeterServiceClient.create( grpcClientSettings, app.asScala().materializer(), app.asScala().actorSystem().dispatcher()); try { diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 55ef03ee1..0eb271711 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -13,7 +13,7 @@ object Dependencies { val akkaHttp = "10.1.5" val akkaDiscovery = "0.18.0" - val play = "2.7.0-M3" + val play = "2.7.0-M4" val scalapb = "0.8.0" val grpc = "1.14.0" @@ -21,7 +21,7 @@ object Dependencies { val sslConfig = "0.3.6" val scalaTest = "3.0.5" - val scalaTestPlusPlay = "4.0.0-M3" + val scalaTestPlusPlay = "4.0.0-M5" val maven = "3.5.4" }