Skip to content

Commit

Permalink
BDOG-1512-b setHeader supports varargs
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lamed committed Jul 7, 2022
1 parent a2eb67c commit 78809aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ trait RequestBuilder {

// support functions

/** Adds the header. If the header has already been defined (e.g. from HeaderCarrier), it will be replaced. */
def setHeader(header: (String, String)): RequestBuilder
/** Adds the header. If the header has already been defined (e.g. from HeaderCarrier), it will be replaced.
* It does not affect headers not mentioned.
*/
def setHeader(header: (String, String)*): RequestBuilder

@deprecated("Use setHeader", "14.5.0")
def replaceHeader(header: (String, String)): RequestBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ final class RequestBuilderImpl(

// -- Transform helpers --

private def replaceHeaderOnRequest(request: WSRequest, header: (String, String)): WSRequest = {
private def replaceHeaderOnRequest(request: WSRequest, headers: (String, String)*): WSRequest = {
def denormalise(hdrs: Map[String, Seq[String]]): Seq[(String, String)] =
hdrs.toList.flatMap { case (k, vs) => vs.map(k -> _) }
val hdrsWithoutKey = request.headers.filterKeys(!_.equalsIgnoreCase(header._1)).toMap // replace existing header
request.withHttpHeaders(denormalise(hdrsWithoutKey) :+ header : _*)
val hdrsWithoutKey = request.headers.filterKeys(k => !headers.map(_._1.toLowerCase).contains(k.toLowerCase)).toMap // replace existing header
request.withHttpHeaders(denormalise(hdrsWithoutKey) ++ headers : _*)
}

override def setHeader(header: (String, String)): RequestBuilderImpl =
transform(replaceHeaderOnRequest(_, header))
override def setHeader(header: (String, String)*): RequestBuilderImpl =
transform(replaceHeaderOnRequest(_, header: _*))

override def replaceHeader(header: (String, String)): RequestBuilderImpl =
setHeader(header)
Expand Down

0 comments on commit 78809aa

Please sign in to comment.