forked from playframework/playframework
/
ServerIntegrationSpecification.scala
91 lines (79 loc) · 3.11 KB
/
ServerIntegrationSpecification.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (C) 2009-2016 Lightbend Inc. <https://www.lightbend.com>
*/
package play.it
import org.specs2.execute._
import org.specs2.mutable.Specification
import org.specs2.specification.AroundEach
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.core.server.{ NettyServer, ServerProvider }
import play.core.server.AkkaHttpServer
import scala.concurrent.duration._
/**
* Helper for creating tests that test integration with different server
* backends. Common integration tests should implement this trait, then
* two specific tests should be created, one extending NettyIntegrationSpecification
* and another extending AkkaHttpIntegrationSpecification.
*
* When a test extends this trait it will automatically get overridden versions of
* TestServer and WithServer that delegate to the correct server backend.
*/
trait ServerIntegrationSpecification extends PendingUntilFixed with AroundEach {
parent =>
implicit def integrationServerProvider: ServerProvider
/**
* Retry up to 3 times.
*/
def around[R: AsResult](r: => R) = {
AsResult(EventuallyResults.eventually(1, 20.milliseconds)(r))
}
implicit class UntilAkkaHttpFixed[T: AsResult](t: => T) {
/**
* We may want to skip some tests if they're slow due to timeouts. This tag
* won't remind us if the tests start passing.
*/
def skipUntilAkkaHttpFixed: Result = parent match {
case _: NettyIntegrationSpecification => ResultExecution.execute(AsResult(t))
case _: AkkaHttpIntegrationSpecification => Skipped()
}
}
implicit class UntilNettyHttpFixed[T: AsResult](t: => T) {
/**
* We may want to skip some tests if they're slow due to timeouts. This tag
* won't remind us if the tests start passing.
*/
def skipUntilNettyHttpFixed: Result = parent match {
case _: NettyIntegrationSpecification => Skipped()
case _: AkkaHttpIntegrationSpecification => ResultExecution.execute(AsResult(t))
}
}
/**
* Override the standard TestServer factory method.
*/
def TestServer(
port: Int,
application: Application = play.api.PlayCoreTestApplication(),
sslPort: Option[Int] = None): play.api.test.TestServer = {
play.api.test.TestServer(port, application, sslPort, Some(integrationServerProvider))
}
/**
* Override the standard WithServer class.
*/
abstract class WithServer(
app: play.api.Application = GuiceApplicationBuilder().build(),
port: Int = play.api.test.Helpers.testServerPort)
extends play.api.test.WithServer(
app, port, serverProvider = Some(integrationServerProvider)
)
}
trait NettyIntegrationSpecification extends ServerIntegrationSpecification {
override def integrationServerProvider: ServerProvider = NettyServer.provider
}
trait AkkaHttpIntegrationSpecification extends ServerIntegrationSpecification {
self: Specification =>
// Provide a flag to disable Akka HTTP tests
private val runTests: Boolean = (System.getProperty("run.akka.http.tests", "true") == "true")
skipAllIf(!runTests)
override def integrationServerProvider: ServerProvider = AkkaHttpServer.provider
}