Skip to content

Commit

Permalink
Common: Explicitly set Accept header (close #667)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmath committed Aug 15, 2022
1 parent 4829a90 commit ab0bdcd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ object HttpClient {
def apply[F[_]](implicit ev: HttpClient[F]): HttpClient[F] = ev

private[utils] def getHeaders(authUser: Option[String], authPassword: Option[String]): Headers = {
val contentTypeHeader = Header("content-type", "application/json")
val alwaysIncludedHeaders = List(Header("content-type", "application/json"), Header("accept", "*/*"))
if (authUser.isDefined || authPassword.isDefined)
Headers(Authorization(BasicCredentials(authUser.getOrElse(""), authPassword.getOrElse(""))), contentTypeHeader)
else Headers(contentTypeHeader)
Headers(Authorization(BasicCredentials(authUser.getOrElse(""), authPassword.getOrElse(""))) :: alwaysIncludedHeaders)
else Headers(alwaysIncludedHeaders)
}

implicit def syncHttpClient[F[_]: Sync](implicit http4sClient: Http4sClient[F]): HttpClient[F] =
Expand Down Expand Up @@ -168,7 +168,7 @@ object HttpClient {

def maybePostData(body: Option[String]): HttpRequest =
body
.map(data => request.postData(data).header("content-type", "application/json"))
.map(data => request.postData(data).header("content-type", "application/json").header("accept", "*/*"))
.getOrElse(request)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@ import org.specs2.mock.Mockito

class HttpClientSpec extends Specification with ValidatedMatchers with Mockito {
def is = s2"""
getHeaders returns an Authorization header and a content-type header if authUser or authPassword is defined $e1
getHeaders returns Authorization, accept header and content-type headers if authUser or authPassword is defined $e1
getHeaders does not return an Authorization header if authUser and authPassword are not defined $e2
"""

def e1 = {
val headers = HttpClient.getHeaders(None, Some("2778e1d8-500b-4f9f-a14e-f68b6b4e7b9f"))
val expected =
Headers(Authorization(BasicCredentials("", "2778e1d8-500b-4f9f-a14e-f68b6b4e7b9f")), Header("content-type", "application/json"))
Headers(Authorization(BasicCredentials("", "2778e1d8-500b-4f9f-a14e-f68b6b4e7b9f")),
Header("content-type", "application/json"),
Header("accept", "*/*")
)
headers must beEqualTo(expected)
}

def e2 = {
val headers = HttpClient.getHeaders(None, None)
val expected = Headers(Header("content-type", "application/json"))
val expected = Headers(Header("content-type", "application/json"), Header("accept", "*/*"))
headers must beEqualTo(expected)
}
}

0 comments on commit ab0bdcd

Please sign in to comment.