Акка http скачать маршаллинг в акка http testkit - PullRequest
0 голосов
/ 16 марта 2020

Я пытаюсь разархивировать загрузку CSV в моем тесте akka http:

val bseq: immutable.Seq[ByteString] = Await.result(response.entity.dataBytes.runWith(Sink.seq), 20 seconds)
        val str = bseq.map(_.utf8String).mkString
        logger.debug(s"res:${str}")

, но я получаю:

akka.http.scaladsl.marshalling.NoStrictlyCompatibleElementMarshallingAvailableException: None of the available marshallings (List(WithFixedContentType(application/octet-stream,<function0>))) directly match the ContentType requested by the top-level streamed entity (text/csv; charset=UTF-8). Please provide an implicit `Marshaller[akka.util.ByteString, HttpEntity]` that can render akka.util.ByteString as [text/csv; charset=UTF-8]
    at akka.http.scaladsl.marshalling.LowPriorityToResponseMarshallerImplicits$$anonfun$fromEntityStreamingSupportAndByteStringSourceMarshaller$1$$anonfun$apply$5$$anonfun$4$$anonfun$6$$anonfun$apply$7.apply(PredefinedToResponseMarshallers.scala:117)

Как я могу разархивировать файл CSV в моем тесте случай

1 Ответ

0 голосов
/ 16 марта 2020

Проблема была на стороне маршрутов. В частности, csvMarshaller отсутствовал.

            import kantan.csv._
            import kantan.csv.ops._

            val src = StreamConverters
              .asOutputStream()
              .mapMaterializedValue { os =>
                Future {
                  try {
                    val ps = List(Person(0, "Nicolas", 38), Person(1, "Kazuma", 1), Person(2, "John", 18))

                    implicit val personEncoder: RowEncoder[Person] = RowEncoder.caseEncoder(0, 2, 1)(Person.unapply)

                    val writer = os.asCsvWriter[Person](rfc.withHeader("Column 1", "Column 2", "Column 3"))
                    ps.foreach { p =>
                      writer.write(p)
                    }
                    writer.close()
                  } catch {
                    case e: Throwable => logger.error("failed", e)
                  }
                }
              }
  implicit val csvStreaming: CsvEntityStreamingSupport = EntityStreamingSupport.csv()
// the missing csvMarshaller causes a runtime exception
            implicit val csvMarshaller: ToEntityMarshaller[ByteString] =
              Marshaller.withFixedContentType(ContentTypes.`text/csv(UTF-8)`) { bytes =>
                HttpEntity(ContentTypes.`text/csv(UTF-8)`, bytes)
              }
  complete(src)
...