Skip to content

Commit

Permalink
Merge pull request #147 from hmrc/BDOG-1512-b
Browse files Browse the repository at this point in the history
BAU Introduce setHeader and deprecate replaceHeader and addHeaders
  • Loading branch information
colin-lamed authored Jul 8, 2022
2 parents c4f2709 + 78809aa commit 2ff56ee
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ httpClientV2.post(url"$url").withBody(Json.toJson(payload)).addHeaders(headers).

#### Header manipulation

With `HttpClient`, replacing a header can require providing a customised client implementation (e.g. to replace the user-agent header), or updating the `HeaderCarrier` (e.g. to replace the authorisation header). This can now all be done with the `replaceHeader` on `HttpClientV2` per call. e.g.
With `HttpClient`, replacing a header can require providing a customised client implementation (e.g. to replace the user-agent header), or updating the `HeaderCarrier` (e.g. to replace the authorisation header). This can now all be done with the `setHeader` on `HttpClientV2` per call. e.g.

```scala
httpClientV2.get(url"$url").replaceHeader("User-Agent" -> userAgent).replaceHeader("Authorization" -> authorization).execute[ResponseType]
httpClientV2.get(url"$url").setHeader("User-Agent" -> userAgent).setHeader("Authorization" -> authorization).execute[ResponseType]
```

As well as replacing existing header values, `setHeader` can be used to add new headers too, and in most cases should be used in preference to `addHeaders` where the values are merged with any existing ones (e.g. from HeaderCarrier).

#### Using proxy

With `HttpClient`, to use a proxy requires creating a new instance of HttpClient to mix in `WSProxy` and configure. With `HttpClientV2` this can be done with the same client, calling `withProxy` per call. e.g.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ trait RequestBuilder {

// support functions

/** 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

@deprecated("Use setHeader to add or replace, or use transform(_.addHttpHeaders) to append header values to existing", "14.5.0")
def addHeaders(headers: (String, String)*): RequestBuilder

def withProxy: RequestBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,18 @@ 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 replaceHeader(header: (String, String)): RequestBuilderImpl =
transform(replaceHeaderOnRequest(_, header))
setHeader(header)

override def addHeaders(headers: (String, String)*): RequestBuilderImpl =
transform(_.addHttpHeaders(headers: _*))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ class HttpClientV2Spec
httpClientV2
.put(url"$wireMockUrl/")
.transform(_.withBody(Json.toJson(ReqDomain("req"))))
.replaceHeader("User-Agent" -> "ua2")
.setHeader("User-Agent" -> "ua2")
.execute[ResDomain]
}

Expand All @@ -547,7 +547,7 @@ class HttpClientV2Spec
httpClientV2
.put(url"$wireMockUrl/")
.withBody(Json.toJson(ReqDomain("req")))
.replaceHeader("User-Agent" -> "ua2")
.setHeader("User-Agent" -> "ua2")
.execute[ResDomain]

res.futureValue shouldBe ResDomain("res")
Expand Down

0 comments on commit 2ff56ee

Please sign in to comment.