Skip to content

Commit

Permalink
migrate to scalatest
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Apr 8, 2023
1 parent d50c653 commit 9019831
Show file tree
Hide file tree
Showing 27 changed files with 658 additions and 655 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
with:
java: 17, 11
scala: 2.13.x, 3.x
cmd: sbt ++$MATRIX_SCALA 'testOnly -- xonly timefactor 5'
cmd: sbt ++$MATRIX_SCALA test

finish:
name: Finish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
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 org.scalatest.concurrent.ScalaFutures
import org.scalatest.wordspec.AnyWordSpec
import play.AkkaServerProvider
import play.libs.ws.DefaultBodyWritables
import play.libs.ws.DefaultWSCookie
Expand All @@ -20,11 +19,11 @@ import uk.org.lidalia.slf4jtest.TestLoggerFactory
import scala.jdk.CollectionConverters._
import scala.jdk.FutureConverters._

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

override def routes: Route = {
Expand Down Expand Up @@ -53,9 +52,9 @@ class AhcCurlRequestLoggerSpec(implicit val executionEnv: ExecutionEnv)
.setRequestFilter(curlRequestLogger)
.get()
.asScala
.awaitFor(defaultTimeout)
.futureValue

testLogger.getLoggingEvents.asScala.map(_.getMessage) must containMatch("--verbose")
assert(testLogger.getLoggingEvents.asScala.map(_.getMessage).exists(_.contains("--verbose")))
}

"add all headers" in withClient() { client =>
Expand All @@ -68,11 +67,11 @@ class AhcCurlRequestLoggerSpec(implicit val executionEnv: ExecutionEnv)
.setRequestFilter(curlRequestLogger)
.get()
.asScala
.awaitFor(defaultTimeout)
.futureValue

val messages = testLogger.getLoggingEvents.asScala.map(_.getMessage)

messages must containMatch("--header 'My-Header: My-Header-Value'")
assert(messages.exists(_.contains("--header 'My-Header: My-Header-Value'")))
}

"add all cookies" in withClient() { client =>
Expand All @@ -85,11 +84,11 @@ class AhcCurlRequestLoggerSpec(implicit val executionEnv: ExecutionEnv)
.setRequestFilter(curlRequestLogger)
.get()
.asScala
.awaitFor(defaultTimeout)
.futureValue

val messages = testLogger.getLoggingEvents.asScala.map(_.getMessage)

messages must containMatch("""--cookie 'cookie1=value1'""")
assert(messages.exists(_.contains("""--cookie 'cookie1=value1'""")))
}

"add method" in withClient() { client =>
Expand All @@ -101,9 +100,11 @@ class AhcCurlRequestLoggerSpec(implicit val executionEnv: ExecutionEnv)
.setRequestFilter(curlRequestLogger)
.get()
.asScala
.awaitFor(defaultTimeout)
.futureValue

testLogger.getLoggingEvents.asScala.map(_.getMessage) must containMatch("--request GET")
assert(
testLogger.getLoggingEvents.asScala.map(_.getMessage).exists(_.contains("--request GET"))
)
}

