Skip to content

Commit

Permalink
Merge pull request #298 from innFactory/feat/ignoreCharset
Browse files Browse the repository at this point in the history
feat: ignoring charset in content-type for encoding and decoding
  • Loading branch information
MoeQuadrat authored Jun 24, 2024
2 parents 72d5efd + e058806 commit 246318d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ case class CodecDecider(readerConfig: ReaderConfig) {
def encoder(
contentType: Seq[String]
): CachedSchemaCompiler[codecs.BlobEncoder] =
contentType match {
contentType.map(_.split(";").head) match {
case Seq(MimeTypes.JSON) => jsonEncoder
case Seq(MimeTypes.XML) => Xml.encoders
case _ =>
Expand Down Expand Up @@ -129,7 +129,7 @@ case class CodecDecider(readerConfig: ReaderConfig) {
def decoder(
contentType: Seq[String]
): CachedSchemaCompiler[BlobDecoder] =
contentType match {
contentType.map(_.split(";").head) match {
case Seq(MimeTypes.JSON) => jsonDecoder
case Seq(MimeTypes.XML) => Xml.decoders
case _ =>
Expand Down
7 changes: 5 additions & 2 deletions smithy4playTest/app/controller/XmlController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ class XmlController @Inject() (implicit

override def xmlTestWithInputAndOutput(
xmlTest: String,
body: XmlTestInputBody
body: XmlTestInputBody,
contentType: Option[String]
): ContextRoute[XmlTestWithInputAndOutputOutput] =
Kleisli { _ =>
EitherT(
Future(
XmlTestWithInputAndOutputOutput(
XmlTestOutput(body.serverzeit, body.requiredTest + xmlTest, body.requiredInt.map(i => i * i))
XmlTestOutput(body.serverzeit, body.requiredTest + xmlTest, body.requiredInt.map(i => i * i)),
contentType
)
.asRight[ContextRouteError]
)
)
}

}
67 changes: 57 additions & 10 deletions smithy4playTest/test/XmlControllerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import models.NodeImplicits.NodeEnhancer
import models.TestBase
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{Json, OFormat}
import play.api.libs.json.{ Json, OFormat }
import play.api.test.FakeRequest
import play.api.test.Helpers._
import smithy4s.http.CaseInsensitive
import testDefinitions.test.{XmlControllerDefGen, XmlTestInputBody, XmlTestOutput}
import testDefinitions.test.{ XmlControllerDefGen, XmlTestInputBody, XmlTestOutput }

import scala.concurrent.ExecutionContext.Implicits.global

Expand All @@ -33,6 +33,53 @@ class XmlControllerTest extends TestBase {
res.body.body.requiredTestStringConcat mustBe "ThisGetsConcat"
}

"route to xml with charset in header endpoint with smithy client" in {
val res = genericClient
.xmlTestWithInputAndOutput(
"Concat",
XmlTestInputBody("05.02.2024", "ThisGets", Some(10)),
Some("application/xml; charset=utf-8")
)
.awaitRight

res.body.body.requiredIntSquared mustBe Some(100)
res.body.body.requiredTestStringConcat mustBe "ThisGetsConcat"
res.headers.get(CaseInsensitive("content-type")) mustBe Some(List("application/xml; charset=utf-8"))
}

"route to xml with charset in header with external client" in {
val concatVal1 = "ConcatThis"
val concatVal2 = "Test2"
val squareTest = 3
val xml =
<XmlTestInputBody serverzeit="05.02.2024">
<requiredTest>{concatVal1}</requiredTest>
<requiredInt>{squareTest}</requiredInt>
</XmlTestInputBody>
val request = route(
app,
FakeRequest("POST", s"/xml/$concatVal2")
.withHeaders(("content-type", "application/xml; charset=utf-8"))
.withXmlBody(
xml
)
).get
status(request) mustBe 200

val result = scala.xml.XML.loadString(contentAsString(request))
val resContentType = contentType(request)
val resCharset = charset(request)


result.normalize mustBe <XmlTestOutput serverzeit="05.02.2024">
<requiredTestStringConcat>
{concatVal1 + concatVal2}</requiredTestStringConcat>
<requiredIntSquared>
{squareTest * squareTest}</requiredIntSquared>
</XmlTestOutput>.normalize
resContentType.map(_ + "; charset=" + resCharset.getOrElse("")) mustBe Some("application/xml; charset=utf-8")
}

"route to xml test endpoint with external client" in {
val concatVal1 = "ConcatThis"
val concatVal2 = "Test2"
Expand Down Expand Up @@ -115,13 +162,13 @@ class XmlControllerTest extends TestBase {
}

"route to test endpoint with external client and json protocol" in {
implicit val formatI: OFormat[XmlTestInputBody] = Json.format[XmlTestInputBody]
implicit val formatO: OFormat[XmlTestOutput] = Json.format[XmlTestOutput]
val concatVal2 = "Test2"
val concatVal1 = "ConcatThis"
val squareTest = Some(15)
val date = "05.02.2024"
val request = route(
implicit val formatI: OFormat[XmlTestInputBody] = Json.format[XmlTestInputBody]
implicit val formatO: OFormat[XmlTestOutput] = Json.format[XmlTestOutput]
val concatVal2 = "Test2"
val concatVal1 = "ConcatThis"
val squareTest = Some(15)
val date = "05.02.2024"
val request = route(
app,
FakeRequest("POST", s"/xml/$concatVal2")
.withHeaders(("content-type", "application/json"))
Expand All @@ -130,7 +177,7 @@ class XmlControllerTest extends TestBase {
)
).get
status(request) mustBe 200
val result = contentAsJson(request).as[XmlTestOutput]
val result = contentAsJson(request).as[XmlTestOutput]
result.requiredTestStringConcat mustBe concatVal1 + concatVal2
result.requiredIntSquared mustBe squareTest.map(s => s * s)
result.serverzeit mustBe date
Expand Down
4 changes: 4 additions & 0 deletions smithy4playTest/testSpecs/XmlController.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ service XmlControllerDef {
operation XmlTestWithInputAndOutput {
input: XmlTestInput
output := {
@httpHeader("content-type")
contentType: String
@required
@httpPayload
body: XmlTestOutput
}
}

structure XmlTestInput {
@httpHeader("content-type")
contentType: String
@httpLabel
@required
xmlTest: String
Expand Down

0 comments on commit 246318d

Please sign in to comment.