无法在Scala HTTP Akka中测试POST请求



虽然这个问题看起来很容易,但我在尝试注册实例时无法获得OK 200响应。

def register(InstanceString: String) : server.Route = {
post
{
log.debug(s"POST /register has been called, parameter is: $InstanceString")
try {
val paramInstance : Instance = InstanceString.parseJson.convertTo[Instance](instanceFormat)
handler.handleRegister(paramInstance) match {
case Success(id) =>
complete{id.toString}
}}}

上面是执行请求的方法。

以下是其API配置:

paths:
/register:
post:
tags:
- Basic Operations
summary: Register a new instance at the registry
operationId: addInstance
parameters:
- in: body
name: InstanceToRegister
required: true
schema:
$ref: '#/definitions/Instance'
responses:
'200':
description: The ID of the registered instance
schema:
type: integer
format: int64
example: 42

以下是我试图执行的测试用例,但我得到的400错误请求不等于200 OK

"The Server" should {
"Successfully Register" in {

Post("/register", HttpEntity(ContentTypes.`application/json`,"""{"InstanceString":"hallo"}""")) ~> Route.seal(routes) ~> check {
assert(status === StatusCodes.OK)

试试这个:

Post("/register", requestBody.toJson).withHeaders("if Any") ~> Route.seal(routes) ~> check {
status shouldBe StatusCodes.OK
}

我可以看到,您的请求主体有Json阅读器,所以使用Json编写器,如new InstanceString("hello").toJson,您可以获得请求主体。

我看到你的请求内容类型是application/json,你正在通过String

最新更新