+htp #15931 Import RespondWithDirectives from spray

This commit is contained in:
Benjamin Thuillier 2014-10-13 11:44:48 +02:00 committed by Johannes Rudolph
parent 0033e74cae
commit c6f8eb95a1
3 changed files with 132 additions and 1 deletions

View file

@ -0,0 +1,78 @@
/*
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.http.server.directives
import akka.http.model._
import MediaTypes._
import headers._
import StatusCodes._
import akka.http.server._
class RespondWithDirectivesSpec extends RoutingSpec {
"overrideStatusCode" should {
"set the given status on successful responses" in {
Get() ~> {
overrideStatusCode(Created) { completeOk }
} ~> check { response shouldEqual HttpResponse(Created) }
}
"leave rejections unaffected" in {
Get() ~> {
overrideStatusCode(Created) { reject }
} ~> check { rejections shouldEqual Nil }
}
}
val customHeader = RawHeader("custom", "custom")
val customHeader2 = RawHeader("custom2", "custom2")
val existingHeader = RawHeader("custom", "existing")
"respondWithHeader" should {
val customHeader = RawHeader("custom", "custom")
"add the given header to successful responses" in {
Get() ~> {
respondWithHeader(customHeader) { completeOk }
} ~> check { response shouldEqual HttpResponse(headers = customHeader :: Nil) }
}
}
"respondWithHeaders" should {
"add the given headers to successful responses" in {
Get() ~> {
respondWithHeaders(customHeader, customHeader2) { completeOk }
} ~> check { response shouldEqual HttpResponse(headers = customHeader :: customHeader2 :: Nil) }
}
}
"respondWithDefaultHeader" should {
def route(extraHeaders: HttpHeader*) = respondWithDefaultHeader(customHeader) {
respondWithHeaders(extraHeaders: _*) {
completeOk
}
}
"add the given header to a response if the header was missing before" in {
Get() ~> route() ~> check { response shouldEqual HttpResponse(headers = customHeader :: Nil) }
}
"don't change a response if the header already existed" in {
Get() ~> route(existingHeader) ~> check { response shouldEqual HttpResponse(headers = existingHeader :: Nil) }
}
}
"respondWithDefaultHeaders" should {
def route(extraHeaders: HttpHeader*) = respondWithDefaultHeaders(customHeader, customHeader2) {
respondWithHeaders(extraHeaders: _*) {
completeOk
}
}
"add the given headers to a response if the header was missing before" in {
Get() ~> route() ~> check { response shouldEqual HttpResponse(headers = customHeader :: customHeader2 :: Nil) }
}
"don't update an existing header" in {
Get() ~> route(existingHeader) ~> check {
response shouldEqual HttpResponse(headers = existingHeader :: customHeader2 :: Nil)
}
}
}
}

View file

@ -27,7 +27,7 @@ trait Directives extends RouteConcatenation
//with ParameterDirectives
with PathDirectives
//with RangeDirectives
//with RespondWithDirectives
with RespondWithDirectives
with RouteDirectives
with SchemeDirectives
//with SecurityDirectives

View file

@ -0,0 +1,53 @@
package akka.http.server
package directives
import akka.http.model._
import scala.collection.immutable
trait RespondWithDirectives {
import BasicDirectives._
/**
* Overrides the given response status on all HTTP responses of its inner Route.
*/
def overrideStatusCode(responseStatus: StatusCode): Directive0 =
mapResponse(_.copy(status = responseStatus))
/**
* Unconditionally adds the given response header to all HTTP responses of its inner Route.
*/
def respondWithHeader(responseHeader: HttpHeader): Directive0 = respondWithHeaders(responseHeader)
/**
* Adds the given response header to all HTTP responses of its inner Route,
* if the response from the inner Route doesn't already contain a header with the same name.
*/
def respondWithDefaultHeader(responseHeader: HttpHeader): Directive0 = respondWithDefaultHeaders(responseHeader)
/**
* Unconditionally adds the given response headers to all HTTP responses of its inner Route.
*/
def respondWithHeaders(responseHeaders: HttpHeader*): Directive0 =
respondWithHeaders(responseHeaders.toVector)
/**
* Unconditionally adds the given response headers to all HTTP responses of its inner Route.
*/
def respondWithHeaders(responseHeaders: immutable.Seq[HttpHeader]): Directive0 =
mapResponseHeaders(responseHeaders ++ _)
/**
* Adds the given response headers to all HTTP responses of its inner Route,
* if a header already exists it is not added again.
*/
def respondWithDefaultHeaders(responseHeaders: HttpHeader*): Directive0 =
respondWithDefaultHeaders(responseHeaders.toVector)
/* Adds the given response headers to all HTTP responses of its inner Route,
* if a header already exists it is not added again.
*/
def respondWithDefaultHeaders(responseHeaders: immutable.Seq[HttpHeader]): Directive0 =
mapResponse(_.withDefaultHeaders(responseHeaders))
}
object RespondWithDirectives extends RespondWithDirectives