Spray 测试用例不会 属性 使用 HTTPEntity 解组 UTF-8 编码 json
Spray test case doesn't property unmarshal UTF-8 encoded json using HTTPEntity
我有一段 API 使用 Spray 框架编写的 Scalatest,如下所示:
"correctly deserializes multi-lang title metadata" in {
implicit def json4sFormats: org.json4s.Formats = ModelJsonHelper.jsonFormats
val v2MultiLangTitle = getStringFromResource("/json_samples/cmp.asset.v2.AssetWriteMultiMetadata.json")
//Deserialize
val v2AssetEither = HttpEntity(`application/cmp.ela.assetWrite.v2+json`, v2MultiLangTitle).as[Asset]
v2AssetEither.isRight shouldEqual true
v2AssetEither.right.map(asset => {
asset.assetMetadata.getOrElse(List()).size shouldEqual(3)
asset.assetMetadata.getOrElse(List())(1).language shouldEqual("es")
asset.assetMetadata.getOrElse(List())(1).data.title shouldEqual(Some("Encabezado prueba de AFP"))
asset.assetMetadata.getOrElse(List())(2).language shouldEqual("tlh")
asset.assetMetadata.getOrElse(List())(2).data.title shouldEqual(Some("Daj jaw AFP"))
asset.assetMetadata.getOrElse(List())(0).language shouldEqual("de")
asset.assetMetadata.getOrElse(List())(0).data.title shouldEqual(Some("Test AFP Überschrift"))
})
}
def getStringFromResource(input: String): String = {
Source.fromInputStream(this.getClass.getResourceAsStream(input))(Codec.UTF8).getLines.mkString("")
}
正在处理的json如下
{
"assetMetadata" : [
{
"title": "Test AFP Überschrift",
"language": "de"
},
{
"title": "Encabezado prueba de AFP",
"language": "es"
},
{
"title": "Daj jaw AFP",
"language": "tlh"
}
]
}
失败发生在德语变音符号上:
[info] - correctly deserializes multi-lang title metadata *** FAILED ***
[info] Some("Test AFP �berschrift") did not equal "Test AFP Überschrift" (V2AssetWriteMarshallerSpec.scala:367)
表面之下,在解包过程中使用了一些 json4s。但是,如果我直接使用 json4s 解析器,则会正确处理德语字符:
import scala.io.Source
import java.io.FileInputStream
val v = Source.fromInputStream(new FileInputStream("/path/to/project/src/test/resources/json_samples/cmp.asset.v2.AssetWriteMultiMetadata.json")).getLines.mkString("")
import org.json4s._
import org.json4s.native.JsonMethods._
val obj = parse(v)
(obj \ "assetMetadata")
给我结果:
res0: org.json4s.JValue = JArray(List(JObject(List((title,JString(Test AFP Überschrift)), ....
我正在使用 Spray 1.3.3 和 json4s 3.2.10。 HttpEntity
中的类型是自定义类型,我尝试添加 ( "charset" -> "UTF-8" )
作为参数,如下所示:
val `application/cmp.ela.assetWrite.v2+json` = register(
MediaType.custom(
mainType = "application",
subType = "cmp.ela.assetWrite.v2+json",
compressible = true,
binary = false,
parameters = Map[String,String]( "charset" -> "UTF-8" )
)
)
..但测试仍然因无效字符而失败。如何让 Spray 正确解组其中包含国际字符的字符串?
我在 Scala IRC 频道中帮助您找到了解决方案。我找到了这个 link,剩下的由你完成!
https://github.com/spray/spray/blob/master/spray-http/src/main/scala/spray/http/MediaType.scala#L168
固定测试:
//Deserialize
val v2AssetEither = HttpEntity(`application/vnd.dsa.assetWrite.v2+json` withCharset(HttpCharsets.`UTF-8`), v2MultiLangTitle).as[Asset]
v2AssetEither.isRight shouldEqual true
正在添加密钥
withCharset(HttpCharsets.`UTF-8`)
我有一段 API 使用 Spray 框架编写的 Scalatest,如下所示:
"correctly deserializes multi-lang title metadata" in {
implicit def json4sFormats: org.json4s.Formats = ModelJsonHelper.jsonFormats
val v2MultiLangTitle = getStringFromResource("/json_samples/cmp.asset.v2.AssetWriteMultiMetadata.json")
//Deserialize
val v2AssetEither = HttpEntity(`application/cmp.ela.assetWrite.v2+json`, v2MultiLangTitle).as[Asset]
v2AssetEither.isRight shouldEqual true
v2AssetEither.right.map(asset => {
asset.assetMetadata.getOrElse(List()).size shouldEqual(3)
asset.assetMetadata.getOrElse(List())(1).language shouldEqual("es")
asset.assetMetadata.getOrElse(List())(1).data.title shouldEqual(Some("Encabezado prueba de AFP"))
asset.assetMetadata.getOrElse(List())(2).language shouldEqual("tlh")
asset.assetMetadata.getOrElse(List())(2).data.title shouldEqual(Some("Daj jaw AFP"))
asset.assetMetadata.getOrElse(List())(0).language shouldEqual("de")
asset.assetMetadata.getOrElse(List())(0).data.title shouldEqual(Some("Test AFP Überschrift"))
})
}
def getStringFromResource(input: String): String = {
Source.fromInputStream(this.getClass.getResourceAsStream(input))(Codec.UTF8).getLines.mkString("")
}
正在处理的json如下
{
"assetMetadata" : [
{
"title": "Test AFP Überschrift",
"language": "de"
},
{
"title": "Encabezado prueba de AFP",
"language": "es"
},
{
"title": "Daj jaw AFP",
"language": "tlh"
}
]
}
失败发生在德语变音符号上:
[info] - correctly deserializes multi-lang title metadata *** FAILED ***
[info] Some("Test AFP �berschrift") did not equal "Test AFP Überschrift" (V2AssetWriteMarshallerSpec.scala:367)
表面之下,在解包过程中使用了一些 json4s。但是,如果我直接使用 json4s 解析器,则会正确处理德语字符:
import scala.io.Source
import java.io.FileInputStream
val v = Source.fromInputStream(new FileInputStream("/path/to/project/src/test/resources/json_samples/cmp.asset.v2.AssetWriteMultiMetadata.json")).getLines.mkString("")
import org.json4s._
import org.json4s.native.JsonMethods._
val obj = parse(v)
(obj \ "assetMetadata")
给我结果:
res0: org.json4s.JValue = JArray(List(JObject(List((title,JString(Test AFP Überschrift)), ....
我正在使用 Spray 1.3.3 和 json4s 3.2.10。 HttpEntity
中的类型是自定义类型,我尝试添加 ( "charset" -> "UTF-8" )
作为参数,如下所示:
val `application/cmp.ela.assetWrite.v2+json` = register(
MediaType.custom(
mainType = "application",
subType = "cmp.ela.assetWrite.v2+json",
compressible = true,
binary = false,
parameters = Map[String,String]( "charset" -> "UTF-8" )
)
)
..但测试仍然因无效字符而失败。如何让 Spray 正确解组其中包含国际字符的字符串?
我在 Scala IRC 频道中帮助您找到了解决方案。我找到了这个 link,剩下的由你完成!
https://github.com/spray/spray/blob/master/spray-http/src/main/scala/spray/http/MediaType.scala#L168
固定测试:
//Deserialize
val v2AssetEither = HttpEntity(`application/vnd.dsa.assetWrite.v2+json` withCharset(HttpCharsets.`UTF-8`), v2MultiLangTitle).as[Asset]
v2AssetEither.isRight shouldEqual true
正在添加密钥
withCharset(HttpCharsets.`UTF-8`)