Skip to content

Commit

Permalink
Add Programmatic API suite
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuancelin committed May 16, 2018
1 parent a571f2e commit 904c8e8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
14 changes: 12 additions & 2 deletions otoroshi/app/api.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package otoroshi.api

import actions._
import akka.actor.ActorSystem
import com.softwaremill.macwire.wire
import com.typesafe.config.{Config, ConfigFactory}
import controllers._
import env._
import gateway.{CircuitBreakersHolder, ErrorHandler, GatewayRequestHandler, WebSocketHandler}
import play.api.http.{DefaultHttpFilters, HttpErrorHandler, HttpRequestHandler}
import play.api.inject.Injector
import play.api.libs.ws.WSClient
import play.api.libs.ws.ahc.AhcWSComponents
import play.api.mvc.{ControllerComponents, DefaultControllerComponents}
import play.api.routing.Router
import play.api.{BuiltInComponents, Configuration, LoggerConfigurator}
import play.core.server.{AkkaHttpServerComponents, ServerConfig}
import play.filters.HttpFiltersComponents
import router.Routes
import storage.DataStores

class ProgrammaticOtoroshiComponents(_serverConfig: play.core.server.ServerConfig, _configuration: Config)
extends AkkaHttpServerComponents
Expand Down Expand Up @@ -85,9 +89,9 @@ class ProgrammaticOtoroshiComponents(_serverConfig: play.core.server.ServerConfi

class Otoroshi(serverConfig: ServerConfig, configuration: Config = ConfigFactory.empty) {

lazy val components = new ProgrammaticOtoroshiComponents(serverConfig, configuration)
private lazy val components = new ProgrammaticOtoroshiComponents(serverConfig, configuration)

lazy val server = components.server
private lazy val server = components.server

def start(): Otoroshi = {
server.httpPort.get + 1
Expand All @@ -107,6 +111,12 @@ class Otoroshi(serverConfig: ServerConfig, configuration: Config = ConfigFactory
}))
this
}

def env: Env = components.env
def dataStores: DataStores = components.env.datastores
def ws: WSClient = components.wsClient
def system: ActorSystem = components.actorSystem
def injector: Injector = components.injector
}

object Otoroshi {
Expand Down
54 changes: 45 additions & 9 deletions otoroshi/test/functional/ProgrammaticApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ProgrammaticApiSpec(name: String, configurationSpec: => Configuration)
with IntegrationPatience {

lazy val serviceHost = "basictest.foo.bar"
lazy val serviceHost2 = "basictest2.foo.bar"
lazy val ws = otoroshiComponents.wsClient

override def getConfiguration(configuration: Configuration) = configuration ++ configurationSpec ++ Configuration(
Expand All @@ -41,7 +42,7 @@ class ProgrammaticApiSpec(name: String, configurationSpec: => Configuration)

val callCounter = new AtomicInteger(0)
val basicTestExpectedBody = """{"message":"hello world"}"""
val basicTestServer = TargetService(Some(serviceHost), "/api", "application/json", { _ =>
val basicTestServer = TargetService(None, "/api", "application/json", { _ =>
callCounter.incrementAndGet()
basicTestExpectedBody
}).await()
Expand All @@ -63,6 +64,23 @@ class ProgrammaticApiSpec(name: String, configurationSpec: => Configuration)
enforceSecureCommunication = false,
publicPatterns = Seq("/.*")
)
val otherDescriptor = ServiceDescriptor(
id = "basic-test-2",
name = "basic-test-2",
env = "prod",
subdomain = "basictest2",
domain = "foo.bar",
targets = Seq(
Target(
host = s"127.0.0.1:${basicTestServer.port}",
scheme = "http"
)
),
localHost = s"127.0.0.1:${basicTestServer.port}",
forceHttps = false,
enforceSecureCommunication = false,
publicPatterns = Seq("/.*")
)

val otoroshi = Otoroshi(
ServerConfig(
Expand All @@ -72,23 +90,41 @@ class ProgrammaticApiSpec(name: String, configurationSpec: => Configuration)
)
).start()

implicit val env = otoroshi.env

awaitF(3.seconds).futureValue

val services = getOtoroshiServices(Some(8888), otoroshi.components.wsClient).futureValue
val services = getOtoroshiServices(Some(8888), otoroshi.ws).futureValue

services.size mustBe 1

val (_, status) = createOtoroshiService(initialDescriptor, Some(8888), otoroshi.components.wsClient).futureValue
// create service using rest api
val (_, status) = createOtoroshiService(initialDescriptor, Some(8888), otoroshi.ws).futureValue

status mustBe 200

val basicTestResponse1 = otoroshi.components.wsClient.url(s"http://127.0.0.1:8888/api").withHttpHeaders(
"Host" -> serviceHost
).get().futureValue
{
val basicTestResponse1 = otoroshi.ws.url(s"http://127.0.0.1:8888/api").withHttpHeaders(
"Host" -> serviceHost
).get().futureValue

basicTestResponse1.status mustBe 200
basicTestResponse1.body mustBe basicTestExpectedBody
callCounter.get() mustBe 1
}

// create service using programmatic api
otoroshi.dataStores.serviceDescriptorDataStore.set(otherDescriptor).futureValue

{
val basicTestResponse1 = otoroshi.ws.url(s"http://127.0.0.1:8888/api").withHttpHeaders(
"Host" -> serviceHost2
).get().futureValue

basicTestResponse1.status mustBe 200
basicTestResponse1.body mustBe basicTestExpectedBody
callCounter.get() mustBe 1
basicTestResponse1.status mustBe 200
basicTestResponse1.body mustBe basicTestExpectedBody
callCounter.get() mustBe 2
}

basicTestServer.stop()
otoroshi.stop()
Expand Down

0 comments on commit 904c8e8

Please sign in to comment.