aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzachdriver <zach@driver.xyz>2017-11-07 16:24:37 -0800
committerGitHub <noreply@github.com>2017-11-07 16:24:37 -0800
commitcd9ca017a1eb246cd0a5227fd984fa188ecd36d4 (patch)
tree262800eef6a9976714cd2a8d3797b15af5dd60b5
parent101c451480649c6d4aa8efb0294adf439512f8be (diff)
parentf4fb8903004f4541e1d624ad3b60b13ca726a069 (diff)
downloaddriver-core-1.6.2.tar.gz
driver-core-1.6.2.tar.bz2
driver-core-1.6.2.zip
Merge pull request #86 from drivergroup/zsmith/cors-headers-basic-routesv1.6.2v1.6.1
Add default headers to basic routes defined in DriverApp
-rw-r--r--src/main/scala/xyz/driver/core/app/DriverApp.scala9
-rw-r--r--src/test/scala/xyz/driver/core/rest/DriverAppTest.scala57
2 files changed, 64 insertions, 2 deletions
diff --git a/src/main/scala/xyz/driver/core/app/DriverApp.scala b/src/main/scala/xyz/driver/core/app/DriverApp.scala
index 751bef7..5297c90 100644
--- a/src/main/scala/xyz/driver/core/app/DriverApp.scala
+++ b/src/main/scala/xyz/driver/core/app/DriverApp.scala
@@ -44,6 +44,7 @@ class DriverApp(appName: String,
scheme: String = "http",
port: Int = 8080,
tracer: Tracer = NoTracer)(implicit actorSystem: ActorSystem, executionContext: ExecutionContext) {
+ self =>
import DriverApp._
implicit private lazy val materializer: ActorMaterializer = ActorMaterializer()(actorSystem)
@@ -69,12 +70,16 @@ class DriverApp(appName: String,
private def extractHeader(request: HttpRequest)(headerName: String): Option[String] =
request.headers.find(_.name().toLowerCase === headerName).map(_.value())
- protected def appRoute: Route = {
+ def appRoute: Route = {
val serviceTypes = modules.flatMap(_.routeTypes)
val swaggerService = swaggerOverride(serviceTypes)
val swaggerRoute = swaggerService.routes ~ swaggerService.swaggerUI
val versionRt = versionRoute(version, gitHash, time.currentTime())
- val combinedRoute = modules.map(_.route).foldLeft(versionRt ~ healthRoute ~ swaggerRoute)(_ ~ _)
+ val basicRoutes = new DriverRoute {
+ override def log: Logger = self.log
+ override def route: Route = versionRt ~ healthRoute ~ swaggerRoute
+ }
+ val combinedRoute = modules.map(_.route).foldLeft(basicRoutes.routeWithDefaults)(_ ~ _)
(extractHost & extractClientIP & trace(tracer)) {
case (origin, ip) =>
diff --git a/src/test/scala/xyz/driver/core/rest/DriverAppTest.scala b/src/test/scala/xyz/driver/core/rest/DriverAppTest.scala
new file mode 100644
index 0000000..82cc8cd
--- /dev/null
+++ b/src/test/scala/xyz/driver/core/rest/DriverAppTest.scala
@@ -0,0 +1,57 @@
+package xyz.driver.core.rest
+
+import akka.http.scaladsl.model.headers._
+import akka.http.scaladsl.model.{HttpMethods, StatusCodes}
+import akka.http.scaladsl.server.Directives._
+import akka.http.scaladsl.server.Route
+import akka.http.scaladsl.settings.RoutingSettings
+import akka.http.scaladsl.testkit.ScalatestRouteTest
+import com.typesafe.config.Config
+import com.typesafe.scalalogging.Logger
+import org.scalatest.{FlatSpec, Matchers}
+import xyz.driver.core.app.{DriverApp, Module}
+
+import scala.reflect.runtime.universe._
+
+class DriverAppTest extends FlatSpec with ScalatestRouteTest with Matchers {
+ class TestRoute extends DriverRoute {
+ override def log: Logger = xyz.driver.core.logging.NoLogger
+ override def route: Route = path("api" / "v1" / "test")(post(complete("OK")))
+ }
+
+ val module: Module = new Module {
+ val testRoute = new TestRoute
+ override def route: Route = testRoute.routeWithDefaults
+ override def routeTypes: Seq[Type] = Seq(typeOf[TestRoute])
+ override val name: String = "test-module"
+ }
+
+ val app: DriverApp = new DriverApp(
+ appName = "test-app",
+ version = "0.1",
+ gitHash = "deadb33f",
+ modules = Seq(module)
+ )
+
+ val config: Config = xyz.driver.core.config.loadDefaultConfig
+ val routingSettings: RoutingSettings = RoutingSettings(config)
+ val appRoute: Route = Route.seal(app.appRoute)(routingSettings = routingSettings, rejectionHandler = DriverApp.rejectionHandler)
+
+ "DriverApp" should "respond with the correct CORS headers for the swagger OPTIONS route" in {
+ Options(s"/api-docs/swagger.json") ~> appRoute ~> check {
+ status shouldBe StatusCodes.OK
+ info(response.toString())
+ headers should contain(`Access-Control-Allow-Origin`(HttpOriginRange.*))
+ headers should contain(`Access-Control-Allow-Methods`(HttpMethods.GET))
+ }
+ }
+
+ it should "respond with the correct CORS headers for the test route" in {
+ Options(s"/api/v1/test") ~> appRoute ~> check {
+ status shouldBe StatusCodes.OK
+ info(response.toString())
+ headers should contain(`Access-Control-Allow-Origin`(HttpOriginRange.*))
+ headers should contain(`Access-Control-Allow-Methods`(HttpMethods.GET, HttpMethods.POST))
+ }
+ }
+}