Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix accept header handling #2391

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,29 @@ object MediaTypeCodec {
def codecsFor(mediaType: Option[String], content: Chunk[BodyCodec[_]]): Map[String, MediaTypeCodec[_]] = {
mediaType match {
case Some(mt) =>
allByType.get(mt) match {
case Some(codec) => Map(mt -> codec.create(content))
case None =>
throw HttpCodecError.UnsupportedContentType(
s"""The Accept header mime type $mt is currently not supported.
|Supported mime types are: ${allByType.keys.mkString(", ")}""".stripMargin,
)
if (mt.contains('*')) {
MediaType.parseCustomMediaType(mt) match {
case Some(parsed) if parsed.mainType == "*" && parsed.subType == "*" =>
allByType.map { case (k, v) => k -> v.create(content) }
case Some(parsed) if parsed.subType == "*" =>
allByType.filter { case (k, _) => k.startsWith(parsed.mainType + "/") }.map { case (k, v) =>
k -> v.create(content)
}
case _ =>
throw HttpCodecError.UnsupportedContentType(
s"""The Accept header mime type $mt is currently not supported.
|Supported mime types are: ${allByType.keys.mkString(", ")}""".stripMargin,
)
}
} else {
allByType.get(mt) match {
case Some(codec) => Map(mt -> codec.create(content))
case None =>
throw HttpCodecError.UnsupportedContentType(
s"""The Accept header mime type $mt is currently not supported.
|Supported mime types are: ${allByType.keys.mkString(", ")}""".stripMargin,
)
}
}
case None => allByType.map { case (k, v) => k -> v.create(content) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ object EndpointSpec extends ZIOHttpSpec {

for {
response <- routes.toHttpApp.runZIO(
Request.get(URL.decode(s"/posts?id=$id").toOption.get),
Request
.get(URL.decode(s"/posts?id=$id").toOption.get)
.addHeader(Header.Accept(MediaType.text.`plain`)),
)
contentType = response.header(Header.ContentType)
} yield assertTrue(extractStatus(response).code == 200) &&
Expand Down