0

I am creating a scenario wherein I'd intentionally remove the quotation in the request body and add the corresponding error message. However, Wiremock returns 422 unprocessable entity. Is there a way I could successfully create this?

Here is my scenario.

{
"scenarioName": "Invalid JSON Format",
"name": "testscenarioinvalidformat",
"persistent": true,
"request": {
    "url": "/test/get",
    "method": "POST",
    "headers": {
        "Accept": {
            "contains": "application/json"
        }
    },
    "bodyPatterns": [
        {
            "equalToJson": {
                "test1":"5eHEzEEhT7CuHhdqkt01,
                "test2": "6656gdfgsdf"

            }
        }
    ]
},
"response": {
    "status": 400,
    "jsonBody": {
        "error": {
            "code": 400,
            "message": "Bad Request",
            "detail": {
                "rowCount": 1,
                "row1": {
                    "exception": [
                         "Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value"
                    ]
                }
            }
        }
    },
    "headers": {
        "Content-Type": "application/json"
    }
}

}

This is the error response

{
"errors": [
    {
        "code": 10,
        "source": {
            "pointer": "/request/bodyPatterns/0"
        },
        "title": "Error parsing JSON",
        "detail": "Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value\n at [Source: (String)\"{\r\n    \"scenarioName\": \"Invalid JSON Format\",\r\n    \"name\": \"testscenarioinvalidformat\",\r\n    \"persistent\": true,\r\n    \"request\": {\r\n        \"url\": \"/test/get\",\r\n        \"method\": \"POST\",\r\n        \"headers\": {\r\n            \"Accept\": {\r\n                \"contains\": \"application/json\"\r\n            }\r\n        },\r\n        \"bodyPatterns\": [\r\n            {\r\n                \"equalToJson\": {\r\n                  \"test2\":\"5eHEzEEhT7CuHhdqkt01,\r\n                    \"test2\": \"6656gdfgsdf\"\r\n         \"[truncated 698 chars]; line: 16, column: 50]"
    }
]

}

nit21
  • 125
  • 1
  • 4
  • 13

1 Answers1

1

If you are creating your mappings via JSON files, then no, you can't intentionally give it bad JSON as you have. What you could do would be to just evaluate the string contents of the request.

"bodyPatterns": [
        {
            "equalTo": "{ \"test1\":\"5eHEzEEhT7CuHhdqkt01,\"test2\": \"6656gdfgsdf\" }"
        }
    ]

This may take some time to get up and running, as the string equalTo is an exact match. Maybe using contains or matches would be more appropriate.

agoff
  • 5,818
  • 1
  • 7
  • 20
  • I tried that but it still gives me 422 code error : (code 92)): was expecting double-quote to start field name\n – nit21 Jun 01 '22 at 10:02