+htp #15932 integrate SchemeDirectives from spray codebase
This commit is contained in:
parent
49f2e8bcf5
commit
0966bf6233
3 changed files with 72 additions and 2 deletions
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
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")) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@ trait Directives extends RouteConcatenation
|
|||
//with RangeDirectives
|
||||
//with RespondWithDirectives
|
||||
with RouteDirectives
|
||||
//with SchemeDirectives
|
||||
with SchemeDirectives
|
||||
//with SecurityDirectives
|
||||
|
||||
object Directives extends Directives
|
||||
object Directives extends Directives
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue