我有一个喷涂端点,它接受像这样的MultipartFormData:
trait ApiRouting extends Routing with ResultService with FormDataUnmarshallers {
override def route: Route =
path("test") {
post {
entity(as[MultipartFormData]) { formData =>
complete(handleRequest(formData))
}
}
}
}
当我通过邮递员发帖时,此功能正常。但是,我正在尝试编写一个测试此端点的规范并得到此错误:
java.lang.ClassCastException:spray.http.HttpEntity $ Empty $不能 cast to spray.http.HttpEntity $ NonEmpty
这就是我所拥有的:
trait Context extends Scope with ApiRouting {}
"check post request" should {
"return response data for post request" in new Context {
val file = new File("test")
val httpEntity = HttpEntity(MediaTypes.`multipart/form-data`, HttpData(file)).asInstanceOf[HttpEntity.NonEmpty]
val formFile = FormFile("file", httpEntity)
val mfd = MultipartFormData(Seq(BodyPart(formFile, "file")))
Post("/test", mfd) ~> route ~> check {
status must_== StatusCodes.OK
contentType must_== `multipart/form-data`
}
}
}
有关如何测试喷涂多部件表单数据的任何想法?
答案 0 :(得分:2)
它正在发生,因为您正在将零长度文件传递到HttpData
。尝试引用一个真实的文件。
此外,您可以将File
直接传递给BodyPart
。它看起来像:
Post(Uri("/test"),
MultipartFormData(
Seq(BodyPart(file, "file", ContentType(MediaTypes.`application/xml`)))
)
)