Skip to content

Commit

Permalink
Merge pull request #3243 from kamilkloch/accept-parameters
Browse files Browse the repository at this point in the history
Add test checking against softwaremill/sttp-model#306
  • Loading branch information
adamw authored Oct 17, 2023
2 parents 0474730 + eed9afa commit c0bbd18
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion project/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Versions {
val circeGenericExtras = "0.14.3"
val circeYaml = "0.14.2"
val sttp = "3.9.0"
val sttpModel = "1.7.2"
val sttpModel = "1.7.4"
val sttpShared = "1.3.16"
val sttpApispec = "0.7.1"
val akkaHttp = "10.2.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ class ServerContentNegotiationTests[F[_], OPTIONS, ROUTE](createServerTest: Crea
testNameSuffix = "not take into account the accept charset header when the body media type doesn't specify one"
)(in => pureResult(in.asRight[Unit])) { (backend, baseUri) =>
basicRequest.post(uri"$baseUri/api/echo").header(HeaderNames.AcceptCharset, "utf8").send(backend).map(_.code shouldBe StatusCode.Ok)
},
testServer(out_json_json_different_parameters, testNameSuffix = "matches content type on accept parameters")(_ =>
pureResult(Organization("sml").asRight[Unit])
) { (backend, baseUri) =>
val r1 = basicRequest
.header(HeaderNames.Accept, "application/json; name=unknown")
.get(uri"$baseUri/content-negotiation/organization-parameters")
.send(backend)
.map(_.body shouldBe Right("{\"name\":\"unknown\"}"))

val r2 = basicRequest
.header(HeaderNames.Accept, "application/json")
.get(uri"$baseUri/content-negotiation/organization-parameters")
.send(backend)
.map(_.body shouldBe Right("{\"name\":\"sml\"}"))

val r3 = basicRequest
.get(uri"$baseUri/content-negotiation/organization-parameters")
.send(backend)
.map(_.body shouldBe Right("{\"name\":\"unknown\"}"))

r1 >> r2 >> r3
}
)
}
24 changes: 21 additions & 3 deletions tests/src/main/scala/sttp/tapir/tests/ContentNegotiation.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package sttp.tapir.tests

import io.circe.generic.auto._
import sttp.model.{HeaderNames, StatusCode}
import sttp.model.{HeaderNames, MediaType, StatusCode}
import sttp.tapir._
import sttp.tapir.Codec.XmlCodec
import sttp.tapir.CodecFormat.TextHtml
import sttp.tapir.json.circe.jsonBody
import sttp.tapir.json.circe.{circeCodec, jsonBody}
import sttp.tapir.tests.data.{Entity, Organization, Person}
import sttp.tapir._

import java.nio.charset.StandardCharsets

Expand All @@ -17,6 +17,10 @@ object ContentNegotiation {
// <name>xxx</name>
private def fromClosedTags(tags: String): Organization = Organization(tags.split(">")(1).split("<").head)

case class JsonCodecFormatOrganizationName() extends CodecFormat {
override val mediaType: MediaType = MediaType.ApplicationJson.copy(otherParameters = Map("name" -> "unknown"))
}

implicit val xmlCodecForOrganization: XmlCodec[Organization] =
Codec.xml(xml => DecodeResult.Value(fromClosedTags(xml)))(o => s"<name>${o.name}-xml</name>")

Expand Down Expand Up @@ -50,6 +54,20 @@ object ContentNegotiation {
)
)

val out_json_json_different_parameters: PublicEndpoint[Unit, Unit, Organization, Any] =
endpoint.get
.in("content-negotiation" / "organization-parameters")
.out(
sttp.tapir.oneOfBody(
jsonBody[Organization].copy(
codec = circeCodec[Organization]
.map(identity[Organization] _)(_.copy(name = "unknown"))
.format(JsonCodecFormatOrganizationName())
),
jsonBody[Organization]
)
)

val out_default_json_or_xml: PublicEndpoint[Unit, Unit, Organization, Any] =
endpoint.get
.in("content-negotiation" / "organization")
Expand Down

0 comments on commit c0bbd18

Please sign in to comment.