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 Oct 7, 2023
1 parent 25a6aee commit 95c9951
Show file tree
Hide file tree
Showing 27 changed files with 657 additions and 653 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 @@ -4,9 +4,8 @@

package play.libs.ws.ahc

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.NettyServerProvider
import play.api.BuiltInComponents
import play.api.mvc.Results
Expand All @@ -22,11 +21,11 @@ import scala.jdk.CollectionConverters._
import scala.jdk.FutureConverters._
import play.api.routing.sird._

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

override def routes(components: BuiltInComponents) = {
Expand Down Expand Up @@ -56,9 +55,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 @@ -71,11 +70,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 @@ -88,11 +87,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 @@ -104,9 +103,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 @@ -119,14 +120,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 @@ -138,9 +145,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 @@ -153,9 +160,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 @@ -171,18 +178,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 org.apache.pekko.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
22 changes: 14 additions & 8 deletions integration-tests/src/test/scala/play/NettyServerProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@
package play

import org.apache.pekko.actor.ActorSystem
import org.specs2.concurrent.ExecutionEnv
import org.specs2.specification.BeforeAfterAll

import scala.concurrent.duration._
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import org.apache.pekko.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
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 {
trait NettyServerProvider extends BeforeAndAfterAll with ScalaFutures { self: Suite =>

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

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

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

lazy val testServerPort: Int = server.httpPort.getOrElse(sys.error("undefined port number"))
val defaultTimeout: FiniteDuration = 5.seconds
Expand All @@ -47,7 +51,9 @@ trait NettyServerProvider extends BeforeAfterAll {
)
)(components => routes(components))

override def beforeAll(): Unit = {}
override def beforeAll(): Unit = {
super.beforeAll()
}

override def afterAll(): Unit = {
server.stop()
Expand Down
Loading

0 comments on commit 95c9951

Please sign in to comment.