diff --git a/akka-http-core/src/main/scala/akka/http/model/HttpMessage.scala b/akka-http-core/src/main/scala/akka/http/model/HttpMessage.scala index 8988c401ee..daa097dde4 100644 --- a/akka-http-core/src/main/scala/akka/http/model/HttpMessage.scala +++ b/akka-http-core/src/main/scala/akka/http/model/HttpMessage.scala @@ -31,7 +31,7 @@ sealed trait HttpMessage extends japi.HttpMessage { def protocol: HttpProtocol /** Returns a copy of this message with the list of headers set to the given ones. */ - def withHeaders(headers: HttpHeader*): Self = withHeaders(immutable.Seq(headers: _*)) + def withHeaders(headers: HttpHeader*): Self = withHeaders(headers.toList) /** Returns a copy of this message with the list of headers set to the given ones. */ def withHeaders(headers: immutable.Seq[HttpHeader]): Self @@ -40,9 +40,16 @@ sealed trait HttpMessage extends japi.HttpMessage { * Returns a new message that contains all of the given default headers which didn't already * exist (by case-insensitive header name) in this message. */ - def withDefaultHeaders(defaultHeaders: immutable.Seq[HttpHeader]) = + def withDefaultHeaders(defaultHeaders: HttpHeader*): Self = withDefaultHeaders(defaultHeaders.toList) + + /** + * Returns a new message that contains all of the given default headers which didn't already + * exist (by case-insensitive header name) in this message. + */ + def withDefaultHeaders(defaultHeaders: immutable.Seq[HttpHeader]): Self = withHeaders { - defaultHeaders.foldLeft(headers) { (acc, h) ⇒ if (acc.exists(_ is h.lowercaseName)) acc else acc :+ h } + if (headers.isEmpty) defaultHeaders + else defaultHeaders.foldLeft(headers) { (acc, h) ⇒ if (headers.exists(_ is h.lowercaseName)) acc else h +: acc } } /** Returns a copy of this message with the entity set to the given one. */ diff --git a/akka-http-tests/src/test/scala/akka/http/server/directives/RespondWithDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/server/directives/RespondWithDirectivesSpec.scala index 711a1497a1..d8f0c24e35 100644 --- a/akka-http-tests/src/test/scala/akka/http/server/directives/RespondWithDirectivesSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/server/directives/RespondWithDirectivesSpec.scala @@ -55,7 +55,7 @@ class RespondWithDirectivesSpec extends RoutingSpec { "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 { + "not change a response if the header already existed" in { Get() ~> route(existingHeader) ~> check { response shouldEqual HttpResponse(headers = existingHeader :: Nil) } } } @@ -69,9 +69,9 @@ class RespondWithDirectivesSpec extends RoutingSpec { "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 { + "not update an existing header" in { Get() ~> route(existingHeader) ~> check { - response shouldEqual HttpResponse(headers = existingHeader :: customHeader2 :: Nil) + response shouldEqual HttpResponse(headers = List(customHeader2, existingHeader)) } } } diff --git a/akka-http/src/main/scala/akka/http/server/directives/RespondWithDirectives.scala b/akka-http/src/main/scala/akka/http/server/directives/RespondWithDirectives.scala index fa1b400db1..e0242391b5 100644 --- a/akka-http/src/main/scala/akka/http/server/directives/RespondWithDirectives.scala +++ b/akka-http/src/main/scala/akka/http/server/directives/RespondWithDirectives.scala @@ -28,7 +28,7 @@ trait RespondWithDirectives { * Unconditionally adds the given response headers to all HTTP responses of its inner Route. */ def respondWithHeaders(responseHeaders: HttpHeader*): Directive0 = - respondWithHeaders(responseHeaders.toVector) + respondWithHeaders(responseHeaders.toList) /** * Unconditionally adds the given response headers to all HTTP responses of its inner Route. @@ -41,7 +41,7 @@ trait RespondWithDirectives { * if a header already exists it is not added again. */ def respondWithDefaultHeaders(responseHeaders: HttpHeader*): Directive0 = - respondWithDefaultHeaders(responseHeaders.toVector) + respondWithDefaultHeaders(responseHeaders.toList) /* Adds the given response headers to all HTTP responses of its inner Route, * if a header already exists it is not added again.