Akka Rota TestKit não poderia desorganizar resposta como uma Seqüência de caracteres

0

Pergunta

Eu tenho o seguinte teste:

 "fail if date is wrongly formatted" in {
    val endpoint = s"/api/prof?date=wrongdate"
    Get(endpoint) ~> check {
      status shouldBe StatusCodes.BadRequest
      val resp = responseAs[String]
      resp shouldBe "could not be parsed"
    }
 }

No entanto, o teste falhar com o seguinte:

Could not unmarshal response to type 'java.lang.String' for `responseAs` assertion: akka.http.scaladsl.unmarshalling.Unmarshaller$UnsupportedContentTypeException: Unsupported Content-Type [Some(text/plain; charset=UTF-8)], supported: application/json

Response was: HttpResponse(400 Bad Request,List(),HttpEntity.Strict(text/plain; charset=UTF-8,106 bytes total),HttpProtocol(HTTP/1.1))

Como posso obter a resposta do corpo como uma Seqüência de caracteres?

akka akka-http scala scala-2.12
2021-11-23 15:46:11
1

Melhor resposta

1

Provavelmente você tem um implícito unmarshaller para JSON em escopo, e é isso que está sendo captado como o unmarshaller em seu teste.

A adição de algo como

implicit val responseBodyUnmarshaller =
  Unmarshaller.strict[HttpResponse, HttpEntity](_.entity)
    .andThen(Unmarshaller.stringUnmarshaller)

deve resolver.

2021-11-23 17:52:23

Em outros idiomas

Esta página está em outros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................