From 0966bf6233838674315d0134058f63974cd5dd9c Mon Sep 17 00:00:00 2001 From: Benjamin Thuillier Date: Fri, 26 Sep 2014 08:52:20 +0200 Subject: [PATCH] +htp #15932 integrate SchemeDirectives from spray codebase --- .../directives/SchemeDirectivesSpec.scala | 43 +++++++++++++++++++ .../scala/akka/http/server/Directives.scala | 4 +- .../server/directives/SchemeDirectives.scala | 27 ++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 akka-http-tests/src/test/scala/akka/http/server/directives/SchemeDirectivesSpec.scala create mode 100644 akka-http/src/main/scala/akka/http/server/directives/SchemeDirectives.scala diff --git a/akka-http-tests/src/test/scala/akka/http/server/directives/SchemeDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/server/directives/SchemeDirectivesSpec.scala new file mode 100644 index 0000000000..30aa251c3f --- /dev/null +++ b/akka-http-tests/src/test/scala/akka/http/server/directives/SchemeDirectivesSpec.scala @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2009-2014 Typesafe Inc. + */ + +package akka.http.server +package directives + +import akka.http.model.StatusCodes._ + +class SchemeDirectivesSpec extends RoutingSpec { + "the schemeName directive" should { + "extract the Uri scheme" in { + Put("http://localhost/", "Hello") ~> schemeName { echoComplete } ~> check { responseAs[String] shouldEqual "http" } + } + } + + """the scheme("http") directive""" should { + "let requests with an http Uri scheme pass" in { + Put("http://localhost/", "Hello") ~> scheme("http") { completeOk } ~> check { response shouldEqual Ok } + } + "reject requests with an https Uri scheme" in { + Get("https://localhost/") ~> scheme("http") { completeOk } ~> check { rejections shouldEqual List(SchemeRejection("http")) } + } + "cancel SchemeRejection if other scheme passed" in { + val route = + scheme("https") { completeOk } ~ + scheme("http") { reject } + + Put("http://localhost/", "Hello") ~> route ~> check { + rejections should be(Nil) + } + } + } + + """the scheme("https") directive""" should { + "let requests with an https Uri scheme pass" in { + Put("https://localhost/", "Hello") ~> scheme("https") { completeOk } ~> check { response shouldEqual Ok } + } + "reject requests with an http Uri scheme" in { + Get("http://localhost/") ~> scheme("https") { completeOk } ~> check { rejections shouldEqual List(SchemeRejection("https")) } + } + } +} diff --git a/akka-http/src/main/scala/akka/http/server/Directives.scala b/akka-http/src/main/scala/akka/http/server/Directives.scala index dbd4d0a3e2..3f5eaa0842 100644 --- a/akka-http/src/main/scala/akka/http/server/Directives.scala +++ b/akka-http/src/main/scala/akka/http/server/Directives.scala @@ -29,7 +29,7 @@ trait Directives extends RouteConcatenation //with RangeDirectives //with RespondWithDirectives with RouteDirectives -//with SchemeDirectives + with SchemeDirectives //with SecurityDirectives -object Directives extends Directives \ No newline at end of file +object Directives extends Directives diff --git a/akka-http/src/main/scala/akka/http/server/directives/SchemeDirectives.scala b/akka-http/src/main/scala/akka/http/server/directives/SchemeDirectives.scala new file mode 100644 index 0000000000..2616fb137f --- /dev/null +++ b/akka-http/src/main/scala/akka/http/server/directives/SchemeDirectives.scala @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2009-2014 Typesafe Inc. + */ + +package akka.http.server +package directives + +trait SchemeDirectives { + import BasicDirectives._ + + /** + * Extracts the Uri scheme from the request. + */ + def schemeName: Directive1[String] = SchemeDirectives._schemeName + + /** + * Rejects all requests whose Uri scheme does not match the given one. + */ + def scheme(name: String): Directive0 = + schemeName.require(_ == name, SchemeRejection(name)) & cancelRejections(classOf[SchemeRejection]) +} + +object SchemeDirectives extends SchemeDirectives { + import BasicDirectives._ + + private val _schemeName: Directive1[String] = extract(_.request.uri.scheme) +}