I'm implementing a mock for a /token endpoint that...
# help
j
I'm implementing a mock for a /token endpoint that needs to return an error scenario. Scenario: If the "grant_type" parameter is equal to authorization_code and the "code" parameter is not present, an error mock must be returned in the following format:
Copy code
{
  "mappings": [
    {
      "name": "Mock error 400: 'code' is not provided",
      "requet": {
        "method": "POST",
        "url": "/as/token.oauth2",
        "bodyPatterns": [
          {
            "contains":"grant_type=client_credentials",
            "notContain":"code="//how to make this??
          }
        ]
      },
      "response": {
        "bodyFileName": "token-400-response.json",
        "status": 400
      }
    }
  ]
}
I don't know if this is important but these parameters are passed using:
x-www-form-urlencoded
d
haven't tried it, but maybe it helps finding a way: To match a form, you should use
formParameters
:
Copy code
"formParameters": {
      "tool": {
        "equalTo": "WireMock"
      }
    }
I don't know whether there is a matcher for `exists`/`doesNotExist` but you may be able mimic this with `doesNotMatch`:
Copy code
"formParameters": {
      "code": {
        "doesNotMatch": ".+"
      }
    }
j
I tested using this way:
Copy code
"form parameters": {
        "code": {
          "doesNotMatch": ".+"
        }
      }
I don't know if I did something wrong: but I'm getting the following error:
Unrecognized field "formParameters" (class com.github.tomakehurst.wiremock.matching.RequestPattern), not marked as ignorable
a
Copy code
"formParameters": {
      "grant_type": {
        "equalTo": "authorization_code"
      },
      "code": {
        "absent": true
      }
    }
Should work
@Julio Marvim What version of WireMock are you using? I think
formParameters
is only in Wiremock 3 and above
j
I got it this way:
Copy code
"formParameters": {
      "grant_type": {
        "equalTo": "authorization_code"
      },
      "code": {
        "absent": true
      }
    }
But I needed to update the version of the wiremock I was using to the latest version available. Thank you very much.
🎉 1