Skip to content

Commit

Permalink
use play-netty instead akka-http
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Apr 11, 2023
1 parent 89cda71 commit 5bd1c4c
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 178 deletions.
5 changes: 1 addition & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,7 @@ lazy val `integration-tests` = project
Test / fork := true,
concurrentRestrictions += Tags.limitAll(1), // only one integration test at a time
Test / testOptions := Seq(Tests.Argument(TestFrameworks.JUnit, "-a", "-v")),
libraryDependencies ++= akkaHttp.map(_ % Test) ++ testDependencies,
libraryDependencies ++= akkaStreams.map(
_.cross(CrossVersion.for3Use2_13) // temporary, to make it tests work with Scala 3
),
libraryDependencies ++= playNettyServer.map(_ % Test) ++ testDependencies,
)
.settings(shadedAhcSettings)
.settings(shadedOAuthSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

package play.libs.ws.ahc

import akka.http.scaladsl.server.Route
import org.specs2.concurrent.ExecutionEnv
import org.specs2.concurrent.FutureAwait
import org.specs2.mutable.Specification
import play.AkkaServerProvider
import play.NettyServerProvider
import play.api.BuiltInComponents
import play.api.mvc.Results
import play.libs.ws.DefaultBodyWritables
import play.libs.ws.DefaultWSCookie
import play.libs.ws.WSAuthInfo
Expand All @@ -19,23 +20,25 @@ import uk.org.lidalia.slf4jtest.TestLoggerFactory

import scala.jdk.CollectionConverters._
import scala.jdk.FutureConverters._
import play.api.routing.sird._

class AhcCurlRequestLoggerSpec(implicit val executionEnv: ExecutionEnv)
extends Specification
with AkkaServerProvider
with NettyServerProvider
with StandaloneWSClientSupport
with FutureAwait
with DefaultBodyWritables {

override def routes: Route = {
import akka.http.scaladsl.server.Directives._
get {
complete("<h1>Say hello to akka-http</h1>")
} ~
post {
entity(as[String]) { echo =>
complete(echo)
}
override def routes(components: BuiltInComponents) = {
case GET(_) =>
components.defaultActionBuilder(
Results.Ok("<h1>Say hello to play</h1>")
)
case POST(_) =>
components.defaultActionBuilder { req =>
Results.Ok(
req.body.asText.getOrElse("")
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,52 @@
package play

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Route
import org.specs2.concurrent.ExecutionEnv
import org.specs2.specification.BeforeAfterAll

import scala.concurrent.duration._
import scala.concurrent.Await
import scala.concurrent.Future
import akka.stream.Materializer

trait AkkaServerProvider extends BeforeAfterAll {
import play.api.mvc.Handler
import play.api.mvc.RequestHeader
import play.core.server.NettyServer
import play.core.server.ServerConfig
import play.api.BuiltInComponents
import play.api.Mode

trait NettyServerProvider extends BeforeAfterAll {

/**
* @return Routes to be used by the test.
*/
def routes: Route
def routes(components: BuiltInComponents): PartialFunction[RequestHeader, Handler]

/**
* The execution context environment.
*/
def executionEnv: ExecutionEnv

var testServerPort: Int = _
lazy val testServerPort: Int = server.httpPort.getOrElse(sys.error("undefined port number"))
val defaultTimeout: FiniteDuration = 5.seconds

// Create Akka system for thread and streaming management
implicit val system: ActorSystem = ActorSystem()
implicit val materializer: Materializer = Materializer.matFromSystem

lazy val futureServer: Future[Http.ServerBinding] = {
// Using 0 (zero) means that a random free port will be used.
// So our tests can run in parallel and won't mess with each other.
Http().bindAndHandle(routes, "localhost", 0)
}
// Using 0 (zero) means that a random free port will be used.
// So our tests can run in parallel and won't mess with each other.
val server = NettyServer.fromRouterWithComponents(
ServerConfig(
port = Option(0),
mode = Mode.Test
)
)(components => routes(components))

override def beforeAll(): Unit = {
val portFuture = futureServer.map(_.localAddress.getPort)(executionEnv.executionContext)
portFuture.foreach(port => testServerPort = port)(executionEnv.executionContext)
Await.ready(portFuture, defaultTimeout)
}
override def beforeAll(): Unit = {}

override def afterAll(): Unit = {
futureServer.foreach(_.unbind())(executionEnv.executionContext)
server.stop()
val terminate = system.terminate()
Await.ready(terminate, defaultTimeout)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,43 @@

package play.api.libs.ws.ahc

import akka.http.scaladsl.server.Route
import org.specs2.concurrent.ExecutionEnv
import org.specs2.concurrent.FutureAwait
import org.specs2.mutable.Specification
import play.AkkaServerProvider
import play.NettyServerProvider
import play.api.BuiltInComponents
import play.api.libs.ws.DefaultBodyWritables
import play.api.libs.ws.DefaultWSCookie
import play.api.libs.ws.EmptyBody
import play.api.libs.ws.WSAuthScheme
import play.api.mvc.Handler
import play.api.mvc.RequestHeader
import play.api.mvc.Results
import uk.org.lidalia.slf4jext.Level
import uk.org.lidalia.slf4jtest.TestLogger
import uk.org.lidalia.slf4jtest.TestLoggerFactory
import play.api.routing.sird._

import scala.jdk.CollectionConverters._

class AhcCurlRequestLoggerSpec(implicit val executionEnv: ExecutionEnv)
extends Specification
with AkkaServerProvider
with NettyServerProvider
with StandaloneWSClientSupport
with FutureAwait
with DefaultBodyWritables {

override def routes: Route = {
import akka.http.scaladsl.server.Directives._
get {
complete("<h1>Say hello to akka-http</h1>")
} ~
post {
entity(as[String]) { echo =>
complete(echo)
}
override def routes(components: BuiltInComponents): PartialFunction[RequestHeader, Handler] = {
case GET(_) =>
components.defaultActionBuilder(
Results
.Ok("<h1>Say hello to play</h1>")
)
case POST(_) =>
components.defaultActionBuilder { req =>
Results.Ok(
req.body.asText.getOrElse("")
)
}
}

Expand Down
Loading

0 comments on commit 5bd1c4c

Please sign in to comment.