422 Unprocessable Entity: Can Wiremock create scenario with a missing quotation?

922 Views Asked by At

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

}

1

There are 1 best solutions below

1
On

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.