Akka Trasy TestKit nemohol unmarshall odpoveď ako Reťazec

0

Otázka

Mám nasledujúci test:

 "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"
    }
 }

Avšak, test zlyhá s nasledovným:

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))

Ako môžem získať reakciu tela ako Reťazec?

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

Najlepšiu odpoveď

1

Pravdepodobne máte implicitným unmarshaller pre JSON, v rozsahu tak, že to stále zdvihol ako unmarshaller vo vašom teste.

Pridať niečo ako

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

by sa mali zamerať.

2021-11-23 17:52:23

V iných jazykoch

Táto stránka je v iných jazykoch

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