简体   繁体   中英

Unable to Test POST request In Scala HTTP Akka

Although the problem seems to be easy, but I am unable to get a OK 200 response while trying to register an Instance.

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

Above is the method that executes the request.

Following is its API configuration:

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

Following is the test case that I am trying to execute but I get 400 Bad Request did not equal 200 OK.

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


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

Try this:

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

I can see that you have Json reader for your request body, so use Json writer like this new InstanceString("hello").toJson you get request body.

I see that your your request content type is application/json and you are passing String .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM