如何在 scaladsl 中设置 header?
How set header in scaladsl?
我正在尝试对 scaladsl 路由进行测试。我的路线代码是:
class EmployeeRoutes (implicit logger: LoggingAdapter) extends JsonSupport {
val route : Route = post {
path("employee" / "echo") {
logger.info("Message recived")
entity(as[Employee]) { employee =>
val idItem = employee.id
val nameItem = employee.name
complete((StatusCodes.OK, s"Employee {$idItem} is $nameItem."))
}
}
}
}
而且,我的测试是:
class EmployeeRoutesTest extends WordSpec with Matchers with ScalatestRouteTest {
implicit val logger = Logging(system, getClass)
val employeeRoutes = new EmployeeRoutes()
val employeeRoute = employeeRoutes.route
val echoPostRequest = Post("/employee/echo", "{\"id\":1,\"name\":\"John\"}")
"The service" should {
"return a Employee {1} is John message for POST request with {\"id\":1,\"name\":\"John\"}" in {
echoPostRequest ~> Route.seal(employeeRoute) ~> check {
status == StatusCodes.OK
responseAs[String] shouldEqual "Employee {1} is John"
}
}
}
}
但是,我总是得到以下错误 运行 我的测试:
"[The request's Content-Type is not supported. Expected:
application/jso]n" did not equal "[Employee {1} is Joh]n"
ScalaTestFailureLocation: routes.EmployeeRoutesTest at (EmployeeRoutesTest.scala:30)
org.scalatest.exceptions.TestFailedException: "[The request's Content-Type is not supported. Expected:
application/jso]n" did not equal "[Employee {1} is Joh]n"
at org.scalatest.MatchersHelper$.indicateFailure(MatchersHelper.scala:340)
at org.scalatest.Matchers$AnyShouldWrapper.shouldEqual(Matchers.scala:6742)
如何在 scaladsl 中设置 "application/json" header?
当使用 Akka-HTTP 测试套件将您的 POST 请求放在一起时,您只是传入了一个字符串。 Akka 无法决定是将其解释为 JSON,还是将其保留为字符串。
您可以在使用
定制您的 HttpEntity
时强制执行特定的内容类型
val echoPostRequest = Post(
"/employee/echo",
HttpEntity(ContentTypes.`application/json`, """{"id":1, "name":"John"}""")
)
PS:三引号帮助您避免转义斜线混乱。
我正在尝试对 scaladsl 路由进行测试。我的路线代码是:
class EmployeeRoutes (implicit logger: LoggingAdapter) extends JsonSupport {
val route : Route = post {
path("employee" / "echo") {
logger.info("Message recived")
entity(as[Employee]) { employee =>
val idItem = employee.id
val nameItem = employee.name
complete((StatusCodes.OK, s"Employee {$idItem} is $nameItem."))
}
}
}
}
而且,我的测试是:
class EmployeeRoutesTest extends WordSpec with Matchers with ScalatestRouteTest {
implicit val logger = Logging(system, getClass)
val employeeRoutes = new EmployeeRoutes()
val employeeRoute = employeeRoutes.route
val echoPostRequest = Post("/employee/echo", "{\"id\":1,\"name\":\"John\"}")
"The service" should {
"return a Employee {1} is John message for POST request with {\"id\":1,\"name\":\"John\"}" in {
echoPostRequest ~> Route.seal(employeeRoute) ~> check {
status == StatusCodes.OK
responseAs[String] shouldEqual "Employee {1} is John"
}
}
}
}
但是,我总是得到以下错误 运行 我的测试:
"[The request's Content-Type is not supported. Expected:
application/jso]n" did not equal "[Employee {1} is Joh]n"
ScalaTestFailureLocation: routes.EmployeeRoutesTest at (EmployeeRoutesTest.scala:30)
org.scalatest.exceptions.TestFailedException: "[The request's Content-Type is not supported. Expected:
application/jso]n" did not equal "[Employee {1} is Joh]n"
at org.scalatest.MatchersHelper$.indicateFailure(MatchersHelper.scala:340)
at org.scalatest.Matchers$AnyShouldWrapper.shouldEqual(Matchers.scala:6742)
如何在 scaladsl 中设置 "application/json" header?
当使用 Akka-HTTP 测试套件将您的 POST 请求放在一起时,您只是传入了一个字符串。 Akka 无法决定是将其解释为 JSON,还是将其保留为字符串。
您可以在使用
定制您的HttpEntity
时强制执行特定的内容类型
val echoPostRequest = Post(
"/employee/echo",
HttpEntity(ContentTypes.`application/json`, """{"id":1, "name":"John"}""")
)
PS:三引号帮助您避免转义斜线混乱。