From 5ec270aa98b806f32338fa25357abdf45dd0625b Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Wed, 22 Aug 2018 12:51:36 -0700 Subject: Trait-based initialization and other utilities Adds the concept of a 'platform', a centralized place in which environment-specific information will be managed, and provides common initialization logic for most "standard" apps. As part of the common initialization, other parts of core have also been reworked: - HTTP-related unmarshallers and path matchers have been factored out from core.json to a new core.rest.directives package (core.json extends those unmarshallers and matchers for backwards compatibility) - CORS handling has also been moved to a dedicated utility trait - Some custom headers have been moved from raw headers to typed ones in core.rest.headers - The concept of a "reporter" has been introduced. A reporter is a context-aware combination of tracing and logging. It is intended to issue diagnostic messages that can be traced across service boundaries. Closes #192 Closes #195 --- .../scala/xyz/driver/core/rest/DriverAppTest.scala | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'src/test') diff --git a/src/test/scala/xyz/driver/core/rest/DriverAppTest.scala b/src/test/scala/xyz/driver/core/rest/DriverAppTest.scala index 118024a..324c8d8 100644 --- a/src/test/scala/xyz/driver/core/rest/DriverAppTest.scala +++ b/src/test/scala/xyz/driver/core/rest/DriverAppTest.scala @@ -12,16 +12,16 @@ class DriverAppTest extends AsyncFlatSpec with ScalatestRouteTest with Matchers val config = ConfigFactory.parseString(""" |application { | cors { - | allowedMethods: ["GET", "PUT", "POST", "PATCH", "DELETE", "OPTIONS"] - | allowedOrigins: [{scheme: https, hostSuffix: example.com}] + | allowedOrigins: ["example.com"] | } |} """.stripMargin).withFallback(ConfigFactory.load) + val origin = Origin(HttpOrigin("https", Host("example.com"))) val allowedOrigins = Set(HttpOrigin("https", Host("example.com"))) val allowedMethods: collection.immutable.Seq[HttpMethod] = { import akka.http.scaladsl.model.HttpMethods._ - collection.immutable.Seq(GET, PUT, POST, PATCH, DELETE, OPTIONS) + collection.immutable.Seq(GET, PUT, POST, PATCH, DELETE, OPTIONS, TRACE) } import scala.reflect.runtime.universe.typeOf @@ -37,7 +37,7 @@ class DriverAppTest extends AsyncFlatSpec with ScalatestRouteTest with Matchers it should "respond with the correct CORS headers for the swagger OPTIONS route" in { val route = new TestApp(get(complete(StatusCodes.OK))) - Options(s"/api-docs/swagger.json") ~> route.appRoute ~> check { + Options(s"/api-docs/swagger.json").withHeaders(origin) ~> route.appRoute ~> check { status shouldBe StatusCodes.OK headers should contain(`Access-Control-Allow-Origin`(HttpOriginRange(allowedOrigins.toSeq: _*))) header[`Access-Control-Allow-Methods`].get.methods should contain theSameElementsAs allowedMethods @@ -46,19 +46,17 @@ class DriverAppTest extends AsyncFlatSpec with ScalatestRouteTest with Matchers it should "respond with the correct CORS headers for the test route" in { val route = new TestApp(get(complete(StatusCodes.OK))) - Get(s"/api/v1/test") ~> route.appRoute ~> check { + Get(s"/api/v1/test").withHeaders(origin) ~> route.appRoute ~> check { status shouldBe StatusCodes.OK headers should contain(`Access-Control-Allow-Origin`(HttpOriginRange(allowedOrigins.toSeq: _*))) - header[`Access-Control-Allow-Methods`].get.methods should contain theSameElementsAs allowedMethods } } it should "respond with the correct CORS headers for a concatenated route" in { val route = new TestApp(get(complete(StatusCodes.OK)) ~ post(complete(StatusCodes.OK))) - Post(s"/api/v1/test") ~> route.appRoute ~> check { + Post(s"/api/v1/test").withHeaders(origin) ~> route.appRoute ~> check { status shouldBe StatusCodes.OK headers should contain(`Access-Control-Allow-Origin`(HttpOriginRange(allowedOrigins.toSeq: _*))) - header[`Access-Control-Allow-Methods`].get.methods should contain theSameElementsAs allowedMethods } } @@ -68,7 +66,6 @@ class DriverAppTest extends AsyncFlatSpec with ScalatestRouteTest with Matchers .withHeaders(Origin(HttpOrigin("https", Host("foo.example.com")))) ~> route.appRoute ~> check { status shouldBe StatusCodes.OK headers should contain(`Access-Control-Allow-Origin`(HttpOrigin("https", Host("foo.example.com")))) - header[`Access-Control-Allow-Methods`].get.methods should contain theSameElementsAs allowedMethods } } @@ -77,8 +74,7 @@ class DriverAppTest extends AsyncFlatSpec with ScalatestRouteTest with Matchers Get(s"/api/v1/test") .withHeaders(Origin(HttpOrigin("https", Host("invalid.foo.bar.com")))) ~> route.appRoute ~> check { status shouldBe StatusCodes.OK - headers should contain(`Access-Control-Allow-Origin`(HttpOriginRange(allowedOrigins.toSeq: _*))) - header[`Access-Control-Allow-Methods`].get.methods should contain theSameElementsAs allowedMethods + headers should contain(`Access-Control-Allow-Origin`(HttpOriginRange.*)) } } -- cgit v1.2.3