"add authorization header" in withClient() { client =>
Expand All @@ -116,14 +117,20 @@ class AhcCurlRequestLoggerSpec(implicit val executionEnv: ExecutionEnv)
.setRequestFilter(curlRequestLogger)
.get()
.asScala
.awaitFor(defaultTimeout)

testLogger.getLoggingEvents.asScala.map(_.getMessage) must containMatch(
"""--header 'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ='"""
.futureValue

assert(
testLogger.getLoggingEvents.asScala
.map(_.getMessage)
.exists(
_.contains(
"""--header 'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ='"""
)
)
)
}

"handle body" in {
"handle body" should {

"add when in memory" in withClient() { client =>
val testLogger = createTestLogger
Expand All @@ -135,9 +142,9 @@ class AhcCurlRequestLoggerSpec(implicit val executionEnv: ExecutionEnv)
.setRequestFilter(curlRequestLogger)
.get()
.asScala
.awaitFor(defaultTimeout)
.futureValue

testLogger.getLoggingEvents.asScala.map(_.getMessage) must containMatch("the-body")
assert(testLogger.getLoggingEvents.asScala.map(_.getMessage).exists(_.contains("the-body")))
}

"do nothing for empty bodies" in withClient() { client =>
Expand All @@ -150,9 +157,9 @@ class AhcCurlRequestLoggerSpec(implicit val executionEnv: ExecutionEnv)
.setRequestFilter(curlRequestLogger)
.get()
.asScala
.awaitFor(defaultTimeout)
.futureValue

testLogger.getLoggingEvents.asScala.map(_.getMessage) must not containMatch "--data"
assert(testLogger.getLoggingEvents.asScala.map(_.getMessage).forall(!_.contains("--data")))
}
}

Expand All @@ -168,18 +175,19 @@ class AhcCurlRequestLoggerSpec(implicit val executionEnv: ExecutionEnv)
.setRequestFilter(curlRequestLogger)
.get()
.asScala
.awaitFor(defaultTimeout)

testLogger.getLoggingEvents.get(0).getMessage must beEqualTo(
s"""
|curl \\
| --verbose \\
| --request GET \\
| --header 'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=' \\
| --header 'content-type: text/plain' \\
| --header 'My-Header: My-Header-Value' \\
| --data 'the-body' \\
| 'http://localhost:$testServerPort/'
.futureValue

assert(
testLogger.getLoggingEvents.get(0).getMessage ==
s"""
|curl \\
| --verbose \\
| --request GET \\
| --header 'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=' \\
| --header 'content-type: text/plain' \\
| --header 'My-Header: My-Header-Value' \\
| --data 'the-body' \\
| 'http://localhost:$testServerPort/'
""".stripMargin.trim
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package play.libs.ws.ahc

import akka.stream.Materializer
import org.specs2.execute.Result
import play.api.libs.ws.ahc.AhcConfigBuilder
import play.api.libs.ws.ahc.AhcWSClientConfig
import play.api.libs.ws.ahc.{ AhcWSClientConfigFactory => ScalaAhcWSClientConfigFactory }
Expand All @@ -15,9 +14,9 @@ trait StandaloneWSClientSupport {

def materializer: Materializer

def withClient(
def withClient[A](
config: AhcWSClientConfig = ScalaAhcWSClientConfigFactory.forConfig()
)(block: StandaloneAhcWSClient => Result): Result = {
)(block: StandaloneAhcWSClient => A): A = {
val asyncHttpClient = new DefaultAsyncHttpClient(new AhcConfigBuilder(config).build())
val client = new StandaloneAhcWSClient(asyncHttpClient, materializer)
try {
Expand Down
24 changes: 14 additions & 10 deletions integration-tests/src/test/scala/play/AkkaServerProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@ 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.ExecutionContext
import scala.concurrent.Future
import akka.stream.Materializer
import org.scalatest.BeforeAndAfterAll
import org.scalatest.Suite
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.Millis
import org.scalatest.time.Span

trait AkkaServerProvider extends BeforeAfterAll {
trait AkkaServerProvider extends BeforeAndAfterAll with ScalaFutures { self: Suite =>

implicit override def patienceConfig: PatienceConfig =
PatienceConfig(Span(5000, Millis))

/**
* @return Routes to be used by the test.
*/
def routes: Route

/**
* The execution context environment.
*/
def executionEnv: ExecutionEnv
protected implicit def executionContext: ExecutionContext = ExecutionContext.global

var testServerPort: Int = _
val defaultTimeout: FiniteDuration = 5.seconds
Expand All @@ -41,13 +45,13 @@ trait AkkaServerProvider extends BeforeAfterAll {
}

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

override def afterAll(): Unit = {
futureServer.foreach(_.unbind())(executionEnv.executionContext)
futureServer.foreach(_.unbind())(executionContext)
val terminate = system.terminate()
Await.ready(terminate, defaultTimeout)
}
Expand Down
Loading

0 comments on commit 9019831

Please sign in to comment.