From 78809aa4a5208f7656fd33789198e89614065742 Mon Sep 17 00:00:00 2001 From: colin-lamed <9568290+colin-lamed@users.noreply.github.com> Date: Thu, 7 Jul 2022 10:48:20 +0100 Subject: [PATCH] BDOG-1512-b setHeader supports varargs --- .../scala/uk/gov/hmrc/http/client/HttpClientV2.scala | 6 ++++-- .../uk/gov/hmrc/http/client/HttpClientV2Impl.scala | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/HttpClientV2.scala b/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/HttpClientV2.scala index 98e7a99c..df232c6d 100644 --- a/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/HttpClientV2.scala +++ b/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/HttpClientV2.scala @@ -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 diff --git a/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/HttpClientV2Impl.scala b/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/HttpClientV2Impl.scala index a466bfc8..d7fa2097 100644 --- a/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/HttpClientV2Impl.scala +++ b/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/HttpClientV2Impl.scala @@ -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)